Skip to content

Commit 81a0b53

Browse files
authored
Merge pull request #2370 from dotty-staging/topic/fix-rate-limits
Fix bot rate limits
2 parents 23fb2d9 + bfdf357 commit 81a0b53

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

bot/src/dotty/tools/bot/Main.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import scalaz.concurrent.Task
77

88
object Main extends ServerApp with PullRequestService {
99

10-
val githubUser = sys.env("GITHUB_USER")
11-
val githubToken = sys.env("GITHUB_TOKEN")
12-
val droneToken = sys.env("DRONE_TOKEN")
13-
val port = sys.env("PORT").toInt
10+
val githubUser = sys.env("GITHUB_USER")
11+
val githubToken = sys.env("GITHUB_TOKEN")
12+
val githubClientId = sys.env("GITHUB_CLIENT_ID")
13+
val githubClientSecret = sys.env("GITHUB_CLIENT_SECRET")
14+
val droneToken = sys.env("DRONE_TOKEN")
15+
val port = sys.env("PORT").toInt
1416

1517
/** Services mounted to the server */
1618
final val services = prService

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,23 @@ trait PullRequestService {
3434
/** OAuth token for drone, needed to cancel builds */
3535
def droneToken: String
3636

37+
/** OAuthed application's "client_id" */
38+
def githubClientId: String
39+
40+
/** OAuthed application's "client_secret" */
41+
def githubClientSecret: String
42+
3743
/** Pull Request HTTP service */
3844
val prService = HttpService {
45+
case GET -> Root / "rate" => {
46+
val client = PooledHttp1Client()
47+
for {
48+
rates <- client.expect(get(rateLimit))(EntityDecoder.text)
49+
resp <- Ok(rates)
50+
_ <- client.shutdown
51+
} yield resp
52+
}
53+
3954
case request @ POST -> Root =>
4055
val githubEvent =
4156
request.headers
@@ -68,21 +83,25 @@ trait PullRequestService {
6883
)
6984

7085
private[this] val githubUrl = "https://api.github.com"
86+
private[this] def withGithubSecret(url: String, extras: String*): String =
87+
s"$url?client_id=$githubClientId&client_secret=$githubClientSecret" + extras.mkString("&", "&", "")
88+
89+
def rateLimit: String = withGithubSecret("https://api.github.com/rate_limit")
7190

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

7594
def commitsUrl(prNumber: Int): String =
76-
s"$githubUrl/repos/lampepfl/dotty/pulls/$prNumber/commits?per_page=100"
95+
withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/pulls/$prNumber/commits", "per_page=100")
7796

7897
def statusUrl(sha: String): String =
79-
s"$githubUrl/repos/lampepfl/dotty/statuses/$sha"
98+
withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/statuses/$sha")
8099

81100
def issueCommentsUrl(issueNbr: Int): String =
82-
s"$githubUrl/repos/lampepfl/dotty/issues/$issueNbr/comments"
101+
withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/issues/$issueNbr/comments")
83102

84103
def reviewUrl(issueNbr: Int): String =
85-
s"$githubUrl/repos/lampepfl/dotty/pulls/$issueNbr/reviews"
104+
withGithubSecret(s"$githubUrl/repos/lampepfl/dotty/pulls/$issueNbr/reviews")
86105

87106
sealed trait CommitStatus {
88107
def commit: Commit

bot/test/PRServiceTests.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ import org.http4s.client.Client
1515
import scalaz.concurrent.Task
1616

1717
class PRServiceTests extends PullRequestService {
18-
val githubUser = sys.env("GITHUB_USER")
19-
val githubToken = sys.env("GITHUB_TOKEN")
20-
val droneToken = sys.env("DRONE_TOKEN")
18+
val githubUser = sys.env("GITHUB_USER")
19+
val githubToken = sys.env("GITHUB_TOKEN")
20+
val droneToken = sys.env("DRONE_TOKEN")
21+
val githubClientId = sys.env("GITHUB_CLIENT_ID")
22+
val githubClientSecret = sys.env("GITHUB_CLIENT_SECRET")
2123

2224
private def withClient[A](f: Client => Task[A]): A = {
2325
val httpClient = PooledHttp1Client()

0 commit comments

Comments
 (0)