diff --git a/src/main/scala/gitbucket/core/api/ApiBranch.scala b/src/main/scala/gitbucket/core/api/ApiBranch.scala index a33f7a9..1852364 100644 --- a/src/main/scala/gitbucket/core/api/ApiBranch.scala +++ b/src/main/scala/gitbucket/core/api/ApiBranch.scala @@ -9,7 +9,7 @@ case class ApiBranch(name: String, commit: ApiBranchCommit, protection: ApiBranchProtection)( repositoryName: RepositoryName ) extends FieldSerializable { - def _links = + val _links = Map( "self" -> ApiPath(s"/api/v3/repos/${repositoryName.fullName}/branches/${name}"), "html" -> ApiPath(s"/${repositoryName.fullName}/tree/${name}") diff --git a/src/test/scala/gitbucket/core/api/ApiSpecModels.scala b/src/test/scala/gitbucket/core/api/ApiSpecModels.scala new file mode 100644 index 0000000..6e701c1 --- /dev/null +++ b/src/test/scala/gitbucket/core/api/ApiSpecModels.scala @@ -0,0 +1,269 @@ +package gitbucket.core.api + +import java.util.{Base64, Calendar, Date, TimeZone} + +import gitbucket.core.model.Account +import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo} +import gitbucket.core.util.RepositoryName +import org.eclipse.jgit.diff.DiffEntry.ChangeType + +object ApiSpecModels { + + implicit val context = JsonFormat.Context("http://gitbucket.exmple.com", None) + + val date1 = { + val d = Calendar.getInstance(TimeZone.getTimeZone("UTC")) + d.set(2011, 3, 14, 16, 0, 49) + d.getTime + } + + def date(date: String): Date = { + val f = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") + f.setTimeZone(TimeZone.getTimeZone("UTC")) + f.parse(date) + } + + val account = Account( + userName = "octocat", + fullName = "octocat", + mailAddress = "octocat@example.com", + password = "1234", + isAdmin = false, + url = None, + registeredDate = date1, + updatedDate = date1, + lastLoginDate = Some(date1), + image = None, + isGroupAccount = false, + isRemoved = false, + description = None + ) + + val sha1 = "6dcb09b5b57875f334f61aebed695e2e4193db5e" + val repo1Name = RepositoryName("octocat/Hello-World") + + val apiUser = ApiUser(account) + + val repository = ApiRepository( + name = repo1Name.name, + full_name = repo1Name.fullName, + description = "This your first repo!", + watchers = 0, + forks = 0, + `private` = false, + default_branch = "master", + owner = apiUser + )(urlIsHtmlUrl = false) + + val apiCommitStatus = ApiCommitStatus( + created_at = date1, + updated_at = date1, + state = "success", + target_url = Some("https://ci.example.com/1000/output"), + description = Some("Build has completed successfully"), + id = 1, + context = "Default", + creator = apiUser + )(sha1, repo1Name) + + val apiCommit = ApiCommit( + id = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + message = "Update README.md", + timestamp = date1, + added = Nil, + removed = Nil, + modified = List("README.md"), + author = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1), + committer = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1) + )(RepositoryName("baxterthehacker", "public-repo"), true) + + val apiComment = ApiComment( + id = 1, + user = apiUser, + body = "Me too", + created_at = date1, + updated_at = date1 + )(RepositoryName("octocat", "Hello-World"), 100, false) + + val apiCommentPR = ApiComment( + id = 1, + user = apiUser, + body = "Me too", + created_at = date1, + updated_at = date1 + )(RepositoryName("octocat", "Hello-World"), 100, true) + + val apiPersonIdent = ApiPersonIdent("Monalisa Octocat", "support@example.com", date1) + + val apiCommitListItem = ApiCommitListItem( + sha = sha1, + commit = ApiCommitListItem.Commit( + message = "Fix all the bugs", + author = apiPersonIdent, + committer = apiPersonIdent + )(sha1, repo1Name), + author = Some(apiUser), + committer = Some(apiUser), + parents = Seq(ApiCommitListItem.Parent("6dcb09b5b57875f334f61aebed695e2e4193db5e")(repo1Name)) + )(repo1Name) + + val apiCombinedCommitStatus = ApiCombinedCommitStatus( + state = "success", + sha = sha1, + total_count = 2, + statuses = List(apiCommitStatus), + repository = repository + ) + + val apiLabel = ApiLabel( + name = "bug", + color = "f29513" + )(RepositoryName("octocat", "Hello-World")) + + val apiIssue = ApiIssue( + number = 1347, + title = "Found a bug", + user = apiUser, + labels = List(apiLabel), + state = "open", + body = "I'm having a problem with this.", + created_at = date1, + updated_at = date1 + )(RepositoryName("octocat", "Hello-World"), false) + + val apiIssuePR = ApiIssue( + number = 1347, + title = "Found a bug", + user = apiUser, + labels = List(apiLabel), + state = "open", + body = "I'm having a problem with this.", + created_at = date1, + updated_at = date1 + )(RepositoryName("octocat", "Hello-World"), true) + + val apiPullRequest = ApiPullRequest( + number = 1347, + state = "open", + updated_at = date1, + created_at = date1, + head = ApiPullRequest.Commit(sha = sha1, ref = "new-topic", repo = repository)("octocat"), + base = ApiPullRequest.Commit(sha = sha1, ref = "master", repo = repository)("octocat"), + mergeable = None, + merged = false, + merged_at = Some(date1), + merged_by = Some(apiUser), + title = "new-feature", + body = "Please pull these awesome changes", + user = apiUser, + labels = List(apiLabel), + assignee = Some(apiUser) + ) + + // https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent + val apiPullRequestReviewComment = ApiPullRequestReviewComment( + id = 29724692, + // "diff_hunk": "@@ -1 +1 @@\n-# public-repo", + path = "README.md", + // "position": 1, + // "original_position": 1, + commit_id = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + // "original_commit_id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", + user = apiUser, + body = "Maybe you should use more emoji on this line.", + created_at = date("2015-05-05T23:40:27Z"), + updated_at = date("2015-05-05T23:40:27Z") + )(RepositoryName("baxterthehacker/public-repo"), 1) + + val apiBranchProtection = ApiBranchProtection( + true, + Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci"))) + ) + + val apiBranch = ApiBranch( + name = "master", + commit = ApiBranchCommit("468cab6982b37db5eb167568210ec188673fb653"), + protection = apiBranchProtection + )( + repositoryName = repo1Name + ) + + val apiBranchForList = ApiBranchForList("master", ApiBranchCommit("468cab6982b37db5eb167568210ec188673fb653")) + + val apiPusher = ApiPusher(account) + + val apiEndPoint = ApiEndPoint() + + // TODO use factory method defined in companion object? + val apiPlugin = ApiPlugin( + id = "gist", + name = "Gist Plugin", + version = "4.16.0", + description = "Provides Gist feature on GitBucket.", + jarFileName = "gitbucket-gist-plugin-gitbucket_4.30.0-SNAPSHOT-4.17.0.jar" + ) + + val apiError = ApiError( + message = "A repository with this name already exists on this account", + documentation_url = Some("https://developer.github.com/v3/repos/#create") + ) + + // TODO use factory method defined in companion object? + val apiGroup = ApiGroup("octocats", Some("Admin group"), date1) + + val apiRef = ApiRef( + ref = "refs/heads/featureA", + `object` = ApiObject("aa218f56b14c9653891f9e74264a383fa43fefbd") + ) + + // TODO use factory method defined in companion object? + val apiContents = ApiContents( + `type` = "file", + name = "README.md", + path = "README.md", + sha = "3d21ec53a331a6f037a91c368710b99387d012c1", + content = Some(Base64.getEncoder.encodeToString("README".getBytes("UTF-8"))), + encoding = Some("base64") + )(repo1Name) + + val apiCommits = ApiCommits( + repositoryName = repo1Name, + commitInfo = CommitInfo( + id = "3d21ec53a331a6f037a91c368710b99387d012c1", + shortMessage = "short message", + fullMessage = "full message", + parents = List("1da452aa92d7db1bc093d266c80a69857718c406"), + authorTime = date1, + authorName = "octocat", + authorEmailAddress = "octocat@example.com", + commitTime = date1, + committerName = "octocat", + committerEmailAddress = "octocat@example.com" + ), + diffs = Seq( + DiffInfo( + changeType = ChangeType.MODIFY, + oldPath = "README.md", + newPath = "README.md", + oldContent = None, + newContent = None, + oldIsImage = false, + newIsImage = false, + oldObjectId = None, + newObjectId = Some("6dcb09b5b57875f334f61aebed695e2e4193db5e"), + oldMode = "old_mode", + newMode = "new_mode", + tooLarge = false, + patch = Some("""@@ -1 +1,2 @@ + |-body1 + |\ No newline at end of file + |+body1 + |+body2 + |\ No newline at end of file""".stripMargin) + ) + ), + author = account, + committer = account, + commentCount = 1 + ) +} diff --git a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala index 173d93f..7b59a40 100644 --- a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala +++ b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala @@ -1,30 +1,12 @@ package gitbucket.core.api -import gitbucket.core.util.RepositoryName - import org.json4s.jackson.JsonMethods.parse import org.json4s._ import org.scalatest.FunSuite -import java.util.{Calendar, TimeZone, Date} - class JsonFormatSpec extends FunSuite { - val date1 = { - val d = Calendar.getInstance(TimeZone.getTimeZone("UTC")) - d.set(2011, 3, 14, 16, 0, 49) - d.getTime - } - def date(date: String): Date = { - val f = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'") - f.setTimeZone(TimeZone.getTimeZone("UTC")) - f.parse(date) - } - val sha1 = "6dcb09b5b57875f334f61aebed695e2e4193db5e" - val repo1Name = RepositoryName("octocat/Hello-World") - implicit val context = JsonFormat.Context("http://gitbucket.exmple.com", None) + import ApiSpecModels._ - val apiUser = - ApiUser(login = "octocat", email = "octocat@example.com", `type` = "User", site_admin = false, created_at = date1) val apiUserJson = """{ "login":"octocat", "email":"octocat@example.com", @@ -37,16 +19,6 @@ "avatar_url":"http://gitbucket.exmple.com/octocat/_avatar" }""" - val repository = ApiRepository( - name = repo1Name.name, - full_name = repo1Name.fullName, - description = "This your first repo!", - watchers = 0, - forks = 0, - `private` = false, - default_branch = "master", - owner = apiUser - )(urlIsHtmlUrl = false) val repositoryJson = s"""{ "name" : "Hello-World", "full_name" : "octocat/Hello-World", @@ -65,16 +37,6 @@ "html_url" : "${context.baseUrl}/octocat/Hello-World" }""" - val apiCommitStatus = ApiCommitStatus( - created_at = date1, - updated_at = date1, - state = "success", - target_url = Some("https://ci.example.com/1000/output"), - description = Some("Build has completed successfully"), - id = 1, - context = "Default", - creator = apiUser - )(sha1, repo1Name) val apiCommitStatusJson = s"""{ "created_at":"2011-04-14T16:00:49Z", "updated_at":"2011-04-14T16:00:49Z", @@ -87,16 +49,6 @@ "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/statuses" }""" - val apiPushCommit = ApiCommit( - id = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - message = "Update README.md", - timestamp = date1, - added = Nil, - removed = Nil, - modified = List("README.md"), - author = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1), - committer = ApiPersonIdent("baxterthehacker", "baxterthehacker@users.noreply.github.com", date1) - )(RepositoryName("baxterthehacker", "public-repo"), true) val apiPushCommitJson = s"""{ "id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", // "distinct": true, @@ -126,11 +78,6 @@ ] }""" - val apiComment = ApiComment(id = 1, user = apiUser, body = "Me too", created_at = date1, updated_at = date1)( - RepositoryName("octocat", "Hello-World"), - 100, - false - ) val apiCommentJson = s"""{ "id": 1, "body": "Me too", @@ -140,11 +87,6 @@ "updated_at": "2011-04-14T16:00:49Z" }""" - val apiCommentPR = ApiComment(id = 1, user = apiUser, body = "Me too", created_at = date1, updated_at = date1)( - RepositoryName("octocat", "Hello-World"), - 100, - true - ) val apiCommentPRJson = s"""{ "id": 1, "body": "Me too", @@ -154,24 +96,12 @@ "updated_at": "2011-04-14T16:00:49Z" }""" - val apiPersonIdent = ApiPersonIdent("Monalisa Octocat", "support@example.com", date1) val apiPersonIdentJson = """ { "name": "Monalisa Octocat", "email": "support@example.com", "date": "2011-04-14T16:00:49Z" }""" - val apiCommitListItem = ApiCommitListItem( - sha = sha1, - commit = ApiCommitListItem.Commit( - message = "Fix all the bugs", - author = apiPersonIdent, - committer = apiPersonIdent - )(sha1, repo1Name), - author = Some(apiUser), - committer = Some(apiUser), - parents = Seq(ApiCommitListItem.Parent("6dcb09b5b57875f334f61aebed695e2e4193db5e")(repo1Name)) - )(repo1Name) val apiCommitListItemJson = s"""{ "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", @@ -191,13 +121,6 @@ ] }""" - val apiCombinedCommitStatus = ApiCombinedCommitStatus( - state = "success", - sha = sha1, - total_count = 2, - statuses = List(apiCommitStatus), - repository = repository - ) val apiCombinedCommitStatusJson = s"""{ "state": "success", "sha": "$sha1", @@ -207,23 +130,12 @@ "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/$sha1/status" }""" - val apiLabel = ApiLabel(name = "bug", color = "f29513")(RepositoryName("octocat", "Hello-World")) val apiLabelJson = s"""{ "name": "bug", "color": "f29513", "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/labels/bug" }""" - val apiIssue = ApiIssue( - number = 1347, - title = "Found a bug", - user = apiUser, - labels = List(apiLabel), - state = "open", - body = "I'm having a problem with this.", - created_at = date1, - updated_at = date1 - )(RepositoryName("octocat", "Hello-World"), false) val apiIssueJson = s"""{ "number": 1347, "state": "open", @@ -238,16 +150,6 @@ "updated_at": "2011-04-14T16:00:49Z" }""" - val apiIssuePR = ApiIssue( - number = 1347, - title = "Found a bug", - user = apiUser, - labels = List(apiLabel), - state = "open", - body = "I'm having a problem with this.", - created_at = date1, - updated_at = date1 - )(RepositoryName("octocat", "Hello-World"), true) val apiIssuePRJson = s"""{ "number": 1347, "state": "open", @@ -268,24 +170,6 @@ "updated_at": "2011-04-14T16:00:49Z" }""" - val apiPullRequest = ApiPullRequest( - number = 1347, - state = "open", - updated_at = date1, - created_at = date1, - head = ApiPullRequest.Commit(sha = sha1, ref = "new-topic", repo = repository)("octocat"), - base = ApiPullRequest.Commit(sha = sha1, ref = "master", repo = repository)("octocat"), - mergeable = None, - merged = false, - merged_at = Some(date1), - merged_by = Some(apiUser), - title = "new-feature", - body = "Please pull these awesome changes", - user = apiUser, - labels = List(apiLabel), - assignee = Some(apiUser) - ) - val apiPullRequestJson = s"""{ "number": 1347, "state" : "open", @@ -335,20 +219,6 @@ // "changed_files": 5 }""" - // https://developer.github.com/v3/activity/events/types/#pullrequestreviewcommentevent - val apiPullRequestReviewComment = ApiPullRequestReviewComment( - id = 29724692, - // "diff_hunk": "@@ -1 +1 @@\n-# public-repo", - path = "README.md", - // "position": 1, - // "original_position": 1, - commit_id = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - // "original_commit_id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - user = apiUser, - body = "Maybe you should use more emoji on this line.", - created_at = date("2015-05-05T23:40:27Z"), - updated_at = date("2015-05-05T23:40:27Z") - )(RepositoryName("baxterthehacker/public-repo"), 1) val apiPullRequestReviewCommentJson = s"""{ "url": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/comments/29724692", "id": 29724692, @@ -377,10 +247,6 @@ } }""" - val apiBranchProtection = ApiBranchProtection( - true, - Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci"))) - ) val apiBranchProtectionJson = """{ "enabled": true, "required_status_checks": { @@ -389,7 +255,113 @@ "continuous-integration/travis-ci" ] } -}""" + }""" + + val apiBranchJson = s"""{ + "name": "master", + "commit": {"sha": "468cab6982b37db5eb167568210ec188673fb653"}, + "protection": $apiBranchProtectionJson, + "_links": { + "self": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master", + "html": "http://gitbucket.exmple.com/octocat/Hello-World/tree/master" + } + }""" + + val apiBranchForListJson = """{"name": "master", "commit": {"sha": "468cab6982b37db5eb167568210ec188673fb653"}}""" + + val apiPluginJson = """{ + "id": "gist", + "name": "Gist Plugin", + "version": "4.16.0", + "description": "Provides Gist feature on GitBucket.", + "jarFileName": "gitbucket-gist-plugin-gitbucket_4.30.0-SNAPSHOT-4.17.0.jar" + }""" + + val apiPusherJson = """{"name":"octocat","email":"octocat@example.com"}""" + + val apiEndPointJson = """{"rate_limit_url":"http://gitbucket.exmple.com/api/v3/rate_limit"}""" + + val apiErrorJson = """{ + "message": "A repository with this name already exists on this account", + "documentation_url": "https://developer.github.com/v3/repos/#create" + }""" + + val apiGroupJson = """{ + "login": "octocats", + "description": "Admin group", + "created_at": "2011-04-14T16:00:49Z", + "id": 0, + "url": "http://gitbucket.exmple.com/api/v3/orgs/octocats", + "html_url": "http://gitbucket.exmple.com/octocats", + "avatar_url": "http://gitbucket.exmple.com/octocats/_avatar" + }""".stripMargin + + val apiRefJson = """{ + "ref": "refs/heads/featureA", + "object": {"sha":"aa218f56b14c9653891f9e74264a383fa43fefbd"} + }""".stripMargin + + val apiContentsJson = + """{ + "type": "file", + "name": "README.md", + "path": "README.md", + "sha": "3d21ec53a331a6f037a91c368710b99387d012c1", + "content": "UkVBRE1F", + "encoding": "base64", + "download_url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/raw/3d21ec53a331a6f037a91c368710b99387d012c1/README.md" + }""" + + val apiCommitsJson = s"""{ + "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/3d21ec53a331a6f037a91c368710b99387d012c1", + "sha": "3d21ec53a331a6f037a91c368710b99387d012c1", + "html_url": "http://gitbucket.exmple.comoctocat/Hello-World/commit/3d21ec53a331a6f037a91c368710b99387d012c1", + "comment_url": "http://gitbucket.exmple.com", + "commit": { + "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/3d21ec53a331a6f037a91c368710b99387d012c1", + "author": { + "name": "octocat", + "email": "octocat@example.com", + "date": "2011-04-14T16:00:49Z" + }, + "committer": { + "name": "octocat", + "email": "octocat@example.com", + "date": "2011-04-14T16:00:49Z" + }, + "message": "short message", + "comment_count": 1, + "tree": { + "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/tree/3d21ec53a331a6f037a91c368710b99387d012c1", + "sha": "3d21ec53a331a6f037a91c368710b99387d012c1" + } + }, + "author": $apiUserJson, + "committer": $apiUserJson, + "parents": [ + { + "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/tree/1da452aa92d7db1bc093d266c80a69857718c406", + "sha": "1da452aa92d7db1bc093d266c80a69857718c406" + } + ], + "stats": { + "additions": 2, + "deletions": 1, + "total": 3 + }, + "files": [ + { + "filename": "README.md", + "additions": 2, + "deletions": 1, + "changes": 3, + "status": "modified", + "raw_url": "http://gitbucket.exmple.com/octocat/Hello-World/raw/3d21ec53a331a6f037a91c368710b99387d012c1/README.md", + "blob_url": "http://gitbucket.exmple.com/octocat/Hello-World/blob/3d21ec53a331a6f037a91c368710b99387d012c1/README.md", + "patch": "@@ -1 +1,2 @@\\n-body1\\n\\\\ No newline at end of file\\n+body1\\n+body2\\n\\\\ No newline at end of file" + } + ] + }""" def assertJson(resultJson: String, expectJson: String) = { import java.util.regex.Pattern @@ -414,7 +386,7 @@ assertJson(JsonFormat(repository), repositoryJson) } test("apiPushCommit") { - assertJson(JsonFormat(apiPushCommit), apiPushCommitJson) + assertJson(JsonFormat(apiCommit), apiPushCommitJson) } test("apiComment") { assertJson(JsonFormat(apiComment), apiCommentJson) @@ -445,4 +417,32 @@ test("apiBranchProtection") { assertJson(JsonFormat(apiBranchProtection), apiBranchProtectionJson) } + test("apiBranch") { + assertJson(JsonFormat(apiBranch), apiBranchJson) + assertJson(JsonFormat(apiBranchForList), apiBranchForListJson) + } + test("apiCommits") { + assertJson(JsonFormat(apiCommits), apiCommitsJson) + } + test("apiContents") { + assertJson(JsonFormat(apiContents), apiContentsJson) + } + test("apiEndPoint") { + assertJson(JsonFormat(apiEndPoint), apiEndPointJson) + } + test("apiError") { + assertJson(JsonFormat(apiError), apiErrorJson) + } + test("apiGroup") { + assertJson(JsonFormat(apiGroup), apiGroupJson) + } + test("apiPlugin") { + assertJson(JsonFormat(apiPlugin), apiPluginJson) + } + test("apiPusher") { + assertJson(JsonFormat(apiPusher), apiPusherJson) + } + test("apiRef") { + assertJson(JsonFormat(apiRef), apiRefJson) + } }