Post function groovy - search summary and according to keywords, populate components field

Anand Unadkat
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.
October 7, 2013

Hi,
I am trying to update thecomponents field using a post function in Create issue, where is looks for the summary keywords and acoording to what is in the keywords, it will update the componenets field. This is what I have done so far but it is not working:

import com.atlassian.jira.ComponentManager 
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.crowd.embedded.api.User 
import com.atlassian.jira.project.Project 

if(issue.getSummary().equals("test this issue"))
{

ComponentManager componentManager = ComponentManager.getInstance()
IssueManager issueManager = componentManager.getIssueManager()
MutableIssue issue = componentManager.getIssueManager().getIssueObject(issue.id)
Project project = issue.getProjectObject()
ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
issue.setComponents([component.getGenericValue()]) 

User user = componentManager.jiraAuthenticationContext.getLoggedInUser()

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

}
else
{
return false
}

Also tried

{code}

import com.atlassian.crowd.embedded.api.User

import com.atlassian.jira.bc.issue.search.SearchService

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.bc.project.component.MutableProjectComponent

import com.atlassian.jira.issue.Issue

import com.atlassian.jira.issue.CustomFieldManager

import com.atlassian.jira.issue.MutableIssue

import com.atlassian.jira.issue.IssueManager

import com.atlassian.jira.user.util.UserUtil

import com.atlassian.jira.web.bean.PagerFilter

import com.atlassian.jira.ComponentManager

import com.atlassian.jira.issue.customfields.view.CustomFieldParams

import com.atlassian.jira.issue.fields.CustomField

jqlSearch = "summary ~ test"

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)

//UserUtil userUtil = ComponentAccessor.getUserUtil()

//User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

IssueManager issueManager = ComponentAccessor.getIssueManager()

SearchService.ParseResult parseResult = searchService.parseQuery(jqlSearch)

if (parseResult.isValid()) {

ComponentManager componentManager = ComponentManager.getInstance()

IssueManager issueManager = componentManager.getIssueManager()

MutableIssue issue = componentManager.getIssueManager().getIssueObject()

Project project = issue.getProjectObject()

ProjectComponent component = componentManager.getProjectComponentManager().findByComponentName(project.getId(), "BITON")

issue.setComponents([component.getGenericValue()])


User user = componentManager.jiraAuthenticationContext.getLoggedInUser()


issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

} else {

log.error("Invalid JQL: " + jqlSearch);

}

{code}

error I get is

{code}

2013-10-08 19:28:03,968 http-bio-8080-exec-2 ERROR valiantys 1168x2806x1 1wl1gbg 80.87.25.98 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getProjectObject() on null object

2013-10-08 19:28:03,999 http-bio-8080-exec-2 ERROR valiantys 1168x2806x1 1wl1gbg 80.87.25.98 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function

javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getProjectObject() on null object

{code}

I changed the code quite a few times now.

You help will be really appreciated.

Regards
Anand

7 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
Anand Unadkat
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.
October 13, 2013

Hi Henning,

Thats great, I got it working. So the final code for both is as follows (may be helpful for other viewers):

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager 
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.crowd.embedded.api.User 
import com.atlassian.jira.project.Project 


CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
CustomField cfEMCRequiredId = customFieldManager.getCustomFieldObjectByName("cf name");
String[] words = issue.getSummary().split("keywords")


