Forums

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

Issue not updated in ScriptRunner Rest Endpoint, while using IssueService instead of IssueManager

Petr Papousek
Contributor
June 5, 2020

A sample code below should update issue fixVersions. If I run it (just without being wrapped in issueUpdateExampleEndpoint function) from ScriptRunner Console or test runner, it works as expected - issue fixVersions are updated properly. If the exactly same code runs within REST Endpoint, validation and update results look OK, no errors, however the fixVersions are not changed.

If I use IssueManager instad of IssueService, it works in both cases, however, I'd like to use a proper way of updating issue (i.e. IssueService), as it doesn't skip validations, indexing etc. like the IssueManager does.

I really don't understand what is the cause of this. It's not about delay - I've checked the issue manually too of course and it has no fixVersions after rest endpoint run

I'm using JIRA v7.13.12 & Scriptrunner 5.6.14

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.IssueResult
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.project.version.Version
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript

import javax.servlet.http.HttpServletRequest
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate
issueUpdateExampleEndpoint(httpMethod: "POST") { MultivaluedMap queryParams, String requestBody, HttpServletRequest request ->
def issueKey = "TEST-1"
IssueService issueService = ComponentAccessor.getIssueService()
def user = ComponentAccessor.getUserManager().getUserByName("johndoe")
def issue = ComponentAccessor.getIssueService().getIssue(user, issueKey).getIssue()
String responseMsg = "Former versions: ${issue.getFixVersions()}"

Version exampleVersion = ComponentAccessor.getVersionManager().getVersion(15944)
responseMsg += "\nTry to add version: ${exampleVersion} (ID: ${exampleVersion.getId()})"
Long[] versionIds = [exampleVersion.getId()]
IssueInputParameters fieldValueChanges = ComponentAccessor.getIssueService().newIssueInputParameters()
fieldValueChanges = fieldValueChanges.setFixVersionIds(versionIds)

UpdateValidationResult validationResult = issueService.validateUpdate(user, issue.getId(), fieldValueChanges)
responseMsg += "\nIs Validation OK? ${validationResult.isValid()}; Errors: ${validationResult.getErrorCollection().getReasons()}"

IssueResult updateResult = issueService.update(user, validationResult, EventDispatchOption.ISSUE_UPDATED, false)
responseMsg += "\nIs Update OK? ${updateResult.isValid()}; Errors: ${updateResult.getErrorCollection().getReasons()}"

issue = ComponentAccessor.getIssueService().getIssue(user, issueKey).getIssue()
responseMsg += "\nNew set of versions: ${issue.getFixVersions()}"

return Response.ok(responseMsg, MediaType.TEXT_PLAIN_TYPE).build()
}

ScriptRunner Console / Test runner output:

Former versions: [] 
Try to add version: exampleVersion (ID: 15944)
Is Validation OK? true; Errors: []
Is Update OK? true; Errors: []
New set of versions: [exampleVersion]

ScriptRunner Rest Endpoint output:

Former versions: []
Try to add version: exampleVersion (ID: 15944)
Is Validation OK? true; Errors: []
Is Update OK? true; Errors: []
New set of versions: []    

 

1 answer

0 votes
Mathis Hellensberg
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.
June 17, 2020

Hmm that is very weird indeed.. Just out of curiosity, why are you calling .getIssue() on .getIssue()?

Petr Papousek
Contributor
June 18, 2020

Hello Mathis, thanks for taking a look! And I definitely agree, this is weird :-)
IssueService.getIssue() returns IssueResult, not the Issue instance itself, that is why i call it twice - the second call is IssueResult.getIssue()

Suggest an answer

Log in or Sign up to answer