Hi Folks!
When creating an issue we need that version type customfield "Found in versions" was prepopulated with first (earliest) unreleased version.
For example we have:
22.3 - unreleased
22.2 - unreleased
22.1 - unreleased
21.12 - released
21.11 - released
On the creation screen we need "22.1" in the field as prepopulated.
I set the following behavior, that works:
def foundinversionField = getFieldByName("Found in Version")
foundinversionField.setFormValue("22.1")
Also I can get last version in Console
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.CustomFieldManager
def issueManager = ComponentAccessor.issueManager // console Only
def issueKey = "KEY-74633" // console only
def issue = ComponentAccessor.issueManager.getIssueByCurrentKey(issueKey) // console only
def foundinversionField = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Found in Version")
def value = issue.getCustomFieldValue(foundinversionField)
def versionManager = ComponentAccessor.getVersionManager()
def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectObjByKey(issue.projectObject.key)
def versions = versionManager.getVersions(project)
versions.last()
But if I copy this code from the console to the behavior it doesn't work. Is there difference between functions of Console and Behaviors?
Also doesn't work function
def versions = versionManager.getVersionsUnreleased(project)
to get all the Unreleased versions and after take a first one.
So, I have two general questions:
1. How to combine both scripts to populate field not with the static one (like "22.1")
2. How to get first (earliest) unreleased version
Thank you in advance, any help will be much appreciated!
I was able to build the script that solved our need. I put it here in case someone is looking for something similar:
import com.atlassian.jira.component.ComponentAccessor
def foundinversionField = getFieldByName("Found in Version")
def versionManager = ComponentAccessor.getVersionManager()
def projectManager = ComponentAccessor.getProjectManager()
def projKey = getIssueContext().getProjectObject().getKey()
def project = projectManager.getProjectObjByKey(projKey)
def versions = versionManager.getVersions(project)
def foundInVersion = versions.findAll {it.released == false && it.archived ==false}.first().name
foundinversionField.setFormValue(foundInVersion)
Hi @Slava Gefen
The approach you are trying is not going to work as expected.
If you want to use the Behaviour to set the latest version by using the last() method, you will first need to get all the versions from the project using the REST Endpoint.
Once you have obtained the values from the REST Endpoint, you will need to pass them to the Versions field using the Behaviour's Initialiser and set the value to the Version field.
For your reference, below is a sample REST Endpoint code in which the version names are extracted from the project:-
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.HttpResponseDecorator
import groovyx.net.http.RESTClient
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
@BaseScript CustomEndpointDelegate delegate
getVersions { MultivaluedMap queryParams ->
def applicationProperties = ComponentAccessor.applicationProperties
def hostUrl = applicationProperties.getString('jira.baseurl')
def username = 'admin'
def password = 'q'
def projectKey = 'BT'
final def headers = ['Authorization': "Basic ${"${username}:${password}".bytes.encodeBase64()}", 'Accept': 'application/json'] as Map
def http = new RESTClient(hostUrl)
http.setHeaders(headers)
def resp = http.get(path: "/rest/api/2/project/${projectKey}/versions") as HttpResponseDecorator
if (resp.status != 200) {
log.warn 'Commander did not respond with 200'
}
def row = resp.data['name']
Response.ok(new JsonBuilder(row).toPrettyString()).build()
}
Below is a print screen of the REST Endpoint configuration:-
Next, you will need to invoke the REST Endpoint via the Behaviour and set the value to the Versions field as shown in the sample code below:-
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.json.JsonSlurper
import groovy.transform.BaseScript
@BaseScript FieldBehaviours behaviours
def versionPicker = getFieldByName('Version Picker')
def baseUrl = 'http://localhost:9091'
def hostUrl = "${baseUrl}/rest/scriptrunner/latest/custom/getVersions".toString()
def response = hostUrl.toURL().text
def json = new JsonSlurper().parseText(response)
def artifacts = json.collect().sort()
versionPicker.setFormValue(artifacts.last())
Below is a print screen of the Behaviour configuration:-
Please note the sample working codes provided are not 100% exact to your environment. Hence, you will need to make the required modifications.
Below are some test print screens:-
1. The print screen below shows the list of versions added to the project. The latest version added is k.
2. When the issue create screen appears, the Behaviour can auto set the Version Picker field to the latest version available in the project, i.e. k.
I hope this helps to solve your question. :)
Thank you and Kind regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ ! Sorry I'm back with a delay and many thanks for your support on the question!
Could you explain to me in a brief: Why can't we use version directly in Behaviors and can in ScriptConsole?
In continue with your solution: How can I get not the latest unreleased version but the earliest one? In other words I need first unreleased version.
After your answer I'll try the solution with the rest-endpoint and will let you know if it works for us.
Thank you in advance!
With kind regards
Slava
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Slava Gefen
In regards to your question, i.e.
Could you explain to me in a brief: Why can't we use version directly in Behaviors and can in ScriptConsole?
The Behaviour works differently from the Script Console, and it has its specific variables. For example, when using the ScriptRunner console, you can invoke the Issue using the issueManager. However, in Behaviour, it has its own embedded variable for the Issue, i.e. the underlyingIssue.
Also, could you please clarify, i.e. when do you want the Behaviour to occur? Is it when you Create a new Issue or when you Edit an existing Issue?
If it is the former, you will need to use the REST Endpoint because the Issue does not yet exist and does not have an Issue key.
On the other hand, if it is the latter, you can use the underlyingIssue variable to invoke the value of the versions and set that field's value. You will, however, have to convert the underlyingIssue to a MutableIssue so that you can update, i.e. set the values of the field.
Please refer to the Adaptavist Documentation on Behaviours for more information.
Thank you and Kind Regards,
Ram
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Ram Kumar Aravindakshan _Adaptavist_ thanks again for the answer!
The Behaviour works differently from the Script Console, and it has its specific variables.
Yes, I marked this.
Also, could you please clarify, i.e. when do you want the Behaviour to occur? Is it when you Create a new Issue or...
First. We need this on creation screen. But we don't need issue key in order to find all versions in the project, do we? Or we can't get the project until we'll create an issue?
And could you help here:
In continue with your solution: How can I get not the latest unreleased version but the earliest one?
As I described in description (this list of versions in project)
For example we have:
22.3 - unreleased
22.2 - unreleased
22.1 - unreleased
21.12 - released
21.11 - releasedOn the creation screen we need "22.1" in the field as prepopulated.
Thanks in advance!
With kind regards
Slava
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Spend the day sharpening your skills in Atlassian Cloud Organization Admin or Jira Administration, then take the exam onsite. Already ready? Take one - or more - of 12 different certification exams while you’re in Anaheim at Team' 25.
Learn more
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.