Skip to content

Instantly share code, notes, and snippets.

@akerl
Created September 29, 2019 08:36
Show Gist options
  • Save akerl/d334523666cc15c567d128b0a317103f to your computer and use it in GitHub Desktop.
Save akerl/d334523666cc15c567d128b0a317103f to your computer and use it in GitHub Desktop.
Groovy scriptrunner jira
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
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment