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

Pre-Hook for Comits to have JIRA IDs when branch name contains JIRA ID as well

Michal Wilczynski January 15, 2020

Hey Guys,

I have tried to implement pre-hook by Scriptrunner in bitbucket - "Require commits to be associated with a JIRA issue" . 

Goal is to achieve that every commit before pushing will contain the JIRA ID

Problem - In the name of branches I use related JIRA Ticket ID, by the conditon in this defined pre-hook"The issue can either be mentioned in the commit message, or be present in the branch name"

I tried using conditions but so far I wasn't able to fix this, could anyone help me in this please?

4 answers

0 votes
Aron Gombas _Midori_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2020

You can use the Better Commit Policy for Bitbucket app for these type of use cases. (It is an app that adapts the Better Commit Policy for Jira app to Bitbucket.)

It can verify commit messages for issue keys, verify branch names for issue keys, even do that selectively with scope limiting.

One of the best things is that it will suggest you the most likely issue keys you want to enter like this:

remote: ========================================================================
remote:                                REJECTED!
remote: ========================================================================
remote: 1.1 COMMIT [6ecb44a] on [master] Exactly one issue key must be mentioned in the commit message "Guard against the NullPointerException"
remote: ========================================================================
remote: Did you mean one of these issues?
remote: [FOO-82] Crash when submitting the form using Safari
remote: [FOO-27] Keyboard shortcuts
remote: [FOO-11] As a user, I want to choose my own language for the app
remote: ========================================================================

So you can make it very convenient for the developer.

(Disclaimer: I am developer working on this paid app.)

0 votes
Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2020

Hi Michal,

The condition block for a pre-defined Pre-Hook such as "Require commits to be associated with a JIRA issue", is used to determine which changes the hook applies to.

I therefore suggest you create a Custom Pre-Hook.

In order to create a Custom Pre-Hook for your needs, please use the Custom Pre-Hook functionality, outlined in the docs here: https://scriptrunner.adaptavist.com/latest/bitbucket/PreReceiveHooks.html#_custom_pre_receive_hooks

You then should be able to add your code above to the inline script field of the Custom Pre-Hook.

However, I think you want to swap the

true

and

false

statements around, as false blocks the push and true allows it.

See the documentation for details.

Please let me know if this resolves your issue?

I am also going to see if we can improve the built in "Require commits to be associated with a JIRA issue" pre-hook for a future release of ScriptRunner.

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

Robert Giddings [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2020

Hi Michal,

Please also watch and/or vote for this issue for updates on the issue you have raised.

https://productsupport.adaptavist.com/browse/SRBITB-355

Kind regards,

Robert Giddings,

Product Manager, ScriptRunner for Bitbucket

0 votes
Michal Wilczynski January 16, 2020

Thank you Stanislav for you answer, unfortunately I won't be able to introduce any plugin in the company which I work. 

 

I wanted to check in condition basically if this commit message follow JIRA Regexp and found code like that:

def commits = refChanges.getCommits(repository)

def issueKeyRegex = /((?<!([A-Z]{1,10})-?)[A-Z]+-\d+)/

if( commits.any { commit ->
!(commit.message =~ issueKeyRegex)
return true
})
return false

 Still no progress :(

0 votes
Stanislav Seletskiy
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 16, 2020

Hello!

I'm part of Reconquest team, which is developing External Hooks Add-on for
Bitbucket Server/Data Center.

You can implement enforcing Jira ID reference in the commit message by writing
a script for our plugin using any language of your choice.

We've prepared a quick guide on how to do it using bash, just for you.

By the way, here is 25% discount link for External Hooks Add-on: https://promo.atlassian.com/3E1UXX

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events