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

How to auto fill Fix Version based on Resolution?

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 15, 2022

I'm attempting to set the Fix Version to the earliestUnreleasedVersion when an issue is resolved.

Our fix version list is by quarter:

23.Q1 << this being the earliestUnreleasedVersion
23.Q2
23.Q3
23.Q4
etc

When an issue is resolved, the Resolution field can be either "Done", "OBE", or "Won't Do".

I'm trying to use a post-function script to check when Resolution is not "Unresolved" (i.e. any of the resolved status above), then the Fix Version will automatically set to the earliestUnreleaseVersion (i.e. 23.Q1). In addition, if the issue was reopen, Resolution is cleared (that is another script already working), and once the issue is closed again, it should either keep the same earliestUnreleasedVersion, or clear any version is had and replaced with the next earliestUnreleasedVersion. (for example: an issue used to 23.Q1, but was finally closed until 23.Q2. Fix version needs to be only one, which is 23.Q2).

But my current script keeps prompting me to select my fix version, when I want it to automatically set the fix version.

import com.atlassian.jira.event.type.EventDisptachOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.user.sedarch.UserSearchService
import com.atlassian.jira.application.ApplicationAuthorizationService

def versionManager = ComponentAccesor.getVersionManager()
def project = issue.getProjectObject().getId()
def version = versionManager.getVersionsUnreleased(project, false)
def resolution = issue.getResolution()
def earliestUnreleasedVersion = versionManager.updateIssueFixVersions(issue,version)

//bypass user permissions so fix version can be set
UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
.ignorePermissionCheck(true)

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jiraServiceContext = new JiraServiceContextImpl(loggedInUser)
def applicationAuthorizationService = ComponentAccessor.getComponent(ApplicationAuthorizationService)
def users = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "",paramBuilder.build())

if (loggedInUser == users && resolution != 'Unresolved'){
issue.setFixVersions([version.first()])
}

ComponentAccessor.getIsuseManager().updateIssue(loggedInUser,issue,EventDispatchOption.Issue_UPDATED, false)

Also, for attempt this, am I placing it in the right location? Instead of workflow's post function, should it be in listeners? Or Behaviors? How would the script would be for that since I believe calling methods would be different.

I had also review other questions similar, but none achieved my goals

https://community.atlassian.com/t5/Jira-questions/ScriptRunner-set-fixVersion-in-transition-post-fuction-not/qaq-p/1288387

https://community.atlassian.com/t5/Adaptavist-questions/Clearing-FixVersion-field-in-Scriptrunner-Behaviours-script/qaq-p/1464058

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 15, 2022

Hi @Diana Gorv 

I don't know if you've pasted the code exactly as you have written it on the workflow PF, but you have three typos:

import com.atlassian.jira.event.type.EventDisptachOption
import com.atlassian.jira.bc.user.sedarch.UserSearchService
ComponentAccessor.getIsuseManager().updateIssue(loggedInUser,issue,EventDispatchOption.Issue_UPDATED, false)

When I corrected these error your PF (I ran it on the console, so I made a few changes) worked, BUT it needed some changes. As I said, running the code on the console like the following worked fine. However, if you want to test it on your instance, change the "SP-1" with an issue of your instance. This was just for test

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.application.ApplicationAuthorizationService
import com.atlassian.jira.issue.MutableIssue

def versionManager = ComponentAccessor.getVersionManager()
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("SP-1") as MutableIssue
def project = issue.getProjectObject().getId()
def version = versionManager.getVersionsUnreleased(project, false)
def resolution = issue.getResolution()

//def earliestUnreleasedVersion = versionManager.updateIssueFixVersions(issue,version) THIS LINE WILL ADD ALL VERSIONS TO YOUR ISSUE

//bypass user permissions so fix version can be set
UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
.ignorePermissionCheck(true)

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jiraServiceContext = new JiraServiceContextImpl(loggedInUser)
def applicationAuthorizationService = ComponentAccessor.getComponent(ApplicationAuthorizationService)
def users = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "",paramBuilder.build())

try {
if (loggedInUser == users && resolution != 'Unresolved'){
issue.setFixVersions([version.first()])
ComponentAccessor.issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
log.warn([version.first()])
}
} catch (Exception e) {}

Now, the following line you have, and I commented out, was adding all versions to the issue:

def earliestUnreleasedVersion = versionManager.updateIssueFixVersions(issue,version) 

 

I would place your script on a workflow PF and not on a listener, and most likely your code (without testing it on a workflow) should look like this:

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.application.ApplicationAuthorizationService

def versionManager = ComponentAccessor.getVersionManager()
def issueManager = ComponentAccessor.getIssueManager()
def project = issue.getProjectObject().getId()
def version = versionManager.getVersionsUnreleased(project, false)
def resolution = issue.getResolution()

//bypass user permissions so fix version can be set
UserSearchParams.Builder paramBuilder = UserSearchParams.builder()
.ignorePermissionCheck(true)

def loggedInUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def jiraServiceContext = new JiraServiceContextImpl(loggedInUser)
def applicationAuthorizationService = ComponentAccessor.getComponent(ApplicationAuthorizationService)
def users = ComponentAccessor.getComponent(UserSearchService).findUsers(jiraServiceContext, "",paramBuilder.build())

try {
if (loggedInUser == users && resolution != 'Unresolved'){
issue.setFixVersions([version.first()])
ComponentAccessor.issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
log.warn([version.first()])
}
} catch (Exception e) {}

Try it and let me know if that works!

Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 15, 2022

Hi @Alex Koxaras _Relational_ thank you for catching my typos, I had to manually type it in since the script is on an air-gapped environment that I can't copy and paste :)

This script works! Thank you so much!!

A couple of notes for future viewers in case they still have trouble.

  • I forgot to clear an old transition screen that's requesting for a Fix Version. 
  • Even if I did kept the transition screen and tried to change the Fix Version to a later version, the post-function script above does automatically set to the correct earliestUnreleasedVersion
  • It's possible the permission check might not be needed. I checked by logging in as another user, and was able to transition an issue to Done and see the Fix Version is auto set correctly.

One last question, if a user had placed 2 Fix Versions, can they both be cleared and then set Fix Version? When I tested by editing an issue to be 23.Q3 and 23.Q4, pushed issue to done, the 2 Fix Version stayed. 

I tried adding:

issue.setFixVersions(null)

But it didn't work. If not, I can still accept this an an answer.

 

Thanks!

Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 15, 2022

So you want something to clear the version you mean?

Alex Koxaras _Relational_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 15, 2022

Try 

issue.setFixVersions([]);
Diana
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 16, 2022

Yes! I had to include an || for my If statement to capture if a user had placed more than 1 fix version by mistake

if ((issue.fixVersions.size()>1) || (loggedInUser == users && resolution != 'Unresolved')){
issue.setFixVersions([]);
issue.setFixVersions([version.first()]);
ComponentAccessor.issueManager.updateIssue(loggedInUser, issue, EventDispatchOption.ISSUE_UPDATED, false);
log.warn([version.first()])
}
TAGS
AUG Leaders

Atlassian Community Events