Hi Jamie Echlin
We would like to require our users to enter jira issue number when they commit before they push the local repository in bitbucket. I looked at the Atlassian JAVADOC API and I found these packages (https://developer.atlassian.co..., https://developer.atlassian.co... to use. Also I found this groovy from Scriptrunner website which is similar what we want.
import com.atlassian.bitbucket.hook.HookResponse
import com.atlassian.bitbucket.repository.RefChange
import com.atlassian.bitbucket.repository.Repository
Repository repository = repository
Collection<refchange> refChanges = refChanges
HookResponse hookResponse = hookResponse
if (repository.name == "test") {
hookResponse.out().print("You cannot commit at the moment to any repository named 'test'\n")
return false
}
return true
Could you please tell me how I can modify this in order to require users to enter jira issue number when they want push to bitbucket?
Thanks
The documentation on pre-receive hooks notes a built-in hook that matches what you're looking for. You'll provide a JQL query that defines what issues are valid, and the script will require that the JIRA issue be present in the branch name or commit message.
There's similar (and simpler) built-in merge check that can require JIRA issues as well.
@Jonny Carter
Is there a way to do with custom groovy Script that does not required to link Bitbucket link to JIRA?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
There are ways.
On the simpler side, you could plausibly just use a regular expression to validate that the commit message matches to a JIRA issue in some way.
If you actually want to pull information from the JIRA instance for your commit hook, you're going to have to use some other means of communication between the two, such as REST calls. Otherwise, how would Bitbucket get info about the JIRA instance? That's very likely going to be more complicated than creating the application link, and debatably worth it. I think I'd just hard-code a regex and have done if it were me.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Jonny is right here that you either need to use the built in pre-receive hook with an application link setup for JIRA or you need to use a regex to check the commit message contains a JIRA issue key. There is no other way.
The following script checks that the commit message starts with a JIRA issue key:
import com.atlassian.bitbucket.hook.HookResponse import com.atlassian.bitbucket.repository.RefChange import com.atlassian.bitbucket.repository.Repository import com.onresolve.scriptrunner.canned.bitbucket.util.BitbucketCannedScriptUtils def repository = repository as Repository def refChanges = refChanges as Collection<RefChange> def hookResponse = hookResponse as HookResponse def commits = refChanges.getCommits(repository) def commitsWithoutJiraKey = commits.findAll { commit -> def regex = "[A-Z]{2,9}-\\d+.*" !(commit.message ==~ /${regex}/) } if (repository.name == "rep_1" && commitsWithoutJiraKey) { def msg = new StringBuilder() msg << "Following commits have no JIRA key in their message!\n" commitsWithoutJiraKey.each { commit -> msg << "$commit.displayId\n" } hookResponse.out().print(BitbucketCannedScriptUtils.wrapHookResponse(msg)) return false } return true
You should add this as a custom pre-receive hook.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.