Get issues based on selected fixVersions

Yogesh Mude
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.
April 12, 2021

I have a field called versions (multi-select) custom field while creating Change the user needs to select the fix versions in versions custom field, based on these selected versions we need to get the issues which are used by this version.

I have written the post function on create transition for the same but getting error.

def customfManager = ComponentAccessor.getCustomFieldManager()
def versionManager = ComponentAccessor.getVersionManager()
def issueLinkMgr = ComponentAccessor.getIssueLinkManager()
def issueManager = ComponentAccessor.getIssueManager()

def versionCF = customfManager.getCustomFieldObject("customfield_13752") //Versions custom field
Version filteredVersions = (Version)masterIssue.getCustomFieldValue(versionCF)

log.error " @@@@ selected versiosns " + filteredVersions

Collection<Issue> vIssues = versionManager.getIssuesWithFixVersion(filteredVersions)

Getting below error Collection<Issue> vIssues = versionManager.getIssuesWithFixVersion(filteredVersions) on line of code. 

 

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'version-name' with class 'java.lang.String' to class 'com.atlassian.jira.project.version.Version'

1 answer

1 accepted

1 vote
Answer accepted
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 15, 2021

Hi @Yogesh Mude 

After going through your description, I understand that you are trying to find Issues that have Fix Version/s which match the values from the current Issue's Version custom field.

And once the current Issue transitions, you want to link it to the Issues that have Fix Version/s, which match its Versions custom field. Is this correct?

If so, you could try something like this in your post function:-

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.Version

def customFieldManager = ComponentAccessor.customFieldManager
def versionManager = ComponentAccessor.versionManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def customVersion = customFieldManager.getCustomFieldObjectsByName("Custom Versions")[0]

def versions = issue.getCustomFieldValue(customVersion) as List<Version>

versions.findAll {
def vIssues = versionManager.getIssuesWithFixVersion(it)

vIssues.findAll {
issueLinkManager.createIssueLink(issue.id, it.id, 10003, 1, loggedInUser)
}
}

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

For this example, you will need to use the Custom script post-function

Below is a print screen of the Post-Function configuration.

post_function_config.png

Below is an example test case:-

1) For the Issue I am going to transition, the value selected for the Versions field is 1 as shown in the print screen below:-

ver1.png

2) For the issues that are going to be linked with it, the  Fixed Version/s selected is 1 as shown in the print screens below:-

ver3.pngver2.png

Finally, once I try to transition the issue from To Do to In Progress, the issues will be linked as shown in the image below:-

linked.png

I hope this helps to solve your question. :)

 

Thank you and Kind Regards,

Ram 

Yogesh Mude
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.
April 15, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you so much for the response.

I'll check this and get back to you soon.

Yogesh Mude
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.
April 15, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Actually, let me describe you a little bit more...

We have two projects called A and B. so whenever we are creating the Change in project B we want to select the fix versions of project A and then linked the tickets of this selected fix version of project A to the Change of project B as relates to.

So I have created a  custom field called "Versions" and using script runner rest endpoint fetching all the fix versions of project A to this Versions field.

Once i made the changes to my custom script pf as per your code and getting the below error. also, the selected fix version is return as j i r a - s o f t w a r e instead of jira-software.

@@@ selected versiosns [j, i, r, a, -, s, o, f, t, w, a, r, e]  

@@@@@ versiosns j 

groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.project.version.DefaultVersionManager.getIssuesWithFixVersion() is applicable for argument types: (String) values: [e] Possible solutions: getIssuesWithFixVersion(com.atlassian.jira.project.version.Version), getIssueIdsWithFixVersion(com.atlassian.jira.project.version.Version) at Script2009$_run_closure1.doCall(Script2009.groovy:24) at Script2009.run(Script2009.groovy:22)

Script code:

def versionCF = customfManager.getCustomFieldObjectsByName("Versions")[0] //customfield_13752
def filteredVersions = issue.getCustomFieldValue(versionCF) as List<Version>

