Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

×
Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

trying to write a bitbucket pre-receive hook in groovy to run pylint or other test

Paul Mansfield March 7, 2018 edited

Hi,

I'm creating a groovy script which I've set as a pre-receive hook so as to run pylint on any .pp files and fail when the score is too low.

 

I set the hook at a URL like this one:

https://mybitbucketserver.example.com/plugins/servlet/scriptrunner/builtin?section=prehooks#

 

I've found lots of snippets of groovy code but I can't find a way to get a list of the actual files being committed...

I've run out of ideas trying random things, and am hoping someone can help. Any help at all would be gratefully received.

 

 

output when you do a git push

remote:
remote: =====================================================================
remote: pmansfield testing pre-commit hook
remote: repository name: paulm
remote: refChange type: refs/heads/master
remote: commit message: DEVOPS-2744 testing 25
remote: hello pmansfield
remote: =====================================================================
remote:

 

===groovy script===

import com.atlassian.bitbucket.content.Change
import com.atlassian.bitbucket.content.ChangeSummary
import com.atlassian.bitbucket.hook.HookResponse
import com.atlassian.bitbucket.repository.RefChange
import com.atlassian.bitbucket.repository.Repository
import com.atlassian.bitbucket.user.ApplicationUser
import com.atlassian.bitbucket.user.Person

import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils

def msg = new StringBuilder()
msg << "pmansfield testing pre-commit hook\n".toString()


def repository = repository as Repository
msg << "repository name: $repository.name \n".toString()


// DOESNT WORK stuff from ChangeSummary
//def changeSummary = changeSummary as Collection<ChangeSummary>

// DOESNT WORK stuff from Changes
//def changes = changes as Collection<Changes>
//for (change in changes) {
// msg << "change path: ${change.getPath()}\n".toString()
//}

// stuff from RefChange
def refChanges = refChanges as Collection<RefChange>
for (refChange in refChanges) {
msg << "refChange type: ${refChange.refId}\n".toString()
}

def commits = refChanges.getCommits(repository)
for (commit in commits) {
msg << "commit message: ${commit.getMessage()}\n".toString()
}

def authors = refChanges.getCommitAuthors(repository)
for (author in authors) {
msg << "hello $author.name \n".toString()
}



def hookResponse = hookResponse as HookResponse
hookResponse.out().write BitbucketCannedScriptUtils.wrapHookResponse(msg)

return true

0 answers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events