How to retrieve the jira issue key from the repository branch name in bamboo using Script runner?
Asked by Sushma:

Hi All

I am using script runner plugin to get the issue key from repository branch.

I added the scriptable task in the bamboo job.

import com.atlassian.bamboo.variable.CustomVariableContext  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
import com.atlassian.sal.api.component.ComponentLocator  
import com.atlassian.bamboo.build.JiraIssueResultsManager  
import com.atlassian.bamboo.plan.PlanKey  
import com.atlassian.sal.api.component.ComponentLocator  
import java.util.regex.Pattern  
import org.apache.log4j.Logger  
import org.apache.log4j.Level

<br />

def log = Logger.getLogger("com.atlassian.bamboo.user.BambooUserManagerImpl")  
log.setLevel(Level.WARN)

<br />

def variableContext = ComponentLocator.getComponent(CustomVariableContext)  
def planbranch = variableContext.getVariableContexts().get("planRepository.branchName")?.getValue()  
log.warn "\*\*\*\*\*\*\*\*\*\*\*\*\*\*plankey is : $planbranch\*\*\*\*\*\*\*\*\*\*\*\*\*\*"  

log :

\*\*\*\*\*\*\*\*\*\*\*\*\*\*plankey is : feature/TM-1-addition-of-2-numbers\*\*\*\*\*\*\*\*\*\*\*\*\*\*

I am getting the name of the branch . How to get the issue key (TM-1)from it (feature/TM-1-addition-of-2-numbers) using groovy script?

any suggestion would be appreciable

please help me

Regards

Sushma


Answers:
author: Rafael Pinto Sperafico
body: Hi [@Sushma](/t5/user/viewprofilepage/user-id/1632516) ,

I am unsure of what you are trying to accomplish and perhaps you could elaborate a bit more.

If ALL branches in Bamboo follow the same pattern, e.g:

* feature/TM-1-addition-of-2-numbers
* feature/TM-2-subtraction-of-2-numbers

Then, you could continue on using regex (*import java.util.regex.\* as per your source-code*) to parse/extract Jira's Issue Key from branch name.

If the name of the branch does not carry Jira's Issue Key, then you could use [CachedPlanManager](https://docs.atlassian.com/atlassian-bamboo/6.6.0/com/atlassian/bamboo/plan/cache/CachedPlanManager.html "CachedPlanManager") and getPlans(), iterating over all of plans looking for branches. Then, with this list of branches you can iterate over each branch ([ImmutableChainBranch](https://docs.atlassian.com/atlassian-bamboo/6.6.0/com/atlassian/bamboo/plan/cache/ImmutableChainBranch.html "ImmutableChainBranch")) and getLinkedJiraIssue()

Here is an example:

```
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.bamboo.plan.cache.CachedPlanManager;
import com.atlassian.bamboo.plan.cache.ImmutableTopLevelPlan;
import com.atlassian.bamboo.plan.cache.ImmutablePlan;
import com.atlassian.bamboo.plan.cache.ImmutableChainBranch;

import org.apache.log4j.Logger
import org.apache.log4j.Level
Logger LOG = Logger.getLogger("com.onresolve");
LOG.setLevel(Level.INFO)

CachedPlanManager cachedPlanManager = ComponentLocator.getComponent(CachedPlanManager);
List<ImmutableTopLevelPlan> immutableTopLevelPlanList = cachedPlanManager.getPlans();

List<ImmutablePlan> immutablePlans = new ArrayList<>();
immutablePlans.addAll(immutableTopLevelPlanList);

for (ImmutablePlan plan : immutablePlans) {
  LOG.info("plan :: " + plan.getKey());

  List<ImmutableChainBranch> chainBranchList = cachedPlanManager.getBranchesForChain(plan);

  for (ImmutableChainBranch branch: chainBranchList) {
    if (branch.getLinkedJiraIssue() != null) {
      LOG.info("branch :: " + branch.getKey());
      LOG.info("jira issue :: " + branch.getLinkedJiraIssue())
    }
  }

}
```

