Hi,
I have a scriptrunner/groovy script that will be called on an "issue changed" event in Jira Cloud and checks at the very beginning whether the current issue belongs to an open sprint using a call to the search api:
// ************************************* script **************************************
logger.info("current issue: ${issue.key}")
def jqlIssue = 'project = LKAPPL AND issueKey="'+ issue.key +'" AND Sprint in OpenSprints()'
logger.info("JQL query: ${jqlIssue}")
def findIssue = get("/rest/api/3/search")
.queryString("jql", jqlIssue)
.asObject(Map)
.body
.issues as List<Map>
if (findIssue.size()<=0){
logger.info("Issue ${issue.key} does not belong to the current sprint")
return
}
// ... remaining code (automatic numbering of agenda items) omitted
// **********************************************************************************
Usually, the above script works fine:
***************************** successful execution log *****************************
2023-11-09 14:34:20.918 INFO - current issue: LKAPPL-619
2023-11-09 14:34:20.920 INFO - JQL query: project = LKAPPL AND issueKey="LKAPPL-619" AND Sprint in OpenSprints()
2023-11-09 14:34:21.661 INFO - Serializing object into 'interface java.util.Map'
2023-11-09 14:34:21.662 INFO - GET /rest/api/3/search?jql=project+%3D+LKAPPL+AND+issueKey%3D%22LKAPPL-619%22+AND+Sprint+in+OpenSprints%28%29 asObject Request Duration: 739ms
2023-11-09 14:34:21.664 INFO - Issue LKAPPL-619 does not belong to the current sprint
************************************************************************************
Sometimes, however, this check creates the following error in the log (and execution fails):
******************************** failing execution log *******************************
2023-11-09 14:34:19.662 INFO - current issue: LKAPPL-619
2023-11-09 14:34:19.683 INFO - JQL query: project = LKAPPL AND issueKey="LKAPPL-619" AND Sprint in OpenSprints()
2023-11-09 14:34:21.569 INFO - Serializing object into 'interface java.util.Map'
2023-11-09 14:34:21.572 INFO - GET /rest/api/3/search?jql=project+%3D+LKAPPL+AND+issueKey%3D%22LKAPPL-619%22+AND+Sprint+in+OpenSprints%28%29 asObject Request Duration: 1847ms
2023-11-09 14:34:21.573 WARN - GET request to /rest/api/3/search?jql=project+%3D+LKAPPL+AND+issueKey%3D%22LKAPPL-619%22+AND+Sprint+in+OpenSprints%28%29 returned an error code: status: 403 - Forbidden
body: java.lang.RuntimeException: com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')
at [Source: (StringReader); line: 10, column: 2]
2023-11-09 14:34:21.602 ERROR - Cannot get property 'issues' on null object on line 8
2023-11-09 14:34:21.608 ERROR - Class: com.adaptavist.sr.cloud.events.WebhookExecution, Config: null
*************************************************************************************
Does anybody have an idea what the problem might be? Any idea welcome, in particular hints how to track those kinds of problems better than just by logging.
Best,
UweC
Hi sciencecovskij ,
I can confirm this error can occur when an issue returned in the JQL query has some form of permissions, such as an issue security scheme, which means the JQL search cannot access it.
I would advise checking the issue security schemes and project permissions to ensure the user that the script is being executed as can access all issues inside the project.
I hope this information helps.
Regards,
Kristian
Hi Kristian, thank you very much for your answer. However, I am a bit confused that the same issue (in the logs above: LKAPPL-619) works completely differently in identical queries. Do you have an explanation for this? Incidentally, I have access to all issues, thus I don't think that permissions or the issue security should be the problem, but maybe I'm wrong. Are there any logging options that I can use to find out more detailed information about the possible causes of such errors?
Best regards,
Uwe Czienskowski
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Kristian, I think that I have found the problem, and actually it appears to be a permission problem. I have inspected the execution history and there it was: obviously I ignored that an issue change is initiated by a specific user, and although my code runs perfectly when is user is "Automation for Jira", "Script Runner for Jira" or frm the group of agents who have the permission to work with the issues in the project, the "rare" execution errors occur when clients change the issues they deliver to the system.
Is it correct, to change the "As this user" script listener setting from "Current User" to "ScriptRunner Add-On User" to avoid this problem? Dumb error, I know.
Again, thank you very much for your hints.
Best,
UweC
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi sciencecovskij ,
Changing the user to the add-on-user is fine to run the script is fine and is a good way to ensure the script works.
When run as the initiating user and the error occurs this means there are some permissions with the user executing the script not allowing them to access the issues, and I would advise checking this user's permissions.
Regards,
Kristian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @sciencecovskij and ,thanks for your question.
Can you try this code;
def findIssue = get("/rest/api/3/search")
.queryString("jql", "project = LKAPPL AND issueKey=${issue.key} AND Sprint in OpenSprints()")
.asObject(Map)
.body
.issues as List<Map>
Hope this helps. If the issue is resolved, you can vote and accepted for this comment.
Best,
Murat Seven
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Murat Seven , thanks for your proposal. I have changed my script and am now waiting for "Issue Changed" events (as I said, the error only occurs sometimes). In the meantime, I'd be keen to know what the explanation for your approach is, as I don't really see a difference between the two API calls.
Best,
Uwe Czienskowski
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @sciencecovskij and thank you for your response.
Both REST API calls are essentially the same. I just conveyed them to you in a different way to account for the possibility of a syntax error in the query expression. Now I see that the cause of this error is the absence of the 'issues' value in the REST API result. To address this, you can add the ?. control expression as follows and use it in the if condition structure.
logger.info("current issue: ${issue.key}")
def jqlIssue = 'project = LKAPPL AND issueKey="'+ issue.key +'" AND Sprint in OpenSprints()'
logger.info("JQL query: ${jqlIssue}")
def findIssue = get("/rest/api/3/search")
.queryString("jql", jqlIssue)
.asObject(Map)
.body
?.issues as List<Map> // there is a controller !
if (findIssue == null || findIssue.size()<=0){ //changed if condition
logger.info("Issue ${issue.key} does not belong to the current sprint")
return
}
Hope this helps. If the issue is resolved, you can vote and accepted for this comment.
Best,
Murat Seven
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Murat,
yes, this would help to avoid the error message, but my problem was not the message but the bare fact that under - apparently - identical conditions different results occurred, i.e. that my script could not access the issue (and the fields). By the way, although your code is fine (I like the nullability addition for the List<Map>: "?.issues as List<Map>", I was not aware of this possibility), the return message would be wrong, since a null value of findIssue does not indicate that the referred issue does not belong to the open sprint (it could indicate quite different things). This could only be concluded if findIssue (as list) is not null, but the number of issues is zero.
Kristians answer above was the hint which helped me to identify the real problem, which was actually a permission issue.
Anyway, thanks a lot for your contribution, in particular the mentioned code line will help a lot in further development of a pretty large project with a complicated workflow with a bunch of post functions, automation rules, script listeners.
Best,
UweC
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.