It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Hook for Pull Request

GPReleaseEngineering Sep 11, 2013

What kind of a hook can I implement to check for Pull Request, or when a pull request has been created in a repository ??

All I want to do is check the message in a pull request and reject the pull request if the pull request message does not contain a valid JIRA...

Thanks,

Ishan

1 answer

0 votes
Charles O'Farrell Sep 12, 2013

Hi Ishan,

You can just add a normal atlassian-plugin.xml component by using PullRequestOpenRequestedEvent.

@EventListener
public void onPullRequestOpen(PullRequestOpenRequestedEvent event) {
    for(Changeset changeset : pullRequestService.getChangesets(event.getPullRequest().getToRef().getRepository().getId(), event.getPullRequest().getId(), new PageRequestImpl(0, 100)) {
        if (changeset.getAttributeValues("jira-key").isEmpty()) {
            event.cancel(i18nService.getKeyedText("your.plugin.i18n.warning", "Missing JIRA key in message '{0}'", changeset.getMessage());
        }
    }
}

That code isn't tested and may not even compile. Apologies it's not something we're really advertising which may explain the lack of documentation.

Just a question though - if the user tries to create the pull request and fails, do you normally advise users to amend their messages in git? That requires you don't mind your users using force push (or deleting/creating the branch again). What if they have multiple commits and the very first one is missing a JIRA key - are your users familiar/comfortable with 'git rebase -i'?

I definitely understand the requirement, but I worry that the distributed nature of Git will make this workflow hard to enforce in all cases. I'd love to understand how this kind of restriction works in your environement?

Cheers,

Charles

Charles O'Farrell Sep 12, 2013

Very good point Adam.

You can also use the above event to restrict the PR based on title/description as well. I agree this is preferable to restricting by commit message, although as you say it doesn't mean that the PR will actually be linked to the JIRA issue.

Cheers,

Charles

Adam Ahmed Atlassian Team Sep 12, 2013

Something that might work better with a Git workflow is checking the pull request description or title for JIRA keys, rather than actual commits. Those should be much easier to amend if they are missing a JIRA key.

That won't enforce any hard, back-end link between the pull request and the JIRA issue, but it ensures there's a link there at least in human-readable form. In 2.7+, issue keys in the description will also show as <a> tags that bring up issue information in a dialog.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Off-topic

Disney Live Action Reboots: Are you for or against?

Happy Friday Everyone! Today marks the international release of Disney's live action version of the animated classic Aladdin. I know that this movie was met with some controversy of over cast...

167 views 21 9
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you