diff --git a/src/main/scala/gitbucket/core/api/ApiCommit.scala b/src/main/scala/gitbucket/core/api/ApiCommit.scala index 8540d03..04c05a5 100644 --- a/src/main/scala/gitbucket/core/api/ApiCommit.scala +++ b/src/main/scala/gitbucket/core/api/ApiCommit.scala @@ -53,5 +53,5 @@ committer = ApiPersonIdent.committer(commit) )(repositoryName, urlIsHtmlUrl) } - def forPushPayload(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiCommit = apply(git, repositoryName, commit, true) + def forWebhookPayload(git: Git, repositoryName: RepositoryName, commit: CommitInfo): ApiCommit = apply(git, repositoryName, commit, true) } diff --git a/src/main/scala/gitbucket/core/api/ApiRepository.scala b/src/main/scala/gitbucket/core/api/ApiRepository.scala index f5d74f6..facaf3f 100644 --- a/src/main/scala/gitbucket/core/api/ApiRepository.scala +++ b/src/main/scala/gitbucket/core/api/ApiRepository.scala @@ -51,7 +51,7 @@ def apply(repositoryInfo: RepositoryInfo, owner: Account): ApiRepository = this(repositoryInfo.repository, ApiUser(owner)) - def forPushPayload(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = + def forWebhookPayload(repositoryInfo: RepositoryInfo, owner: ApiUser): ApiRepository = ApiRepository(repositoryInfo.repository, owner, forkedCount=repositoryInfo.forkedCount, urlIsHtmlUrl=true) def forDummyPayload(owner: ApiUser): ApiRepository = diff --git a/src/main/scala/gitbucket/core/service/WebHookService.scala b/src/main/scala/gitbucket/core/service/WebHookService.scala index fb17fe1..f7a4e07 100644 --- a/src/main/scala/gitbucket/core/service/WebHookService.scala +++ b/src/main/scala/gitbucket/core/service/WebHookService.scala @@ -362,6 +362,35 @@ object WebHookService { trait WebHookPayload + // https://developer.github.com/v3/activity/events/types/#createevent + case class WebHookCreatePayload( + sender: ApiUser, + description: String, + ref: String, + ref_type: String, + master_branch: String, + repository: ApiRepository + ) extends FieldSerializable with WebHookPayload { + val pusher_type = "user" + } + + object WebHookCreatePayload { + + def apply(git: Git, sender: Account, refName: String, repositoryInfo: RepositoryInfo, + commits: List[CommitInfo], repositoryOwner: Account, + ref: String, refType: String): WebHookCreatePayload = + WebHookCreatePayload( + sender = ApiUser(sender), + ref = ref, + ref_type = refType, + description = repositoryInfo.repository.description.getOrElse(""), + master_branch = repositoryInfo.repository.defaultBranch, + repository = ApiRepository.forWebhookPayload( + repositoryInfo, + owner= ApiUser(repositoryOwner)) + ) + } + // https://developer.github.com/v3/activity/events/types/#pushevent case class WebHookPushPayload( pusher: ApiPusher, @@ -391,8 +420,8 @@ ref = refName, before = ObjectId.toString(oldId), after = ObjectId.toString(newId), - commits = commits.map{ commit => ApiCommit.forPushPayload(git, RepositoryName(repositoryInfo), commit) }, - repository = ApiRepository.forPushPayload( + commits = commits.map{ commit => ApiCommit.forWebhookPayload(git, RepositoryName(repositoryInfo), commit) }, + repository = ApiRepository.forWebhookPayload( 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 d2592d8..1583e76 100644 --- a/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala @@ -306,6 +306,18 @@ newId = command.getNewId(), oldId = command.getOldId()) } } + if (command.getType == ReceiveCommand.Type.CREATE) { + callWebHookOf(owner, repository, WebHook.Create) { + for { + pusherAccount <- getAccountByUserName(pusher) + ownerAccount <- getAccountByUserName(owner) + } yield { + val refType = if (refName(1) == "tags") "tag" else "branch" + WebHookCreatePayload(git, pusherAccount, command.getRefName, repositoryInfo, newCommits, ownerAccount, + ref = branchName, refType = refType) + } + } + } // call post-commit hook PluginRegistry().getReceiveHooks.foreach(_.postReceive(owner, repository, receivePack, command, pusher)) diff --git a/src/main/twirl/gitbucket/core/settings/edithookform.scala.html b/src/main/twirl/gitbucket/core/settings/edithookform.scala.html index 37e4beb..c569bf5 100644 --- a/src/main/twirl/gitbucket/core/settings/edithookform.scala.html +++ b/src/main/twirl/gitbucket/core/settings/edithookform.scala.html @@ -48,7 +48,9 @@ + +