Call wiki web hook in pushing via git client

This commit is contained in:
Naoki Takezoe
2017-06-02 17:46:25 +09:00
parent d75d1eed10
commit 2ec8189d47
2 changed files with 87 additions and 8 deletions

View File

@@ -496,9 +496,16 @@ object WebHookService {
repository: RepositoryInfo, repository: RepositoryInfo,
repositoryUser: Account, repositoryUser: Account,
sender: Account sender: Account
): WebHookGollumPayload = apply(Seq((action, pageName, sha)), repository, repositoryUser, sender)
def apply(
pages: Seq[(String, String, String)],
repository: RepositoryInfo,
repositoryUser: Account,
sender: Account
): WebHookGollumPayload = { ): WebHookGollumPayload = {
WebHookGollumPayload( WebHookGollumPayload(
pages = Seq( pages = pages.map { case (action, pageName, sha) =>
WebHookGollumPagePayload( WebHookGollumPagePayload(
action = action, action = action,
page_name = pageName, page_name = pageName,
@@ -506,7 +513,7 @@ object WebHookService {
sha = sha, sha = sha,
html_url = ApiPath(s"/${RepositoryName(repository).fullName}/wiki/${StringUtil.urlDecode(pageName)}") html_url = ApiPath(s"/${RepositoryName(repository).fullName}/wiki/${StringUtil.urlDecode(pageName)}")
) )
), },
repository = ApiRepository(repository, repositoryUser), repository = ApiRepository(repository, repositoryUser),
sender = ApiUser(sender) sender = ApiUser(sender)
) )

View File

@@ -1,6 +1,7 @@
package gitbucket.core.servlet package gitbucket.core.servlet
import java.io.File import java.io.File
import java.util
import java.util.Date import java.util.Date
import gitbucket.core.api import gitbucket.core.api
@@ -22,6 +23,7 @@ import org.slf4j.LoggerFactory
import javax.servlet.ServletConfig import javax.servlet.ServletConfig
import javax.servlet.http.{HttpServletRequest, HttpServletResponse} import javax.servlet.http.{HttpServletRequest, HttpServletResponse}
import org.eclipse.jgit.diff.DiffEntry.ChangeType
import org.json4s.jackson.Serialization._ import org.json4s.jackson.Serialization._
@@ -161,6 +163,12 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
receivePack.setPostReceiveHook(hook) receivePack.setPostReceiveHook(hook)
} }
} }
if(repository.endsWith(".wiki")){
defining(request) { implicit r =>
receivePack.setPostReceiveHook(new WikiCommitHook(owner, repository.replaceFirst("\\.wiki$", ""), pusher, baseUrl))
}
}
} }
} }
@@ -170,7 +178,7 @@ class GitBucketReceivePackFactory extends ReceivePackFactory[HttpServletRequest]
import scala.collection.JavaConverters._ import scala.collection.JavaConverters._
class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: String)/*(implicit session: Session)*/ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl: String)
extends PostReceiveHook with PreReceiveHook extends PostReceiveHook with PreReceiveHook
with RepositoryService with AccountService with IssuesService with ActivityService with PullRequestService with WebHookService with RepositoryService with AccountService with IssuesService with ActivityService with PullRequestService with WebHookService
with WebHookPullRequestService with CommitsService { with WebHookPullRequestService with CommitsService {
@@ -185,9 +193,10 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
// call pre-commit hook // call pre-commit hook
PluginRegistry().getReceiveHooks PluginRegistry().getReceiveHooks
.flatMap(_.preReceive(owner, repository, receivePack, command, pusher)) .flatMap(_.preReceive(owner, repository, receivePack, command, pusher))
.headOption.foreach { error => .headOption
command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error) .foreach { error =>
} command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error)
}
} }
using(Git.open(Directory.getRepositoryDir(owner, repository))) { git => using(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
existIds = JGitUtil.getAllCommitIds(git) existIds = JGitUtil.getAllCommitIds(git)
@@ -285,8 +294,10 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
// call web hook // call web hook
callWebHookOf(owner, repository, WebHook.Push) { callWebHookOf(owner, repository, WebHook.Push) {
for (pusherAccount <- getAccountByUserName(pusher); for {
ownerAccount <- getAccountByUserName(owner)) yield { 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()) newId = command.getNewId(), oldId = command.getOldId())
} }
@@ -309,6 +320,67 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
} }
class WikiCommitHook(owner: String, repository: String, pusher: String, baseUrl: String)
extends PostReceiveHook with WebHookService with AccountService with RepositoryService {
private val logger = LoggerFactory.getLogger(classOf[WikiCommitHook])
override def onPostReceive(receivePack: ReceivePack, commands: util.Collection[ReceiveCommand]): Unit = {
Database() withTransaction { implicit session =>
try {
commands.asScala.headOption.foreach { command =>
implicit val apiContext = api.JsonFormat.Context(baseUrl)
val refName = command.getRefName.split("/")
val commitIds = if (refName(1) == "tags") {
None
} else {
command.getType match {
case ReceiveCommand.Type.DELETE => None
case _ => Some((command.getOldId.getName, command.getNewId.name))
}
}
commitIds.map { case (oldCommitId, newCommitId) =>
val commits = using(Git.open(Directory.getWikiRepositoryDir(owner, repository))) { git =>
JGitUtil.getCommitLog(git, oldCommitId, newCommitId).flatMap { commit =>
val diffs = JGitUtil.getDiffs(git, commit.id, false)
diffs._1.collect { case diff if diff.newPath.toLowerCase.endsWith(".md") =>
val action = if(diff.changeType == ChangeType.ADD) "created" else "edited"
val fileName = diff.newPath
println(action + " - " + fileName + " - " + commit.id)
(action, fileName, commit.id)
}
}
}
val pages = commits
.groupBy { case (action, fileName, commitId) => fileName }
.map { case (fileName, commits) =>
(commits.head._1, fileName, commits.last._3)
}
callWebHookOf(owner, repository, WebHook.Gollum) {
for {
pusherAccount <- getAccountByUserName(pusher)
repositoryUser <- getAccountByUserName(owner)
repositoryInfo <- getRepository(owner, repository)
} yield {
WebHookGollumPayload(pages.toSeq, repositoryInfo, repositoryUser, pusherAccount)
}
}
}
}
} catch {
case ex: Exception => {
logger.error(ex.toString, ex)
throw ex
}
}
}
}
}
object GitLfs { object GitLfs {
case class BatchRequest( case class BatchRequest(