Inherit Components from Epic to User Story, Epic link updated

Gsep Support December 7, 2017

Hi,

Could somebody correct me with below script:

Create a user story, "Component" field should be empty. 
Update Epic Link for user story, Component field should be copied from Epic to User Story. I am user Script listener with "issue updated" Event.

I have below script, could any one correct this script:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.CustomFieldType
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.layout.field.FieldLayoutItemImpl
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.util.ImportUtils

MutableIssue issue = (MutableIssue) event.issue;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
def issueManager = ComponentAccessor.getIssueManager()

// Check if Issue Type is Story
if (issue.getIssueType().getName() == "Story" ) {

// Get Custom Field as string (Epic Link)
def epicLinkCf = customFieldManager.getCustomFieldObjectByName("Epic Link")

// Get Custom Field Value as String
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');
String EpicName = issue.getCustomFieldValue(epicLink);

if(EpicName){
// Get Epic from Issue
def epicIssue = issue.getCustomFieldValue(epicLinkCf) as Issue

// Get Component from Epic
def currentValue = epicIssue.getComponents()

// Set component to Story
issue.setComponent(currentValue)
}
}

1 answer

0 votes
Marc Minten _EVS_
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.
December 7, 2017

Hi,

I did some similar things, but I never used the custom field Epic Link as it is not clear what kind of data type is in it. I guess that your "...getCustomFieldValue(...) as Issue" will for sure not work!

I used the link info instead to get the Epic issue:

import com.atlassian.jira.issue.link.IssueLinkManager
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getInwardLinks(event.issue.getId()).each {issueLink ->
    if (issueLink.getIssueLinkType().getName() == "Epic-Story Link") {
        Issue epicIssue = issueLink.getSourceObject()
    }
}

In addition, I think you should also save the changes in your issue:

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption
IssueManager issueManager = ComponentAccessor.getIssueManager()
ApplicationUser userId = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue (userId, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

(not dispatching update events further on as you might get in an endless loop)

 

Hope this helps

Gsep Support December 7, 2017

Hi Marc,

Thank you for your help.

I am still having issue in getting this work. If you don't mind, could you please paste the paste the complete script.

Sorry for troubling you more and thank you in advance.

Thanks,

Suresh

Marc Minten _EVS_
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.
December 8, 2017

Hi Suresh,

 

The scrript is quite long and refers to a configuration (fields, workflows, ...) that is specific to our environment, so I think it is not very useful and will be difficult to understand.

Can you rather clarify what issues you still have ?

Marc

Suggest an answer

Log in or Sign up to answer