Raise a Jira when a pull request is created
Asked by Chris Shepherd:

Hi folks,

We have a requirement that whenever a pull request is created for a specific branch (production), a Jira issue is created. Is this possible? Would we need a plugin like scriptrunner?

Ideally, we would also only be able to merge the PR if the Jira issue is in a specific status. Is that possible too?

thanks in advance for any pointers.


Answers:
author: Aron Gombas _Midori_
body: > whenever a pull request is created for a specific branch (production), a Jira issue is created

This part of the problem could be solved if you could call the [incoming webhook](https://confluence.atlassian.com/automation/jira-automation-triggers-993924804.html) Jira automation trigger every time when a PR is created. That way you could use the automation rule to test if the branch is a "production" branch and create the issue if it is. The question is what is the best way to send a webhook call from Bitbucket after a PR creation? You could surely develop a little custom app for that, but if you aren't a developer it could be cumbersome.

This is where ScriptRunner could help, but then you could even develop the "check branch" and "create issue through the Jira REST API" steps in the ScriptRunner executed Groovy script.
> Ideally, we would also only be able to merge the PR if the Jira issue is in a specific status.

This part of the problem can be elegantly solved with the [Better Commit Policy for Bitbucket](https://marketplace.atlassian.com/apps/1214430/better-commit-policy-for-bitbucket?hosting=datacenter&tab=overview) app. See the [verifying pull requests](https://www.midori-global.com/products/better-commit-policy-for-bitbucket/server/documentation/#verifying-pull-requests) section in the product guide.

![rejected-pull-request.png](https://community.atlassian.com/t5/image/serverpage/image-id/315669iADC20CD99C6E0983/image-size/large?v=v2&px=999 "rejected-pull-request.png")


author: Yannis Argyropoulos __ScriptRunner - The Adaptavist Group__
body: Hi [@Chris Shepherd](/t5/user/viewprofilepage/user-id/544481),

Yes, ScriptRunner for Bitbucket has some built-in features that will help you check against a Jira instance. Having set an application link between your bitbucket and jira, you can:

<br />

- Use a[Custom Listener](https://docs.adaptavist.com/sr4bib/latest/features/listeners/custom-listeners) , triggered by the 'PullRequestOpenedEvent' to call the[jira api](https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/#creating-an-issue-examples) and create a new issue, if it doesn't exist (this will need some scripting)  
- Or even better you can Require a valid jira exists before a branch or commit is pushed, using the [Require Commits to be Associated with a Valid Jira Issue](https://docs.adaptavist.com/sr4bib/latest/features/script-pre-receive-hooks/built-in-pre-receive-hooks/require-commits-to-be-associated-with-a-valid-jira-issue) Pre-Hook  
- You can block the Merge until a jira ticket is in a certain status by using the '[Require That a Pull Request is Associated with a Valid Jira Issue](https://docs.adaptavist.com/sr4bib/latest/features/script-merge-checks/built-in-merge-checks/require-that-a-pull-request-is-associated-with-a-valid-jira-issue)' Merge-Check by modifying the JQL parameter to specify the status(es) you'd like.

Hope this helps! Please let us know if you have further questions or if you want to give it a try and need help with the scripting and setup.

Regards,

Yannis


Tags: bitbucket-data-center

data-center

scriptrunner

server