Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,298,803
Community Members
 
Community Events
165
Community Groups

Script to set custom variable once a Jira release happens?

Hi,

 

I'm trying to write a quick script that sets a custom variable called "Environment" which is a single pull-down selectable choice of the following values:

None

Dev

Test

QA

QA2

Prod

 

I have a Custom Listener setup as "VersionReleaseEvent and when I made my release, it does appear to be triggering my script which errors out immediately before and of my debug log statements even run.

Once a release happens, I want my custom variable "Environment" set to "Prod".

 

The code I've written is the following and it is erroring out on the event.issue line

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Logger
import org.apache.log4j.Level

def mylog = Logger.getLogger("PushReleaseVersionToProd")

// Set log level
mylog.setLevel(Level.DEBUG)

def issue = event.issue as Issue

def EnvFieldName = 'Environment'

mylog.debug "EnvFieldName is $EnvFieldName"

def customFieldManager = ComponentAccessor.customFieldManager

def EnvField = customFieldManager.getCustomFieldObjects(issue).findByName(EnvFieldName)
assert EnvField : "Could not find custom field with name $EnvFieldName"

mylog.debug "EnvField is $EnvField at end of script"

EnvField = "Prod"

 

The error log is the following:

 

2021-07-29 14:18:32,284 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-07-29 14:18:32,286 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.project.VersionReleaseEvent, file: null
groovy.lang.MissingPropertyException: No such property: issue for class: com.atlassian.jira.event.project.VersionReleaseEvent
at PushReleaseVersionToProd.run(PushReleaseVersionToProd.groovy:15)

 

I'd appreciate any help in trying to suss out what the issue may be.

 

Thanks,
Jeff

 

1 answer

0 votes

For VersionReleaseEvent the event won't have an issue variable.

So event.issue is not an option

Which issue do you expect to be updated?

Think in terms of the UI. If you release a version in the project release page, how would you select which issue to update?

Peter,

 

Thanks for the excellent questions.  I'm chalking it up to not enough coffee this morning.  :)

 

What I'm really wanting to do is set every single issue's "Environment" to "Prod" in a given release.  So if I have 6 issues in a release, I want all of their Environment custom variable values to be "Prod".


Does that help clarify?

 

Thanks,

Jeff

For reference, what I HAVE been doing is using a JQuery to show me every issue in a specific "fix version" and then using the bulk update tool to set that custom variable called "Environment" to "Prod".

 

Jeff

 think you mean JQL (Jira query language) not JQuery (javascript module).

I think your scriptrunner custom listener can take care of the JQL and Bulk update ... something along the lines of the following should get you close:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.bc.issue.search.SearchService

def mylog = Logger.getLogger("PushReleaseVersionToProd")
// Set log level
mylog.setLevel(Level.DEBUG)

def issueManager = ComponentAccessor.issueManager
def customFieldManager = ComponentAccessor.customFieldManager
def searchService = ComponentAccessor.getComponent(SearchService)
def indexingService = ComponentAccessor.getComponent(IssueIndexingService)

def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//this closure will be caller later for each issue
def updateIssueAndReIndex = {MutableIssue issue->
issueManager.updateIssue(currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, false) //set to true to send notifications
//update the JIRA index
boolean wasIndexing = ImportUtils.isIndexIssues()
ImportUtils.setIndexIssues(true)
indexingService.reIndex(issue)
ImportUtils.setIndexIssues(wasIndexing)
}


def version = event.version
def EnvFieldName = 'Environment'
mylog.debug "EnvFieldName is $EnvFieldName"

def jql = """fixVersion = $version.id and "$EnvFieldName" != Prod"""
def parseResult = searchService.parseQuery(currentUser, jql)
searchService.search(currentUser, parseResult.query, PagerFilter.unlimitedFilter ).results.each{indexIssue ->
def issue = issueManager.getIssueObject(indexIssue.id)
def EnvField = customFieldManager.getCustomFieldObjects(issue).findByName(EnvFieldName)
mylog.debug "EnvField is $EnvField"
assert EnvField : "Could not find custom field with name $EnvFieldName for $issue.key"
mylog.debug "EnvField current Value for $issue is ${issue.getCustomFieldValue(EnvField)} setting it to 'Prod'"
issue.setCustomFieldValue(EnvField, 'Prod') // <-- this will only work if EnvField is a text field. If it's a select, you'll need to find the OptionId that correspond to the label "Prod"

//now you must save and re-index the issue
updateIssueAndReIndex(issue )
}

mylog.debug "At end of script"
Like Jeff Hayes likes this

Thank you!  And yes, my custom field is an actual 6 (single) choice select, so I'll need to figure out how to change your syntax to use that specific choice field.  I will let try to work on this and get back to you if I run into further issues.

Jeff

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

233 views 1 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you