Extending and integrating with Xray using ScriptRunner Some customers and some partners may be using ScriptRunner in order to automate some tasks and even extend the features provided by Jira. You can also do some automation-related with Xray, especially because we use Jira entities and concepts. ScriptRunner may be used in order to access existing features or even to extend the built-in features. Let us know if you're using also ScriptRunner and your use cases so we can improve and share them with other users. Create a Test Set or a Test Plan Create a Test Execution Validate requirement before alowing to make a transition Reopen/transition linked Tests to a requirement Requirement projects Create Test Execution from Test Set issue screen Calculate requirement status for a certain version Show Tests Count for a requirement Show Defects Count for a requirement Configured Issue Types as being requirements or defects Trigger a Jenkins project build from a Test Plan Example ScriptRunner configuration Jenkins configuration Trigger a Jenkins project build from a Test Plan, for the Tests contained in the Test Plan Example ScriptRunner configuration Jenkins configuration Trigger a Bamboo plan build from a Test Plan Example ScripRunner configuration Bamboo configuration Trigger a Bamboo plan/stage build from a Test Plan, for the Tests contained in the Test Plan Example ScripRunner configuration Bamboo configuration Extending REST API for interacting with requirement projects Example of requests Obtaining all projects with requirement coverage enabled Enabling requirement coverage for a project Disabling requirement coverage for a project Synchronize Tests from "related" Test Sets to a Test Execution or to a Test Plan ScripRunner configuration Example Add a custom link to the Tests top menu ScripRunner configuration Example Create a Test Set or a Test Plan Sometimes you may need to create a Test Set or a Test Plan programmatically. The following example shows how to create a Test Plan or a Test Set based on setting the value for specific Xray custom fields. create_xray_entities.groovy import com.atlassian.jira.issue.Issue import com.atlassian.jira.issue.link.IssueLinkManager import com.atlassian.jira.issue.link.IssueLinkType Please note The following scripts are provided as-is, no warranties attached. Use these scripts carefully. Please feel free to adapt them to your needs. Note: We don't provide support for ScriptRunner; if you have doubts concerning its usage, please contact . ScriptRunner's support
49
Embed
Extending and integrating with Xray using ScriptRunner
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Extending and integrating with Xray using ScriptRunnerSome customers and some partners may be using ScriptRunner in order to automate some tasks and even extend the features provided by Jira.
You can also do some automation-related with Xray, especially because we use Jira entities and concepts.
ScriptRunner may be used in order to access existing features or even to extend the built-in features.
Let us know if you're using also ScriptRunner and your use cases so we can improve and share them with other users.
Create a Test Set or a Test PlanCreate a Test ExecutionValidate requirement before alowing to make a transitionReopen/transition linked Tests to a requirementRequirement projectsCreate Test Execution from Test Set issue screenCalculate requirement status for a certain versionShow Tests Count for a requirementShow Defects Count for a requirementConfigured Issue Types as being requirements or defectsTrigger a Jenkins project build from a Test Plan
def issueInputParameters = issueService.newIssueInputParameters()issueInputParameters.with { projectId = project.id summary = "Issue created from script" issueTypeId = newIssueType.id reporterId = user.name}
def jql = "project = ${project.key} and issuetype = Test and component = UI"def issues = getIssues(jql)def arr = issues.collect{ it.key }log.debug("testKeys: "+arr)testKeys=arr.toArray(new String[arr.size()])
// Tests association with a Test Execution: setting it through the CF is currently not possible due to bug XRAY-2010//setFieldValueByNameInParameters(issueInputParameters,"Tests association with a Test Set",testKeys)setFieldValueByNameInParameters(issueInputParameters,"Tests associated with a Test Plan",testKeys)
Sometimes you may need to create a Test Execution programmatically.
Currently there is a limitation to associate the Tests by custom field. Thus, a possible workaround using Xray's REST API is shown in the following example.
// you should use a specific user for this purpose username = "admin" password = "admin" def authString = "${username}:${password}".bytes.encodeBase64().toString()
Validate requirement before alowing to make a transition
Sometimes you may need to assure that the requirement is actually OK before transitioning it to some status, or before resolving it.
The following script validates the requirement based on the tests executed for the version assigned to the requirement issue.
You can either make the validation based on the existence of failed tests (i.e. requirement status is "NOK") or, in a more complete way by making sure all of them are passing (i.e. requirement status is "OK").
log.debug(issue.project.name)projectName = issue.project.nameversion = issue.fixVersions.join('')log.debug(version) //jql = "key = ${issue.key} and issue in requirements('NOK','${projectName}','${version}')"jql = "key = ${issue.key} and issue in requirements('OK','${projectName}','${version}')"issues = getIssues(jql)count = issues.size //log.debug(count)
/*if (count>0) { invalidInputException = new InvalidInputException("Some tests are failing. You must assure that they pass before making the transition.")}*/
if (count == 0) { invalidInputException = new InvalidInputException("Some tests need to be executed. You must assure that they pass before making the transition.")}
Reopen/transition linked Tests to a requirement
Whenever you change the specification of a requirement, you most probably will need to review the Tests that you have already specified.
The following script tries to make a transition on all linked Tests to a requirement. You can hook it to a post-function on the transition of the requirement.
MutableIssue tempissue = issueManager.getIssueObject(it.key) workflowTransitionUtil.setIssue(tempissue); workflowTransitionUtil.setUsername(serviceAccount.getUsername()); def actionId = 3 // change it accordingly if (issueWorkflowManager.isValidAction(tempissue, actionId)){ workflowTransitionUtil.setAction(actionId);//Id of the status you want to transition to try { workflowTransitionUtil.progress(); } catch (InvalidActionException e) { log.error("Caught exception trying to transition issue" + e.getMessage()); } }}
Requirement projects
Although you can go to Xray settings, in the "Requirement Projects" tab, and define a project as containing requirement issues, sometimes you may want to automate this.
The following script shows several functions that you can use to obtain the list of projects with requirement coverage enabled and to set or unset a project as being a requirements project.
if (getBehaviourContextId() == "create-testexec-from-testset") { getFieldById("project-field").setReadOnly(true) getFieldById("issuetype-field").setReadOnly(true)
def contextIssue = issueManager.getIssueObject(getContextIssueId()) getFieldById("summary").setFormValue("Issue created from ${contextIssue.key}").setReadOnly(true) def jql = "issue in testSetTests('${contextIssue.key}')" def issues = getIssues(jql) getFieldByName("Tests association with a Test Execution").setFormValue(issues.collect { it.key})}
As a mere example, you can see below how it could be setup, by creating a web section and then a webitem to trigger the actual script for creating the Test Execution.
Calculate requirement status for a certain version
Xray provides the "Requirement Status" custom field that shows the calculated coverage status for some version(s) depending on the configuration of that field in Xray's Custom Fields Preferences settings.
Sometimes you may need to evaluate the calculate requirement coverage status on some specific version, based on the executions made for that version.
A possible use case could be defining a scripted field present on the requirement issue screen that calculates the coverage status for some specific hardcoded version.
jql = "issue in requirementTests('${issue.key}')"issues = getIssues(jql)issues.size
Show Defects Count for a requirement
In the following example, a "script field" is used to show the total amount of linked defects to a given requirement and also provide a link to easily obtain those defects in the Issue search page.
This script field is configured to generate the output as HTML.
In scenarios with CI implemented, you may want to trigger certain Jenkins "jobs" (i.e. project build) from a Test Plan and link back the result to Xray.
In this example, we're assuming that the list of automated tests that will be run is managed in the CI side, depending on the project configuration.
The results will be submitted back to Xray, if the project is configured to do so in Jenkins.
In order to add this option in Jira's UI, we'll need to add a custom "web item" that provides an action that will interact with a custom ScriptRunner endpoint, which will be the one doing the HTTP request to the Jenkins server, passing the Test Plan issue key. In order to submit the request to Jenkins, we need to obtain Jenkins username and respective API token along with the project-specific authentication token.
trigger_jenkins_build_restapi_endpoint.groovy
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegateimport groovy.json.JsonOutputimport groovy.transform.BaseScriptimport groovy.json.JsonSlurper;import groovy.json.StreamingJsonBuilder;import javax.ws.rs.core.MultivaluedMapimport javax.ws.rs.core.Response @BaseScript CustomEndpointDelegate delegate triggerJenkinsBuild(httpMethod: "GET") { MultivaluedMap queryParams -> def issueId = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue def flag = [ type : 'success', title: "Build scheduled", close: 'auto', body : "A new build has been scheduled related with "+issueId ]
URL url; def jobName = "java-junit-calc" // could come from a CF in the Test Plan def jenkinsHostPort = "192.168.56.102:8081" // could be defined elsewhere def token = "iFBDOBhNhaxL4T9ass93HRXun2JF161Z" // could also come from a CF in the Test Plan def username = "admin" // probably, would need to be stored elsewhere def password = "fa02840152aa2e4da3d8db933ec708d6" // probably, would need to be stored elsewhere def baseURL = "http://${jenkinsHostPort}/job/${jobName}/buildWithParameters?token=${token}&TESTPLAN=$issueId"
In Jenkins, we need to generate an API token for some user, which can be done from the profile settings page.
At the project level, we need to enable remote build triggers, so we can obtain an "authentication token" to be used in the HTTP request afterwards.
The project itself is a normal one; the only thing relevant to mention is that this project is a parameterized one, so it receives a TESTPLAN variable, that in our case will be coming from Jira.
The final task submits the results linking the Test Execution to the Test Plan passed as argument.
Trigger a Jenkins project build from a Test Plan, for the Tests contained in the Test Plan
This scenario is somehow similar to the previous one, except that the list of Tests that will be run in the CI side will be based on the Tests contained in the Test Plan.
The results will be submitted back to Xray, if the project is configured to do so in Jenkins.
In order to add this option in Jira's UI, we'll need to add a custom "web item" that provides an action that will interact with a custom ScriptRunner endpoint, which will be the one doing the HTTP request to the Jenkins server, passing the Test Plan issue key. In the ScriptRunner endpoint script, we'll obtain the list of Generic Tests (we're assuming that they will come from Junit, so they have a certain syntax in the Generic Test Definition field.
In order to submit the request to Jenkins, we need to obtain Jenkins username and respective API token along with the project-specific authentication token.
String replaceLast(String string, String substring, String replacement){ int index = string.lastIndexOf(substring); if (index == -1) return string; return string.substring(0, index) + replacement + string.substring(index+substring.length());}
triggerJenkinsBuildWithTestList(httpMethod: "GET") { MultivaluedMap queryParams -> // the details of getting and modifying the current issue are ommitted for brevity def issueId = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue
def flag = [ type : 'success', title: "Build scheduled", close: 'auto', body : "A new build has been scheduled related with "+issueId ]
URL url; def jobName = "java-junit-calc-triggered" // could be defined in a CF in the Test Plan def jenkinsHostPort = "192.168.56.102:8081" // could be defined elsewhere def token = "iFBDOBhNhaxL4T9ass93HRXun2JF161Z" // could also come from a CF in the Test Plan
def username = "admin" // probably, would need to be stored elsewhere def password = "fa02840152aa2e4da3d8db933ec708d6" // probably, would need to be stored elsewhere //def baseURL = "http://${username}:${password}@${jenkinsHostPort}/job/${jobName}/build?token=${token}" //def baseURL = "http://${jenkinsHostPort}/job/${jobName}/build?token=${token}"
jql = "issue in testPlanTests('${issueId}') and \"Test Type\" = Generic" issues = getIssues(jql) // we're assuming that we have Junit based Tests.. so we need to do some conversion beforehand, so maven can process the list of tests to be run def testlist = issues.collect { getFieldValue(it,"Generic Test Definition")} def testlist2 = testlist.collect { replaceLast(it,".","%23") } def baseURL = "http://${jenkinsHostPort}/job/${jobName}/buildWithParameters?token=${token}&TESTPLAN=${issueId}&TESTLIST=${testlist2.join(',')}" url = new URL(baseURL); def body_req = []
In Jenkins, we need to generate an API token for some user, which can be done from the profile settings page.
At the project level, we need to enable remote build triggers, so we can obtain an "authentication token" to be used in the HTTP request afterwards.
The project itself is a normal one; the only thing relevant to mention is that this project is a parameterized one, so it receives TESTPLAN and TESTLIST variables, that in our case will be coming from Jira.
Maven is configured in order to run just the tests identified in the TESTLIST variable, using the "-Dtest" JVM option.
The final task submits the results linking the Test Execution to the Test Plan passed as argument.
Trigger a Bamboo plan build from a Test Plan
In scenarios with CI implemented, you may want to trigger certain Bamboo "plans" (i.e. builds) from a Test Plan and link back the result to Xray.
In this example, we're assuming that the list of automated tests that will be run is managed in the CI side, depending on the plan configuration.
The results will be submitted back to Xray, if the project is configured to do so in Bamboo.
In order to add this option in Jira's UI, we'll need to add a custom "web item" that provide an action that will interact with a custom ScriptRunner endpoint, which will be the one doing the HTTP request to the Bamboo server, passing the Test Plan issue key. In order to submit the request to Bamboo we just need to use the credentials of some user.
@BaseScript CustomEndpointDelegate delegate triggerBambooBuild(httpMethod: "GET") { MultivaluedMap queryParams -> def issueId = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue def flag = [ type : 'success', title: "Build scheduled", close: 'auto', body : "A new build has been scheduled related with "+issueId ]
URL url; // curl --user admin:admin -X POST -d "default&ExecuteAllStages=true" http://yourbambooserver/rest/api/latest/queue/XRAY-JUNITCALC def projectKey = "XRAY" // could come from a CF in the Test Plan def planKey = "JUNITCALC" // could come from a CF in the Test Plan def bambooHostPort = "192.168.56.102:8085" // could be defined elsewhere def username = "admin" // probably, would need to be stored elesewhere def password = "admin" // probably, would need to be stored elesewhere def baseURL = "http://${bambooHostPort}/rest/api/latest/queue/${projectKey}-${planKey}" String urlParameters = "default&ExecuteAllStages=true&bamboo.TESTPLAN=${issueId}"; byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); int postDataLength = postData.length; url = new URL(baseURL); def body_req = []
The project itself is a normal one; the only thing relevant to mention is that this project is a parameterized one, so it receives a TESTPLAN variable, that in our case will be coming from Jira.
The final task submits the results linking the Test Execution to the Test Plan passed as an argument.
Trigger a Bamboo plan/stage build from a Test Plan, for the Tests contained in the Test Plan
This scenario is somehow similar to the previous one, except that the list of Tests that will be run in the CI side will be based on the Tests contained in the Test Plan.
The results will be submitted back to Xray, if the project is configured to do so in Bamboo.
In order to add this option in Jira's UI, we'll need to add a custom "web item" that provides an action that will interact with a custom ScriptRunner endpoint, which will be the one doing the HTTP request to the Bamboo server, passing the Test Plan issue key. In the ScriptRunner endpoint script, we'll obtain the list of Generic Tests (we're assuming that they will come from Junit, so they have a certain syntax in the Generic Test Definition field.
In order to submit the request to Bamboo, we need the credentials of some Bamboo user.
String replaceLast(String string, String substring, String replacement){ int index = string.lastIndexOf(substring); if (index == -1) return string; return string.substring(0, index) + replacement + string.substring(index+substring.length());} triggerBambooBuildWithTestList(httpMethod: "GET") { MultivaluedMap queryParams ->
def issueId = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue def flag = [ type : 'success', title: "Build scheduled", close: 'auto', body : "A new build has been scheduled related with "+issueId ]
jql = "issue in testPlanTests('${issueId}') and \"Test Type\" = Generic" issues = getIssues(jql) // // we're assuming that we have Junit based Tests.. so we need to do some conversion beforehand, so maven can process the list of tests to be run def testlist = issues.collect { getFieldValue(it,"Generic Test Definition")} def testlist2 = testlist.collect { replaceLast(it,".","%23") }
def projectKey = "XRAY" // could come from a CF in the Test Plan def planKey = "JUNITCALCPARAMS" // could come from a CF in the Test Plan def stage = "default" // could be hardcoded or come from a CF in the Test Plan def bambooHostPort = "192.168.56.102:8085" // could be defined elsewhere def username = "admin" // probably, would need to be stored elsewhere def password = "admin" // probably, would need to be stored elsewhere def baseURL = "http://${bambooHostPort}/rest/api/latest/queue/${projectKey}-${planKey}" String urlParameters = "${stage}&ExecuteAllStages=true&bamboo.TESTPLAN=${issueId}&bamboo.TESTLIST=${testlist2.join(',')}"; byte[] postData = urlParameters.getBytes( StandardCharsets.UTF_8 ); int postDataLength = postData.length; url = new URL(baseURL); def body_req = []
The project itself is a normal one; the only thing relevant to mention is that this project is a parameterized one, so it receives TESTPLAN and TESTLIST variables, that in our case will be coming from Jira.
Maven is configured in order to run just the tests identified in the TESTLIST variable, using the "-Dtest" JVM option.
The final task submits the results linking the Test Execution to the Test Plan passed as argument.
Extending REST API for interacting with requirement projects
In this example, we'll be creating some endpoints for obtaining the requirement projects and also for enabling or disabling requirement coverage for a certain project.
This makes use of ScriptRunner's custom REST API capabilities.
Synchronize Tests from "related" Test Sets to a Test Execution or to a Test Plan
Test Plans and (Sub) Test Executions contain a list of Tests. Although you can add Tests using Test Sets, you're not actually adding the Test Set itself; you're adding the Tests that belong to that Test Set(s) at that given moment. Thus, there is no relation whatsoever between Test Plans<=>Test Sets or between Test Executions<=>Test Sets.
However, and taking the Test Plan as an example, you may find handy to have some sort of "dynamic Test Plan" that will contain the Tests of the Test Sets that would be related to that Test Plan.
Since there is no relation between Test Plans<=>Test Sets, you have to define a convention for that. One option would be to use issue links (e.g. "relates to" or "includes"); in this case, you would need to manually create these links between Test Sets and Test Plans (or Test Executions).
After this, you can create a custom script that will obtain the Tests from the related (e.g. the linked) Test Sets using JQL, followed by REST API requests to specific endpoints to add these Tests to the destination entity (e.g. Test Plan).
We start by adding a button in Jira's UI using a custom "web item", providing an action that will interact with a custom ScriptRunner endpoint containing the following logic:
receive the source issue key that triggered the synchronization request (e.g. Test Plan, Test Execution, Sub Test Execution)obtain the linked Test Sets, using JQL
obtain the Tests on each Test Set, using JQLsubmit a REST API request to Xray specific endpoints to add the Test to the entity (e.g Test Plan)
// you should use a specific user for this purpose username = "admin" password = "admin" def authString = "${username}:${password}".bytes.encodeBase64().toString()
Object getIssues(jqlQuery){ // A list of GenericValues representing issues List<Issue> searchResults = null; SearchService.ParseResult parseResult = searchService.parseQuery(serviceAccount, jqlQuery); if (parseResult.isValid()) { // throws SearchException SearchResults results = searchService.search(serviceAccount, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()); searchResults = results.getIssues(); return searchResults; } return []}
synchTestsFromRelatedTestSets(httpMethod: "GET") { MultivaluedMap queryParams -> // issue_key may refer to a Test Plan or to a Test Execution def issue_key = queryParams.getFirst("issueId") as String // use the issueId to retrieve this issue projectManager = ComponentAccessor.getProjectManager() componentManager = ComponentManager.getInstance(); searchService = ComponentAccessor.getComponent(SearchService.class); issueManager = ComponentAccessor.getIssueManager() customFieldManager = ComponentAccessor.getCustomFieldManager() userUtil = ComponentAccessor.getUserUtil(); serviceAccount = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() pluginAccessor = componentManager.getPluginAccessor(); MutableIssue issue = issueManager.getIssueObject(issue_key)
// assume that Test Plan/Execution is linked to Test Sets using the issue link "relates to"; customize if needed jql = "issue in linkedIssues('${issue_key}', 'relates to')" testset_issues = getIssues(jql) def had_errors = false def success = false testset_issues.each { // process only "Test Set" issues if (it.issueType.name == "Test Set") { jql = "issue in testSetTests('${it.key}')" issues = getIssues(jql) test_keys = issues.collect{ it.key } //log.debug(test_keys) if (issue.issueType.name == "Test Plan") {
success = associateTestsToTestPlan(issue_key, test_keys) } else if ((issue.issueType.name == "Sub Test Execution") || (issue.issueType.name == "Test Execution")) { success = associateTestsToTestExecution(issue_key, test_keys) } if (!success) { had_errors = true } } } def flag = [] if (success) { flag = [ type : 'success', title: "Test Sets Synchronization", close: 'auto', body : "Tests have been synchronized for " + issue_key ] Response.ok(JsonOutput.toJson(flag)).build() } else { flag = [ type : 'success', title: "Test Sets Synchronization", close: 'auto', body : "Tests have been synchronized for " + issue_key ] Response.serverError().entity([error: "some Tests were no synchronized"]).build() } }
Example
Add a custom link to the Tests top menu
Sometimes it may be useful to add a custom link to the Tests top menu.
As an example, you may have an internal documentation/Confluence space with some valuable information concerning Xray usage in your organization.
ScripRunner configuration
Adding an entry to the top menu is easy.
Just go the Add-ons section in your Jira administration and then "Script Fragments".
Add a new "Raw xml module".
And add the configuration for the link; this configuration is exactly the same as if you were going to develop your own app, so it follows Atlassian developer element.documentation guidelines for the "web-item"