Skip to content

Commit 494a027

Browse files
committed
Merge pull request #49 from code-check/branchOp
Add BranchOp
2 parents 11ffa3a + 59fa466 commit 494a027

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

src/main/scala/codecheck/github/api/GitHubAPI.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class GitHubAPI(token: String, client: AsyncHttpClient, tokenType: String = "tok
2727
with MilestoneOp
2828
with WebhookOp
2929
with CollaboratorOp
30+
with BranchOp
3031
{
3132

3233
private val endpoint = "https://api.github.com"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package codecheck.github.models
2+
import org.json4s.JValue
3+
4+
case class BranchListItem(value: JValue) extends AbstractJson(value) {
5+
def name = get("name")
6+
lazy val commit = CommitInfo(value \ "commit")
7+
}
8+
9+
case class CommitInfo(value: JValue) extends AbstractJson(value) {
10+
def sha = get("sha")
11+
def url = get("url")
12+
}
13+
14+
case class Branch(value: JValue) extends AbstractJson(value) {
15+
def name = get("name")
16+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package codecheck.github.operations
2+
3+
import java.net.URLEncoder
4+
import scala.concurrent.Future
5+
import scala.concurrent.ExecutionContext.Implicits.global
6+
import org.json4s.JArray
7+
import org.json4s.JString
8+
import org.json4s.JNothing
9+
10+
import codecheck.github.api.GitHubAPI
11+
import codecheck.github.exceptions.NotFoundException
12+
import codecheck.github.models.BranchListItem
13+
import codecheck.github.models.Branch
14+
15+
trait BranchOp {
16+
self: GitHubAPI =>
17+
18+
def listBranches(owner: String, repo: String): Future[List[BranchListItem]] = {
19+
exec("GET", s"/repos/$owner/$repo/branches").map(
20+
_.body match {
21+
case JArray(arr) => arr.map(v => BranchListItem(v))
22+
case _ => throw new IllegalStateException()
23+
}
24+
)
25+
}
26+
27+
def getBranch(owner: String, repo: String, branch: String): Future[Option[Branch]] = {
28+
exec("GET", s"/repos/$owner/$repo/branches/$branch", fail404=false).map{ res =>
29+
res.statusCode match {
30+
case 404 => None
31+
case 200 => Some(Branch(res.body))
32+
}
33+
}
34+
}
35+
36+
}

src/test/scala/BranchOpSpec.scala

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import org.scalatest.FunSpec
2+
import org.scalatest.BeforeAndAfterAll
3+
import codecheck.github.exceptions.NotFoundException
4+
import codecheck.github.models._
5+
import codecheck.github.exceptions.GitHubAPIException
6+
import codecheck.github.exceptions.NotFoundException
7+
import scala.concurrent.Await
8+
import scala.concurrent.ExecutionContext.Implicits.global
9+
import codecheck.github.models.UserInput
10+
11+
class BranchOpSpec extends FunSpec
12+
with Constants
13+
with BeforeAndAfterAll
14+
{
15+
describe("getBranch") {
16+
it("with valid repo and branch should succeed") {
17+
val branchOp = Await.result(api.getBranch(user, userRepo, "master"), TIMEOUT)
18+
assert(branchOp.isDefined)
19+
assert(branchOp.get.name == "master")
20+
}
21+
it("with invalid branch should be None") {
22+
val branchOp = Await.result(api.getBranch(user, userRepo, "unknown"), TIMEOUT)
23+
assert(branchOp.isEmpty)
24+
}
25+
}
26+
27+
describe("listBranches") {
28+
it("with valid repo should succeed") {
29+
val list = Await.result(api.listBranches(user, userRepo), TIMEOUT)
30+
assert(list.length > 0)
31+
assert(list.exists(_.name == "master"))
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)