How to Dynamically Retrieve and Delete GitHub Branches Using Jira Automation?
Asked by zhakyp zhoomart uulu:

Hi everyone, I'm trying to set up a Jira automation rule that automatically deletes a GitHub branch when an issue is transitioned to "Done". The challenge I'm facing is that our branch names vary, but they always include the Jira issue key. I've managed to delete branches by hardcoding the branch name in the web request, but I want to make this dynamic. How can I retrieve the exact branch name linked to the issue from GitHub?


Answers:
author: Bill Sheboy
body: Hi [@zhakyp zhoomart uulu](/t5/user/viewprofilepage/user-id/5572453) -- Welcome to the Atlassian Community!

You could try calling the endpoint to list the branches, filter within the rule using the issue key, and then use the result for your deletion.

Kind regards,  
Bill

comments: author: zhakyp zhoomart uulu
body: Hi , [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313)

Thanks for your suggestion! I was able to retrieve the branch names using a GET web request, and it returned the following:

LUN-12-jira-org, LUN-13-test, main

I used the smart value {{webResponse.body.name}} to extract the branch names, but now I'm struggling with filtering to get only the branch name that contains the issue key. Do you have any advice on how I can achieve this within the Jira automation rule?

Kind regards,  
Zhakyp


author: Bill Sheboy
body: You may use the match() function with a regular expression to extract the one needed: <https://support.atlassian.com/cloud-automation/docs/jira-smart-values-text-fields/#match-->

As the regular expression will need to be dynamic, to contain the key, there is an extra step using a created variable. Let's assume there is an {{issue.key}} for use.

* action: create variable
  * name: varRegEx
  * value: ({{issue.key}}.++)
* action: to search for the value
  * {{webResponse.body.name.match(varRegEx)}}

That will produce a list of matches (zero to many) so consider what to do when there are more than one matches.


author: Lunara Inashova
body: Hi [@Bill Sheboy](/t5/user/viewprofilepage/user-id/1685313) ,

I am also facing the same issue as [@zhakyp zhoomart uulu](/t5/user/viewprofilepage/user-id/5572453) .Thank you for the detailed explanation and the solution using the `match()` function with a regular expression. I tried it, and it worked perfectly in the scenario where the branch name follows the pattern `{{issue.key}}.++`.

In my case, we don't have a strict branching strategy, so the branch names can vary significantly. Sometimes, the branch name might just be the issue key (e.g., `ABC-123`), and other times it might have additional text attached to the issue key (e.g., `ABC-123-feature`).

Given this variability, is there a way to make the regex more dynamic? I want to ensure that the automation works regardless of whether the branch name is just the issue key or has additional characters appended. Any suggestions on how to adjust the approach for this more flexible matching?

Thanks again for your help!


author: Bill Sheboy
body: Hi [@Lunara Inashova](/t5/user/viewprofilepage/user-id/5571430) -- Welcome to the Atlassian Community!

The regular expression I provided assumed at least one character after the issue key, and may be modified to meet your needs. Please review the match() documentation as it includes a link to the Java pattern expressions rules are based upon to see other patterns.

For the cases you note, a simple expression could be this instead:

```
{{issue.key}}.*
```

Kind regards,  
Bill


Tags: automation

branch

cloud

jira-cloud

rule