log.error " @@@@ selected versiosns " + filteredVersions // example. selected version is jira-software then it returns like [j, i, r, a, -, s, o, f, t, w, a, r, e
]
filteredVersions.findAll {
log.error " @@@@@ versiosns " + it // it return only j but not whole version like jira-software
def vIssues = versionManager.getIssuesWithFixVersion(it)
vIssues.findAll {
issueLinkMgr.createIssueLink(issue.id, it.id, 10442, 1, systemUser)
}

}
 
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 18, 2021

Hi @Yogesh Mude ,

I need to clarify with you, do both projects A and B have the same set of versions? 

If so, you can use the version list; else, the version list may not be the correct approach.

 

Thank you and Kind Regards,
Ram

Yogesh Mude
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.
April 22, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Nope, project B does not have the fix versions, and the populated list of fix versions in the "Versions" custom field while creating the change is from project A.

We want to link the issues of selected fix versions from project A to Project B change issue.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 3, 2021

Hi @Yogesh Mude,

For your requirement, you will need to do work with a couple of components, i.e. REST Endpoint, Behaviour and Listener.

The URL you will need to access is <JIRA HOME>/rest/api/2/project/${projectKey}/versions. 

In the example below, the URL used is http://localhost:9091/rest/api/2/project/GV/versions and GV is the project key.

Before using the REST Endpoint to filter the fields, below is a sample output you should receive:-

rest_before.png

 

1) First, to get the list of versions from another project using the URL shown above, you will need to use the REST Endpoint.

Please note, the sample codes shared here are not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a sample code for your reference:- 

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonBuilder
import groovy.transform.BaseScript
import groovyx.net.http.ContentType
import groovyx.net.http.HTTPBuilder
import groovyx.net.http.Method

import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
convertToJson { MultivaluedMap queryParams ->

def hostUrl = "http://localhost:9091"
def userName = "admin"
def password = "q"
def projectKey = "GV" // Project Key from which to get the Version list

def httpBuilder = new HTTPBuilder(hostUrl)

def issueJson = httpBuilder.request(Method.GET, ContentType.JSON) { req ->

uri.path = "/rest/api/2/project/${projectKey}/versions"

headers.'Authorization' = "Basic ${"${userName}:${password}".bytes.encodeBase64().toString()}"

response.failure = { resp, reader ->
log.warn("Failed to query JIRA API: " + reader.errorMessages)
return
}
}

def issueMap = [
items : issueJson.collect { Map row ->
[
value: row.get("name"),
html : row.get("name"),
label: row.get("name")
]
}
]

return Response.ok(new JsonBuilder(issueMap).toPrettyString()).build()
}

Below is a print screen of the REST Endpoint Configuration:-

rest_config.png

 

When you test the REST Endpoint, you should get something like this:-

sr_rest_endpoint_output.png

2) Second, you will need to use the Behaviour initialiser to convert a text field to a single select / multi-select and pass the output from the REST Endpoint to the field.

Below is a sample code for the Behaviour:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def linkedProjectVersions = getFieldByName("Linked Project Versions")

linkedProjectVersions.convertToSingleSelect([
ajaxOptions: [
url : "${baseUrl}/rest/scriptrunner/latest/custom/convertToJson",
keyInputPeriod: 500,
formatResponse: "general"
]
])

Below is a print screen of the Behaviour configuration:-

behaviour_config.png

Below is a print screen of the Create dialog with the text field converted to single select:-

behaviour_screen.png

If you observe, you can see the list of version similar to the REST Endpoint result.

3) Finally, you will need to add a Listener to link the issues. In this example, I am using the Custom Listener with the Issue Created event and Issue Updated event.

Below is a sample code for the Listener:-

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue

def customFieldManager = ComponentAccessor.customFieldManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def customVersion = customFieldManager.getCustomFieldObjectsByName("Linked Project Versions")[0]
def customVersionValue = issue.getCustomFieldValue(customVersion).toString()

def issueIds = issueManager.getIssueIdsForProject(11900)
def issues = issueManager.getIssueObjects(issueIds)

