Inherit Components from Epic to User Story, Epic link updated

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

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

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

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 Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

359 views 1 13
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot