Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to use Script Runner (Post Function) to clone an issue but only when a link does not exist?

Dan Sitarski October 16, 2018

I've added a Post Function - Script Runner function to one of the workflow transition steps to clone the issue and would like to only perform the clone if none of the issue links have an issue type of 'Change Control' for example.

I've found a lot of example on the internet but none seem to cover finding all the issue links then checking each issue type.

thank you

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 17, 2018

This is a script I use to transition linked issues.

I assume you can just adjust to use it as a condition

 

import com.atlassian.jira.user.*
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.*
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.workflow.IssueWorkflowManager
import com.atlassian.jira.util.JiraUtils
import com.opensymphony.workflow.loader.ActionDescriptor
import com.atlassian.jira.ComponentManager

//Get the current Issue
Issue issue = issue;

//Get User
def userManager = ComponentAccessor.getUserManager()
def sdUser = userManager.getUserByKey('sdagent')

List<IssueLink> allOutIssueLink = ComponentAccessor.getIssueLinkManager().getOutwardLinks(issue.getId());

WorkflowTransitionUtil workflowTransitionUtil = (WorkflowTransitionUtil) JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
IssueWorkflowManager issueWflwMan = ComponentManager.getComponentInstanceOfType(IssueWorkflowManager.class);

//Define action name for parent issue
String actionName = "Resolve";

List<Issue> allParentIssues = new ArrayList<Issue>();
List<Issue> allChildIssues = new ArrayList<Issue>();
Boolean allClosed = true;
int actionID;

//you might want to change allOut with allIn depending on the link type
//Get all Parent Issues
for (Iterator<IssueLink> outIterator = allOutIssueLink.iterator(); outIterator.hasNext();) {
    IssueLink outIssueLink = (IssueLink) outIterator.next();
    allParentIssues.add(outIssueLink.getDestinationObject());
}

 

//here is the iteration for each linked issue, check again inward or outwards links
for(Issue parentIssue : allParentIssues){
    List<IssueLink> allInwardIssueLink = ComponentAccessor.getIssueLinkManager().getInwardLinks(parentIssue.getId());

    //Get all linked Issues
    for (Iterator<IssueLink> inIterator = allInwardIssueLink.iterator(); inIterator.hasNext();) {
        IssueLink inIssueLink = (IssueLink) inIterator.next();
        allChildIssues.add(inIssueLink.getSourceObject());
    }

    //If all  closed, update the parent issue
    if(allClosed) {
        MutableIssue mutableParentIssue = (MutableIssue) parentIssue;
        if(parentIssue.projectObject.name=='Service Center'){
            def disallowedStatuses = ['Resolved','Closed','Done','Cancelled']
            if(!disallowedStatuses.contains(parentIssue.getStatus().getName())){
                Collection<ActionDescriptor> coll = issueWflwMan.getAvailableActions(parentIssue, sdUser);
                for(ActionDescriptor ad : coll) {
                    if(ad.getName().equals(actionName)) {
                        actionID = ad.getId();
                    }
                }
       
        workflowTransitionUtil.setIssue(mutableParentIssue);
        workflowTransitionUtil.setUserkey('userkey');
        workflowTransitionUtil.setAction(actionID);
        workflowTransitionUtil.validate();
        workflowTransitionUtil.progress();
        }
        }
    }
}
//x

Dan_Sitarski October 17, 2018

Thank you and I'll give it try

TAGS
AUG Leaders

Atlassian Community Events