Created
September 29, 2019 08:36
-
-
Save akerl/d334523666cc15c567d128b0a317103f to your computer and use it in GitHub Desktop.
Groovy scriptrunner jira
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def statusChange = changelog | |
.items | |
.find { (it as Map).field == "status" } | |
if ( statusChange == null ) { | |
logger.info("No status change in event") | |
return | |
} | |
def blocking = (statusChange as Map).toString != "Resolved" | |
def linkedIssues = issue | |
.fields | |
.issuelinks | |
.findAll { | |
def m = it as Map | |
m.containsKey("outwardIssue") && (m.type as Map).name == "Blocks" | |
} | |
def originalId = issue.id | |
linkedIssues.each { | |
def id = (it.outwardIssue as Map).id | |
def issueStatus = (it.outwardIssue as Map).fields.status.name | |
if ( issueStatus == "Blocked" && blocking ) { | |
logger.info("Issue already blocked: ${id}") | |
} else if ( issueStatus == "Open" && !blocking) { | |
logger.info("Issue already unblocked: ${id}") | |
} else if ( blocking ) { | |
logger.info("Marking issue as blocked: ${id}") | |
def transitions = get("/rest/api/3/issue/${id}/transitions") | |
.asObject(Map) | |
.body | |
.transitions as List<Map> | |
def blockedTransition = transitions.find { (it.to as Map).name == "Blocked" } | |
def result = post("/rest/api/3/issue/${id}/transitions") | |
.header("Content-Type", "application/json") | |
.body([ | |
transition:[ | |
"id": blockedTransition.id | |
] | |
]) | |
.asString() | |
assert result.status >= 200 && result.status < 300 | |
} else { | |
logger.info("Checking if issue can be unblocked: ${id}") | |
def otherIssueFields = get("/rest/api/3/issue/${id}?fields=issuelinks,status") | |
.asObject(Map) | |
.body | |
.fields as Map | |
def otherIssueLinks = otherIssueFields | |
.issuelinks | |
.findAll { | |
def m = it as Map | |
m.containsKey("inwardIssue") && m.type.name == "Blocks" && m.inwardIssue.id != originalId | |
} | |
def otherBlockingIssues = otherIssueLinks | |
.findAll { | |
(it.inwardIssue as Map).fields.status.name != "Resolved" | |
} | |
if ( otherBlockingIssues.size() > 0 ) { | |
logger.info("This issue cannot be unblocked due to other blocking issues") | |
} else { | |
logger.info("Unblocking this issue") | |
def transitions = get("/rest/api/3/issue/${id}/transitions") | |
.asObject(Map) | |
.body | |
.transitions as List<Map> | |
def openTransition = transitions.find { (it.to as Map).name == "Open" } | |
def result = post("/rest/api/3/issue/${id}/transitions") | |
.header("Content-Type", "application/json") | |
.body([ | |
transition:[ | |
"id": openTransition.id | |
] | |
]) | |
.asString() | |
assert result.status >= 200 && result.status < 300 | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ((issueLink.issueLinkType as Map).name != "Blocks") { | |
logger.info("Not a blocking link") | |
return | |
} | |
def blockedId = issueLink.destinationIssueId | |
logger.info("Blocked issue ID: ${blockedId}") | |
def transitions = get("/rest/api/3/issue/${blockedId}/transitions") | |
.asObject(Map) | |
.body | |
.transitions as List<Map> | |
def blockedTransition = transitions.find { (it.to as Map).name == "Blocked" } | |
logger.info("Block transition ID: ${blockedTransition.id}") | |
logger.info("Moving issue to blocked") | |
def result = post("/rest/api/3/issue/${blockedId}/transitions") | |
.header("Content-Type", "application/json") | |
.body([ | |
transition:[ | |
"id": blockedTransition.id | |
] | |
]) | |
.asString() | |
assert result.status >= 200 && result.status < 300 | |
logger.info("Issue moved to blocked") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ((issueLink.issueLinkType as Map).name != "Blocks") { | |
logger.info("Not a blocking link") | |
return | |
} | |
def blockedId = issueLink.destinationIssueId | |
logger.info("Blocked issue ID: ${blockedId}") | |
def issueFields = get("/rest/api/3/issue/${blockedId}?fields=issuelinks") | |
.asObject(Map) | |
.body | |
.fields as Map | |
def issueLinks = issueFields | |
.issuelinks | |
.findAll { | |
def m = it as Map | |
m.containsKey("inwardIssue") && (m.type as Map).name == "Blocks" | |
} | |
logger.info("${issueLinks}") | |
if (issueLinks.size() > 0) { | |
logger.info("Other blocking issues folks") | |
return | |
} | |
def transitions = get("/rest/api/3/issue/${blockedId}/transitions") | |
.asObject(Map) | |
.body | |
.transitions as List<Map> | |
def openTransition = transitions.find { (it.to as Map).name == "Open" } | |
logger.info("Open transition ID: ${openTransition.id}") | |
logger.info("Moving issue to open") | |
def result = post("/rest/api/3/issue/${blockedId}/transitions") | |
.header("Content-Type", "application/json") | |
.body([ | |
transition:[ | |
"id": openTransition.id | |
] | |
]) | |
.asString() | |
assert result.status >= 200 && result.status < 300 | |
logger.info("Issue moved to open") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment