Remove clone issues links for a issue

srinivas kattla January 10, 2020

Hi ,I'm writing the script to remove the clone links for an issue in the same project.It seems script complied with no errors but the clones issue links are not removing.can you please suggests.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.link.IssueLink

import com.atlassian.jira.user.ApplicationUser

 

ApplicationUser currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

for(IssueLink link: ComponentAccessor.getIssueLinkManager().getIssueLinks(issue.getId())){

    if(link.getIssueLinkType().getName().equals("Clones"))

    ComponentAccessor.issueLinkManager.removeIssueLinks(issue, currentUser)

}

1 answer

1 accepted

0 votes
Answer accepted
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2020

Hi @srinivas kattla ,

first of all - please check the name of the link in administration - "Clones" is probably wrong and you have to replace it with "Cloners"

links.png

Method removeIssueLinks will remove all the links, not only the one for clone, so probably you should use removeIssueLink instead.

I would recommend you to add some debug code to be able to find out, if you are getting any issue links at all.

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 10, 2020

Also method getIssueLinks expects to get link type id (not issue id), so I would recommend you to replace it.

Where did you put your code? Is this a post function in your workflow?

Code below works for me, but I'm not sure, if it will be good enough for your use case, maybe some more modifications will be needed.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getInwardLinks(issue.getId()).each {issueLink ->
if (issueLink.getIssueLinkType().getName().equals("Cloners")) {
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}

 

srinivas kattla January 10, 2020

Hi @Hana Kučerová  , Thanks for your input. I will test it and updates as per the need. I'm adding this in post function. 

srinivas kattla January 14, 2020

1111.PNGHi @Hana Kučerová  : I was able to work it out for issuelink type cloners but for other issuelinktype the script is not working .Can you please suggest

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getInwardLinks(issue.getId()).each {issueLink ->
if (issueLink.getIssueLinkType().getName().equals("System Test Execution") && issue.issueType.name == 'Story'){
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}

issueLinkManager.getOutwardLinks(issue.getId()).each {issueLink ->
if (issueLink.getIssueLinkType().getName().equals("System Test Execution") && issue.issueType.name == 'Story'){
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}
issueLinkManager.getInwardLinks(issue.getId()).each {issueLink ->
if (issueLink.getIssueLinkType().getName().contains("Test") && issue.issueType.name == 'Story'){
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}

issueLinkManager.getOutwardLinks(issue.getId()).each {issueLink ->
if (issueLink.getIssueLinkType().getName().contains("Test") && issue.issueType.name == 'Story'){
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 15, 2020

Hi @srinivas kattla ,

it seems to me, that the script should work. Did you try it without the condition

issue.issueType.name == 'Story'

, i. e. for all types of issues?

Like srinivas kattla likes this
srinivas kattla January 15, 2020

I tried this but it is removing all issue.links for issuelink type. This should not happen .It should only remove the destination issuelinke type story which is linked. please suggest

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 17, 2020

Hi @srinivas kattla ,

so, you are trying to remove all the issue links, which meet two requirements:

  • the issue link has specific name
  • the linked issue has the type Story

Am I right?

If so, please try this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

final ISSUE_LINK_NAMES = [
"System Test Execution",
"Test",
]

final ISSUE_TYPE_NAME = "Story"

ApplicationUser loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()

issueLinkManager.getInwardLinks(issue.getId()).each {issueLink ->
String linkedIssueType = issueLink.getSourceObject().getIssueType().getName()
String issueLinkName = issueLink.getIssueLinkType().getName()
if (ISSUE_LINK_NAMES.contains(issueLinkName) && linkedIssueType == ISSUE_TYPE_NAME) {
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}

issueLinkManager.getOutwardLinks(issue.getId()).each {issueLink ->
String linkedIssueType = issueLink.getDestinationObject().getIssueType().getName()
String issueLinkName = issueLink.getIssueLinkType().getName()
if (ISSUE_LINK_NAMES.contains(issueLinkName) && linkedIssueType == ISSUE_TYPE_NAME) {
issueLinkManager.removeIssueLink(issueLink, loggedInUser)
}
}
Tina Xu January 16, 2022

Hi @Hana Kučerová ,

i added this script in Create Workflow with "Custom script post-function". and put it in the last step.

when i create an issue from Clone, "Clone From" link cannot be removed in the new created issue.

Could you please give me some suggestions?

Tina Xu January 18, 2022

@Hana Kučerová ,

i updated the scripts and move it to Listener, now it works.

more reference here,

1. in post function, 

issueLinkManager.getOutwardLinks(issue.getId())

always returns [].

2. but after the issue is created, i execute the same command in Scriptrunner ->console, it works.

3. so i guess maybe there are something wrong in timing sequence. but even i add 30 seconds to wait before the command is execute, it still cannot work.

Suggest an answer

Log in or Sign up to answer