Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Define component for a groovy script to assign the component

Matthew Gaffney
Contributor
August 5, 2019

I'm attempting to write a workflow function script to automatically set a component on an issue at issue creation, anytime there isn't already a component set. Example: an end user fills out a generic IT request. Then based on our criteria we will set a component to the issue. 

The problem I run into is the documentation and forums I've read all point to listing one specific component to set. We want to set several. (secondary problem, my placeholder comment comments on all issues, not just where the component is blank)

 

Here is what I have so far:

{code:groovy}
import com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.CustomFieldManagerimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.comments.CommentManagerimport com.atlassian.jira.issue.fields.CustomFieldimport com.atlassian.jira.util.ImportUtilsimport com.atlassian.jira.user.util.DefaultUserManagerimport com.atlassian.jira.project.Projectimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.scriptrunner.runner.util.UserMessageUtilimport com.atlassian.jira.event.type.EventDispatchOption
def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();MutableIssue myIssue = issueProject project = myIssue.getProjectObject()
def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'SH')
if (theComponent == null) {commentManager.create(    issue,        user,    "Generic placeholder comment", true)}
{code}

 

Thanks!

2 answers

1 accepted

0 votes
Answer accepted
Matthew Gaffney
Contributor
August 7, 2019

The specific part I was wanting to do, find an issue where the component is blank and then do something (set component), I found the answer here:

https://community.atlassian.com/t5/Jira-questions/Test-if-Component-s-is-empty-with-Groovy-script/qaq-p/453138

 

I modified mine to be == 0 and it acted on an issue where the component was blank. 

0 votes
Raynard Rhodes
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 5, 2019

I haven't tested this, but I changed it so that there were no errors. Let me know if that works.

 

 

 

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.event.type.EventDispatchOption

def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def myIssue = event.issue as MutableIssue
Project project = myIssue.getProjectObject()
def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'SH')
if (theComponent == null) {commentManager.create(myIssue, user, "Test Comment", true)}

 

Matthew Gaffney
Contributor
August 6, 2019

In all my reading I was seeing that in this line you need to include the component that you want to set on the issue with the script. 

def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'SH')

 

The issue in our situation is we have several, 70 in our case, that we want to set automatically based on specific criteria. 

My question was is there a better way to do this then what we've attempted so far? I did attempt to put all 70 in place of where it says 'SH' above. That didn't work. 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events