script console setsecuritylevelid doesn't write to database?

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

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

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

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?

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

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?

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?

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

80 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you