Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Script Field that calculates number of issues in it's Fix version

Miranda Fenton September 20, 2018

I have a scripted field that is applied to one issue type X.  I have multiple issues of X type with different Fix Versions. When I filter X type issues, I'd like to show the script field values in the columns.

For example, X1 is the issue with Fix Version Y1, X2 with Y1, X3 with Y2 and X4 with Y3 so on.

I want the script field in each issue X type(X1, X2, X3 and X4) to fetch it's Fix Version, and count the number of issues with that Fix Version. The value will be the unique count of each issue.

I have the following code already in place. But the version is throwing an error. Please help me achieve this.

 

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserUtil
import com.atlassian.jira.web.bean.PagerFilter
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.project.version.Version

Issue issue = issue
def version = issue.getFixVersions()
log.warn("version = "+ version);

def jqlTestCases = "project = RTO2A AND and "Fix Version" =" +version


SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
UserUtil userUtil = ComponentAccessor.getUserUtil()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()

if (!user) {
user = userUtil.getUserObject('jira_bot')
}

List <Issue> testCases = null

SearchService.ParseResult parseResult = searchService.parseQuery(user, jqlTestCases)


if (parseResult.isValid()) {
def searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())

testCases = searchResult.issues.collect { issueManager.getIssueObject(it.id) }
log.warn(testCases.size());

} else {
log.error("Invalid JQL: " + jqlTestCases);
}

1 answer

0 votes
Daniel Yelamos [Adaptavist]
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.
September 24, 2018

Hi!

What error are you getting? 

Cheers

DY

Miranda Fenton September 24, 2018

Hi Daniel,

The log shows there is an error at the line "Issue issue = event.issue" in the code while I have imported "import com.atlassian.jira.issue.Issue; "

Please suggest any format corrections there could be or, even better, if you could provide a snippet to my requirement.

2018-09-25 09:42:51,899 WARN [common.UserScriptEndpoint]: Script console script failed: groovy.lang.MissingPropertyException: No such property: event for class: Script393 at Script393.run(Script393.groovy:13)

Thanks!

Miranda

Daniel Yelamos [Adaptavist]
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.
September 25, 2018

Hi Miranda.

Where are you executing this script? From the code it looks like it should be an event listener, but if you are getting a MissingPropertyException for variable event, that would point out that this code isn't in an event listener. Is that the case? If so, you will have to get your issue in different ways depending on where you are fetching that value. Some of the most common ones:

1. Postfunction: issue is already in the binding variables, as you can see in this example:

https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/set-issue-attributes.html

2. Script console: You will need to get your issue explicitly, for example

import com.atlassian.jira.component.ComponentAccessor
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey("YOURKEY-123")

If you tell me where you are using this code I can be more specific, listing out every way to get these variables would be quite time consuming.

Cheers

DY

Daniel Yelamos [Adaptavist]
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.
September 25, 2018

Hi Miranda. Rereading your code, it seems that the line of code that you specified:

Issue issue = event.issue

Is not in the code that you provided in the question!

You might be getting the logs from the wrong script!

Cheers!

DY

Miranda Fenton September 30, 2018

 

Hi Daniel,

Currently, I am trying to use this code through script console. Eventually, I would like to put the working code in a "scripted field", so the calculation is automatically assigned to the custom field.

The code I put above has been changed to latter.  That is when I get the error. In my code, I have changed "Issue issue = issue" to "Issue issue = event.issue". 

Please let me know how I can get the fix version of the issue which contains the scripted field.

Thanks & Regards,

Miranda

Daniel Yelamos [Adaptavist]
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.
October 4, 2018

You can do:

def fixversions = issue.getFixVersions()

The variable 'issue' is already in the binding for scripted fields. Do not instantiate it again. Simply call the code that I've given you. Do not do something like this:

def issue = issue
def
fixversions = issue.getFixVersions()

Does this make sense?

Cheers

DY

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events