Skip to content

Commit 1bfb394

Browse files
Merge pull request code-check#64 from code-check/list-prs
List prs
2 parents 4474cbe + 111f235 commit 1bfb394

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

src/main/scala/codecheck/github/models/PullRequest.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ object PullRequestAction {
3838
def fromString(str: String) = values.filter(_.name == str).head
3939
}
4040

41+
case class PullRequestListOption(
42+
state: IssueState = IssueState.open,
43+
head: Option[String] = None,
44+
base: Option[String] = None,
45+
sort: IssueSort = IssueSort.created,
46+
direction: SortDirection = SortDirection.desc
47+
)
48+
4149
case class PullRequestRef(value: JValue) extends AbstractJson(value) {
4250
def label = get("label")
4351
def ref = get("ref")

src/main/scala/codecheck/github/operations/PullRequestOp.scala

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,37 @@ package codecheck.github.operations
22

33
import scala.concurrent.Future
44
import scala.concurrent.ExecutionContext.Implicits.global
5-
import org.json4s.{JObject, JString}
5+
import org.json4s.JArray
6+
import org.json4s.JObject
7+
import org.json4s.JString
68

79
import codecheck.github.api.GitHubAPI
810
import codecheck.github.models.PullRequestInput
11+
import codecheck.github.models.PullRequestListOption
912
import codecheck.github.models.PullRequest
1013

1114
trait PullRequestOp {
1215
self: GitHubAPI =>
1316

17+
def listPullRequests(
18+
owner: String,
19+
repo: String,
20+
option: PullRequestListOption = PullRequestListOption()
21+
): Future[List[PullRequest]] = {
22+
val q = s"?state=${option.state}" +
23+
s"&sort=${option.sort}" +
24+
s"&direction=${option.direction}" +
25+
option.head.map("&head=" + _).getOrElse("") +
26+
option.base.map("&base=" + _).getOrElse("")
27+
28+
exec("GET", s"/repos/$owner/$repo/pulls$q").map(
29+
_.body match {
30+
case JArray(arr) => arr.map(v => PullRequest(v))
31+
case _ => throw new IllegalStateException()
32+
}
33+
)
34+
}
35+
1436
def createPullRequest(owner: String, repo: String, input: PullRequestInput): Future[PullRequest] = {
1537
val path = s"/repos/$owner/$repo/pulls"
1638
exec("POST", path, input.value).map { result =>

src/test/scala/PullRequestOpSpec.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@ import java.util.Date
66

77
class PullRequestOpSpec extends FunSpec with Constants {
88

9+
describe("listPullRequests") {
10+
it("with valid repo should succeed") {
11+
val list = Await.result(api.listPullRequests(otherUser, otherUserRepo), TIMEOUT)
12+
assert(list.length >= 0)
13+
assert(list.exists(_.state == "open"))
14+
assert(list.exists(_.base.repo.full_name == s"$otherUser/$otherUserRepo"))
15+
assert(list.exists(_.base.user.login == otherUser))
16+
assert(list.exists(_.base.repo.name == otherUserRepo))
17+
}
18+
}
19+
920
describe("createPullRequest(owner, repo, input)") {
1021
val username = otherUser
1122
val reponame = otherUserRepo

0 commit comments

Comments
 (0)