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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,456,032
Community Members
 
Community Events
176
Community Groups

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

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

0 votes

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

Thank you and I'll give it try

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events