Creating Rest EndPoint for Bulk Updating Issues with Groovy Scriptrunner

Preetham K S August 8, 2024

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!

2 answers

0 votes
ramyaallena
Contributor
November 15, 2024

Hi @Preetham K S 

 

Did you find a solution for this? We are also looking for the same.

 

Thanks,

Allena

0 votes
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.
August 8, 2024

Hi @Preetham K S

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

 

Preetham K S August 8, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

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

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.
August 8, 2024

Hi @Preetham K S

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 feature on the ScriptRunner console.

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

Thank you and Kind regards,

Ram

Preetham K S August 8, 2024

Hi @Ram Kumar Aravindakshan _Adaptavist_ 

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 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

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events