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,299,292
Community Members
 
Community Events
165
Community Groups

Scriptrunner Listener: Edit Version Description upon creation of Version

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

0 votes

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)

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
}

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

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...

269 views 2 5
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