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

Add issue to epic in Postfuntion step script

Alex Cumberland
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.
February 19, 2020

System specs:

  • JIRA Server 8.42
  • Adaptavisit Scriptrunner for Jira - 5.6.8.1-jira8

 

What I am trying to get to work is linking a newly cloned issue to an Epic Issue that is the parent of the sub-task that we are cloning information from to create the new Issue.

I have found several examples on the web but have not seemed to be able to get any to work.   I double checked that the issue type that I am creating has the "Epic Link" field on its create screen and it does.   I have managed to get the epic link key but can not set it on the new issue.

 

So we have a transition that when fired does the following:

  • Creates a new issue via clone of type "Task"  via a postscript step 1 (works fine)
  • Based on Field on original issue (that is copied to new issue) will create appropriate sub-tasks on the new issue via a second postscript step 7 (works fine for sub-tasks but not doing epic link to main issue created in step 1)

 

image.png

I finally deleted and rewrote the script in step 7 without actually setting the value on the new issue and it shows no errors.   But any time I had option of setting the epic link it would just fail.  So I am getting my subtasks but the new issue is not being linked under the epic it should.

 

Tried the following:

  • newissue.setCustomFieldValue(epicLinkField ,epicLinkKey.toString())  -- did nothing 
  • Same as above with changing it to use the issueboject of the epic
  • epicLink.updateValue(null, newissue, new ModifiedValue(newissue.getCustomFieldValue(epicLinkField ), epicIssue),new DefaultIssueChangeHolder())  -- this also did nothing based on examples that I found and copied.
  • Also tried moving the location of the script in step 7 to different order of the postfuction steps but that tends to just break everything even the new sub-task creations.

 

Here is relevant part of my script:

 

def newissue = (MutableIssue) issue

for (IssueLink link : relatedOutLinks)
{log.debug "outlink: " + link
log.debug "issue: " + link.getSourceObject()
log.debug "status: " + link.getSourceObject().getStatus()
log.debug "status name: " + link.getSourceObject().getStatus().name
}

for (IssueLink link : relatedLinks)
{log.debug "inLink: " + link
log.debug "issue: " + link.getSourceObject()
log.debug "status: " + link.getSourceObject().getStatus()
log.debug "status name: " + link.getSourceObject().getStatus().name

if (link.getSourceObject().getStatus().name == "Open") {
newissue = link.getSourceObject()
log.debug "New Issue: " + newissue}
}

// Here we are going to attempt to get epic link if it exists for the linked issues Parent
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
IssueManager epicIssueManager = ComponentAccessor.getIssueManager();
CustomField epicLinkField = customFieldManager.getCustomFieldObject('customfield_10006'); // cf[10006] = Epic Link

def epicLinkKey = issue.getParentObject().getCustomFieldValue(epicLinkField) // gets value of Parent of original issue that has epic link

// If we find the epic key then we need to update newissue with this epic link
if (epicLinkKey != null) {
log.debug "newissue = " + newissue.key + " epicLinkKey = " + epicLinkKey
}

 

Here is log output showing that I am getting the new Issue number and the original master epic issue

 

console] inLink: com.atlassian.jira.issue.link.IssueLinkImpl@68aa28b5[id=85659,sourceId=98862,destinationId=98864,issueLinkType=10100]
[console] issue: ABC-47
[console] status: IssueConstantImpl[[GenericEntity:Status][sequence,1][statuscategory,2][name,Open][iconurl,/images/icons/statuses/open.png][description,The issue is open and ready for the assignee to start work on it.][id,1]]
[console] status name: Open
[console] New Issue: ABC-47

[console] inLink: com.atlassian.jira.issue.link.IssueLinkImpl@6143c41d[id=85707,sourceId=98911,destinationId=98864,issueLinkType=10001]
[console] issue: ABC-96
[console] status: IssueConstantImpl[[GenericEntity:Status][sequence,1][statuscategory,2][name,Open][iconurl,/images/icons/statuses/open.png][description,The issue is open and ready for the assignee to start work on it.][id,1]]
[console] status name: Open
[console] New Issue: ABC-96

[console] newissue = ABC-96 epicLinkKey = ABC-40

[console] Custom Field Value for Related I/O contains [JAVA05B]

[console] Creating Java Coding Subtask


ABC-47 - Task that has Epic Link to ABC-40
ABC-96 - New Issue Task cloned from sub-task linked to ABC-47

 

So in the step where there is an epic I just need to update ABC-96 with the Epic Link to ABC-40 and then just keep moving into the script to create the needed sub-tasks.

 

 

 

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 19, 2020

I think you might be complicating things unnecessarily.

In the clone post-function, the additional issue actions will have 2 variables available in the context: sourceIssue (the issue that triggered the workflow) and issue (the cloned issue created by the post function).

In your example, sourceIssue will be ABC-47 and issue will be ABC-96.

To set the epic link ABC-96 to ABC-40, it should be as simple as setting the epic link customfield from the source issue:

2020-02-19 15_34_45-Add Workflow Function Parameters.png

Alex Cumberland
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.
February 20, 2020

Thanks for pointing this out to me.   I am still learning the scripting with JIRA and learn something new every day.   I was able to do what I needed based off your answer.

 

Thanks so much 

Tushar July 5, 2021

@Peter-Dave Sheehan ,

I tried with your above solution to create a clone issue in post function and should be in Issue in Epic link. But only clone issue is working and not the linking to parent (issue in Epic).

 

postfunction.JPG

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 5, 2021

You can try adding some debug messages to the "additional issue actions" and try to figure out why this wasn't working for you.

Try with the code below and then look at the resulting logs:

Note: This was initially posted before scriptunner started deprecating the built-in customFieldManager. The code before will should the current preferred method.

import com.atlassian.jira.component.ComponentAccessor

def epicLinkCf = ComponentAccessor.getCustomFieldObjects(issue).findByName('Epic Link')
log.info "Got Epic Link Custom field from target issue: $epicLinkCf "

def epicToLink = sourceissue.getCustomFieldValue(epicLinkCf)
log.info "The source issue being cloned has epicLink value of: $epicToLink"

issue.setCustomFieldValue(cf, epicToLink)
Tushar July 7, 2021

Worked with below script:

 

import com.atlassian.jira.component.ComponentAccessor

def epicLink = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find { it.name == 'Epic Link' }
issue.setCustomFieldValue(epicLink, sourceIssue)

TAGS
AUG Leaders

Atlassian Community Events