aboutsummaryrefslogtreecommitdiff
path: root/bot/src/dotty/tools/bot/PullRequestService.scala
blob: 8b568b134808a0f6f89a867c0c203798cdf6351f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package dotty.tools.bot

import org.http4s._
import org.http4s.client.blaze._
import org.http4s.client.Client

import scalaz.concurrent.Task

import io.circe._
import io.circe.generic.auto._
import io.circe.syntax._
import org.http4s.circe._
import org.http4s.dsl._

import github4s.Github
import github4s.jvm.Implicits._
import github4s.free.domain.{ Commit, Issue }

trait PullRequestService {

  val prService = HttpService {
    case request @ POST -> Root =>
      request.as(jsonOf[Issue]).flatMap(checkPullRequest)
  }

  private case class CLASignature(
    user: String,
    signed: Boolean,
    version: String,
    currentVersion: String
  )

  private case class Status(
    state: String,
    target_url: String,
    description: String,
    context: String = "continuous-integration/CLA"
  )

  def claUrl(userName: String): String =
   s"https://www.lightbend.com/contribute/cla/scala/check/$userName"

  def commitsUrl(prNumber: Int): String =
    s"https://api.github.com/repos/lampepfl/dotty/pulls/$prNumber/commits"

  def toUri(url: String): Task[Uri] =
    Uri.fromString(url).fold(Task.fail, Task.now)

  def getRequest(endpoint: Uri): Task[Request] = Task.now {
    Request(uri = endpoint, method = Method.GET)
  }

  def postRequest(endpoint: Uri): Task[Request] = Task.now {
    Request(uri = endpoint, method = Method.POST)
  }

  def shutdownClient(client: Client): Task[Unit] = Task.now {
    client.shutdownNow()
  }

  def users(xs: List[Commit]): Task[Set[String]] = Task.now {
    xs.map(_.login).flatten.toSet
  }

  sealed trait CommitStatus {
    def commit: Commit
    def isValid: Boolean
  }
  final case class Valid(commit: Commit) extends CommitStatus { def isValid = true }
  final case class Invalid(commit: Commit) extends CommitStatus { def isValid = false }

  /** Partitions invalid and valid commits */
  def checkCLA(xs: List[Commit], httpClient: Client): Task[List[CommitStatus]] = {
    def checkUser(commit: Commit): Task[CommitStatus] = for {
      endpoint <- toUri(claUrl(commit.login.get))
      claReq   <- getRequest(endpoint)
      claRes   <- httpClient.expect(claReq)(jsonOf[CLASignature])
      res = if (claRes.signed) Valid(commit) else Invalid(commit)
    } yield res

    Task.gatherUnordered(xs.filter(_.login.isDefined).map(checkUser))
  }

  def sendStatuses(xs: List[CommitStatus], httpClient: Client): Task[Unit] = {
    def setStatus(cm: CommitStatus): Task[Unit] = for {
      endpoint <- toUri(cm.commit.url.replaceAll("git\\/commits", "statuses"))

      target = claUrl(cm.commit.login.getOrElse("<invalid-user>"))
      state = if (cm.isValid) "success" else "failure"
      desc =
        if (cm.isValid) "User signed CLA"
        else "User needs to sign cla: https://www.lightbend.com/contribute/cla/scala"

      statusReq <- postRequest(endpoint).map(_.withBody(Status(state, target, desc).asJson))
      statusRes <- httpClient.expect(statusReq)(jsonOf[String])
      print     <- Task.now(println(statusRes))
    } yield print

    Task.gatherUnordered(xs.map(setStatus)).map(_ => ())
  }

  def checkPullRequest(issue: Issue): Task[Response] = {
    val httpClient = PooledHttp1Client()

    for {
      // First get all the commits from the PR
      endpoint   <- toUri(commitsUrl(issue.number))
      commitsReq <- getRequest(endpoint)
      commitsRes <- httpClient.expect(commitsReq)(jsonOf[List[Commit]])

      // Then get check the CLA of each commit
      statuses   <- checkCLA(commitsRes, httpClient)

      // Send statuses to Github and exit
      _          <- sendStatuses(statuses, httpClient)
      _          <- shutdownClient(httpClient)
      resp       <- Ok("All statuses checked")
    } yield resp
  }
}