Adding a component as a post function

Roy Chapman March 27, 2019

Researching various solutions, and drawing a blank.

I want to add a component to an issue as it is created (for why, see below, maybe there are two questions in one) and added a post function that executes the following

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
MutableIssue issue = issue
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def componentA = projectComponentManager.findByComponentNameCaseInSensitive('Jira Access Request')
issue.setComponent(componentA)

No errors are generated, yet the components field remains empty.

Any idea why this update is not being saved?

* As to the why

I want to assign an issue to a fixed user.  I could add this user in each of the steps but then users have a habit of moving so don't want to edit the workflow each time.  Opted for option to assign to Developer Lead (i.e. component lead or project lead if component not set).  If user leaves they simply update the component lead.  Any other suggestions?

 

3 answers

1 accepted

0 votes
Answer accepted
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2019

I believe you also need to use the IssueManager.updateIssue() method to save the changes. I haven't tested this code, but this might work for you:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

MutableIssue issue = issue
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def issueManager = ComponentAccessor.getIssueManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def componentA = projectComponentManager.findByComponentNameCaseInSensitive('Jira Access Request')
issue.setComponent(componentA)
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
Roy Chapman March 27, 2019

Alex,

Awesome.  Worked perfectly.

Thanks again

0 votes
Valerie Christine Knapp September 24, 2020

Thanks so much for this! I have been trying to figure out how to do this for weeks.

0 votes
Mark Markov
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.
March 27, 2019

Hello @Roy Chapman 

Try to save your changer with issueManager.

As for your second question, i dont clearly understand last part, but you can set as assignee component lead or project lead like in my example

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue
MutableIssue issue = issue
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def issueManager = ComponentAccessor.getIssueManager()
def componentA = projectComponentManager.findByComponentNameCaseInSensitive('Jira Access Request')
issue.setComponent(componentA)
def componentLead = componentA.first().getComponentLead()
def projectLead = issue.getProjectObject().getProjectLead()
if (componentLead){
issue.setAssignee(componentLead)
} else {
issue.setAssignee(projectLead)
}
issueManager.updateIssue(issue.getReporter(), issue, EventDispatchOption.DO_NOT_DISPATCH, false)

 

Hope it helps!

Roy Chapman March 27, 2019

Mark,

Thanks.  I tried Alex's version first and this worked.  Bu thanks for your suggestion as well.  Greatly appreciated.

Regarding the question on assigning I am already using Developer lead (which feeds from the component lead).  I was wondering if there was a better way (components could be removed, for example).  We can assign to project lead, reporter and a random member of a role.  Would be nice to be more specific without having to edit the workflow each time.

Thanks again. It really makes my life easier to get such a prompt response.

Roy 

Suggest an answer

Log in or Sign up to answer