Skip to content

Fix bot rate limits #2370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions bot/src/dotty/tools/bot/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 23 additions & 4 deletions bot/src/dotty/tools/bot/PullRequestService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 5 additions & 3 deletions bot/test/PRServiceTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down