Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Copying Project while logged-in as another user (Scriptrunner)

Sngy October 15, 2020

Hello,
so as the title says, I want to use my Script, so that it will be executed by someone who is not me, but another user (ServiceUser) in the Jira Instance.
This is my functioning code, but I do not know how to make someone else execute it.

import com.atlassian.jira.project.Project
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import org.apache.log4j.Logger
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.bc.project.ProjectCreationData
import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.AssigneeTypes
import com.atlassian.jira.project.type.ProjectTypeKey
// the key for the new project
def projectKey = "EXA987"
def projectName = "EXA987"
def log = Logger.getLogger("com.onresolve.jira.groovy.MyScript")
Thread executorThread = new Thread(new Runnable() {
void run() {
def copyProject = new CopyProject()
def inputs = [
(CopyProject.FIELD_SOURCE_PROJECT) : "SWTEMP",
(CopyProject.FIELD_TARGET_PROJECT) : projectKey,
(CopyProject.FIELD_TARGET_PROJECT_NAME) : projectName,
(CopyProject.FIELD_COPY_VERSIONS) : false,
(CopyProject.FIELD_COPY_COMPONENTS) : false,
(CopyProject.FIELD_COPY_ISSUES) : false,
(CopyProject.FIELD_COPY_DASH_AND_FILTERS) : false,
]
def errorCollection = copyProject.doValidate(inputs, false)
if(errorCollection.hasAnyErrors()) {
log.warn("Couldn't create project: $errorCollection")
}
else {
def util = ComponentAccessor.getUserUtil()
def adminsGroup = util.getGroupObject("jira-administrators")
assert adminsGroup // must have jira-administrators group defined
def admins = util.getAllUsersInGroups([adminsGroup])
assert admins // must have at least one admin
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
copyProject.doScript(inputs)
}
}
})
executorThread.start()

I stumbled upon other codes, using things like

def oldLoggedInUser = jiraAuthenticationContext.getLoggedInUser()

jiraAuthenticationContext.setLoggedInUser(serviceUser)

jiraAuthenticationContext.setLoggedInUser(oldLoggedInUser)
but it was not succesful for me. Thanks in advance!

 

1 answer

1 accepted

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Champion
October 15, 2020

hi @Sngy

what is exact problem? I bet 

def oldLoggedInUser = jiraAuthenticationContext.getLoggedInUser()

is null if you try it in the new Thread? Problem is with new Thread. The code does not have current user in this "new" context. But you are in new thread so 

ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))

should be enough.

Could you describe your problem in detail? What exactly does not work?

Sngy October 15, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__ 

so after testing a bit, my code looks like this, I thought it should do what it is supposed to do (copy a project as another user)

def projectKey = "EXA17"
def projectName = "Example 17 Space"
def log = Logger.getLogger("com.onresolve.jira.groovy.MyScript")

JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
ApplicationUser origUser = authContext.getUser();
def serviceUser =ComponentAccessor.getUserManager().getUserByKey("z004xnnn")
authContext.setLoggedInUser(serviceUser);


Thread executorThread = new Thread(new Runnable() { void run() {
def copyProject = new CopyProject() def inputs = [ (CopyProject.FIELD_SOURCE_PROJECT) : "SWTEMP", (CopyProject.FIELD_TARGET_PROJECT) : projectKey, (CopyProject.FIELD_TARGET_PROJECT_NAME) : projectName, (CopyProject.FIELD_COPY_VERSIONS) : false, (CopyProject.FIELD_COPY_COMPONENTS) : false, (CopyProject.FIELD_COPY_ISSUES) : false, (CopyProject.FIELD_COPY_DASH_AND_FILTERS) : false, ]
def errorCollection = copyProject.doValidate(inputs, false) if(errorCollection.hasAnyErrors()) { log.warn("Couldn't create project: $errorCollection") }
else { def util = ComponentAccessor.getUserUtil() def adminsGroup = util.getGroupObject("jira-administrators") assert adminsGroup // must have jira-administrators group defined
def admins = util.getAllUsersInGroups([adminsGroup]) assert admins // must have at least one admin ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name)) copyProject.doScript(inputs) } }})
executorThread.start()


authContext.setLoggedInUser(origUser);

it does succesfully create the project; but  with the "origUser", not the "serviceUser". Did I do anything obvious wrong? 

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
October 16, 2020

Hi @Sngy I'm kind of lost in your script :), but not problem, it's about formatting here :).

You have three users in your script which are set as loggedInUser

  • serviceUser (z004xnnn)
  • origUser (logged in user)
  • first found admin

If you want to use serviceUser you should replace

  • ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(util.getUserByName(admins.first().name))
  • WITH
  • ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(serviceUser)

Do I understand it correctly?

Sngy October 16, 2020

Hey @Martin Bayer _MoroSystems_ s_r_o__ 

yes that solved it, thank you :)

I'm still new to all of this, so thanks for helping :D

Martin Bayer _MoroSystems_ s_r_o__
Community Champion
October 16, 2020

Sure, we're here for you :). I hope you will enjoy the Jira experience. Could you please accept the answer? You should do it everywhere on Community portal, if you're satisfied with the answer :). Thank you and have a nice weekend

Suggest an answer

Log in or Sign up to answer