Skip to content

Instantly share code, notes, and snippets.

@Thermionix
Last active August 29, 2015 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thermionix/9b0fc9340f9b7b4bfe7a to your computer and use it in GitHub Desktop.
Save Thermionix/9b0fc9340f9b7b4bfe7a to your computer and use it in GitHub Desktop.
Gitblit Post-Receive Hook: flyspray-comment
import com.gitblit.GitBlit
import com.gitblit.utils.JGitUtils
import org.eclipse.jgit.lib.Repository
import org.slf4j.Logger
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.2' )
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.ContentType
import groovyx.net.http.HttpResponseException
/*
Gitblit Post-Receive Hook: flyspray-comment
The purpose of this script is to post a commit comment to flyspray
This script is only executed when pushing to *Gitblit*, not to other Git tooling you may be using.
Set parameters in hook according to your environment
*/
logger.info("flyspray comment hook triggered by ${user.username} for ${repository.name}")
Repository repo = gitblit.getRepository(repository.name)
def flysprayUrl = "http://flyspray.example.com.au/"
def flysprayTaskRegex = "\\bbug#([0-9]*)\\b"
def flysprayUserid = "54"
def flysprayPasshash = "77d6d477bf6f97cb377d6d477bf6f97cb3"
def gitwebUrl = "http://gitblit"
for (command in commands) {
def commits = JGitUtils.getRevLog(repo, command.oldId.name, command.newId.name).reverse()
for (commit in commits) {
def commitMessage = commit.getFullMessage().trim()
if ((match = commitMessage =~ flysprayTaskRegex)) {
def flysprayTaskId = match[0][1]
def repoName = repository.name
def updatedRef = command.refName
def commitRev = commit.getId().getName()
def revUrl = "$gitwebUrl/commit/$repoName/$commitRev"
String flysprayComment = "**git commit** in: ${updatedRef} \n"+
"$repoName $commitRev reviewable \n"+
"**committer**: ${user.username} \n"+
"**message**: " + commitMessage.minus(match[0][0])
def http = new HTTPBuilder(flysprayUrl)
def postPath = "/fs/index.php"
def postBody = ["action": "details.addcomment",
"task_id": flysprayTaskId,
"comment_text": flysprayComment]
def postCookies = [flyspray_userid:flysprayUserid,
flyspray_passhash:flysprayPasshash]
def postQuery = ["do":"details","task_id":"$flysprayTaskId"]
http.headers["Cookie"] = postCookies.collect{it}.join(';')
try {
http.post(
path: postPath,
body: postBody,
query: postQuery,
requestContentType: ContentType.URLENC)
{ resp ->
println "POST Success: ${resp.statusLine}"
assert resp.statusLine.statusCode == 200
}
}
catch(HttpResponseException ex) {
println "Unexpected response error: ${ex.statusCode}"
}
}
}
}
repo.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment