diff --git a/bot/src/dotty/tools/bot/Main.scala b/bot/src/dotty/tools/bot/Main.scala index 8edd1dcc9cab..f209d4547aef 100644 --- a/bot/src/dotty/tools/bot/Main.scala +++ b/bot/src/dotty/tools/bot/Main.scala @@ -7,10 +7,12 @@ import scalaz.concurrent.Task object Main extends ServerApp with PullRequestService { - val githubUser = sys.env("GITHUB_USER") - val githubToken = sys.env("GITHUB_TOKEN") - val droneToken = sys.env("DRONE_TOKEN") - val port = sys.env("PORT").toInt + val githubUser = sys.env("GITHUB_USER") + val githubToken = sys.env("GITHUB_TOKEN") + val githubClientId = sys.env("GITHUB_CLIENT_ID") + val githubClientSecret = sys.env("GITHUB_CLIENT_SECRET") + val droneToken = sys.env("DRONE_TOKEN") + val port = sys.env("PORT").toInt /** Services mounted to the server */ final val services = prService diff --git a/bot/src/dotty/tools/bot/PullRequestService.scala b/bot/src/dotty/tools/bot/PullRequestService.scala index afe5f10ce623..89651ab6bc26 100644 --- a/bot/src/dotty/tools/bot/PullRequestService.scala +++ b/bot/src/dotty/tools/bot/PullRequestService.scala @@ -34,8 +34,23 @@ trait PullRequestService { /** OAuth token for drone, needed to cancel builds */ def droneToken: String + /** OAuthed application's "client_id" */ + def githubClientId: String + + /** OAuthed application's "client_secret" */ + def githubClientSecret: String + /** Pull Request HTTP service */ val prService = HttpService { + case GET -> Root / "rate" => { + val client = PooledHttp1Client() + for { + rates <- client.expect(get(rateLimit))(EntityDecoder.text) + resp <- Ok(rates) + _ <- client.shutdown + } yield resp + } + case request @ POST -> Root => val githubEvent = request.headers @@ -68,21 +83,25 @@ trait PullRequestService { ) private[this] val githubUrl = "https://api.github.com" + private[this] def withGithubSecret(url: String, extras: String*): String = + s"$url?client_id=$githubClientId&client_secret=$githubClientSecret" + extras.mkString("&", "&", "") + + def rateLimit: String = withGithubSecret("https://api.github.com/rate_limit") def claUrl(userName: String): String = s"https://www.lightbend.com/contribute/cla/scala/check/$userName" def commitsUrl(prNumber: Int): String = - s"$githubUrl/repos/lampepfl/dotty/pulls/$prNumber/commits?per_page=100" + withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/pulls/$prNumber/commits", "per_page=100") def statusUrl(sha: String): String = - s"$githubUrl/repos/lampepfl/dotty/statuses/$sha" + withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/statuses/$sha") def issueCommentsUrl(issueNbr: Int): String = - s"$githubUrl/repos/lampepfl/dotty/issues/$issueNbr/comments" + withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/issues/$issueNbr/comments") def reviewUrl(issueNbr: Int): String = - s"$githubUrl/repos/lampepfl/dotty/pulls/$issueNbr/reviews" + withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/pulls/$issueNbr/reviews") sealed trait CommitStatus { def commit: Commit diff --git a/bot/test/PRServiceTests.scala b/bot/test/PRServiceTests.scala index f80def044a2d..c94c553224c3 100644 --- a/bot/test/PRServiceTests.scala +++ b/bot/test/PRServiceTests.scala @@ -15,9 +15,11 @@ import org.http4s.client.Client import scalaz.concurrent.Task class PRServiceTests extends PullRequestService { - val githubUser = sys.env("GITHUB_USER") - val githubToken = sys.env("GITHUB_TOKEN") - val droneToken = sys.env("DRONE_TOKEN") + val githubUser = sys.env("GITHUB_USER") + val githubToken = sys.env("GITHUB_TOKEN") + val droneToken = sys.env("DRONE_TOKEN") + val githubClientId = sys.env("GITHUB_CLIENT_ID") + val githubClientSecret = sys.env("GITHUB_CLIENT_SECRET") private def withClient[A](f: Client => Task[A]): A = { val httpClient = PooledHttp1Client()