Copy project with scriptrunner creates new project but new project is not accessible

Pavol Gočal
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 5, 2024

Hello guys, 

I use Data center Jira instance, version 9.12.13.

I have a scriptrunner custom script, that creates a new project by copying from existing project and setting specific project category.

Script:

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.canned.jira.admin.CopyProject
import org.apache.log4j.Logger

def log = Logger.getLogger("com.onresolve.jira.groovy.MyScript")
def projectManager = ComponentAccessor.projectManager;
def projectId = issue.getCustomFieldValue("Projektové šablóny");
def adminUser = 'JiraAdmin'
def sourceKey = projectManager.getProjectObj(projectId as Long).key
def targetKey = issue.getCustomFieldValue("Kód projektu");
def targetName = issue.getCustomFieldValue("Názov projektu");
def projectCreationError = "Chyba pri vytváraní projektu. Kontaktujte Jira Administrátora"
def projectCreationSuccess = "Projekt vytvorený úspešne"
def projectCreationInputError = "Konfigurácia vytvorenia projektu obsahuje chyby:"
def projectCategorySet = "Aktualizácia kategórie projektu"
def copyProject = new CopyProject()

def inputs = [

    (CopyProject.FIELD_SOURCE_PROJECT): sourceKey,
    (CopyProject.FIELD_TARGET_PROJECT): targetKey,
    (CopyProject.FIELD_TARGET_PROJECT_NAME): targetName,
    (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.error(projectCreationInputError + " $errorCollection")
    throw Exception(projectCreationInputError + " $errorCollection")
}

log.warn(
"Validation passed")
ComponentAccessor.getJiraAuthenticationContext().setLoggedInUser(ComponentAccessor.userManager.getUserByName(adminUser))
copyProject.doScript(inputs)
def newProject = ComponentAccessor.projectManager.getProjectObjByKey(targetKey)
if (!newProject) {
    log.error(projectCreationError)
    throw new Exception(projectCreationError)
}

log.warn(projectCreationSuccess + " $targetKey: $targetName")
def projectCategory = projectManager.getProjectCategoryObjectByName('Implementačný projekt')
projectManager.setProjectCategory(newProject, projectCategory)

log.warn(projectCategorySet)

New project is copied from the project specified in projectId variable, that gets value from field "Projektové šablóny" (sorry for non-english language:)), that is a custom project picker:

sablona proj.png

I get a name for new project from "Názov projektu" and new project key from "Kód projektu".

The script is executed successfully. New project is created (with new name and project code):

newProject.png

But, when I want to go to the project details, I get an error (you can see that the projectKey is correct):

newProjectError.png

And here funny things happen.

  1. When I call the API /rest/api/2/project, the new project is there:
    newProjectAPI.png
  2. When I call the API for this project /rest/api/2/project/10803, where id is the project Id, there are results as expected with the whole structure.newProjectAPIDetail.png
  3. When I go to the scriptrunner console and use
    return ComponentAccessor.getProjectManager().getProjectByCurrentKeyIgnoreCase("TPD").getId()
    the result is the ID of project.
    When using
    return ComponentAccessor.getProjectManager().getProjectByCurrentKey("TPD")
    also returns result "Project: TPD"
    When using

    return ComponentAccessor.getProjectManager().getProjectObjByKey("TPD")

    this returns NULL.

  4. And when in Admin -> Project section I change with "Edit" button any attribute:
    changeDetails.png
    Then the project is accessible and works as expected:
    accessibleProjekt.png

So to sum it up  - I create a new project using scriptrunner custom script and "CopyProject" functionality. The script creates a project, but the project is not accessible (returns error, that project cannot be found with project key) until I change some details like "Name", "Key", "Avatar" (that are available in "Projects" tab in "Admin" section.

No idea why this is happening. 

I'll be glad to hear any suggestions.

Thank you in advance.

PS: Sorry for long post, just wanted to give you all the information.

4 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Pavol Gočal
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 17, 2024

Refresh method of project manager does the job. 

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 15, 2024

Hi @Pavol Gočal

When copying the issues from one project to another, have you tried using the Built-in Script for ScriptRunner for Jira Cloud, i.e. Bulk Clone Issues?

Let me know how it goes.

Thank you and Kind regards,

Ram

0 votes
Pavol Gočal
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 5, 2024

Hello @Shiva Kumar Hiremath

Reindexing was one of the things I tried and it did not help. Forgot to mention it in the main post.

0 votes
Shiva Kumar Hiremath
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 5, 2024

Hello @pavol,

I think Reindexing jira might resolve the issue.

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events