issues.each { sourceIssue ->

def versions = sourceIssue.fixVersions

versions.findAll {version ->
if(version.name == customVersionValue) {
issueLinkManager.createIssueLink(issue.id, sourceIssue.id, 10003, 1, loggedInUser)
}
}
}

In the Listener, you will need to modify this line of code according to your environment, i.e.

def issueIds = issueManager.getIssueIdsForProject(11900)

In my instance, the project id for the GV project is 11900. So you will need to modify it accordingly to match the id of the source project from which the versions are taken.

Below is a print screen of the listener configuration:-

listener_config.png

 

 

Once the ticket has been created, you should see the issues from the other projects which have the same version as the issue created linked to the issue as shown in the image below:-

issue_link.png

I hope this helps to solve your question. :)

 

Thank you and Kind regards,

Ram

Yogesh Mude
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.
May 3, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you for your response.

Previously I was checking with workflow script post function and now checked with the listener, its working/linking issues to the current issue based on selected fix versions as expected but for single fix versions only. if I select the multiple versions then it's not working.

As you have shown above if I select the two versions like 1 and 2 in the "Linked Project Versions" field then it's not linking both the versions issue to the current one but if I select as 1 version then it's linking.

Thanks.

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 4, 2021

Hi @Yogesh Mude

As I explicitly mentioned in my previous comment, I was using the single select example in the sample code that I shared. Hence, you are only able to make one selection at a time.

If you want to select multiple versions, you will need to modify the Behaviour code and the Listener code accordingly.  

In the Behaviour, you will need to change from 

linkedProjectVersions.convertToSingleSelect

to

linkedProjectVersions.convertToMultiSelect

 This will allow you to choose multiple versions.

Below is the sample code for your reference:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def linkedProjectVersions = getFieldByName("Linked Project Versions")

linkedProjectVersions.convertToMultiSelect([
ajaxOptions: [
url : "${baseUrl}/rest/scriptrunner/latest/custom/convertToJson",
keyInputPeriod: 500,
formatResponse: "general"
]
])

And for the Listener, you will need to modify the customVersionValue to output as a List instead of a String, i.e. from

def customVersionValue = issue.getCustomFieldValue(customVersion).toString()

 to

def customVersionValue = issue.getCustomFieldValue(customVersion).toString() as List<String>

 and also modify the if condition from

if(version.name == customVersionValue) 

 to

if(version.name in customVersionValue) 

 Below is a sample code for your reference:-

import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue

def customFieldManager = ComponentAccessor.customFieldManager
def issueLinkManager = ComponentAccessor.issueLinkManager
def issueManager = ComponentAccessor.issueManager
def loggedInUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def customVersion = customFieldManager.getCustomFieldObjectsByName("Linked Project Versions")[0]
def customVersionValue = issue.getCustomFieldValue(customVersion).toString() as List<String>

def issueIds = issueManager.getIssueIdsForProject(11900)
def issues = issueManager.getIssueObjects(issueIds)

issues.each { sourceIssue ->

def versions = sourceIssue.fixVersions

versions.findAll {version ->
if(version.name in customVersionValue) {
issueLinkManager.createIssueLink(issue.id, sourceIssue.id, 10003, 1, loggedInUser)
}
}
}

Please note, the example codes are not 100% exact to your environment. Hence, you will need to modify them accordingly.

Below are a few print screen examples:-

multi-select.png

 

linked_with_multiple_versions.png

Below are print screens of some of the linked issues

linked_issue_1.png

 

linked_issue_2.png

linked_issue_3.png

 

linked_issue_4.png

I hope this helps to solve your question. :)

 

Thank you and Kind regards,

Ram

Yogesh Mude
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.
May 4, 2021

Hello @Ram Kumar Aravindakshan _Adaptavist_ 

Thank you for your swift response.

I have changes the below code

 if(version.name in customVersionValue) 

To 

if(customVersionValue.contains(version.name))

It worked fine. :)

 

Thank you once again for your response and patience.

Thanks,

Suggest an answer

Log in or Sign up to answer