User Tools

Site Tools


post_functions

This is an old revision of the document!


Post Functions

Post functions allow you to perform specific actions upon the transition. Following are some useful examples.

Add Comment on Transition

import com.atlassian.jira.component.ComponentAccessor
 
def commentManager = ComponentAccessor.getCommentManager()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
    commentManager.create(
        issue,
        currentUser,
        "This is my comment",
        false) 

Update Fix Version/s field with first Unreleased version if there isn't a version already there or give error message

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
 
def versionManager = ComponentAccessor.getVersionManager()
def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectObjByKey(issue.projectObject.key)
def versions = versionManager.getVersions(project)
def firstUnreleasedFound = versions.find{! it.released}
 
if (issue.fixVersions.size() == 0){
    if (firstUnreleasedFound) {    
		issue.setFixVersions([firstUnreleasedFound])
		issue.store()
	} else {
    	UserMessageUtil.error("MANUAL INTERVENTION REQUIRED!!! No Unreleased Fix Version Found. Please add a Fix Version manually to this ticket....")
	}    
}

Set Reporter ID to be a value from a custom field called User ID

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.component.ComponentAccessor
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField cfUID = customFieldManager.getCustomFieldObjectByName("User ID")
def cfUIDvalue = issue.getCustomFieldValue(cfUID)
 
issue.reporterId=cfUIDvalue

Add users to watch list on create or transition

If you're using this code on issue creation, make sure the script is after the “Create the issue originally” task.

import com.atlassian.jira.component.ComponentAccessor
def watcherManager = ComponentAccessor.getWatcherManager()
def userManager = ComponentAccessor.getUserManager()
def watchUsers = {usernames ->
    usernames.each {
        def user = userManager.getUserByKey(it.toString())
        if (user) {
            watcherManager.startWatching(user, issue)
        }
 
    }
}
def users = ["abc", "def", "ghi"]
if (issue.projectObject.key == 'KEY') {
    watchUsers(users)
}
post_functions.1517598564.txt.gz · Last modified: 2018/02/02 14:09 by root