Skip to content

Commit dea3d5e

Browse files
OlivierBlanvillainfelixmulder
authored andcommitted
Only make one request per author
1 parent 35337ea commit dea3d5e

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

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

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.http4s.headers.Authorization
88
import cats.syntax.applicative._
99
import scalaz.concurrent.Task
1010
import scala.util.control.NonFatal
11+
import scala.Function.tupled
1112

1213
import io.circe._
1314
import io.circe.generic.auto._
@@ -85,34 +86,32 @@ trait PullRequestService {
8586

8687
/** Partitions invalid and valid commits */
8788
def checkCLA(xs: List[Commit], httpClient: Client): Task[List[CommitStatus]] = {
88-
def checkUser(user: String, commit: Commit): Task[CommitStatus] = {
89+
def checkUser(user: String): Task[Commit => CommitStatus] = {
8990
val claStatus = for {
9091
endpoint <- toUri(claUrl(user))
9192
claReq <- getRequest(endpoint).pure[Task]
9293
claRes <- httpClient.expect(claReq)(jsonOf[CLASignature])
93-
res = if (claRes.signed) Valid(user, commit) else Invalid(user, commit)
94-
} yield res
94+
} yield { (commit: Commit) =>
95+
if (claRes.signed) Valid(user, commit)
96+
else Invalid(user, commit)
97+
}
9598

9699
claStatus.handleWith {
97100
case NonFatal(e) =>
98101
println(e)
99-
Task.now(CLAServiceDown(user, commit))
102+
Task.now((commit: Commit) => CLAServiceDown(user, commit))
100103
}
101104
}
102105

103-
def checkCommit(commit: Commit, author: Author): Task[CommitStatus] =
104-
author.login.map(checkUser(_, commit)).getOrElse(Task.now(MissingUser(commit)))
106+
def checkCommit(author: Author, commit: List[Commit]): Task[List[CommitStatus]] =
107+
author.login.map(checkUser)
108+
.getOrElse(Task.now(MissingUser))
109+
.map(f => commit.map(f))
105110

106111
Task.gatherUnordered {
107-
xs.flatMap {
108-
case c @ Commit(_, author, commiter, _) =>
109-
if (author == commiter) List(checkCommit(c, author))
110-
else List(
111-
checkCommit(c, author),
112-
checkCommit(c, commiter)
113-
)
114-
}
115-
}
112+
val groupedByAuthor: Map[Author, List[Commit]] = xs.groupBy(_.author)
113+
groupedByAuthor.map(tupled(checkCommit)).toList
114+
}.map(_.flatten)
116115
}
117116

118117
def sendStatuses(xs: List[CommitStatus], httpClient: Client): Task[List[StatusResponse]] = {
@@ -176,15 +175,15 @@ trait PullRequestService {
176175

177176
for {
178177
// First get all the commits from the PR
179-
commits <- getCommits(issue.number, httpClient)
178+
commits <- getCommits(issue.number, httpClient)
180179

181180
// Then check the CLA of each commit for both author and committer
182-
statuses <- checkCLA(commits, httpClient)
181+
statuses <- checkCLA(commits, httpClient)
183182

184183
// Send statuses to Github and exit
185-
_ <- sendStatuses(statuses, httpClient)
186-
_ <- shutdownClient(httpClient).pure[Task]
187-
resp <- Ok("All statuses checked")
184+
_ <- sendStatuses(statuses, httpClient)
185+
_ <- shutdownClient(httpClient).pure[Task]
186+
resp <- Ok("All statuses checked")
188187
} yield resp
189188
}
190189
}

0 commit comments

Comments
 (0)