if(issue.getCustomFieldObjectByName("cf name").contains("cf value"){
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name")
    issue.setComponentObjects([component])
} else {
return false
}

for(int i = 0 ; i < words.length ; i++) {
if(words[i].equals("keyword")) {
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name")
    issue.setComponentObjects([component])
}else {
return false
}


1 vote
Henning Tietgens
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.
October 8, 2013

While creating the issue you cannot get the issue object from the issue manager because it doesn't exist at this moment. And if, you will get trouble with the index.

Just set the component on the already existing issue object (using issue.setComponentObjects(), maybe you should add the component to the existing components on the issue?) and don't do anything further (like updating the issue object or something similar). Make sure your post function is placed befor the "Creates the issue originally" post function.

Anand Unadkat
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.
October 8, 2013

Hi Henning,

Thanks for you answer. I don't want to add the componenet to an existing issue, I want it to auto populate. Are you saying leave the below blank:

{code}

MutableIssue issue = componentManager.getIssueManager().getIssueObject(issue.id);

{code}

The error that I am gteting is the following:

{code}

/secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function

javax.script.ScriptException: javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method getProjectObject() on null object

{code}
So I am bit confused when you said about adding it to arleady created issue.
Thanks again
Anand
Henning Tietgens
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.
October 8, 2013

Yes, I understand that, but maybe the user already choose a component? If you are sure this could not be possible (because Component/s is not on the create screen), you can simply set the component.

Try this

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project

if(issue.getSummary().equals("test this issue"))
{
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
    issue.setComponentObjects([component])
}

as your post function.

0 votes
Anand Unadkat
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.
October 13, 2013

Hi Henning,

Thats great, I got it working. So the final code for both is as follows (may be helpful for other viewers):

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager 
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.crowd.embedded.api.User 
import com.atlassian.jira.project.Project 


CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
CustomField cfEMCRequiredId = customFieldManager.getCustomFieldObjectByName("cf name");
String[] words = issue.getSummary().split("keywords")


if(issue.getCustomFieldObjectByName("cf name").contains("cf value"){
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name")
    issue.setComponentObjects([component])
} else {
return false
}

for(int i = 0 ; i < words.length ; i++) {
if(words[i].equals("keyword")) {
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name")
    issue.setComponentObjects([component])
}else {
return false
}


Henning Tietgens
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.
October 20, 2013

Hi Anand, I'm glad it's working for you. Maybe you could also mark my answer as correct?

Thanks, Henning

0 votes
A October 13, 2013

Hi Henning,

Thats great, I got it working. So the final code for both is as follows (may be helpful for other viewers):

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager 
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.crowd.embedded.api.User 
import com.atlassian.jira.project.Project 


CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();
CustomField cfEMCRequiredId = customFieldManager.getCustomFieldObjectByName("cf name");
String[] words = issue.getSummary().split("keywords")


if(issue.getCustomFieldObjectByName("cf name").contains("cf value"){
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name")
    issue.setComponentObjects([component])
} else {
return false
}

for(int i = 0 ; i < words.length ; i++) {
if(words[i].equals("keyword")) {
    Project project = issue.getProjectObject()
    ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "component name")
    issue.setComponentObjects([component])
}else {
return false
}


0 votes
Anand Unadkat
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.
October 10, 2013

Hi Henning,

So I am using:

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.ComponentManager 
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.crowd.embedded.api.User 
import com.atlassian.jira.project.Project 

cf = customFieldManager.getCustomFieldObjectByName('cf Name')
val = issue.getCustomFieldValue(cf)

if(val.contains("name.surname")){

Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
}

That doesn't work for me. Any ideas?

Henning Tietgens
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.
October 10, 2013

To get the customfield you have to use an existing customFieldManager. So try

cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName('cf Name')

And than, your code does nothing... you only find the component, you don't set it.

A October 10, 2013

Hi Henning,

I don't understand when you say find it and not set it. I want it to be able to set the component if the customfield value contains a specific string. Hence

if(val.contains("name.surname")){
 
Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")

which is not working.

Thanks for you help!!

Henning Tietgens
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.
October 10, 2013

As you can see in my previous comments

issue.setComponentObjects([component])

is missing at the end of your if statement to set the components.

0 votes
Darren Pegg
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.
October 8, 2013

Hey Henning,

That worked perfectly!! Thanks. One other question thoug, can you look for a jql query rather than

if(issue.getSummary().equals("test this issue"))
So what we are trying to achieve is the following:
{code}
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.IssueManager
mport com.atlassian.jira.issue.customfields.view.CustomFieldParams

jqlSearch = "summary ~ test this issue"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
//UserUtil userUtil = ComponentAccessor.getUserUtil()
//User user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()

SearchService.ParseResult parseResult = searchService.parseQuery(jqlSearch)
if (parseResult.isValid()) {
Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
issue.setComponentObjects([component])
} else {
return false
}
{code}

Thanks once again!

Henning Tietgens
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.
October 8, 2013

You have to do the search after parsing the JQL.

Try this

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.jira.web.bean.PagerFilter

jqlSearch = "summary ~ test this issue"
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser()

SearchService.ParseResult parseResult = searchService.parseQuery(ApplicationUsers.toDirectoryUser(user), jqlSearch)
if (parseResult.isValid()) {
    searchResult = searchService.search(ApplicationUsers.toDirectoryUser(user), parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
    if (searchResult?.total > 0) {
        Project project = issue.getProjectObject()
        ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
        issue.setComponentObjects([component])
    }
}

This is not tested by me, so take care :-)

A October 8, 2013

Hi Henning, It works apart from, the JQL query. It should in theory look for 'testt', 'this', issue' in keywords jqlSearch = "summary ~ 'test this issue'"

but right now, even if the summary includes just the word 'other', it will still map the component when it shouln't. Is there another way out in either one of the scripts tthat you provided?

Many thanks for your help!

Henning Tietgens
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.
October 8, 2013

I though your JQL is only an example... Because it's used while creating the issue the issue will never be found, it could only be used to check for other issues. For matching against the summary of the issue itself you should use issue.summary ==~ /some regexp/ in you if statement. See here.

Maybe you could match against some more specific critieria like a checkbox or similar?

Anand Unadkat
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.
October 9, 2013

Hi Henning,

Thanks for your help so far. I got it working with the for loop and searching for each word. Another question I have is how to I update the Component using customfield value? Right now I am using this and it is not working:

CustomFieldManaer customFieldManager = componentManager.gerCustomFieldManager();

if(issue.getCustomFieldObjectByName("cf Name").contains("cf value")}{

Project project = issue.getProjectObject()
ProjectComponent component = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(), "BITON")
}
This doesn't work. Also how can I ask groovy to ignore case sensitive String?
Thanks!!
Henning Tietgens
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.
October 9, 2013

You can get a customfield value through

cfValues['cf Name']

Please be aware that if it's a selct field or similar you have to use

cfValues['cf Name'].getValue()

to get the String value.

A October 9, 2013

I want to get is through groovy script. That is through workflow validator. Could you please show me how to di it in a custom groovy script. I tried the way shown in my previous comment.

Thanks

Henning Tietgens
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.
October 9, 2013

cf = customFieldManager.getCustomFieldObjectByName('cf Name')

val = issue.getCustomFieldValue(cf)

0 votes
Bob Swift (personal)
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.
October 7, 2013

I don't know what is wrong with your code, but, Conditioned Workflow Functions for JIRA does some of this type of thing including running a JQL query and conditioning to determine whether to make a change. This may solve your problem in an easier way.

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events