Getting components from epic links to Issue which is created under epic link

Hello everyone,

I am really stuck here with problem of setting a value to compoments. Description of what I am doing and what I would like to get.

1. We have some EPIC links which have set components, labels etc.

2. When we create Issue which is linked to some epic we would like to have a newly created Issue with same information in components. Instead of chosing it manually.

 

so what we did, was we created a post function which looks like this:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import groovy.transform.Field;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
def log = Logger.getLogger("com.acme.CreateSubtask");
log.setLevel(Level.DEBUG);

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');

Issue epic = (Issue)issue.getCustomFieldValue(epicLink);

if(epic)
{


issue.setComponent(epic.getComponents());


}

 

after debugging everything, it works. "log.debug issue.getComponents();" will show that all components was set by "issue.setComponent(epic.getComponents());" but on the site of issue. Value in field "component/s" is "none"

Can you please help me with this? I tried "issue.stored" or sth to update it, but nothing worked.

1 answer

1 accepted

This widget could not be displayed.
Aleksandr Zuevich Community Champion Aug 14, 2017

Hi Stanislav,

I suppose you should create new IssueInputParameters using IssueService, then invoke setComponentIds(yourComponentIds), then issueService.validateUpdate() and then issueService.update().

Isn't there a different way? I like the way that you add some post function script that should evaluate within the creating issue.

Aleksandr Zuevich Community Champion Aug 14, 2017

No, it's the same way, just implement the logic described above instead of issue.setComponent(epic.getComponents()). Because in your case you just change the issue object without real updating the issue.

Could you please write it down for me? I am having trouble with it, can't really see how to declare it, eventhough I am looking to Jira docs about it.

I would really appreciate it. Thank you.

Can you at least advise me how do I get componentsID? I mean I am getting values from epic into Issue, but that issue cant be parsed into IssueInputParameters

You already have Collection<ProjectComponent> from epic.getComponents() so you can just collect ids:

Long[] componentIds = epic.getComponents().stream().map(ProjectComponent::getId).toArray(Long[]::new);
issueInputParameters.setComponentIds(componentIds);

 

Oh, that's where I get into trouble, collecting IDs. Your code doesn't work, it seems that stream(), does not have method of map.

Could you please explain what is that "map", "stream" or just fix the code? 

Aleksandr Zuevich Community Champion Aug 15, 2017

"map", "stream" are java 8 Stream API.

Ok, try old style:

Collection<ProjectComponent> components = epic.getComponents();
List<Long> componentIds = new ArrayList<>(components.size());
for (ProjectComponent component : components) {
componentIds.add(component.getId());
}
issueInputParameters.setComponentIds(componentIds.toArray(new Long[0]));

No compilation error, but when I executed:

2017-08-15 09:49:57,487 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: IS-4111, actionId: 1, file: <inline script>
groovy.lang.MissingPropertyException: No such property: issueInputParameters for class: Script511
 at Script511.run(Script511.groovy:39)

Full script:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueInputParameters;
import com.atlassian.jira.bc.issue.IssueService;
import com.atlassian.jira.bc.project.component.ProjectComponent;

import groovy.transform.Field;
import org.apache.log4j.Logger;
import org.apache.log4j.Level;
def log = Logger.getLogger("com.acme.CreateSubtask");
log.setLevel(Level.DEBUG);


CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField epicLink = customFieldManager.getCustomFieldObjectByName('Epic Link');


IssueService issueService = ComponentAccessor.getIssueService();
IssueInputParameters issueInputParameters = issueService.newIssueInputParameters();

 

Issue epic= (Issue)issue.getCustomFieldValue(epicLink);

if(epic)
{

//Your part
Collection<ProjectComponent> components = epic.getComponents();
List<Long> componentIds = new ArrayList<>(components.size());
for (ProjectComponent component : components) {
componentIds.add(component.getId());
}
issueInputParameters.setComponentIds(componentIds.toArray(new Long[0]));


}

Dear Aleksandr,

forgive me my hastiness, I didn't thought this through and forgot on validate / update. Everything now works as we want. I am really grateful for your support.

You are awesome! 

Cheers.

Aleksandr Zuevich Community Champion Aug 15, 2017

Thanks Stanislav, 

nice to hear that it works well. Glad to help you!

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted 5 hours ago in Europe

Topic Tuesday: How did you learn about the AUG programme?

Back in 2010 I hadn't been using the Atlassian tools (JIRA, Confluence, Bamboo) that long and I started searching online to see if I could chat to other users primarily about how they used the produc...

13 views 0 0
View post

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