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
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.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
So you want something to clear the version you mean?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()])
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.