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

Set linked issue

Dan27 August 5, 2018

Hello!

I want to set a linked issue with groovy post function.

 

My current code didn't work... why ? Thanks!

def newFeatureIssue = issueManager.getIssueObject(temp) // Epic issue key here
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def featureLink = customFieldManager.getCustomFieldObjectByName("Parent Feature")
featureLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(featureLink), newFeatureIssue),new DefaultIssueChangeHolder())
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 5, 2018

Hello,

if you want to set a link then your code should look like this;

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
linkMgr.createIssueLink(11111, 22222, 33333, 1, currentUser)

You can find more details here:

https://community.atlassian.com/t5/Jira-questions/How-to-set-issue-links-through-groovy-in-script-postfunction/qaq-p/713709

Dan27 August 5, 2018

Thanks @Alexey Matveev ,

Do you know how can I take the issue link type of inward ? I find the id of the link but how can I refer to the inward specifically ?

 

Thank you!

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 5, 2018

Hello,

Each link has the outward and inward descriptions. Do you mean that you want to know the id of the inward description? If so, you do not need it. 

When you create a link between issues using createIssueLink, the first variable is the source issue(outward) and the second variable is the destination issue (inward).

Dan27 August 5, 2018

Thanks !! you right :) @Alexey Matveev

The only problem is that the link is added and not switch.. How can I delete the previous one ??

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 5, 2018

It would be something like this:

    def linkMgr = ComponentAccessor.getIssueLinkManager();
    def issueLink = linkMgr.getIssueLink(sourceIssue.getId(), destinationIssue.getId(), linkTypeID);
   if (issueLink != null) {
      linkMgr.removeIssueLink(issueLink, user);
   }

The IssueLinkManager class is responsible for handling links. You can have a look at all available methods here:

https://docs.atlassian.com/software/jira/docs/api/7.1.4/com/atlassian/jira/issue/link/IssueLinkManager.html 

Dan27 August 5, 2018

@Alexey Matveev Put this code before or after the 

createIssueLink

?

 

When I put it after, it deleted the link I just added, I need to delete the previous one. 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 6, 2018

You should put it before the createIssueLink

Dan27 August 6, 2018

@Alexey Matveev

Hi, I did it like this, and it worked in the console..

I copy it to post function.. not working, why is that ?

 

 

//remove the new feature
issueLinkManager.getInwardLinks(issue.id).each {IssueLink iLink ->
if(iLink.getLinkTypeId() == 10402){ //Feature Epic

Issue linkedIssue = iLink.sourceObject
issueLinkManager.removeIssueLink(iLink, user);
}
}
def userManager = ComponentAccessor.userManager
//Add the previous feature
try
{
issueLinkManager.createIssueLink(PrevFeature.getId() as Long,issue.getId() as Long, 10402L as Long, 1 as Long, user);
//issue.store();
//ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
}
catch (Exception e) {
throw e
}

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
August 6, 2018

Is this post function in the create issue tranistion? If so, is it last in the list of post functions? What are the errors? What does not work?

Dan27 August 6, 2018

@Alexey Matveev , It is a post function of 'change parent' , and I put it first inside epic workflow.

There are no error, the link didn't change.. I try to take the previous one, and switch the new one with the previous..

 

This is my full code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean 
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import com.atlassian.jira.project.version.VersionManager 
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.config.IssueTypeManager;
import com.atlassian.jira.issue.link.LinkCollectionImpl;
import com.atlassian.jira.issue.link.IssueLink;

IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

//def issueManager = ComponentAccessor.getIssueManager();
//test
//Issue issue = issueManager.getIssueObject("WBL-19638");
// your actions if the field has changed

//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
//Get temp with fix version
def tempVersion=fixVersions[0];

//Get Epic Link Key
def feature = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Feature");
def featureKey = issue.getCustomFieldValue(feature);
Issue featureIssue = issueManager.getIssueObject(featureKey.toString());
log.warn featureKey;

//Internal feature checkbox ?
def internal = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Internal Feature");
//return internal;
def selectedValues = internal.getValue(featureIssue);

if(selectedValues.toString()!='[Yes]')
{

//Get fix version of Epic
Collection<Version>fixVersionsFeature = new ArrayList<Version>();
fixVersionsFeature=featureIssue.getFixVersions();
def tempVersion2=fixVersionsFeature[0];
//If tempVersion == fixVersionsEpic
if(tempVersion.toString()==tempVersion2.toString())
{
return;
}
//if temp != fixVersionsEpic
if(tempVersion.toString()!=tempVersion2.toString())
{
//error message
UserMessageUtil.error("Failed. Fix version of Epic should be like the Feature's fix version");
//Get list and size of the previous versions
ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Parent Feature")
def size = changeList.size();
def temp= changeList[size-1].getFromString();
//Set the previous feature
Issue PrevFeature = issueManager.getIssueObject(temp.toString());
//Set the previous feature
def userManager = ComponentAccessor.userManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
def issueLinkManager = ComponentAccessor.getIssueLinkManager();

//remove the new feature
issueLinkManager.getInwardLinks(issue.id).each {IssueLink iLink ->
if(iLink.getLinkTypeId() == 10402L){ //Feature Epic

Issue linkedIssue = iLink.sourceObject 
issueLinkManager.removeIssueLink(iLink, user);
}
} 

//Add the previous feature
try
{
issueLinkManager.createIssueLink(PrevFeature.getId() as Long,issue.getId() as Long, 10402L as Long, 1 as Long, user); 
//issue.store();
//ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false);
}
catch (Exception e) {
throw e
}

}
}
else
{
return;
}

 

TAGS
AUG Leaders

Atlassian Community Events