Skip to content

Commit e4e4810

Browse files
authored
Merge pull request #2384 from dotty-staging/topic/make-bot-check-contribs
Make bot check contributors before sending heart-warming greeting
2 parents 4886857 + 6f49d6f commit e4e4810

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

bot/src/dotty/tools/bot/PullRequestService.scala

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,9 @@ trait PullRequestService {
103103
def reviewUrl(issueNbr: Int): String =
104104
withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/pulls/$issueNbr/reviews")
105105

106+
def contributorsUrl: String =
107+
withGithubSecret("https://api.github.com/repos/lampepfl/dotty/contributors")
108+
106109
sealed trait CommitStatus {
107110
def commit: Commit
108111
def isValid: Boolean
@@ -183,6 +186,13 @@ trait PullRequestService {
183186
.headOption
184187
}
185188

189+
/** Get all contributors from GitHub */
190+
def getContributors(implicit client: Client): Task[Set[String]] =
191+
for {
192+
authors <- client.expect(get(contributorsUrl))(jsonOf[List[Author]])
193+
logins = authors.map(_.login).flatten
194+
} yield logins.toSet
195+
186196
/** Ordered from earliest to latest */
187197
def getCommits(issueNbr: Int)(implicit httpClient: Client): Task[List[Commit]] = {
188198
def makeRequest(url: String): Task[List[Commit]] =
@@ -226,8 +236,11 @@ trait PullRequestService {
226236
wrongTense || firstLine.last == '.' || firstLine.length > 80
227237
}
228238

229-
def sendInitialComment(issueNbr: Int, invalidUsers: List[String], commits: List[Commit], client: Client): Task[ReviewResponse] = {
230-
239+
/** Returns the body of a `ReviewResponse` */
240+
def sendInitialComment(issueNbr: Int,
241+
invalidUsers: List[String],
242+
commits: List[Commit],
243+
newContributors: Boolean)(implicit client: Client): Task[String] = {
231244
val cla = if (invalidUsers.nonEmpty) {
232245
s"""|## CLA ##
233246
|In order for us to be able to accept your contribution, all users
@@ -272,9 +285,16 @@ trait PullRequestService {
272285

273286
val review = Review.comment(body)
274287

288+
val shouldPost = newContributors || commitRules.nonEmpty || invalidUsers.nonEmpty
289+
275290
for {
276291
req <- post(reviewUrl(issueNbr)).withAuth(githubUser, githubToken)
277-
res <- client.expect(req.withBody(review.asJson))(jsonOf[ReviewResponse])
292+
res <- {
293+
if (shouldPost)
294+
client.expect(req.withBody(review.asJson))(jsonOf[ReviewResponse]).map(_.body)
295+
else
296+
Task.now("")
297+
}
278298
} yield res
279299
}
280300

@@ -299,10 +319,12 @@ trait PullRequestService {
299319
setStatus(statuses.last, httpClient)
300320
}
301321

302-
// Send positive comment:
303-
_ <- sendInitialComment(issue.number, invalidUsers, commits, httpClient)
304-
_ <- httpClient.shutdown
305-
resp <- Ok("Fresh PR checked")
322+
authors = commits.map(_.author.login).flatten.toSet
323+
contribs <- getContributors
324+
newContr = !authors.forall(contribs.contains)
325+
_ <- sendInitialComment(issue.number, invalidUsers, commits, newContr)
326+
_ <- httpClient.shutdown
327+
resp <- Ok("Fresh PR checked")
306328
} yield resp
307329

308330
}

bot/test/PRServiceTests.scala

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,14 @@ class PRServiceTests extends PullRequestService {
123123
@Test def canPostReview = {
124124
val invalidUsers = "felixmulder" :: "smarter" :: Nil
125125
val commit = Commit("", Author(Some("smarter")), Author(Some("smarter")), CommitInfo("Added stuff"))
126-
val res = withClient(sendInitialComment(2281, invalidUsers, commit :: Nil, _))
126+
val resBody =
127+
withClient(sendInitialComment(2281, invalidUsers, commit :: Nil, false)(_))
127128

128129
assert(
129-
res.body.contains("We want to keep history") &&
130-
res.body.contains("Could you folks please sign the CLA?") &&
131-
res.body.contains("Have an awesome day!"),
132-
s"Body of review was not as expected:\n${res.body}"
130+
resBody.contains("We want to keep history") &&
131+
resBody.contains("Could you folks please sign the CLA?") &&
132+
resBody.contains("Have an awesome day!"),
133+
s"Body of review was not as expected:\n${resBody}"
133134
)
134135
}
135136

@@ -159,4 +160,9 @@ class PRServiceTests extends PullRequestService {
159160

160161
assert(respondToComment(issueComment).run.status.code == 200)
161162
}
163+
164+
@Test def canGetContributors = {
165+
val contributors = withClient(getContributors(_))
166+
assert(contributors.contains("felixmulder"))
167+
}
162168
}

0 commit comments

Comments
 (0)