Delete branch after JIRA status changes
Asked by Vikas kumar:

Hi Team,

I have requirement as below, could you help me in providing the necessary links or inputs to achieve this.

Currently, after merging our feature branch to release(PROD), will delete the feature manually after PROD signoff received.

Now I need to handle it using JIRA. Like as below.

1. Track feature branch name in JIRA. can we do this using <https://support.atlassian.com/jira-software-cloud/docs/process-issues-with-smart-commits/> ???

2. Once we move the JIRA status to Done, it should create pull request for the feature branch to release , how to automate this?

3. Once approver approved.

4. Merge the pull requested and delete feature. how to automate this?

Thanks,

Vikas


Answers:
author: Theodora Boudale
body: Hi Vikas and welcome to the community!

Since you created the question in the Bitbucket forum with the tag 'bitbucket-cloud', I assume that your repositories are hosted in Bitbucket Cloud?

> 1. Track feature branch name in JIRA. can we do this using <https://support.atlassian.com/jira-software-cloud/docs/process-issues-with-smart-commits/> ???

Smart commits are useful if you want to transition an issue, comment on an issue, or track time for an issue when you make a commit.

If you want to track a feature branch in Jira, you can create a branch in your repo that includes the Jira issue key name in its name. E.g. for the issue **TEST-10** , you can create a Bitbucket branch named **feature-TEST-10**. This way the branch will show up in the Jira issue in the Development panel.

You can also use the Jira issue key in the commits you make to this branch, if you want these commits to show in the Development panel.

> 2. Once we move the JIRA status to Done, it should create pull request for the feature branch to release , how to automate this?

It is possible to create Automation rules in Jira:

* <https://support.atlassian.com/cloud-automation/docs/jira-cloud-automation/>.

You could create an Automation rule with the trigger **Issue transitioned** and then add as an action **Send web request**. The web request can use our API to create a PR in Bitbucket Cloud:

* <https://developer.atlassian.com/cloud/bitbucket/rest/api-group-pullrequests/#api-repositories-workspace-repo-slug-pullrequests-post>

<br />

The tricky thing here is that when the trigger is **Issue transitioned**, the values of any associated branches and repos are not available. In theory, a single Jira issue could be referenced by multiple different branches in multiple different repositories and Jira wouldn't know which one you want to create a pull request for.

You could solve this by creating [custom fields](https://support.atlassian.com/jira-cloud-administration/docs/create-a-custom-field/) in Jira that will hold the values of the source branch name, the destination branch name, the workspace id, and the repo slug. These fields would need to be filled in with the correct values by the user before transitioning the issue, and you could then use their values in the automation rule.

Additionally, when you configure the web request, it is important to include the data
> "close_source_branch": true

so that the source branch gets deleted once you merge the PR.

> 3. Once approver approved, merge the pull requested and delete feature. how to automate this?

What action are you referring to by approval?

Do you mean a PR reviewer selecting the **Approve** button in the Pull Request, on Bitbucket Cloud website? Or something different?

Kind regards,  
Theodora

comments: author: Vikas kumar
body: Thanks Theodora, I will try the first two points.

For the 3rd one, yes you are right it's PR reviewer. It basically links to my 2nd question.

Once JIRA is implemented and moved to PROD, after changing JIRA status to Done. It should create PR request in all the associated repository. Then, my 3rd question comes "Once PR reviewer approved those PR's, merge the pull request and delete feature. how to automate this?

In a high-level when JIRA moved to Done and create PR request, it should send mail to PR reviewer. Once reviewer approved this, it should merge and delete feature branches automatically.


author: Theodora Boudale
body: Hi Vikas,

When a user gets added as a PR reviewer, they will get an email from Bitbucket informing them that they have been added as a reviewer in a PR. This happens anyway, so you don't need to configure something extra.

We don't have any automation that automatically merges a PR once it is approved. However, the **Merge** button of the PR is on the same page as the **Approve** button. If you only have one reviewer and if that reviewer has enough permissions to merge the PR, they can merge the PR right after they approve it.

It is possible to configure in a repository a [webhook](https://support.atlassian.com/bitbucket-cloud/docs/manage-webhooks/) that gets triggered when a PR gets approved. This webhook could then send a request to a server of yours and then your server could use our API to merge the PR. I'm not sure if this is worth the trouble though. It might be easier to have the PR reviewer merge the PR as soon as they approve it, but that's up to you to decide.

If the PR was created with the option "close_source_branch": true, then the source branch will get deleted after the PR is merged.

Kind regards,  
Theodora


author: Vikas kumar
body: Thanks Theodora, that makes sense. Sure I will try on the webhook part.


author: Theodora Boudale
body: You are very welcome Vikas.

One more thing I forgot to mention earlier regarding the email to the PR reviewer: a user will automatically receive an email when they are added as a PR reviewer unless they have disabled notifications on <https://bitbucket.org/account/settings/notifications/>. Notifications are enabled by default, but it would be good to ask the users who are usually PR reviewers to make sure they have not disabled this setting.

Please feel free to reach out if you have any questions.

Kind regards,  
Theodora


author: Vikas kumar
body: Thanks Theodora, I checked and it's enabled and have not changed anything in the settings .

Thanks,

Vikas


Tags: bitbucket-cloud

cloud