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.
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:

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

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

And here funny things happen.
- When I call the API /rest/api/2/project, the new project is there:

- 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.

- 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.
- And when in Admin -> Project section I change with "Edit" button any attribute:

Then the project is accessible and works as expected:

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.