Snippets

Adaptavist Script Console Transition An Issue When All Linked Issues Are Resolved

Created by Kristian Walker

File ScriptConsoleTransitionAnIssueWhenAllLinkedIssuesAreResolved Added

  • Ignore whitespace
  • Hide word diff
+/*
+ * "This script console script can be ran on the Script Console inside of Jira Cloud to show how you can return all of the linked issues for an issue and perform a transition on the issue if all of the 
+ * linked issues are in a Closed status.
+ * All right, title and interest in this code snippet shall remain the exclusive intellectual property of Adaptavist Group Ltd and its affiliates. Customers with a valid ScriptRunner 
+ * license shall be granted a  non-exclusive, non-transferable, freely revocable right to use this code snippet only within their own instance of Atlassian products. This licensing notice cannot be removed
+ * or amended and must be included in any circumstances where the code snippet is shared by You or a third party." 
+ */
+ 
+ // specify the issue key
+def issueKey = "<IssueKeyHere>"
+
+// Specify the name of the version to extract from the issue
+def closedStatus = '<ClosedStatusHere>'
+
+// Specify the transition to execute on the issue
+// NOTE - The transition ID must represent a valid transition for the workflow that the issue uses.
+def transitionID = '<TransitionIDHere>'
+
+// Specify a boolean to flag if all issue links are resolved
+boolean allIssueLinksResolved;
+
+// Get the issue object
+def result = get('/rest/api/2/issue/' + issueKey)
+        .header('Content-Type', 'application/json')
+        .asObject(Map)
+
+// Find the linked issues
+def linkedIssues = result.body.fields.issuelinks
+
+// if all issue links have the Done status set the flag to true
+if(linkedIssues.findAll{closedStatus.contains(it.outwardIssue.fields.status.name)}){
+    allIssueLinksResolved = true
+}
+
+// If any of the linked issues do not have the done status set the flag to false
+if (linkedIssues.findAll{!closedStatus.contains(it.outwardIssue.fields.status.name)}){
+    allIssueLinksResolved = false
+}
+
+// If all issue links are resolved transition the issue
+if(allIssueLinksResolved == true){
+
+// The rest call to transition the issue
+    def transitionIssue = post("/rest/api/2/issue/${issueKey}/transitions")
+            .header("Content-Type", "application/json")
+            .body([transition: [id: transitionID]])
+            .asObject(Map)
+
+    return "All issue links are to closed issues and the issue has been transitioned."
+// If the issue contains unresolved linked issues return a message and do not transition the issue
+}else if (allIssueLinksResolved == false){
+    return "The issue contain issue links which are not closed"
+}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.