Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Using Groovy script to link a newly created issue with a related ticket

Michael
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.
March 11, 2020

Hi everyone,

I have a subtask being created (subtask A) via the parent ticket that relates to another subtask which is always previously created. (Subtask B)

I'd like to use a Groovy Script through the Workflow Magic Box Jira plugin to link subtasks A and B as being "related".

Can anyone help me create a Groovy script for this?

Thanks,

Michael

2 answers

0 votes
Michael
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.
March 18, 2020

Hi again @Payne or anyone else who might be able to help,

Can you think of any way to have the above code "auto retrieve":

a) Issue ID that the post function is being created 

b) The issue ID of the most recent specifically defined subtask / subtask ID within the same parent issue?

 

The main reason for creating this type of link, is that we want both of the issues to be subtasks under the same parent, but we want to link them together so that people know they are related to each other.

0 votes
Payne
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.
March 11, 2020

I use the following code that you can hopefully massage and put to use.

jiraAdminUser is an ApplicationUser object representing the user that is performing the action.

issue and linkedIssue are Issue objects representing the 2 issues that we wish to link.

linkTypeApprove is the ID (e.g. 10200) of the link type.

import com.atlassian.jira.bc.issue.IssueService

IssueService.IssueResult iResult = issueService.create(jiraAdminUser, issue);
issueLinkManager.createIssueLink(linkedIssue.getId(),iResult.getIssue().getId(),linkTypeApprove,(Long)1,jiraAdminUser)
Michael
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.
March 12, 2020

Hello @Payne thanks for the quick reply.

This is my first time messing with groovy, so sorry if I get this wrong but am I understanding correctly that I only need to add the issue link ID within the ",(Long)1" section?

If so, how does this script know which subtask I'd like to link under the parent ticket? I was expecting I'd have to call out the issue type name / ID so that it knows how to link the current issue I'm deploying this script within to the one called out within the script.

Thanks,
~Mike

Payne
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.
March 12, 2020
Michael
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.
March 12, 2020

Hi again @Payne 

Thanks for the continued back and forth. After look at the documentation a lot more and browsing more community posts / forums, I've come up with the following script, but I still can't get it to work:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.crowd.embedded.api.User

Issue issue = issue;
def parentIssue = issue.getParentObject();
issueManager = ComponentManager.getInstance().getIssueManager()
subTaskManager = ComponentManager.getInstance().getSubTaskManager();
subTasks = subTaskManager.getSubTaskObjects(parentIssue)
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
User user = authContext.getLoggedInUser();
String issueKey = issue.key;

subTasks.each() {
String issueTypeId = it.issueTypeObject.id
if (issueTypeId == "10107") {
issueLinkManager.createIssueLink(issue.getId(), it.getId(), Long.parseLong("10303"),Long.valueOf(1), user);
issueManager.updateIssue(user, it, EventDispatchOption.ISSUE_UPDATED, false)
}
}

 

The issueTypeId 10107 is the issue type ID of the subtask I want the script to find.

The Long.parseLong("10303") is the issue link ID of the issue link I want to create between the subtask I'm adding this script to, and the subtask I want linked.

If you could point me in the right direction I'd appreciate it.

Thanks again,

~mike

Payne
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.
March 12, 2020

Hey Mike,

I'd put some logging in to figure out where breakdown is occurring. I stripped your script down to the bare minimum to simply create a link between 2 issues and confirm that it runs as expected in the ScriptRunner console:

import com.atlassian.jira.issue.link.IssueLinkManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.security.JiraAuthenticationContext

def issue1 = ComponentAccessor.issueManager.getIssueByCurrentKey("BR-1")
def issue2 = ComponentAccessor.issueManager.getIssueByCurrentKey("BR-2")

IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
def user = authContext.getLoggedInUser();
issueLinkManager.createIssueLink(issue1.getId(), issue2.getId(), Long.parseLong("10001"),Long.valueOf(1), user);
Michael
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.
March 12, 2020

Hi again @Payne ,

Thanks again for the continued help. I input the above code and massaged it to work with our instance, and it worked:

script test.png

Is it normal to get "no return value" for linking issues when testing groovy scripts?

Also, I now need to figure out how to "auto" grab the required issue types... I'm thinking the newly created subtask shouldn't be that hard, but I need to somehow search all subtasks within the parent ticket, and grab the latest ticket of a specific issue type.

 

@Paynecan you think of anyway that I can do the above?

 

Thanks again,

~Mike

Suggest an answer

Log in or Sign up to answer