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

Run a small groovy script on any issue change

e May 18, 2014

I've got a groovy script that sets customfield_100 based on values of customfield_200. A script of 10-15 strings.

What I want to do now it to make it work every time an issue gets changed. Be it comments, or some other changes. More importanly - I want my script work when customfield_200 gets changed so that customfield_100 gets changed too, by my script.

Is there any mechanism in JIRA? Thanks beforehand.

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Paresh Gandhi
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.
May 18, 2014
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import org.apache.log4j.Logger
import static org.apache.log4j.Level.DEBUG
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.index.IssueIndexManager
import java.sql.Timestamp
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption

class UpdateField extends AbstractIssueEventListener {
	Logger log = Logger.getLogger(UpdateField.class)
	@Override
	void workflowEvent(IssueEvent event) {
		log.setLevel(org.apache.log4j.Level.DEBUG)
		def field = event.getChangeLog().getRelated('ChildChangeItem').any{ it.field.toString().equalsIgnoreCase("CustomField_200")}				
		if (field){						
			MutableIssue issue = event.issue
			ComponentManager componentManager = ComponentManager.getInstance()
			def customFieldManager = componentManager.getCustomFieldManager()
			IssueManager issueManager = componentManager.getIssueManager()
			def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == "CustomField_100"}
			issue.setCustomFieldValue(cf, "new value")
			User currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()	
        issueManager.updateIssue(currentUserObj, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
        issue.store()
		}
	}
}

Above code will help you to update that field if customfield_200 changes.

If you want to run it on any updated remove if clause from code.

  1. Save above script with name UpdateField.groovy and copy it in:
    1. Windows:

      C:\Program Files\Atlassian\JIRA\atlassian-jira\WEB-INF\classes\com\<yourfoldername>\UpdateField.groovy
    2. Linux:

      /opt/atlassian/jira/atlassian-jira/WEB-INF/classes/com/<yourfoldername>\UpdateField.groovy
  2. Then go to JIRA > Setting (gear icon on right-hand top corner)
  3. Click on Add-ons
  4. Go to Script Listeners on left-hand side menu
  5. Click on Custom listener link
  6. Select Event - in your case it would be "Issue updated" and "Generic event"
  7. Select Project
  8. Provide groovy class path as:

    com/<yourfoldername>\UpdateField.groovy
  9. Click on Add Listener.

You are good to go.

e May 19, 2014

Caused by: groovy.lang.MissingPropertyException: No such property: event for class: UpdateField

I get this error :( I have modified the source a little bit.

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import org.apache.log4j.Logger
import static org.apache.log4j.Level.DEBUG
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import org.apache.log4j.Logger
import static org.apache.log4j.Level.DEBUG
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.TransitionValidationResult
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.index.IssueIndexManager
import java.sql.Timestamp
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.component.ComponentAccessor
import com.opensymphony.workflow.WorkflowContext
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
 

class UpdateField extends AbstractIssueEventListener 
{    
    Logger log = Logger.getLogger(UpdateField.class)
    MutableIssue myIssue = event.issue;

    @Override
    void workflowEvent(IssueEvent event) {
        log.warn("Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by UpdateField")
    }
}

BenjiI
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.
May 19, 2014

Hi e t,

Have you added a package to the class file? Can you give us a screenshot of your script runner configuration?

e May 19, 2014

It worked!

Thanks

Justin Alex Paramanandan
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.
December 12, 2016

Great script, Paresh! I've updated your answer with better formatting for future readers. smile

0 votes
e May 19, 2014

Thank you very much, guys!

I appreciate.

I am going to try right now.

0 votes
BenjiI
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.
May 18, 2014

Hi again e t,

In JIRA you can add listeners that get triggered every time an issue is updated. If you use the script runner plugin, you can register a listener as follows:

1) Goto Admin > Add-ons > Script Listeners

2) Select the Custom listener

3) Select all events that need notification (it's multiselect)

4) Select your project (again multiselect)

5) Upload your event listener class to the server and select it as Name of groovy class

BenjiI
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.
May 18, 2014

On this page Jamie describes how you can implement a listener class in Groovy:

https://jamieechlin.atlassian.net/wiki/display/GRV/Listeners

BenjiI
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.
May 18, 2014

Inside the workflowEvent method you can do custom field operations, f.e. like we discussed in this thread earlier:

https://answers.atlassian.com/questions/296363/set-custom-field-select-list-value

e May 19, 2014

Thanks a lot!

TAGS
AUG Leaders

Atlassian Community Events