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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,308
Community Members
 
Community Events
184
Community Groups

How to auto fill Fix Version based on Resolution?

Edited

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

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.
Nov 15, 2022 • edited

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!

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.
Nov 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.
Nov 15, 2022

Try 

issue.setFixVersions([]);

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()])
}

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events