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 680486f..53b5cef 100644 --- a/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala +++ b/src/main/scala/gitbucket/core/controller/RepositoryViewerController.scala @@ -665,7 +665,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..06efce9 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,18 +193,30 @@ case class WebHookPushPayload( pusher: ApiUser, ref: String, + before: String, + after: String, commits: List[ApiPushCommit], 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}") + } + } 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 => ApiPushCommit(git, RepositoryName(repositoryInfo), commit) }, + repository = ApiRepository( repositoryInfo, 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()) } } }