Creating Rest EndPoint for Bulk Updating Issues with Groovy Scriptrunner
Asked by Preetham K S:

Hi,

I am trying to create a Rest endpoint using Groovy Scriptrunner in Jira and need help.

my requirement:

EX: jql: project = "XYZ"

field value : version=2.5  

when i run my Endpoint with the above inputs , all the issues which return from that project=XYZ should get version field updated to 2.5

endpoint: PUT/rest/api/update

Problem;

when i run my end point and see in my logs i showing that no issues found in project.

below is my script:

Imported necessary package

@BaseScript CustomEndpointDelegate delegate

jqlquery(  
httpMethod: "PUT"  
) { MultivaluedMap queryParams, String body, HttpServletRequest request -\>  
def jsonSlurper = new JsonSlurper()  
def requestBody = jsonSlurper.parseText(body)

String jql = requestBody.jql  
String fieldName = requestBody.fieldName  
String fieldValue = requestBody.fieldValue

// Log received values  
log.warn("Received request:")  
log.warn("JQL: " +jql)  
log.warn("Field Name: " +fieldName)  
log.warn("Field Value: " +fieldValue)

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser  
// def users = ComponentAccessor.jiraAuthenticationContext.user  
def searchService = ComponentAccessor.getComponent(SearchService)  
def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)  
log.warn("query are" +jqlQueryParser)

def parsedQuery  
try {  
parsedQuery = jqlQueryParser.parseQuery(jql)  
log.warn("Parsed Query: "+parsedQuery)  
} catch (Exception e) {  
log.warn("Error parsing JQL query: " +jql+ "-" +e.message)  
}

def searchResult = searchService.search(user, parsedQuery, PagerFilter.unlimitedFilter)  
def issues = searchResult.results  
log.warn("Number of Issues Found: " +issues.size())  
IssueManager issueManager = ComponentAccessor.issueManager  
IssueService issueService = ComponentAccessor.issueService  
CustomFieldManager customFieldManager = ComponentAccessor.customFieldManager  
CustomField customField = customFieldManager.getCustomFieldObjectByName(fieldName)

if (!customField) {  
return Response.status(Response.Status.BAD_REQUEST).entity("Invalid field name: ${fieldName}").build()  
}  
log.warn("Custom Field ID: " +customField.idAsLong)  
def updatedIssues = \[\]  
def failedIssues = \[\]

def updateValidationResult = issueService.validateUpdate(user, issue.id, issueInputParameters)  
def updateResult = issueService.update(user, updateValidationResult)  
if (updateResult.isValid()) {  
log.warn("Issue Updated Successfully: "+issue.key)  
updatedIssues \<\< issue.key  
} else {  
log.warn("Issue Update Failed for Issue: " +issue.key+ "- Errors:" +updateResult.errorCollection)  
failedIssues \<\< issue.key  
}  
} else {  
log.warn("Issue Validation Failed for Issue: " +issue.key+ "- Errors:" +updateValidationResult.errorCollection)  
failedIssues \<\< issue.key  
}  
}

def responseBody = \[  
message: "Updated " +updatedIssues.size()+ "issues with " +fieldName+ "=" +fieldValue,  
updatedIssues: updatedIssues,  
failedIssues: failedIssues  
\]

return Response.ok(JsonOutput.toJson(responseBody)).build()  
}  
Could someone please provide guidance.  
Thank you in advance for your help!


Answers:
author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Preetham K S](/t5/user/viewprofilepage/user-id/5471292)

Please clarify: What do you want to perform Update using the REST Endpoint?

If you intend to update only values in fields, you could use the ScriptRunner Console to do so.

I am looking forward to your feedback and clarification.

Thank you and Kind regards,  
Ram

comments: author: Preetham K S
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

My concern is to create a rest endpoint for my requirement where that end point get all the issues from the particular project and update a value for that all issues in bulk.

Thank you and Kind regards,  
Preetham


author: Ram Kumar Aravindakshan _Adaptavist_
body: Hi [@Preetham K S](/t5/user/viewprofilepage/user-id/5471292)

What values do you intend to update via REST? Is it custom field values or system field values?

Instead of doing this via REST, it would be simpler to use the [ScriptRunner HAPI](https://docs.adaptavist.com/sr4js/latest/hapi) feature on the ScriptRunner console.

Please provide the clarification requested above so I can provide a sample script.

Thank you and Kind regards,

Ram


author: Preetham K S
body: Hi [@Ram Kumar Aravindakshan _Adaptavist_](/t5/user/viewprofilepage/user-id/2988015)

As if know I am trying to update the values for custom field values only. But I have to do this via through REST END only..

Based on REST can you please provide me similar script which will bulk update the issues value in particular project.

[ScriptRunner HAPI](https://docs.adaptavist.com/sr4js/latest/hapi)can be used only by Jira admins but this bulk update as to be done by third party for that reason I have to create REST EndPoint.

Thank you and Kind regards,

Preetham


Tags: groovy

jira-data-center

scriptrunner