Scriptrunner Listener: Edit Version Description upon creation of Version

Virginia Byrne April 8, 2022

Objective: Upon VersionCreateEvent, automatically edit created Version to have the template text "Approver: ____ Tester: ____" in the Version Description. 

 


 

This should be so simple, and I'm clearly missing something. When a version is created for the "TST" project, I want to update the Version Description text to "Approver: ____ Tester: ____". I am looking to use a Listener, although if there's a better way I'm open to it. 

I started with the Create a New Fix Version of the Selected Project from Adaptivist Library, as well as the editVersionDetails method from the VersionManager of the API reference. Looking to cobble it together, I have the very basic (and very non-compiling) Listener of:

 

import com.atlassian.jira.component.ComponentAccessor


// the key of the project under which the version will be edited

final String projectKey = "TST"


// the name of the version just created, also incorrect

final String versionName = $createdVersion


// a description for the new version

final String reldesc = "Approver: _____ Tester: _____ "


ComponentAccessor.versionManager.editVersionDetails($createdVersion, ????, reldesc)
1) How do I get the id of the Version that was created with the event I'm listening for/to? And then insert that as the Version to be updated with editVersionDetails?
2) None of the content I've put in the parenthesis for editVersionDetails has worked, I always get an error. Even when hardcoding an existing Version ID and an existing Version Name like so:
ComponentAccessor.versionManager.editVersionDetails(80641, VB1, "Approver: ____ Tester: _____")
I get this error result
groovy.lang.MissingPropertyException: No such property: VB1 for class: Script1402 at Script1402.run(Script1402.groovy:3)

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 8, 2022

The event variable that will be available within the context of your event listener when listening for Version Create Event will be of type VersionCreateEvent.

And one of the available method for that class are "getVersion" and getVersionId.

So in your groovy script, you can call event.version.id or event.versionId

You might also want to consult event.version.description and possibly use the user-submitted description in your template.

Something like this:

import com.atlassian.jira.component.ComponentAccessor

def versionManager = ComponentAccessor.versionManager
def version = event.version
def descriptionTemplate = 'Approver: _____ Tester: _____ '
def newDescription =descriptionTemplate

if
(version.description){
newDescription = descriptionTemplate + '\n' + version.description
}
versionManager.editVersionDetails(version, version.name, newDescription)
Virginia Byrne April 12, 2022

Thank you @Peter-Dave Sheehan, much appreciate the quick response. This does the trick.

Want to check my slowly evolving groovy knowledge here...the if statement at the end is to take into account if the user creating the version puts text in the Description field (or doesn't), yes? If I wanted my template text in the Description to overwrite anything the user enters, I could remove the following and drop the newDescription variable entirely?

if(version.description){
newDescription = descriptionTemplate + '\n' + version.description
}
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 12, 2022

Correct, if you don't care what the user enters and you just want to overwrite it, you could just use:

import com.atlassian.jira.component.ComponentAccessor

def versionManager = ComponentAccessor.versionManager
def version = event.version
def descriptionTemplate = 'Approver: _____ Tester: _____ '

versionManager.editVersionDetails(version, version.name, descriptionTemplate)
Like Virginia Byrne likes this
TAGS
AUG Leaders

Atlassian Community Events