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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Updating Fix Version/s field through Script-runner listener Edited

Hi,

I have created a custom listener to check the Fix Version/s field for any new issues that were created and if that field is blank, it will check whether a new unreleased version for that project needs to be created before updating the issue with the version. 

The script is creating the new version fine however it is not updating the FIX version field with the newly created version. 

My Jira is running version 7.11.1 and the script-runner plugin version is 5.6.9.

Please can someone advise?

I have pasted below my script:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Level
import org.apache.log4j.Logger
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager;

def log = Logger.getLogger("com.acme.workflows")
log.setLevel(Level.DEBUG)

def versionManager = ComponentAccessor.getVersionManager()
def issueVersions = event.issue.getFixVersions()
def projectId = event.issue.getProjectId()
def projectVersionsUnreleased = versionManager.getVersionsUnreleased(projectId,false)
def projectVersionsReleased = versionManager.getVersionsReleased(projectId,false)

log.debug(issueVersions)

if (issueVersions == []) {

//if projectVersionsUnreleased is blank, create new version
if (projectVersionsUnreleased == []) {
def latestReleasedVersion = projectVersionsReleased[projectVersionsReleased.size() - 1].toString()

log.debug(latestReleasedVersion.isFloat())

float newUnreleasedVersion = latestReleasedVersion.toFloat() + 0.1

def version = newUnreleasedVersion.toString()

def newVersion = versionManager.createVersion(version, null, null, null, projectId, null, false)

log.debug(newVersion)

IssueManager issueManager = ComponentAccessor.getIssueManager();

MutableIssue mutableIssue = issueManager.getIssueObject(event.issue.id);

log.debug(event.issue.id)

mutableIssue.setFixVersions([newVersion])

}

log.debug(projectVersionsUnreleased.size())
log.debug(projectVersionsReleased.size())
log.info("Fix version not assigned to issue.")
}

1 answer

1 accepted

0 votes
Answer accepted
brbojorque Community Leader Dec 29, 2019

Hi @Michael Poon ,

What you did there is correct, the last thing you need to do is to actually save it.

Add this script:

def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
issueManager.updateIssue(currentUserObj, mutableIssue, EventDispatchOption.ISSUE_UPDATED, false)

After this line:

mutableIssue.setFixVersions([newVersion])

Also, import the EventDispatchOption at the top like so below:

import com.atlassian.jira.event.type.EventDispatchOption

Make sure whoever is triggering this script has the ability to create versions.

Hi @brbojorque ,

Many thanks for your assistance. It has worked for me. 

Suggest an answer

Log in or Sign up to
This widget could not be displayed.
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Delivery and Operations Teams can be more Effective and Profitable

    Managing services end-to-end, optimizing efforts to deliver the best services to customers. This is what team leaders and managers want to achieve. When using a tool like Jira, it...

48 views 0 2
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