Skip to content

Instantly share code, notes, and snippets.

@gtaing1
Forked from utopius/TransitionLinkedIssues
Created April 19, 2016 22:44
Show Gist options
  • Save gtaing1/4d10db9d164d0749c5bd793a481272fa to your computer and use it in GitHub Desktop.
Save gtaing1/4d10db9d164d0749c5bd793a481272fa to your computer and use it in GitHub Desktop.
JIRA Script Runner groovy post-function which transitions linked issues with the given link type to the given step.Still needs some improvement.Put into atlassian-jira\WEB-INF\classes\com\onresolve\jira\groovy\canned\workflow\postfunctionsTo update you may have to edit a workflow transition -> add postfunction -> select ResolveLinkedIssues. This…
package com.onresolve.jira.groovy.canned.workflow.postfunctions
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ErrorCollection
import com.onresolve.jira.groovy.canned.CannedScript
import com.onresolve.jira.groovy.canned.utils.CannedScriptUtils
import org.apache.log4j.Category
import com.atlassian.jira.issue.link.IssueLinkManager
import com.onresolve.jira.groovy.canned.utils.WorkflowUtils
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.crowd.embedded.api.User
class TransitionLinkedIssues implements CannedScript{
ComponentManager componentManager = ComponentManager.getInstance()
Category log = Category.getInstance(UpdateBlockedIssues.class)
def projectManager = componentManager.getProjectManager()
public static final String FIELD_LINK_TYPE = 'FIELD_LINK_TYPE'
public static final String FIELD_COMMENT = 'FIELD_COMMENT'
public static final String FIELD_LINKED_ACTION = 'FIELD_LINKED_ACTION'
public static final String FIELD_RESOLUTION_ID = 'FIELD_RESOLUTION_ID'
String getName() {
"Transition linked issues."
}
public String getHelpUrl() {
""
}
String getDescription() {
"Transitions linked issues when this issue is transitioned." + """<br>
This function should be put on the Resolve transition (or similar).
"""
}
List getCategories() {
["Function"]
}
List getParameters(Map params) {
[
[
Name:FIELD_LINKED_ACTION,
Label:"Linked issue action",
Description:"Choose the action to do on the linked issue.",
Type: "list",
Values: CannedScriptUtils.getAllWorkflowActions(false)
],
[
Label:"Resolution",
Name:FIELD_RESOLUTION_ID,
Type: "list",
Description:"Resolution to use on the parent",
Values: CannedScriptUtils.getResolutionOptions(true),
],
[
Name:FIELD_LINK_TYPE,
Label:'Issue Link Type',
Type: "list",
Description:"What link types to look for... e.g. <b><i>blocks</i> will transition all issues that this one blocks</b>",
Values: CannedScriptUtils.getAllLinkTypesWithDirections(true)
],
[
Name:FIELD_COMMENT,
Label: 'Comment',
Description: 'Comment to add to linked issues. Click an example below.',
Type:'mediumtext',
Examples:[
"Blocking issue ABC-123 resolved with resolution Fixed.":
"Blocking issue \$issue resolved with resolution <% out << issue.resolution?.name %>."
],
]
]
}
public ErrorCollection doValidate(Map params, boolean forPreview) {
SimpleErrorCollection errorCollection = new SimpleErrorCollection()
if (! params[FIELD_LINKED_ACTION]) {
errorCollection.addError(FIELD_LINKED_ACTION, "You must provide a linked issue action. Editing this function and clicking the example link will copy an example.")
}
if (! params[FIELD_COMMENT]) {
errorCollection.addError(FIELD_COMMENT, "You must provide a comment. Editing this function and clicking the example link will copy an example.")
}
if (! params[FIELD_LINK_TYPE]) {
errorCollection.addError(FIELD_LINK_TYPE, "You must provide a link type.")
}
errorCollection
}
Map doScript(Map params) {
Issue issue = params['issue'] as Issue
String linkTypeStr = params[FIELD_LINK_TYPE] as String
String commentTemplate = params[FIELD_COMMENT] as String
commentTemplate = "AUTO: " + commentTemplate
String linkTypeId = ""
String linkDirection = ""
(linkTypeId, linkDirection) = linkTypeStr.split(/ /) as List
IssueLinkManager linkMgr = ComponentManager.getInstance().getIssueLinkManager()
CommentManager commentManager = componentManager.getCommentManager()
User currentUser = WorkflowUtils.getUser(params)
List links = []
if (linkDirection == CannedScriptUtils.OUTWARD_FIELD_NAME) {
links = linkMgr.getOutwardLinks(issue.id)
}
else if (linkDirection == CannedScriptUtils.INWARD_FIELD_NAME) {
links = linkMgr.getInwardLinks(issue.id)
}
String actionName = params[FIELD_LINKED_ACTION] as String
String resolutionId = params[FIELD_RESOLUTION_ID] as String
links.each {IssueLink link ->
if (link.getLinkTypeId() == linkTypeId as Long) {
MutableIssue destIssue = linkDirection == CannedScriptUtils.OUTWARD_FIELD_NAME ? link.getDestinationObject() : link.getSourceObject()
Writable template = WorkflowUtils.mergeTemplate(params, commentTemplate)
log.info ("Create comment on ${destIssue.key}")
commentManager.create(destIssue, currentUser.getName(), template.toString(), true)
log.debug ("Resolve linked issue")
Integer actionId = actionName?.replaceAll(/ .*/, "") as Integer
if (WorkflowUtils.hasAction(destIssue, actionId)) {
WorkflowUtils.resolveIssue(destIssue, actionId, currentUser, resolutionId, [:])
} else {
log.warn("Action name: $actionName not found for this step.")
}
}
}
[:]
}
String getDescription(Map params, boolean forPreview) {
getName()
}
public Boolean isFinalParamsPage(Map params) {
true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment