Updating Fix Version/s field through Script-runner listener

Michael Poon December 29, 2019

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

1 vote
Answer accepted
brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 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.

Michael Poon December 30, 2019

Hi @brbojorque ,

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events