diff --git a/src/main/scala/gitbucket/core/api/ApiCommit.scala b/src/main/scala/gitbucket/core/api/ApiCommit.scala index 95128c1..9229d0c 100644 --- a/src/main/scala/gitbucket/core/api/ApiCommit.scala +++ b/src/main/scala/gitbucket/core/api/ApiCommit.scala @@ -20,13 +20,21 @@ removed: List[String], modified: List[String], author: ApiPersonIdent, - committer: ApiPersonIdent)(repositoryName:RepositoryName) extends FieldSerializable{ - val url = ApiPath(s"/api/v3/${repositoryName.fullName}/commits/${id}") - val html_url = ApiPath(s"/${repositoryName.fullName}/commit/${id}") + committer: ApiPersonIdent)(repositoryName:RepositoryName, urlIsHtmlUrl: Boolean) extends FieldSerializable{ + val url = if(urlIsHtmlUrl){ + ApiPath(s"/${repositoryName.fullName}/commit/${id}") + }else{ + ApiPath(s"/api/v3/${repositoryName.fullName}/commits/${id}") + } + val html_url = if(urlIsHtmlUrl){ + None + }else{ + Some(ApiPath(s"/${repositoryName.fullName}/commit/${id}")) + } } object ApiCommit{ - def apply(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiCommit = { + def apply(git: Git, repositoryName: RepositoryName, commit: CommitInfo, urlIsHtmlUrl: Boolean = false): ApiCommit = { val diffs = JGitUtil.getDiffs(git, commit.id, false) ApiCommit( id = commit.id, @@ -43,6 +51,7 @@ }, author = ApiPersonIdent.author(commit), committer = ApiPersonIdent.committer(commit) - )(repositoryName) + )(repositoryName, urlIsHtmlUrl) } + def forPushPayload(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiCommit = apply(git, repositoryName, commit, true) } diff --git a/src/main/scala/gitbucket/core/api/ApiPushCommit.scala b/src/main/scala/gitbucket/core/api/ApiPushCommit.scala deleted file mode 100644 index 46a99e0..0000000 --- a/src/main/scala/gitbucket/core/api/ApiPushCommit.scala +++ /dev/null @@ -1,39 +0,0 @@ -package gitbucket.core.api - -import gitbucket.core.util.JGitUtil -import gitbucket.core.util.JGitUtil.CommitInfo -import gitbucket.core.util.RepositoryName - -import org.eclipse.jgit.diff.DiffEntry -import org.eclipse.jgit.api.Git - -import java.util.Date - -/** - * https://developer.github.com/v3/activity/events/types/#pushevent - */ -case class ApiPushCommit( - id: String, - message: String, - timestamp: Date, - added: List[String], - removed: List[String], - modified: List[String], - author: ApiPersonIdent, - committer: ApiPersonIdent)(repositoryName:RepositoryName) extends FieldSerializable { - val url = ApiPath(s"/${repositoryName.fullName}/commit/${id}") -} - -object ApiPushCommit{ - def apply(commit: ApiCommit, repositoryName: RepositoryName): ApiPushCommit = ApiPushCommit( - id = commit.id, - message = commit.message, - timestamp = commit.timestamp, - added = commit.added, - removed = commit.removed, - modified = commit.modified, - author = commit.author, - committer = commit.committer)(repositoryName) - def apply(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiPushCommit = - ApiPushCommit(ApiCommit(git, repositoryName, commit), repositoryName) -} diff --git a/src/main/scala/gitbucket/core/api/ApiRepository.scala b/src/main/scala/gitbucket/core/api/ApiRepository.scala index ee97ea3..dce8443 100644 --- a/src/main/scala/gitbucket/core/api/ApiRepository.scala +++ b/src/main/scala/gitbucket/core/api/ApiRepository.scala @@ -13,10 +13,14 @@ forks: Int, `private`: Boolean, default_branch: String, - owner: ApiUser) { + owner: ApiUser)(urlIsHtmlUrl: Boolean) { val forks_count = forks val watchers_count = watchers - val url = ApiPath(s"/api/v3/repos/${full_name}") + val url = if(urlIsHtmlUrl){ + ApiPath(s"/${full_name}") + }else{ + ApiPath(s"/api/v3/repos/${full_name}") + } val http_url = ApiPath(s"/git/${full_name}.git") val clone_url = ApiPath(s"/git/${full_name}.git") val html_url = ApiPath(s"/${full_name}") @@ -27,7 +31,8 @@ repository: Repository, owner: ApiUser, forkedCount: Int =0, - watchers: Int = 0): ApiRepository = + watchers: Int = 0, + urlIsHtmlUrl: Boolean = false): ApiRepository = ApiRepository( name = repository.repositoryName, full_name = s"${repository.userName}/${repository.repositoryName}", @@ -37,7 +42,7 @@ `private` = repository.isPrivate, default_branch = repository.defaultBranch, owner = owner - ) + )(urlIsHtmlUrl) def apply(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = ApiRepository(repositoryInfo.repository, owner, forkedCount=repositoryInfo.forkedCount) @@ -45,4 +50,7 @@ def apply(repositoryInfo: RepositoryInfo, owner: Account): ApiRepository = this(repositoryInfo.repository, ApiUser(owner)) + def forPushPayload(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = + ApiRepository(repositoryInfo.repository, owner, forkedCount=repositoryInfo.forkedCount, urlIsHtmlUrl=true) + } diff --git a/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala b/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala index 5fd8ec0..cf5dfe8 100644 --- a/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositorySettingsController.scala @@ -14,6 +14,7 @@ import org.scalatra.i18n.Messages import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.Constants +import org.eclipse.jgit.lib.ObjectId class RepositorySettingsController extends RepositorySettingsControllerBase @@ -165,13 +166,15 @@ import scala.collection.JavaConverters._ val commits = if(repository.commitCount == 0) List.empty else git.log .add(git.getRepository.resolve(repository.repository.defaultBranch)) - .setMaxCount(3) - .call.iterator.asScala.map(new CommitInfo(_)) + .setMaxCount(4) + .call.iterator.asScala.map(new CommitInfo(_)).toList getAccountByUserName(repository.owner).foreach { ownerAccount => callWebHook("push", List(WebHook(repository.owner, repository.name, form.url)), - WebHookPushPayload(git, ownerAccount, "refs/heads/" + repository.repository.defaultBranch, repository, commits.toList, ownerAccount) + WebHookPushPayload(git, ownerAccount, "refs/heads/" + repository.repository.defaultBranch, repository, (if(commits.isEmpty){Nil}else{commits.tail}), ownerAccount, + oldId = commits.lastOption.map(_.id).map(ObjectId.fromString).getOrElse(ObjectId.zeroId()), + newId = commits.headOption.map(_.id).map(ObjectId.fromString).getOrElse(ObjectId.zeroId())) ) } flash += "url" -> form.url diff --git a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala index 27afd47..989af69 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -669,7 +669,8 @@ val commit = new JGitUtil.CommitInfo(JGitUtil.getRevCommitFromId(git, commitId)) callWebHookOf(repository.owner, repository.name, "push") { getAccountByUserName(repository.owner).map{ ownerAccount => - WebHookPushPayload(git, loginAccount, headName, repository, List(commit), ownerAccount) + WebHookPushPayload(git, loginAccount, headName, repository, List(commit), ownerAccount, + oldId = headTip, newId = commitId) } } } diff --git a/src/main/scala/gitbucket/core/service/WebHookService.scala b/src/main/scala/gitbucket/core/service/WebHookService.scala index 25c8e73..da5cdc8 100644 --- a/src/main/scala/gitbucket/core/service/WebHookService.scala +++ b/src/main/scala/gitbucket/core/service/WebHookService.scala @@ -12,6 +12,7 @@ import org.apache.http.client.entity.UrlEncodedFormEntity import org.apache.http.message.BasicNameValuePair import org.eclipse.jgit.api.Git +import org.eclipse.jgit.lib.ObjectId import org.slf4j.LoggerFactory @@ -192,21 +193,33 @@ case class WebHookPushPayload( pusher: ApiUser, ref: String, - commits: List[ApiPushCommit], + before: String, + after: String, + commits: List[ApiCommit], repository: ApiRepository - ) extends WebHookPayload + ) extends FieldSerializable with WebHookPayload { + val compare = commits.size match { + case 0 => ApiPath(s"/${repository.full_name}") // maybe test hook on un-initalied repository + case 1 => ApiPath(s"/${repository.full_name}/commit/${after}") + case _ if before.filterNot(_=='0').isEmpty => ApiPath(s"/${repository.full_name}/compare/${commits.head.id}^...${after}") + case _ => ApiPath(s"/${repository.full_name}/compare/${before}...${after}") + } + val head_commit = commits.lastOption + } object WebHookPushPayload { def apply(git: Git, pusher: Account, refName: String, repositoryInfo: RepositoryInfo, - commits: List[CommitInfo], repositoryOwner: Account): WebHookPushPayload = + commits: List[CommitInfo], repositoryOwner: Account, + newId: ObjectId, oldId: ObjectId): WebHookPushPayload = WebHookPushPayload( - ApiUser(pusher), - refName, - commits.map{ commit => ApiPushCommit(git, RepositoryName(repositoryInfo), commit) }, - ApiRepository( + pusher = ApiUser(pusher), + ref = refName, + before = ObjectId.toString(oldId), + after = ObjectId.toString(newId), + commits = commits.map{ commit => ApiCommit.forPushPayload(git, RepositoryName(repositoryInfo), commit) }, + repository = ApiRepository.forPushPayload( repositoryInfo, - owner= ApiUser(repositoryOwner) - ) + owner= ApiUser(repositoryOwner)) ) } diff --git a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala index 3deecfb..0c12872 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -203,7 +203,8 @@ callWebHookOf(owner, repository, "push"){ for(pusherAccount <- getAccountByUserName(pusher); ownerAccount <- getAccountByUserName(owner)) yield { - WebHookPushPayload(git, pusherAccount, command.getRefName, repositoryInfo, newCommits, ownerAccount) + WebHookPushPayload(git, pusherAccount, command.getRefName, repositoryInfo, newCommits, ownerAccount, + newId = command.getNewId(), oldId = command.getOldId()) } } } diff --git a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala index f3a2f97..fe317ce 100644 --- a/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala +++ b/src/test/scala/gitbucket/core/api/JsonFormatSpec.scala @@ -45,7 +45,7 @@ forks = 0, `private` = false, default_branch = "master", - owner = apiUser) + owner = apiUser)(urlIsHtmlUrl = false) val repositoryJson = s"""{ "name" : "Hello-World", "full_name" : "octocat/Hello-World", @@ -85,7 +85,7 @@ "url": "http://gitbucket.exmple.com/api/v3/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e/statuses" }""" - val apiPushCommit = ApiPushCommit( + val apiPushCommit = ApiCommit( id = "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", message = "Update README.md", timestamp = date1, @@ -93,7 +93,7 @@ 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")) + committer = ApiPersonIdent("baxterthehacker","baxterthehacker@users.noreply.github.com",date1))(RepositoryName("baxterthehacker", "public-repo"), true) val apiPushCommitJson = s"""{ "id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c", // "distinct": true,