It is important to notice that [ScriptRunner for Bamboo (BETA)](https://marketplace.atlassian.com/apps/1217785/scriptrunner-for-bamboo-beta) still in BETA version and [Adaptavist](https://marketplace.atlassian.com/vendors/81/adaptavist "Adaptavist") has not updated the plugin to support latest Bamboo versions. So scripts made to version 6.6.1 (*latest Bamboo version supported according to Atlassian's Marketplace*) may not work in latest Bamboo versions.

Kind regards,  
Rafael

comments: author: Sushma
body: Hi [@Rafael Pinto Sperafico](/t5/user/viewprofilepage/user-id/2286143)

Let me briefly explain my requirement .

As and when the user commit the code in the development branch(which is created from Jira Ex:"feature/\<issuekey-summary\>" ) of Bitbucket, bamboo should be capable of running the all "Tests" belongs to that particular issue and after successful build bamboo should be able to post result back to jira server by creating "Test Execution" and link it to the corresponding "Test Plan".

Can you suggest me how to achieve the above Integration ?

I am literally confused like how to start?, which plugins I make use of ?..or can I achieve this without installing any third party tools?

currently I am using

Bitbucket Server - v5.14.1

Bamboo Server - 6.9.1

Jira Server - v7.10.1

and one more thing, In Jira we are handling "Test Management" with the help of a third party tool called "X-ray Test Management for Jira".

So After getting the issue key from the development branch , I need to

* get the "Test Plan" from the issue key
* get the "Tests" which belongs to that particular "Test Plan"
* Run the "Tests"
* Post the result back to by creating "Test Execution"
* Link the created "Test Execution" to the corresponding "Test plan"

Need help to implement this.Please help me

Regards

Sushma


author: Rafael Pinto Sperafico
body: Hi [@Sushma](/t5/user/viewprofilepage/user-id/1632516) ,

Out-of-box you have Development Panel in Jira that once integrated with Bamboo will provide you with build result as an example. However, based on your 1st paragraph, the integration you are looking for is not ready to accommodate your needs straight from Atlassian products. Because of that, you will need to rely on 3rd party plugins and/or extend some of the functionalities.

[Xray Test Management for Jira](https://marketplace.atlassian.com/apps/1211769/xray-test-management-for-jira "Xray Test Management for Jira") along with [Xray for Jira Add-on for Bamboo](https://marketplace.atlassian.com/apps/1216932/xray-for-jira-add-on-for-bamboo "Xray for Jira Add-on for Bamboo") (by [Xpand IT](https://marketplace.atlassian.com/vendors/31085/xpand-it "Xpand IT")) seem to be an option. Alternatively, [Zephyr for Jira - Test Management](https://marketplace.atlassian.com/apps/1014681/zephyr-for-jira-test-management "Zephyr for Jira - Test Management") (by [Zephyr, a SmartBear company](https://marketplace.atlassian.com/vendors/17834/zephyr-a-smartbear-company "Zephyr, a SmartBear company")) among others available in [Atlassian Marketplace](https://marketplace.atlassian.com/ "Atlassian Marketplace")

Perhaps, you should consider raising an inquire directly with those vendors, as they will be able to provide you with more information about their products, what is already available, how to make use of the it and perhaps what will become available in new releases.

Please, find below links to their support channel:

* <http://xraysupport.xpand-it.com/> (Xray)
* <https://support.getzephyr.com/hc/en-us> (Zephyr)

If you also agree, the latest comment relates to a different topic from the one described in your initial inquire "*How to retrieve the jira issue key from the repository branch name in bamboo using Script runner?*". Therefore, if we continue on this thread now commenting about this new topic, those who may be facing the same problem and/or seeking for the same resolution will find very difficult finding the information due to indexing.

As a suggestion, if a new topic is to be discussed there is an opportunity to raise a new inquire in the Atlassian Community, increasing the chances of someone with experience in Testing, Jira and Bamboo integration participate and contribute on your inquire.

Last but not least, based on the Atlassian Products versions you have mentioned, I would encourage you on having a look at [Atlassian - Security Advisories](https://www.atlassian.com/trust/security/advisories "Atlassian - Security Advisories") as some of those products/versions have security vulnerabilities announced.

Kind regards,  
Rafael


author: Sushma
body: Hi [@Rafael Pinto Sperafico](/t5/user/viewprofilepage/user-id/2904254)

Thanks alot for the suggestion.

As you mentioned in the last comment

"[Xray Test Management for Jira](https://marketplace.atlassian.com/apps/1211769/xray-test-management-for-jira "Xray Test Management for Jira") along with [Xray for Jira Add-on for Bamboo](https://marketplace.atlassian.com/apps/1216932/xray-for-jira-add-on-for-bamboo "Xray for Jira Add-on for Bamboo") (by [Xpand IT](https://marketplace.atlassian.com/vendors/31085/xpand-it "Xpand IT")) seem to be an option"

we are decided to follow the above approach.

But my question is **how to get the set of Tests from jira to Bamboo Server dynamically?**

**and**

**How to use those tests in the Xray tasks of Bamboo?**

I think to do this we need one more third party tool like Script runner/Groovy for Bamboo etc to get the tests dynamically.

am I right?

This is where I am facing the issue like

* how to get the issue key
* how to get the corresponding "Test Plan"
* how to get the "Tests" of the "Test Plan"

so on...

and after getting the set of "Tests" ,how to put those "tests" in the "Xray taks" to run the test?

Your help would be appreciable

Thanks in advance

Regards

Sushma


Tags: scriptrunner