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

Stanislav Caja August 14, 2017

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

1 vote
Answer accepted
Aleksandr Zuevich
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.
August 14, 2017

Hi Stanislav,

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

Stanislav Caja August 14, 2017

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
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.
August 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.

Stanislav Caja August 14, 2017

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.

Stanislav Caja August 14, 2017

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

Aleksandr Zuevich
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.
August 15, 2017

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);

 

Stanislav Caja August 15, 2017

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
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.
August 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]));
Stanislav Caja August 15, 2017

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)
Stanislav Caja August 15, 2017

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]));


}

Stanislav Caja August 15, 2017

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
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.
August 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