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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

1 vote
Answer accepted

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.

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? 

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

Thanks Stanislav, 

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

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you