Update priority field if user does not have a permission "Assign issues"

zaharovvv_suek_ru
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.
December 19, 2018

We are using ScriptRunner.
We are changing a priority field in Custom Listener when an event `Issue updated` is happened.
This code updates a `priority` field of issue if user has permission "Assign issues" in Issue Permissions tab of my project.
However, if user does not have permission "Assign issues" in Issue Permissions tab of my project, then this code is not updating priority field.
What can I do to update priority field if user does not have a permission "Assign issues", but user has a permission to "Edit Issues"?
In my view, it should be enough to have a permission "Edit Issues" to be eligible to edit priority field. Is it correct?

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParametersImpl


def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

def issue = event.issue
def idPriorityLoIssue = issue.getPriorityObject().getId().toString();

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sequenceField = customFieldManager.getCustomFieldObjects(issue).find {it.name == "Послед."}
log.info(sequenceField)
def sequenceFieldValue = issue.getCustomFieldValue(sequenceField)
log.info(sequenceFieldValue)


def issueLinkManager = ComponentAccessor.getIssueLinkManager()
issueLinkManager.getOutwardLinks(issue.id).each{ issueLink ->
def linkedIssue = issueLink.getDestinationObject()
log.warn('1. ***** LinkedIssue: *****' + linkedIssue)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
log.warn('1.0. User is' + user)
def issueInputParameters = new IssueInputParametersImpl()
def issueService = ComponentAccessor.getIssueService()
issueInputParameters.setPriorityId(idPriorityLoIssue.toString())
issueInputParameters.setSkipScreenCheck(true)
def validationResult = issueService.validateUpdate(user, linkedIssue.id, issueInputParameters)
log.warn('validationResult.isValid() ' + validationResult.isValid())

if (validationResult.isValid()) {
log.warn('vaild')
issueService.update(user, validationResult)
log.warn('2. ***** Priority of issue is set to: *****' + idPriorityLoIssue)
def seqFieldLinkedIssue = customFieldManager.getCustomFieldObjects(linkedIssue).find {it.name == "Послед."}
if(seqFieldLinkedIssue){
def changeHolder = new DefaultIssueChangeHolder()
seqFieldLinkedIssue.updateValue(null, linkedIssue, new ModifiedValue(linkedIssue.getCustomFieldValue(seqFieldLinkedIssue), sequenceFieldValue),changeHolder)
}
} else {
log.info('not vaild')
log.warn validationResult.errorCollection.errors
log.info('3. ***** Priority of issue is NOT set to: *****' + idPriorityLoIssue)
}
}

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
Ivan Tovbin
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.
December 24, 2018

I suggest using IssueManager to update issues instead of IssueService. 

Like so:

import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.component.ComponentAccessor

issue.setPriority(priority)
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
zaharovvv_suek_ru
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.
December 25, 2018

@Ivan Tovbin Thanks for your answer. I've tried to use your code, but the following code is outlined by red color:

 

issue.setPriority(priority)
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

The error says:

1. Cannot find matching method "setPriority"

2. Cannot find matching method "updateIssue"

Ivan Tovbin
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.
December 28, 2018

Can you show me the code as it looks like on your end?

zaharovvv_suek_ru
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.
March 15, 2019

So the final code should look like this:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.web.bean.PagerFilter
import org.apache.log4j.Logger
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.index.IssueIndexingService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.util.ImportUtils
import org.apache.log4j.Category
import java.sql.Timestamp
import com.atlassian.jira.event.type.EventDispatchOption;

def log = Logger.getLogger("com.acme.CreateSubtask")
IssueManager issueManager = ComponentAccessor.getIssueManager()

def issue = issueManager.getIssueObject("Bar-1")

def customFieldManager = ComponentAccessor.getCustomFieldManager()


def environmentValue = issue.getEnvironment();
log.info('environmentValue ' + environmentValue)
issue.setEnvironment('New Environment 1')


def user = ComponentAccessor.getJiraAuthenticationContext()
.getLoggedInUser()
log.info('user ' + user)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH
, false)
TAGS
AUG Leaders

Atlassian Community Events