|
1 | 1 | package dotty.tools.bot
|
2 | 2 |
|
3 |
| -import org.http4s._ |
| 3 | +import org.http4s.{ Status => _, _ } |
4 | 4 | import org.http4s.client.blaze._
|
5 | 5 | import org.http4s.client.Client
|
| 6 | +import org.http4s.headers.Authorization |
6 | 7 |
|
7 | 8 | import scalaz.concurrent.Task
|
| 9 | +import scala.util.control.NonFatal |
8 | 10 |
|
9 | 11 | import io.circe._
|
10 | 12 | import io.circe.generic.auto._
|
11 | 13 | import io.circe.syntax._
|
12 | 14 | import org.http4s.circe._
|
13 | 15 | import org.http4s.dsl._
|
| 16 | +import org.http4s.util._ |
14 | 17 |
|
15 |
| -import github4s.Github |
16 |
| -import github4s.jvm.Implicits._ |
17 |
| -import github4s.free.domain.{ Commit, Issue } |
| 18 | +import model.Github._ |
18 | 19 |
|
19 | 20 | trait PullRequestService {
|
20 | 21 |
|
| 22 | + /** Username for authorized admin */ |
| 23 | + def user: String |
| 24 | + |
| 25 | + /** OAuth token needed for user to create statuses */ |
| 26 | + def token: String |
| 27 | + |
| 28 | + /** Pull Request HTTP service */ |
21 | 29 | val prService = HttpService {
|
22 | 30 | case request @ POST -> Root =>
|
23 | 31 | request.as(jsonOf[Issue]).flatMap(checkPullRequest)
|
24 | 32 | }
|
25 | 33 |
|
26 |
| - private case class CLASignature( |
| 34 | + private[this] lazy val authHeader = { |
| 35 | + val creds = BasicCredentials(user, token) |
| 36 | + new Authorization(creds) |
| 37 | + } |
| 38 | + |
| 39 | + private final case class CLASignature( |
27 | 40 | user: String,
|
28 | 41 | signed: Boolean,
|
29 | 42 | version: String,
|
30 | 43 | currentVersion: String
|
31 | 44 | )
|
32 | 45 |
|
33 |
| - private case class Status( |
34 |
| - state: String, |
35 |
| - target_url: String, |
36 |
| - description: String, |
37 |
| - context: String = "continuous-integration/CLA" |
38 |
| - ) |
39 |
| - |
40 | 46 | def claUrl(userName: String): String =
|
41 | 47 | s"https://www.lightbend.com/contribute/cla/scala/check/$userName"
|
42 | 48 |
|
43 | 49 | def commitsUrl(prNumber: Int): String =
|
44 |
| - s"https://api.github.com/repos/lampepfl/dotty/pulls/$prNumber/commits" |
| 50 | + s"https://api.github.com/repos/lampepfl/dotty/pulls/$prNumber/commits?per_page=100" |
| 51 | + |
| 52 | + def statusUrl(sha: String): String = |
| 53 | + s"https://api.github.com/repos/lampepfl/dotty/statuses/$sha" |
45 | 54 |
|
46 | 55 | def toUri(url: String): Task[Uri] =
|
47 | 56 | Uri.fromString(url).fold(Task.fail, Task.now)
|
48 | 57 |
|
49 | 58 | def getRequest(endpoint: Uri): Task[Request] = Task.now {
|
50 |
| - Request(uri = endpoint, method = Method.GET) |
| 59 | + Request(uri = endpoint, method = Method.GET).putHeaders(authHeader) |
51 | 60 | }
|
52 | 61 |
|
53 | 62 | def postRequest(endpoint: Uri): Task[Request] = Task.now {
|
54 |
| - Request(uri = endpoint, method = Method.POST) |
| 63 | + Request(uri = endpoint, method = Method.POST).putHeaders(authHeader) |
55 | 64 | }
|
56 | 65 |
|
57 | 66 | def shutdownClient(client: Client): Task[Unit] = Task.now {
|
58 | 67 | client.shutdownNow()
|
59 | 68 | }
|
60 | 69 |
|
61 | 70 | def users(xs: List[Commit]): Task[Set[String]] = Task.now {
|
62 |
| - xs.map(_.login).flatten.toSet |
| 71 | + xs |
| 72 | + .flatMap { commit => |
| 73 | + List(commit.author.login, commit.committer.login).flatten |
| 74 | + } |
| 75 | + .toSet |
63 | 76 | }
|
64 | 77 |
|
65 | 78 | sealed trait CommitStatus {
|
66 | 79 | def commit: Commit
|
67 | 80 | def isValid: Boolean
|
68 | 81 | }
|
69 |
| - final case class Valid(commit: Commit) extends CommitStatus { def isValid = true } |
70 |
| - final case class Invalid(commit: Commit) extends CommitStatus { def isValid = false } |
| 82 | + final case class Valid(user: String, commit: Commit) extends CommitStatus { def isValid = true } |
| 83 | + final case class Invalid(user: String, commit: Commit) extends CommitStatus { def isValid = false } |
| 84 | + final case class CLAServiceDown(user: String, commit: Commit) extends CommitStatus { def isValid = false } |
| 85 | + final case class MissingUser(commit: Commit) extends CommitStatus { def isValid = false } |
71 | 86 |
|
72 | 87 | /** Partitions invalid and valid commits */
|
73 | 88 | def checkCLA(xs: List[Commit], httpClient: Client): Task[List[CommitStatus]] = {
|
74 |
| - def checkUser(commit: Commit): Task[CommitStatus] = for { |
75 |
| - endpoint <- toUri(claUrl(commit.login.get)) |
76 |
| - claReq <- getRequest(endpoint) |
77 |
| - claRes <- httpClient.expect(claReq)(jsonOf[CLASignature]) |
78 |
| - res = if (claRes.signed) Valid(commit) else Invalid(commit) |
79 |
| - } yield res |
80 |
| - |
81 |
| - Task.gatherUnordered(xs.filter(_.login.isDefined).map(checkUser)) |
| 89 | + def checkUser(user: String, commit: Commit): Task[CommitStatus] = { |
| 90 | + val claStatus = for { |
| 91 | + endpoint <- toUri(claUrl(user)) |
| 92 | + claReq <- getRequest(endpoint) |
| 93 | + claRes <- httpClient.expect(claReq)(jsonOf[CLASignature]) |
| 94 | + res = if (claRes.signed) Valid(user, commit) else Invalid(user, commit) |
| 95 | + } yield res |
| 96 | + |
| 97 | + claStatus.handleWith { |
| 98 | + case NonFatal(e) => |
| 99 | + println(e) |
| 100 | + Task.now(CLAServiceDown(user, commit)) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + def checkCommit(commit: Commit, author: Author): Task[CommitStatus] = |
| 105 | + author.login.map(checkUser(_, commit)).getOrElse(Task.now(MissingUser(commit))) |
| 106 | + |
| 107 | + Task.gatherUnordered { |
| 108 | + xs.flatMap { |
| 109 | + case c @ Commit(_, author, commiter, _) => |
| 110 | + if (author == commiter) List(checkCommit(c, author)) |
| 111 | + else List( |
| 112 | + checkCommit(c, author), |
| 113 | + checkCommit(c, commiter) |
| 114 | + ) |
| 115 | + } |
| 116 | + } |
82 | 117 | }
|
83 | 118 |
|
84 |
| - def sendStatuses(xs: List[CommitStatus], httpClient: Client): Task[Unit] = { |
85 |
| - def setStatus(cm: CommitStatus): Task[Unit] = for { |
86 |
| - endpoint <- toUri(cm.commit.url.replaceAll("git\\/commits", "statuses")) |
87 |
| - |
88 |
| - target = claUrl(cm.commit.login.getOrElse("<invalid-user>")) |
89 |
| - state = if (cm.isValid) "success" else "failure" |
90 |
| - desc = |
| 119 | + def sendStatuses(xs: List[CommitStatus], httpClient: Client): Task[List[StatusResponse]] = { |
| 120 | + def setStatus(cm: CommitStatus): Task[StatusResponse] = { |
| 121 | + val desc = |
91 | 122 | if (cm.isValid) "User signed CLA"
|
92 | 123 | else "User needs to sign cla: https://www.lightbend.com/contribute/cla/scala"
|
93 | 124 |
|
94 |
| - statusReq <- postRequest(endpoint).map(_.withBody(Status(state, target, desc).asJson)) |
95 |
| - statusRes <- httpClient.expect(statusReq)(jsonOf[String]) |
96 |
| - print <- Task.now(println(statusRes)) |
97 |
| - } yield print |
| 125 | + val stat = cm match { |
| 126 | + case Valid(user, commit) => |
| 127 | + Status("success", claUrl(user), desc) |
| 128 | + case Invalid(user, commit) => |
| 129 | + Status("failure", claUrl(user), desc) |
| 130 | + case MissingUser(commit) => |
| 131 | + Status("failure", "", "Missing valid github user for this PR") |
| 132 | + case CLAServiceDown(user, commit) => |
| 133 | + Status("pending", claUrl(user), "CLA Service is down") |
| 134 | + } |
| 135 | + |
| 136 | + for { |
| 137 | + endpoint <- toUri(statusUrl(cm.commit.sha)) |
| 138 | + req <- postRequest(endpoint).map(_.withBody(stat.asJson)) |
| 139 | + res <- httpClient.expect(req)(jsonOf[StatusResponse]) |
| 140 | + } yield res |
| 141 | + } |
| 142 | + |
| 143 | + Task.gatherUnordered(xs.map(setStatus)) |
| 144 | + } |
| 145 | + |
| 146 | + private[this] val ExtractLink = """<([^>]+)>; rel="([^"]+)"""".r |
| 147 | + def findNext(header: Option[Header]): Option[String] = header.flatMap { header => |
| 148 | + val value = header.value |
| 149 | + |
| 150 | + value |
| 151 | + .split(',') |
| 152 | + .collect { |
| 153 | + case ExtractLink(url, kind) if kind == "next" => |
| 154 | + url |
| 155 | + } |
| 156 | + .headOption |
| 157 | + } |
| 158 | + |
| 159 | + def getCommits(issueNbr: Int, httpClient: Client): Task[List[Commit]] = { |
| 160 | + def makeRequest(url: String): Task[List[Commit]] = |
| 161 | + for { |
| 162 | + endpoint <- toUri(url) |
| 163 | + req <- getRequest(endpoint) |
| 164 | + res <- httpClient.fetch(req){ res => |
| 165 | + val link = CaseInsensitiveString("Link") |
| 166 | + val next = findNext(res.headers.get(link)).map(makeRequest).getOrElse(Task.now(Nil)) |
| 167 | + |
| 168 | + res.as[List[Commit]](jsonOf[List[Commit]]).flatMap(commits => next.map(commits ++ _)) |
| 169 | + } |
| 170 | + } yield res |
98 | 171 |
|
99 |
| - Task.gatherUnordered(xs.map(setStatus)).map(_ => ()) |
| 172 | + makeRequest(commitsUrl(issueNbr)) |
100 | 173 | }
|
101 | 174 |
|
102 | 175 | def checkPullRequest(issue: Issue): Task[Response] = {
|
103 | 176 | val httpClient = PooledHttp1Client()
|
104 | 177 |
|
105 | 178 | for {
|
106 | 179 | // First get all the commits from the PR
|
107 |
| - endpoint <- toUri(commitsUrl(issue.number)) |
108 |
| - commitsReq <- getRequest(endpoint) |
109 |
| - commitsRes <- httpClient.expect(commitsReq)(jsonOf[List[Commit]]) |
| 180 | + commits <- getCommits(issue.number, httpClient) |
110 | 181 |
|
111 | 182 | // Then get check the CLA of each commit
|
112 |
| - statuses <- checkCLA(commitsRes, httpClient) |
| 183 | + statuses <- checkCLA(commits, httpClient) |
113 | 184 |
|
114 | 185 | // Send statuses to Github and exit
|
115 | 186 | _ <- sendStatuses(statuses, httpClient)
|
|
0 commit comments