diff --git a/src/main/scala/gitbucket/core/api/ApiRepository.scala b/src/main/scala/gitbucket/core/api/ApiRepository.scala index 8fe2c8a..86d5c68 100644 --- a/src/main/scala/gitbucket/core/api/ApiRepository.scala +++ b/src/main/scala/gitbucket/core/api/ApiRepository.scala @@ -51,10 +51,7 @@ ApiRepository(repositoryInfo.repository, owner, forkedCount = repositoryInfo.forkedCount) def apply(repositoryInfo: RepositoryInfo, owner: Account): ApiRepository = - this(repositoryInfo.repository, ApiUser(owner)) - - def forWebhookPayload(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = - ApiRepository(repositoryInfo.repository, owner, forkedCount = repositoryInfo.forkedCount, urlIsHtmlUrl = true) + this(repositoryInfo, ApiUser(owner)) def forDummyPayload(owner: ApiUser): ApiRepository = ApiRepository( diff --git a/src/main/scala/gitbucket/core/service/WebHookService.scala b/src/main/scala/gitbucket/core/service/WebHookService.scala index d392426..a9ab3c7 100644 --- a/src/main/scala/gitbucket/core/service/WebHookService.scala +++ b/src/main/scala/gitbucket/core/service/WebHookService.scala @@ -540,11 +540,8 @@ object WebHookCreatePayload { def apply( - git: Git, sender: Account, - refName: String, repositoryInfo: RepositoryInfo, - commits: List[CommitInfo], repositoryOwner: Account, ref: String, refType: String @@ -555,7 +552,7 @@ ref_type = refType, description = repositoryInfo.repository.description.getOrElse(""), master_branch = repositoryInfo.repository.defaultBranch, - repository = ApiRepository.forWebhookPayload(repositoryInfo, owner = ApiUser(repositoryOwner)) + repository = ApiRepository(repositoryInfo, repositoryOwner) ) } @@ -599,7 +596,7 @@ commits = commits.map { commit => ApiCommit.forWebhookPayload(git, RepositoryName(repositoryInfo), commit) }, - repository = ApiRepository.forWebhookPayload(repositoryInfo, owner = ApiUser(repositoryOwner)) + repository = ApiRepository(repositoryInfo, repositoryOwner) ) def createDummyPayload(sender: Account): WebHookPushPayload = diff --git a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala index 46dbcc4..dc0b143 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -380,11 +380,8 @@ } yield { val refType = if (refName(1) == "tags") "tag" else "branch" WebHookCreatePayload( - git, pusherAccount, - command.getRefName, repositoryInfo, - newCommits, ownerAccount, ref = branchName, refType = refType diff --git a/src/test/scala/gitbucket/core/api/ApiSpecModels.scala b/src/test/scala/gitbucket/core/api/ApiSpecModels.scala index 6e701c1..2231097 100644 --- a/src/test/scala/gitbucket/core/api/ApiSpecModels.scala +++ b/src/test/scala/gitbucket/core/api/ApiSpecModels.scala @@ -1,11 +1,15 @@ package gitbucket.core.api -import java.util.{Base64, Calendar, Date, TimeZone} +import java.util.{Calendar, Date, TimeZone} -import gitbucket.core.model.Account -import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo} +import gitbucket.core.model._ +import gitbucket.core.plugin.PluginInfo +import gitbucket.core.service.ProtectedBranchService.ProtectedBranchInfo +import gitbucket.core.service.RepositoryService.RepositoryInfo +import gitbucket.core.util.JGitUtil.{CommitInfo, DiffInfo, FileInfo, TagInfo} import gitbucket.core.util.RepositoryName import org.eclipse.jgit.diff.DiffEntry.ChangeType +import org.eclipse.jgit.lib.ObjectId object ApiSpecModels { @@ -23,6 +27,8 @@ f.parse(date) } + // Models + val account = Account( userName = "octocat", fullName = "octocat", @@ -42,228 +48,609 @@ val sha1 = "6dcb09b5b57875f334f61aebed695e2e4193db5e" val repo1Name = RepositoryName("octocat/Hello-World") + val repository = Repository( + userName = repo1Name.owner, + repositoryName = repo1Name.name, + isPrivate = false, + description = Some("This your first repo!"), + defaultBranch = "master", + registeredDate = date1, + updatedDate = date1, + lastActivityDate = date1, + originUserName = Some("octopus plus cat"), + originRepositoryName = Some("Hello World"), + parentUserName = Some("github"), + parentRepositoryName = Some("Hello-World"), + options = RepositoryOptions( + issuesOption = "PUBLIC", + externalIssuesUrl = Some("https://external.com/gitbucket"), + wikiOption = "PUBLIC", + externalWikiUrl = Some("https://external.com/gitbucket"), + allowFork = true, + mergeOptions = "merge-commit,squash,rebase", + defaultMergeOption = "merge-commit" + ) + ) + + val repositoryInfo = RepositoryInfo( + owner = repo1Name.owner, + name = repo1Name.name, + repository = repository, + issueCount = 1, + pullCount = 1, + forkedCount = 1, + branchList = Seq("master", "develop"), + tags = Seq( + TagInfo(name = "v1.0", time = date("2015-05-05T23:40:27Z"), id = "id1", message = "1.0 released"), + TagInfo(name = "v2.0", time = date("2016-05-05T23:40:27Z"), id = "id2", message = "2.0 released") + ), + managers = Seq("myboss") + ) + + val label = Label( + userName = repo1Name.owner, + repositoryName = repo1Name.name, + labelId = 10, + labelName = "bug", + color = "f29513" + ) + + val issue = Issue( + userName = repo1Name.owner, + repositoryName = repo1Name.name, + issueId = 1347, + openedUserName = "bear", + milestoneId = None, + priorityId = None, + assignedUserName = None, + title = "Found a bug", + content = Some("I'm having a problem with this."), + closed = false, + registeredDate = date1, + updatedDate = date1, + isPullRequest = false + ) + + val issuePR = issue.copy( + title = "new-feature", + content = Some("Please pull these awesome changes"), + closed = true, + isPullRequest = true + ) + + val issueComment = IssueComment( + userName = repo1Name.owner, + repositoryName = repo1Name.name, + issueId = issue.issueId, + commentId = 1, + action = "comment", + commentedUserName = "bear", + content = "Me too", + registeredDate = date1, + updatedDate = date1 + ) + + val pullRequest = PullRequest( + userName = repo1Name.owner, + repositoryName = repo1Name.name, + issueId = issuePR.issueId, + branch = "master", + requestUserName = "bear", + requestRepositoryName = repo1Name.name, + requestBranch = "new-topic", + commitIdFrom = sha1, + commitIdTo = sha1 + ) + + val commitComment = CommitComment( + userName = repo1Name.owner, + repositoryName = repo1Name.name, + commitId = sha1, + commentId = 29724692, + commentedUserName = "bear", + content = "Maybe you should use more emoji on this line.", + fileName = Some("README.md"), + oldLine = Some(1), + newLine = Some(1), + registeredDate = date("2015-05-05T23:40:27Z"), + updatedDate = date("2015-05-05T23:40:27Z"), + issueId = Some(issuePR.issueId), + originalCommitId = sha1, + originalOldLine = None, + originalNewLine = None + ) + + val commitStatus = CommitStatus( + commitStatusId = 1, + userName = repo1Name.owner, + repositoryName = repo1Name.name, + commitId = sha1, + context = "Default", + state = CommitState.SUCCESS, + targetUrl = Some("https://ci.example.com/1000/output"), + description = Some("Build has completed successfully"), + creator = account.userName, + registeredDate = date1, + updatedDate = date1 + ) + + // APIs + val apiUser = ApiUser(account) - val repository = ApiRepository( - name = repo1Name.name, - full_name = repo1Name.fullName, - description = "This your first repo!", + val apiRepository = ApiRepository( + repository = repository, + owner = apiUser, + forkedCount = repositoryInfo.forkedCount, 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 + urlIsHtmlUrl = false ) val apiLabel = ApiLabel( - name = "bug", - color = "f29513" - )(RepositoryName("octocat", "Hello-World")) + label = label, + repositoryName = repo1Name + ) val apiIssue = ApiIssue( - number = 1347, - title = "Found a bug", + issue = issue, + repositoryName = repo1Name, 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) + labels = List(apiLabel) + ) val apiIssuePR = ApiIssue( - number = 1347, - title = "Found a bug", + issue = issuePR, + repositoryName = repo1Name, 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) + labels = List(apiLabel) + ) + + val apiComment = ApiComment( + comment = issueComment, + repositoryName = repo1Name, + issueId = issueComment.issueId, + user = apiUser, + isPullRequest = false + ) + + val apiCommentPR = ApiComment( + comment = issueComment, + repositoryName = repo1Name, + issueId = issueComment.issueId, + user = apiUser, + isPullRequest = 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", + issue = issuePR, + pullRequest = pullRequest, + headRepo = apiRepository, + baseRepo = apiRepository, user = apiUser, labels = List(apiLabel), - assignee = Some(apiUser) + assignee = Some(apiUser), + mergedComment = Some((issueComment, account)) ) // 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) + comment = commitComment, + commentedUser = apiUser, + repositoryName = repo1Name, + issueId = commitComment.issueId.get + ) + + val commitInfo = (id: String) => + CommitInfo( + id = id, + shortMessage = "short message", + fullMessage = "full message", + parents = List("1da452aa92d7db1bc093d266c80a69857718c406"), + authorTime = date1, + authorName = account.userName, + authorEmailAddress = account.mailAddress, + commitTime = date1, + committerName = account.userName, + committerEmailAddress = account.mailAddress + ) + + val apiCommitListItem = ApiCommitListItem( + commit = commitInfo(sha1), + repositoryName = repo1Name + ) + + val apiCommit = { + val commit = commitInfo(sha1) + ApiCommit( + id = commit.id, + message = commit.fullMessage, + timestamp = commit.commitTime, + added = Nil, + removed = Nil, + modified = List("README.md"), + author = ApiPersonIdent.author(commit), + committer = ApiPersonIdent.committer(commit) + )(repo1Name, true) + } + + val apiCommits = ApiCommits( + repositoryName = repo1Name, + commitInfo = commitInfo(sha1), + diffs = Seq( + DiffInfo( + changeType = ChangeType.MODIFY, + oldPath = "doc/README.md", + newPath = "doc/README.md", + oldContent = None, + newContent = None, + oldIsImage = false, + newIsImage = false, + oldObjectId = None, + newObjectId = Some(sha1), + 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 = 2 + ) + + val apiCommitStatus = ApiCommitStatus( + status = commitStatus, + creator = apiUser + ) + + val apiCombinedCommitStatus = ApiCombinedCommitStatus( + sha = sha1, + statuses = Iterable((commitStatus, account)), + repository = apiRepository + ) val apiBranchProtection = ApiBranchProtection( - true, - Some(ApiBranchProtection.Status(ApiBranchProtection.Everyone, Seq("continuous-integration/travis-ci"))) + info = ProtectedBranchInfo( + owner = repo1Name.owner, + repository = repo1Name.name, + enabled = true, + contexts = Seq("continuous-integration/travis-ci"), + includeAdministrators = true + ) ) val apiBranch = ApiBranch( name = "master", - commit = ApiBranchCommit("468cab6982b37db5eb167568210ec188673fb653"), + commit = ApiBranchCommit(sha1), protection = apiBranchProtection )( repositoryName = repo1Name ) - val apiBranchForList = ApiBranchForList("master", ApiBranchCommit("468cab6982b37db5eb167568210ec188673fb653")) + val apiBranchForList = ApiBranchForList( + name = "master", + commit = ApiBranchCommit(sha1) + ) - val apiPusher = ApiPusher(account) + val apiContents = ApiContents( + fileInfo = FileInfo( + id = ObjectId.fromString(sha1), + isDirectory = false, + name = "README.md", + path = "doc/README.md", + message = "message", + commitId = sha1, + time = date1, + author = account.userName, + mailAddress = account.mailAddress, + linkUrl = None + ), + repositoryName = repo1Name, + content = Some("README".getBytes("UTF-8")) + ) 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 apiGroup = ApiGroup( + account.copy( + isAdmin = true, + isGroupAccount = true, + description = Some("Admin group") + ) + ) + + val apiPlugin = ApiPlugin( + plugin = PluginInfo( + pluginId = "gist", + pluginName = "Gist Plugin", + pluginVersion = "4.16.0", + gitbucketVersion = Some("4.30.1"), + description = "Provides Gist feature on GitBucket.", + pluginClass = null, + pluginJar = new java.io.File("gitbucket-gist-plugin-gitbucket_4.30.0-SNAPSHOT-4.17.0.jar"), + classLoader = null + ) + ) + + val apiPusher = ApiPusher(account) val apiRef = ApiRef( ref = "refs/heads/featureA", - `object` = ApiObject("aa218f56b14c9653891f9e74264a383fa43fefbd") + `object` = ApiObject(sha1) ) - // 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) + // JSON String for APIs - 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 - ) + val jsonUser = """{ + |"login":"octocat", + |"email":"octocat@example.com", + |"type":"User", + |"site_admin":false, + |"created_at":"2011-04-14T16:00:49Z", + |"id":0, + |"url":"http://gitbucket.exmple.com/api/v3/users/octocat", + |"html_url":"http://gitbucket.exmple.com/octocat", + |"avatar_url":"http://gitbucket.exmple.com/octocat/_avatar" + |}""".stripMargin + + val jsonRepository = s"""{ + |"name":"Hello-World", + |"full_name":"octocat/Hello-World", + |"description":"This your first repo!", + |"watchers":0, + |"forks":1, + |"private":false, + |"default_branch":"master", + |"owner":$jsonUser, + |"id":0, + |"forks_count":1, + |"watchers_count":0, + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World", + |"http_url":"http://gitbucket.exmple.com/git/octocat/Hello-World.git", + |"clone_url":"http://gitbucket.exmple.com/git/octocat/Hello-World.git", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World" + |}""".stripMargin + + val jsonLabel = + """{"name":"bug","color":"f29513","url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/labels/bug"}""" + + val jsonIssue = s"""{ + |"number":1347, + |"title":"Found a bug", + |"user":$jsonUser, + |"labels":[$jsonLabel], + |"state":"open", + |"created_at":"2011-04-14T16:00:49Z", + |"updated_at":"2011-04-14T16:00:49Z", + |"body":"I'm having a problem with this.", + |"id":0, + |"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347" + |}""".stripMargin + + // TODO comments_url is correct? + val jsonIssuePR = s"""{ + |"number":1347, + |"title":"new-feature", + |"user":$jsonUser, + |"labels":[$jsonLabel], + |"state":"closed", + |"created_at":"2011-04-14T16:00:49Z", + |"updated_at":"2011-04-14T16:00:49Z", + |"body":"Please pull these awesome changes", + |"id":0, + |"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347", + |"pull_request":{ + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347"} + |}""".stripMargin + + // TODO comments_url is correct? + val jsonPullRequest = s"""{ + |"number":1347, + |"state":"closed", + |"updated_at":"2011-04-14T16:00:49Z", + |"created_at":"2011-04-14T16:00:49Z", + |"head":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"new-topic","repo":$jsonRepository,"label":"new-topic","user":$jsonUser}, + |"base":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","ref":"master","repo":$jsonRepository,"label":"master","user":$jsonUser}, + |"merged":true, + |"merged_at":"2011-04-14T16:00:49Z", + |"merged_by":$jsonUser, + |"title":"new-feature", + |"body":"Please pull these awesome changes", + |"user":$jsonUser, + |"labels":[$jsonLabel], + |"assignee":$jsonUser, + |"id":0, + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347", + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347", + |"commits_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/commits", + |"review_comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347/comments", + |"review_comment_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/{number}", + |"comments_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/issues/1347/comments", + |"statuses_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e" + |}""".stripMargin + + val jsonPullRequestReviewComment = s"""{ + |"id":29724692, + |"path":"README.md", + |"commit_id":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"user":$jsonUser, + |"body":"Maybe you should use more emoji on this line.", + |"created_at":"2015-05-05T23:40:27Z", + |"updated_at":"2015-05-05T23:40:27Z", + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692", + |"pull_request_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347", + |"_links":{ + |"self":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/comments/29724692"}, + |"html":{"href":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#discussion_r29724692"}, + |"pull_request":{"href":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/pulls/1347"}} + |}""".stripMargin + + val jsonComment = s"""{ + |"id":1, + |"user":$jsonUser, + |"body":"Me too", + |"created_at":"2011-04-14T16:00:49Z", + |"updated_at":"2011-04-14T16:00:49Z", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/issues/1347#comment-1" + |}""".stripMargin + + val jsonCommentPR = s"""{ + |"id":1, + |"user":$jsonUser, + |"body":"Me too", + |"created_at":"2011-04-14T16:00:49Z", + |"updated_at":"2011-04-14T16:00:49Z", + |"html_url":"http://gitbucket.exmple.com/octocat/Hello-World/pull/1347#comment-1" + |}""".stripMargin + + val jsonCommitListItem = s"""{ + |"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"commit":{ + |"message":"full message", + |"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"}, + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e" + |}, + |"parents":[{ + |"sha":"1da452aa92d7db1bc093d266c80a69857718c406", + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/1da452aa92d7db1bc093d266c80a69857718c406"}], + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e" + |}""".stripMargin + + // TODO url is correct? + val jsonCommit = (id: String) => s"""{ + |"id":"$id", + |"message":"full message", + |"timestamp":"2011-04-14T16:00:49Z", + |"added":[], + |"removed":[], + |"modified":["README.md"], + |"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"}, + |"url":"http://gitbucket.exmple.com/octocat/Hello-World/commit/$id" + |}""".stripMargin + + // TODO comment_url + val jsonCommits = s"""{ + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"html_url":"http://gitbucket.exmple.comoctocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"comment_url":"http://gitbucket.exmple.com", + |"commit":{ + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"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":2, + |"tree":{"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/tree/6dcb09b5b57875f334f61aebed695e2e4193db5e","sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"} + |}, + |"author":$jsonUser, + |"committer":$jsonUser, + |"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":"doc/README.md", + |"additions":2, + |"deletions":1, + |"changes":3, + |"status":"modified", + |"raw_url":"http://gitbucket.exmple.com/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/doc/README.md", + |"blob_url":"http://gitbucket.exmple.com/octocat/Hello-World/blob/6dcb09b5b57875f334f61aebed695e2e4193db5e/doc/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"}] + |}""".stripMargin + + val jsonCommitStatus = s"""{ + |"created_at":"2011-04-14T16:00:49Z", + |"updated_at":"2011-04-14T16:00:49Z", + |"state":"success", + |"target_url":"https://ci.example.com/1000/output", + |"description":"Build has completed successfully", + |"id":1, + |"context":"Default", + |"creator":$jsonUser, + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/statuses" + |}""".stripMargin + + val jsonCombinedCommitStatus = s"""{ + |"state":"success", + |"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"total_count":1, + |"statuses":[$jsonCommitStatus], + |"repository":$jsonRepository, + |"url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/status" + |}""".stripMargin + + val jsonBranchProtection = + """{ + |"enabled":true, + |"required_status_checks":{"enforcement_level":"everyone","contexts":["continuous-integration/travis-ci"]} + |}""".stripMargin + + val jsonBranch = s"""{ + |"name":"master", + |"commit":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"}, + |"protection":$jsonBranchProtection, + |"_links":{ + |"self":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/branches/master", + |"html":"http://gitbucket.exmple.com/octocat/Hello-World/tree/master"} + |}""".stripMargin + + val jsonBranchForList = """{"name":"master","commit":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"}}""" + + val jsonContents = + """{ + |"type":"file", + |"name":"README.md", + |"path":"doc/README.md", + |"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"content":"UkVBRE1F", + |"encoding":"base64", + |"download_url":"http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/raw/6dcb09b5b57875f334f61aebed695e2e4193db5e/doc/README.md" + |}""".stripMargin + + val jsonEndPoint = """{"rate_limit_url":"http://gitbucket.exmple.com/api/v3/rate_limit"}""" + + val jsonError = """{ + |"message":"A repository with this name already exists on this account", + |"documentation_url":"https://developer.github.com/v3/repos/#create" + |}""".stripMargin + + val jsonGroup = """{ + |"login":"octocat", + |"description":"Admin group", + |"created_at":"2011-04-14T16:00:49Z", + |"id":0, + |"url":"http://gitbucket.exmple.com/api/v3/orgs/octocat", + |"html_url":"http://gitbucket.exmple.com/octocat", + |"avatar_url":"http://gitbucket.exmple.com/octocat/_avatar" + |}""".stripMargin + + val jsonPlugin = """{ + |"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" + |}""".stripMargin + + val jsonPusher = """{"name":"octocat","email":"octocat@example.com"}""" + + val jsonRef = """{"ref":"refs/heads/featureA","object":{"sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e"}}""" + } diff --git a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala index 7b59a40..0cf00cd 100644 --- a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala +++ b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala @@ -1,448 +1,76 @@ package gitbucket.core.api -import org.json4s.jackson.JsonMethods.parse -import org.json4s._ import org.scalatest.FunSuite class JsonFormatSpec extends FunSuite { import ApiSpecModels._ - val apiUserJson = """{ - "login":"octocat", - "email":"octocat@example.com", - "type":"User", - "site_admin":false, - "id": 0, - "created_at":"2011-04-14T16:00:49Z", - "url":"http://gitbucket.exmple.com/api/v3/users/octocat", - "html_url":"http://gitbucket.exmple.com/octocat", - "avatar_url":"http://gitbucket.exmple.com/octocat/_avatar" - }""" - - val repositoryJson = s"""{ - "name" : "Hello-World", - "full_name" : "octocat/Hello-World", - "description" : "This your first repo!", - "id": 0, - "watchers" : 0, - "forks" : 0, - "private" : false, - "default_branch" : "master", - "owner" : $apiUserJson, - "forks_count" : 0, - "watchers_count" : 0, - "url" : "${context.baseUrl}/api/v3/repos/octocat/Hello-World", - "http_url" : "${context.baseUrl}/git/octocat/Hello-World.git", - "clone_url" : "${context.baseUrl}/git/octocat/Hello-World.git", - "html_url" : "${context.baseUrl}/octocat/Hello-World" - }""" - - val apiCommitStatusJson = s"""{ - "created_at":"2011-04-14T16:00:49Z", - "updated_at":"2011-04-14T16:00:49Z", - "state":"success", - "target_url":"https://ci.example.com/1000/output", - "description":"Build has completed successfully", - "id":1, - "context":"Default", - "creator":$apiUserJson, - "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/statuses" - }""" - - val apiPushCommitJson = s"""{ - "id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - // "distinct": true, - "message": "Update README.md", - "timestamp": "2011-04-14T16:00:49Z", - "url": "http://gitbucket.exmple.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", - "author": { - "name": "baxterthehacker", - "email": "baxterthehacker@users.noreply.github.com", - // "username": "baxterthehacker", - "date" : "2011-04-14T16:00:49Z" - }, - "committer": { - "name": "baxterthehacker", - "email": "baxterthehacker@users.noreply.github.com", - // "username": "baxterthehacker", - "date" : "2011-04-14T16:00:49Z" - }, - "added": [ - - ], - "removed": [ - - ], - "modified": [ - "README.md" - ] - }""" - - val apiCommentJson = s"""{ - "id": 1, - "body": "Me too", - "user": $apiUserJson, - "html_url" : "${context.baseUrl}/octocat/Hello-World/issues/100#comment-1", - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z" - }""" - - val apiCommentPRJson = s"""{ - "id": 1, - "body": "Me too", - "user": $apiUserJson, - "html_url" : "${context.baseUrl}/octocat/Hello-World/pull/100#comment-1", - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z" - }""" - - val apiPersonIdentJson = """ { - "name": "Monalisa Octocat", - "email": "support@example.com", - "date": "2011-04-14T16:00:49Z" - }""" - - val apiCommitListItemJson = s"""{ - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", - "commit": { - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/git/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "author": $apiPersonIdentJson, - "committer": $apiPersonIdentJson, - "message": "Fix all the bugs" - }, - "author": $apiUserJson, - "committer": $apiUserJson, - "parents": [ - { - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e" - } - ] - }""" - - val apiCombinedCommitStatusJson = s"""{ - "state": "success", - "sha": "$sha1", - "total_count": 2, - "statuses": [ $apiCommitStatusJson ], - "repository": $repositoryJson, - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/commits/$sha1/status" - }""" - - val apiLabelJson = s"""{ - "name": "bug", - "color": "f29513", - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/labels/bug" - }""" - - val apiIssueJson = s"""{ - "number": 1347, - "state": "open", - "title": "Found a bug", - "body": "I'm having a problem with this.", - "user": $apiUserJson, - "id": 0, - "labels": [$apiLabelJson], - "comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments", - "html_url": "${context.baseUrl}/octocat/Hello-World/issues/1347", - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z" - }""" - - val apiIssuePRJson = s"""{ - "number": 1347, - "state": "open", - "title": "Found a bug", - "body": "I'm having a problem with this.", - "user": $apiUserJson, - "id": 0, - "labels": [$apiLabelJson], - "comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments", - "html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347", - "pull_request": { - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347", - "html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347" - // "diff_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.diff", - // "patch_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.patch" - }, - "created_at": "2011-04-14T16:00:49Z", - "updated_at": "2011-04-14T16:00:49Z" - }""" - - val apiPullRequestJson = s"""{ - "number": 1347, - "state" : "open", - "id": 0, - "updated_at": "2011-04-14T16:00:49Z", - "created_at": "2011-04-14T16:00:49Z", - // "closed_at": "2011-04-14T16:00:49Z", - "head": { - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", - "ref": "new-topic", - "repo": $repositoryJson, - "label": "new-topic", - "user": $apiUserJson - }, - "base": { - "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", - "ref": "master", - "repo": $repositoryJson, - "label": "master", - "user": $apiUserJson - }, - // "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6", - // "mergeable": true, - "merged": false, - "merged_at": "2011-04-14T16:00:49Z", - "merged_by": $apiUserJson, - "title": "new-feature", - "body": "Please pull these awesome changes", - "user": $apiUserJson, - "assignee": $apiUserJson, - "labels": [$apiLabelJson], - "html_url": "${context.baseUrl}/octocat/Hello-World/pull/1347", - "url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347", - "commits_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/commits", - "review_comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/1347/comments", - "review_comment_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/pulls/comments/{number}", - "comments_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347/comments", - "statuses_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e" - // "diff_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.diff", - // "patch_url": "${context.baseUrl}/octocat/Hello-World/pull/1347.patch", - // "issue_url": "${context.baseUrl}/api/v3/repos/octocat/Hello-World/issues/1347", - // "state": "open", - // "comments": 10, - // "commits": 3, - // "additions": 100, - // "deletions": 3, - // "changed_files": 5 - }""" - - val apiPullRequestReviewCommentJson = s"""{ - "url": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/comments/29724692", - "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": $apiUserJson, - "body": "Maybe you should use more emoji on this line.", - "created_at": "2015-05-05T23:40:27Z", - "updated_at": "2015-05-05T23:40:27Z", - "html_url": "http://gitbucket.exmple.com/baxterthehacker/public-repo/pull/1#discussion_r29724692", - "pull_request_url": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/1", - "_links": { - "self": { - "href": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/comments/29724692" - }, - "html": { - "href": "http://gitbucket.exmple.com/baxterthehacker/public-repo/pull/1#discussion_r29724692" - }, - "pull_request": { - "href": "http://gitbucket.exmple.com/api/v3/repos/baxterthehacker/public-repo/pulls/1" - } - } - }""" - - val apiBranchProtectionJson = """{ - "enabled": true, - "required_status_checks": { - "enforcement_level": "everyone", - "contexts": [ - "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 - val json2 = Pattern.compile("""^\s*//.*$""", Pattern.MULTILINE).matcher(expectJson).replaceAll("") - val js2 = try { - parse(json2) - } catch { - case e: com.fasterxml.jackson.core.JsonParseException => { - val p = java.lang.Math.max(e.getLocation.getCharOffset() - 10, 0).toInt - val message = json2.substring(p, java.lang.Math.min(p + 100, json2.length)) - throw new com.fasterxml.jackson.core.JsonParseException(e.getProcessor, message + e.getMessage) - } - } - val js1 = parse(resultJson) - assert(js1 === js2) - } + private def expected(json: String) = json.replaceAll("\n", "") test("apiUser") { - assertJson(JsonFormat(apiUser), apiUserJson) + assert(JsonFormat(apiUser) == expected(jsonUser)) } - test("repository") { - assertJson(JsonFormat(repository), repositoryJson) + test("apiRepository") { + assert(JsonFormat(apiRepository) == expected(jsonRepository)) } - test("apiPushCommit") { - assertJson(JsonFormat(apiCommit), apiPushCommitJson) + test("apiCommit") { + assert(JsonFormat(apiCommit) == expected(jsonCommit(sha1))) } test("apiComment") { - assertJson(JsonFormat(apiComment), apiCommentJson) - assertJson(JsonFormat(apiCommentPR), apiCommentPRJson) + assert(JsonFormat(apiComment) == expected(jsonComment)) + assert(JsonFormat(apiCommentPR) == expected(jsonCommentPR)) } test("apiCommitListItem") { - assertJson(JsonFormat(apiCommitListItem), apiCommitListItemJson) + assert(JsonFormat(apiCommitListItem) == expected(jsonCommitListItem)) } test("apiCommitStatus") { - assertJson(JsonFormat(apiCommitStatus), apiCommitStatusJson) + assert(JsonFormat(apiCommitStatus) == expected(jsonCommitStatus)) } test("apiCombinedCommitStatus") { - assertJson(JsonFormat(apiCombinedCommitStatus), apiCombinedCommitStatusJson) + assert(JsonFormat(apiCombinedCommitStatus) == expected(jsonCombinedCommitStatus)) } test("apiLabel") { - assertJson(JsonFormat(apiLabel), apiLabelJson) + assert(JsonFormat(apiLabel) == expected(jsonLabel)) } test("apiIssue") { - assertJson(JsonFormat(apiIssue), apiIssueJson) - assertJson(JsonFormat(apiIssuePR), apiIssuePRJson) + assert(JsonFormat(apiIssue) == expected(jsonIssue)) + assert(JsonFormat(apiIssuePR) == expected(jsonIssuePR)) } test("apiPullRequest") { - assertJson(JsonFormat(apiPullRequest), apiPullRequestJson) + assert(JsonFormat(apiPullRequest) == expected(jsonPullRequest)) } test("apiPullRequestReviewComment") { - assertJson(JsonFormat(apiPullRequestReviewComment), apiPullRequestReviewCommentJson) + assert(JsonFormat(apiPullRequestReviewComment) == expected(jsonPullRequestReviewComment)) } test("apiBranchProtection") { - assertJson(JsonFormat(apiBranchProtection), apiBranchProtectionJson) + assert(JsonFormat(apiBranchProtection) == expected(jsonBranchProtection)) } test("apiBranch") { - assertJson(JsonFormat(apiBranch), apiBranchJson) - assertJson(JsonFormat(apiBranchForList), apiBranchForListJson) + assert(JsonFormat(apiBranch) == expected(jsonBranch)) + assert(JsonFormat(apiBranchForList) == expected(jsonBranchForList)) } test("apiCommits") { - assertJson(JsonFormat(apiCommits), apiCommitsJson) + assert(JsonFormat(apiCommits) == expected(jsonCommits)) } test("apiContents") { - assertJson(JsonFormat(apiContents), apiContentsJson) + assert(JsonFormat(apiContents) == expected(jsonContents)) } test("apiEndPoint") { - assertJson(JsonFormat(apiEndPoint), apiEndPointJson) + assert(JsonFormat(apiEndPoint) == expected(jsonEndPoint)) } test("apiError") { - assertJson(JsonFormat(apiError), apiErrorJson) + assert(JsonFormat(apiError) == expected(jsonError)) } test("apiGroup") { - assertJson(JsonFormat(apiGroup), apiGroupJson) + assert(JsonFormat(apiGroup) == expected(jsonGroup)) } test("apiPlugin") { - assertJson(JsonFormat(apiPlugin), apiPluginJson) + assert(JsonFormat(apiPlugin) == expected(jsonPlugin)) } test("apiPusher") { - assertJson(JsonFormat(apiPusher), apiPusherJson) + assert(JsonFormat(apiPusher) == expected(jsonPusher)) } test("apiRef") { - assertJson(JsonFormat(apiRef), apiRefJson) + assert(JsonFormat(apiRef) == expected(jsonRef)) } } diff --git a/src/test/scala/gitbucket/core/service/WebHookJsonFormatSpec.scala b/src/test/scala/gitbucket/core/service/WebHookJsonFormatSpec.scala new file mode 100644 index 0000000..2a1deae --- /dev/null +++ b/src/test/scala/gitbucket/core/service/WebHookJsonFormatSpec.scala @@ -0,0 +1,175 @@ +package gitbucket.core.service + +import gitbucket.core.api.JsonFormat +import gitbucket.core.service.WebHookService._ +import org.scalatest.{Assertion, FunSuite} + +class WebHookJsonFormatSpec extends FunSuite { + import gitbucket.core.api.ApiSpecModels._ + + private def assert(payload: WebHookPayload, expected: String): Assertion = { + val json = JsonFormat(payload) + assert(json == expected.replaceAll("\n", "")) + } + + test("WebHookCreatePayload") { + val payload = WebHookCreatePayload( + sender = account, + repositoryInfo = repositoryInfo, + repositoryOwner = account, + ref = "v1.0", + refType = "tag" + ) + val expected = s"""{ + |"sender":$jsonUser, + |"description":"This your first repo!", + |"ref":"v1.0", + |"ref_type":"tag", + |"master_branch":"master", + |"repository":$jsonRepository, + |"pusher_type":"user" + |}""".stripMargin + assert(payload, expected) + } + + test("WebHookPushPayload") { + import gitbucket.core.util.GitSpecUtil._ + import org.eclipse.jgit.lib.{Constants, ObjectId} + + withTestRepository { git => + createFile(git, Constants.HEAD, "README.md", "body1", message = "initial") + createFile(git, Constants.HEAD, "README.md", "body1\nbody2", message = "modified") + + val branchId = git.getRepository.resolve("master") + + val payload = WebHookPushPayload( + git = git, + sender = account, + refName = "refs/heads/master", + repositoryInfo = repositoryInfo, + commits = List(commitInfo(branchId.name)), + repositoryOwner = account, + newId = ObjectId.fromString(sha1), + oldId = ObjectId.fromString(sha1) + ) + val expected = s"""{ + |"pusher":{"name":"octocat","email":"octocat@example.com"}, + |"sender":$jsonUser, + |"ref":"refs/heads/master", + |"before":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"after":"6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"commits":[${jsonCommit(branchId.name)}], + |"repository":$jsonRepository, + |"compare":"http://gitbucket.exmple.com/octocat/Hello-World/commit/6dcb09b5b57875f334f61aebed695e2e4193db5e", + |"head_commit":${jsonCommit(branchId.name)} + |}""".stripMargin + assert(payload, expected) + } + } + + test("WebHookIssuesPayload") { + val payload = WebHookIssuesPayload( + action = "edited", + number = 1347, + repository = apiRepository, + issue = apiIssue, + sender = apiUser + ) + val expected = s"""{ + |"action":"edited", + |"number":1347, + |"repository":$jsonRepository, + |"issue":$jsonIssue, + |"sender":$jsonUser + |}""".stripMargin + assert(payload, expected) + } + + test("WebHookPullRequestPayload") { + val payload = WebHookPullRequestPayload( + action = "closed", + issue = issuePR, + issueUser = account, + assignee = Some(account), + pullRequest = pullRequest, + headRepository = repositoryInfo, + headOwner = account, + baseRepository = repositoryInfo, + baseOwner = account, + labels = List(apiLabel), + sender = account, + mergedComment = Some((issueComment, account)) + ) + val expected = s"""{ + |"action":"closed", + |"number":1347, + |"repository":$jsonRepository, + |"pull_request":$jsonPullRequest, + |"sender":$jsonUser + |}""".stripMargin + assert(payload, expected) + } + + test("WebHookIssueCommentPayload") { + val payload = WebHookIssueCommentPayload( + issue = issue, + issueUser = account, + comment = issueComment, + commentUser = account, + repository = repositoryInfo, + repositoryUser = account, + sender = account, + labels = List(label) + ) + val expected = s"""{ + |"action":"created", + |"repository":$jsonRepository, + |"issue":$jsonIssue, + |"comment":$jsonComment, + |"sender":$jsonUser + |}""".stripMargin + assert(payload, expected) + } + + test("WebHookPullRequestReviewCommentPayload") { + val payload = WebHookPullRequestReviewCommentPayload( + action = "create", + comment = commitComment, + issue = issuePR, + issueUser = account, + assignee = Some(account), + pullRequest = pullRequest, + headRepository = repositoryInfo, + headOwner = account, + baseRepository = repositoryInfo, + baseOwner = account, + labels = List(apiLabel), + sender = account, + mergedComment = Some((issueComment, account)) + ) + val expected = s"""{ + |"action":"create", + |"comment":$jsonPullRequestReviewComment, + |"pull_request":$jsonPullRequest, + |"repository":$jsonRepository, + |"sender":$jsonUser + |}""".stripMargin + assert(payload, expected) + } + + test("WebHookGollumPayload") { + val payload = WebHookGollumPayload( + pages = Seq(("edited", "Home", sha1)), + repository = repositoryInfo, + repositoryUser = account, + sender = account + ) + val expected = s"""{ + |"pages":[{"page_name":"Home","title":"Home","action":"edited","sha":"6dcb09b5b57875f334f61aebed695e2e4193db5e","html_url":"http://gitbucket.exmple.com/octocat/Hello-World/wiki/Home"}], + |"repository":$jsonRepository, + |"sender":$jsonUser + |}""".stripMargin + assert(payload, expected) + } + +}