Skip to content

Commit b61df6f

Browse files
committed
Merge pull request #51 from code-check/create-repo
Implement createUserRepository
2 parents 079aafa + e20eaf9 commit b61df6f

File tree

3 files changed

+49
-7
lines changed

3 files changed

+49
-7
lines changed

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package codecheck.github.models
22

3+
import java.net.URL
34
import org.json4s.JValue
45
import codecheck.github.utils.ToDo
56

@@ -46,7 +47,19 @@ case class RepositoryListOption(
4647
direction: SortDirection = SortDirection.asc
4748
)
4849

49-
/*case*/ class RepositoryInput extends ToDo
50+
case class RepositoryInput(
51+
name: String,
52+
description: Option[String] = None,
53+
homepage: Option[URL] = None,
54+
`private`: Boolean = false,
55+
has_issues: Boolean = true,
56+
has_wiki: Boolean = true,
57+
has_downloads: Boolean = true,
58+
team_id: Option[Int] = None,
59+
auto_init: Boolean = false,
60+
gitignore_template: Option[String] = None,
61+
license_template: Option[String] = None
62+
) extends AbstractInput
5063

5164
case class Repository(value: JValue) extends AbstractJson(value) {
5265
def id = get("id").toLong
@@ -65,4 +78,4 @@ case class Permissions(value: JValue) extends AbstractJson(value) {
6578
def admin = boolean("admin")
6679
def push = boolean("push")
6780
def pull = boolean("pull")
68-
}
81+
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ trait RepositoryOp {
2222
}
2323
}
2424
}
25-
def listOwnRepositories(option: RepositoryListOption = RepositoryListOption()): Future[List[Repository]] =
25+
def listOwnRepositories(option: RepositoryListOption = RepositoryListOption()): Future[List[Repository]] =
2626
doList("/user/repos", option)
2727

28-
def listUserRepositories(user: String, option: RepositoryListOption = RepositoryListOption()): Future[List[Repository]] =
28+
def listUserRepositories(user: String, option: RepositoryListOption = RepositoryListOption()): Future[List[Repository]] =
2929
doList(s"/users/$user/repos", option)
3030

3131
def listOrgRepositories(org: String, option: RepositoryListOption = RepositoryListOption()): Future[List[Repository]] =
@@ -51,7 +51,11 @@ trait RepositoryOp {
5151
}
5252
}
5353

54-
def createRepository(input: RepositoryInput): Future[Repository] = ToDo[Future[Repository]]
54+
def createUserRepository(input: RepositoryInput): Future[Repository] = {
55+
exec("POST", s"/user/repos", input.value).map { res =>
56+
new Repository(res.body)
57+
}
58+
}
5559
def updateRepository(input: RepositoryInput): Future[Repository] = ToDo[Future[Repository]]
5660

5761
/*

src/test/scala/RepositoryOpSpec.scala

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import org.scalatest.path.FunSpec
22
import codecheck.github.exceptions.NotFoundException
33
import codecheck.github.models.Repository
4+
import codecheck.github.models.RepositoryInput
45
import codecheck.github.exceptions.GitHubAPIException
56
import codecheck.github.exceptions.NotFoundException
67
import scala.concurrent.Await
78
import scala.concurrent.ExecutionContext.Implicits.global
89

9-
class RepositoryOpSpec extends FunSpec with Constants
10+
class RepositoryOpSpec extends FunSpec with Constants
1011
{
1112

1213
describe("listOwnRepositories") {
@@ -45,7 +46,7 @@ class RepositoryOpSpec extends FunSpec with Constants
4546
assert(list.size > 0)
4647
}
4748

48-
49+
4950
}
5051
describe("getRepository") {
5152
it("should succeed") {
@@ -61,4 +62,28 @@ class RepositoryOpSpec extends FunSpec with Constants
6162
assert(Await.result(api.getRepository(organization, repoInvalid), TIMEOUT).isEmpty)
6263
}
6364
}
65+
66+
/*
67+
describe("createUserRepository") {
68+
val createRepoName = "create-repo-name"
69+
it("should succeed") {
70+
val input = RepositoryInput(name = createRepoName)
71+
val repo = Await.result(api.createUserRepository(input), TIMEOUT)
72+
assert(repo.owner.login == user)
73+
assert(repo.name == "create-repo-test")
74+
}
75+
it("should fail with existing repository name") {
76+
val input = RepositoryInput(name = createRepoName)
77+
try {
78+
val repo = Await.result(api.createUserRepository(input), TIMEOUT)
79+
fail
80+
} catch {
81+
case e: GitHubAPIException =>
82+
assert(e.error.errors.head.field == "name")
83+
case e: Throwable =>
84+
fail
85+
}
86+
}
87+
}
88+
*/
6489
}

0 commit comments

Comments
 (0)