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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase
Published in Confluence Cloud

Share your Confluence Cloud experience for a chance to win $500!

Take our 5-minute survey to win a $500 Visa gift card! Are you currently using Confluence Cloud? We want to hear from you! Fill out this quick survey about your Confluence Cloud experience so we ...

74 views 1 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you