JQL search with a different user when scripting: Scriptrunner

Andrew Lonardelli July 11, 2017

I am trying to change the user when doing a JQL search for a scripted field. I realizd that the current user will not be able to browse the project so I would need to use a user that is able to browse the project. A piece of my script can be seen below.  I would like to change the def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() to a different user. As an example, maybe the user name can be JohnDoe

maybe

def user = ComponentAccessor.getJiraAuthenicationContext().getUserByName("JohnDoe")

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import java.lang.Object
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import static java.lang.Math.*
import org.apache.log4j.Logger
import org.apache.log4j.Level

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


boolean check =true


String jqlVaca = 'project = HR AND issuetype = "Employee Information"';
log.debug("Get filter string")

// Get a search-Object for JIRA
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issuesInfo
SearchService.ParseResult parseResultVaca =  searchService.parseQuery(user, jqlVaca)


if (parseResultVaca.isValid()) {
   	 issuesInfo = (searchService.search(user, parseResultVaca.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues())
log.debug("Parse is Valid")
} 
else
	check =1;

 

1 answer

0 votes
Stefan Arnold
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.
July 11, 2017

 

JiraAuthenticationContext

It has the function "setLoggedInUser(ApplicationUser user)". 
This is used in places like Jelly where we need to switch the identity of a user during execution.

To get the user i think you need UserManager and the getUserByName (or Key) function.

 I never used it but this should be the function you need.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events