Skip to content

Commit b49faeb

Browse files
committed
Add bot ability to get issue comments
1 parent 4a91d18 commit b49faeb

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,17 @@ trait PullRequestService {
6363
def statusUrl(sha: String): String =
6464
s"https://api.github.com/repos/lampepfl/dotty/statuses/$sha"
6565

66+
def issueCommentsUrl(issueNbr: Int): String =
67+
s"https://api.github.com/repos/lampepfl/dotty/issues/$issueNbr/comments"
68+
6669
def toUri(url: String): Task[Uri] =
6770
Uri.fromString(url).fold(Task.fail, Task.now)
6871

69-
def getRequest(endpoint: Uri): Request =
70-
Request(uri = endpoint, method = Method.GET).putHeaders(authHeader)
72+
def getRequest(endpoint: Uri): Task[Request] =
73+
Request(uri = endpoint, method = Method.GET).putHeaders(authHeader).pure[Task]
7174

72-
def postRequest(endpoint: Uri): Request =
73-
Request(uri = endpoint, method = Method.POST).putHeaders(authHeader)
75+
def postRequest(endpoint: Uri): Task[Request] =
76+
Request(uri = endpoint, method = Method.POST).putHeaders(authHeader).pure[Task]
7477

7578
def shutdownClient(client: Client): Unit =
7679
client.shutdownNow()
@@ -89,7 +92,7 @@ trait PullRequestService {
8992
def checkUser(user: String): Task[Commit => CommitStatus] = {
9093
val claStatus = for {
9194
endpoint <- toUri(claUrl(user))
92-
claReq <- getRequest(endpoint).pure[Task]
95+
claReq <- getRequest(endpoint)
9396
claRes <- httpClient.expect(claReq)(jsonOf[CLASignature])
9497
} yield { (commit: Commit) =>
9598
if (claRes.signed) Valid(user, commit)
@@ -133,7 +136,7 @@ trait PullRequestService {
133136

134137
for {
135138
endpoint <- toUri(statusUrl(cm.commit.sha))
136-
req <- postRequest(endpoint).withBody(stat.asJson).pure[Task]
139+
req <- postRequest(endpoint).withBody(stat.asJson)
137140
res <- httpClient.expect(req)(jsonOf[StatusResponse])
138141
} yield res
139142
}
@@ -158,7 +161,7 @@ trait PullRequestService {
158161
def makeRequest(url: String): Task[List[Commit]] =
159162
for {
160163
endpoint <- toUri(url)
161-
req <- getRequest(endpoint).pure[Task]
164+
req <- getRequest(endpoint)
162165
res <- httpClient.fetch(req){ res =>
163166
val link = CaseInsensitiveString("Link")
164167
val next = findNext(res.headers.get(link)).map(makeRequest).getOrElse(Task.now(Nil))
@@ -170,6 +173,13 @@ trait PullRequestService {
170173
makeRequest(commitsUrl(issueNbr))
171174
}
172175

176+
def getComments(issueNbr: Int, httpClient: Client): Task[List[Comment]] =
177+
for {
178+
endpoint <- toUri(issueCommentsUrl(issueNbr))
179+
req <- getRequest(endpoint)
180+
res <- httpClient.expect(req)(jsonOf[List[Comment]])
181+
} yield res
182+
173183
def checkPullRequest(issue: Issue): Task[Response] = {
174184
val httpClient = PooledHttp1Client()
175185

bot/src/dotty/tools/bot/model/Github.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,6 @@ object Github {
4040
id: Long,
4141
state: String
4242
)
43+
44+
case class Comment(user: Author)
4345
}

bot/test/PRServiceTests.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,20 @@ import io.circe.parser.decode
1010

1111
import model.Github._
1212
import org.http4s.client.blaze._
13+
import org.http4s.client.Client
1314
import scalaz.concurrent.Task
1415

1516
class PRServiceTests extends PullRequestService {
1617
val user = sys.env("USER")
1718
val token = sys.env("TOKEN")
1819

20+
private def withClient[A](f: Client => Task[A]): A = {
21+
val httpClient = PooledHttp1Client()
22+
val ret = f(httpClient).run
23+
httpClient.shutdownNow()
24+
ret
25+
}
26+
1927
def getResource(r: String): String =
2028
Option(getClass.getResourceAsStream(r)).map(scala.io.Source.fromInputStream)
2129
.map(_.mkString)
@@ -60,6 +68,13 @@ class PRServiceTests extends PullRequestService {
6068
)
6169
}
6270

71+
@Test def canGetComments = {
72+
val comments: List[Comment] = withClient(c => getComments(2136, c))
73+
74+
assert(comments.find(_.user.login == Some("odersky")).isDefined,
75+
"Could not find Martin's comment on PR 2136")
76+
}
77+
6378
@Test def canCheckCLA = {
6479
val httpClient = PooledHttp1Client()
6580
val validUserCommit = Commit("sha-here", Author(Some("felixmulder")), Author(Some("felixmulder")), CommitInfo(""))

0 commit comments

Comments
 (0)