Skip to content

Commit 713854a

Browse files
committed
Make the bot check contributors before sending greeting
1 parent 81a0b53 commit 713854a

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
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
}

0 commit comments

Comments
 (0)