script console setsecuritylevelid doesn't write to database?

Jeff Pandinha May 4, 2017

Hello all,

I'm trying to write a listener that would set security level ID whenever there is an issue update or issue moved event. Essentially, if an issue is moved from a bug to non-bug, the security level should be set accordingly and vice-versa.

Since I'm not a coder, I usually just copy snippets of code and modify it for my purpose, however on this problem, I'm completely stumped. The listener doesn't work and when I try something simpler on script console such as:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager();
def issue = issueManager.getIssueObject("ABCD-123");
issue.setSecurityLevelId(10100L);
return issue.getSecurityLevelId();

The code above does return 10100, but when I check on the issue itself, the security level hasn't changed, i.e. still 10000. Does anyone know why the script is not writing the changes to the issue itself? Is it because it's a MutableIssue and there is another function to write the change to the database?

Thanks for any help.

1 answer

1 accepted

1 vote
Answer accepted
JamieA
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.
May 5, 2017

Hi Jeff,

Your code is just setting the property on the object, but it is not persisted. You have to do that yourself, in the same way you would do an edit or a transition if using the GUI.

There are various methods to do this, here is one:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.UpdateIssueRequest

def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject("JRA-9")

issue.setSecurityLevelId(10000)
def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
issueManager.updateIssue(user, issue, UpdateIssueRequest.builder().sendMail(false).build())
Jeff Pandinha May 10, 2017

Thanks Jamie. That works. I'll try this method on listener as well.

Jeff Pandinha May 11, 2017

Hi Jamie,

Thanks to your suggestion, I have this script listerner which sort of work. It is set to listen to "Issue Updated" and "Issue Moved":

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.event.issue.IssueEvent

def issue = event.issue as MutableIssue
def issueManager = ComponentAccessor.issueManager

if (issue.getIssueType() == "Bug" {
    issue.setSecurityLevelId(10100L)
}
else if (issue.getIssueType() != "Bug") {
    issue.setSecurityLevelId(10000L) 
}

def user = ComponentAccessor.jiraAuthenticationContext.loggedInUser
issueManager.updateIssue(user, issue, UpdateIssueRequest.builder().sendMail(false).build())

The problem is the script listener runs on the first time the issue is updated or moved. The subsequent update/move on the same issue seems to have fallen on deaf ears. Is this a known issue or maybe there is something wrong with my script?

JamieA
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.
May 12, 2017

Hi Jeff... your code is wrong. The "else" statement will always be true.

Try:

if (issue.issueType.name == "Bug") {
    issue.setSecurityLevelId(10100L)
}
else {
    issue.setSecurityLevelId(10000L)
}
Jeff Pandinha May 12, 2017

Hi Jamie,

Thanks for the response. The code is indeed mistaken. I blame it on too much Python recently :)

However, the problem still stands. I used a simple script console to

issue.setSecurityLevelId(10100L)

and check the jira issue, it is indeed updated the first time. But if I run in script console again:

issue.setSecurityLevelId(10000L)

This doesn't seem to update the issue anymore. It looks like updateIssue can only be run once. Is there something wrong with my code or maybe I need to run a slightly different updateIssue call the second time?

JamieA
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.
May 16, 2017

I don't recognise this... you can run updateIssue as many times as you want. Are there definitely no errors? 

Is it possible that after changing the sec level the current user has no permissions to that issue? Then you would get an error I would imagine.

Can you check the issue update history from the UI? Does it show both updates?

Jeff Pandinha May 16, 2017

Hi Jamie,

It is indeed very odd. When I look at the issue's history, the script updated the security level, but as soon as that happens, the security level is reverted back to the original (by the same user). Enabling the log doesn't show anything out of the ordinary.

After a little of googling, I've decided to go with issue.store() which works but may be deprecated in the future. IssueUpdate seems to have some weird problems associated with it, such as:
https://community.atlassian.com/t5/Answers-Developer-Questions/issueManager-update

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events