ScriptRunner class equivalents for Cloud

Matt Noe August 5, 2020

I'm trying to run a script in the Script Console, but I'm not able to access some of the classes I'm trying to import.

I've read in other articles that it is likely a difference between JIRIA Cloud and Server.

 

Is there an equivalent class that can be imported for the following?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter

 

My script in its entirety is as follows:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.jql.parser.JqlQueryParser
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Logger;

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)
def searchProvider = ComponentAccessor.getComponent(SearchProvider)

def issueManager = ComponentAccessor.issueManager

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def projectKey = "CBE" //THE PROJECT KEY
def queryString = "project = ${projectKey}"

def query = jqlQueryParser.parseQuery(queryString)

def projectIssues = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter()).issues.key
def inputAccInitialEstimate = 'customfield_10134'
def valueAccInitialEstimate = issue.fields[inputAccInitialEstimate]['value'] as int

def inputNCInitialEstimate = 'customfield_10135'
def valueNCInitialEstimate = issue.fields[inputNCInitialEstimate]['value'] as int

def inputOthInitialEstimate = 'customfield_10136'
def valueothInitialEstimate = issue.fields[inputOthInitialEstimate]['value'] as int

def sumInitialEstimate = valueAccInitialEstimate + valueNCInitialEstimate + valueothInitialEstimate


def inputAccDetailEstimate = 'customfield_10152'
def valueAccDetailEstimate = issue.fields[inputAccDetailEstimate]['value'] as int

def inputNCDetailEstimate = 'customfield_10154'
def valueNCDetailEstimate = issue.fields[inputNCDetailEstimate]['value'] as int

def inputOthDetailEstimate = 'customfield_10155'
def valueothDetailEstimate = issue.fields[inputOthDetailEstimate]['value'] as int

def sumDetailEstimate = valueAccDetailEstimate + valueNCDetailEstimate + valueothDetailEstimate


def inputAccFinalEstimate = 'customfield_10156'
def valueAccFinalEstimate = issue.fields[inputAccFinalEstimate]['value'] as int

def inputNCFinalEstimate = 'customfield_10157'
def valueNCFinalEstimate = issue.fields[inputNCFinalEstimate]['value'] as int

def inputOthFinalEstimate = 'customfield_10163'
def valueothFinalEstimate = issue.fields[inputOthFinalEstimate]['value'] as int

def sumFinalEstimate = valueAccFinalEstimate + valueNCFinalEstimate + valueothFinalEstimate


def log = Logger.getLogger("com.onresolve.jira.groovy");

projectIssues.each
{ issueKey ->
def issue = issueManager.getIssueByCurrentKey(issueKey)

// Specify the issue key to update
//def issueKey = issue.getKey()


log.info ("Acc Detail Estimate: "+valueAccDetailEstimate);
log.info ("NC Detail Estimate: "+valueNCDetailEstimate);
log.info ("Oth Detail Estimate: "+valueothDetailEstimate);
log.info ("Total Detail: "+sumDetailEstimate);
log.info ("Acc Intial Estimate: "+valueAccInitialEstimate);
log.info ("NC Initial Estimate: "+valueNCInitialEstimate);
log.info ("Oth Initial Estimate: "+valueothInitialEstimate);
log.info ("Total Initial: "+sumInitialEstimate);
log.info ("Acc Intial Estimate: "+valueAccFinalEstimate);
log.info ("NC Final Estimate: "+valueNCFinalEstimate);
log.info ("Oth Final Estimate: "+valueothFinalEstimate);
log.info ("Total Final: "+sumFinalEstimate);

if(sumDetailEstimate > 2000 || sumInitialEstimate>2000 || sumFinalEstimate>2000)
{
// Specify the name of the select list field to set
def selectListFieldName = 'IT PM Audit Eligible Project? (2000+ hours)'

// Get the Custom field to get the option value from
def customField = get("/rest/api/2/field")
.asObject(List)
.body
.find {
(it as Map).name == selectListFieldName
} as Map
// Check if the custom field returns a valid field and is not null
assert customField != null : "Cannot find custom field with name of: ${selectListFieldName}"

def result = put("/rest/api/2/issue/${issue.key}")
// Uncomment the line below if you want to set a field which is not pressent on the screen. Note - If using this you must run the script as the ScriptRunner Add-On User.
.queryString("overrideScreenSecurity", Boolean.TRUE)
.header('Content-Type', 'application/json')
.body([
fields: [
(customField.id):[value: "Yes"] as Map
]
])
.asString()

if (result.status == 204) {
return "The ${customField.name} select list field was successfully updated on the ${issue.key} issue"
} else {
return "${result.status}: ${result.body}"
}
}

}

1 answer

1 accepted

0 votes
Answer accepted
Kristian Walker _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.
August 6, 2020

Hi Matt,

Thank you for your question.

I can confirm that the reason that your code above will not work inside of ScriptRunner for Jira Cloud is due to the fact the code you have provided is fro ScriptRunner for Jira Server and uses the Java API which Atlassian provide only inside of Jira Server

The reason this code  will not work as Atlassian only provide a Rest API in Jira Cloud and does not provide a Java API in the cloud like they do in Jira Server.

You can see more detailed information on the differences between the cloud and server versions inside of our documentation page located here.

We would recommend reviewing the documentation for ScriptRunner for Jira Cloud which is located here along with the Jira Cloud Rest API Documentation in order to see how the REST API's work in Jira cloud.

This is the reason why your import statements and method calls to classes like the SearchProvider class cannot be resolved as the Java API's which provide these do not exist inside of Jira Cloud.

This means that you will need to re write your script to use the Rest API's for Jira cloud, but I can confirm you can make a JQL search by calling the Search for Issues Rest API and I can confirm we have an example of how to call this API inside the documentation page located here.

Also I can confirm you can call the Get Current User API to get the current user.

You will be able to take these examples and this information and use it to help create the equivalent script that you require for Jira Cloud.

I hope this information helps.

Regards,

Kristian

Stan Dalenc February 14, 2023

hi, now that we have Behaviours on Jira Cloud we can use groovy is there any update on this?

Like salob likes this
Jesus Octavio Gutierrez Villegas March 11, 2024

Why is this answer accepted? Clearly the question is, if there are any equivalents to Classes like in Scriptrunner for Jira Server, for Scriptrunner for Jira Cloud....

Like BZwolinski likes this

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events