Forums

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

Server-side script to validate project key/project name

-
Contributor
April 30, 2020

Hi all,

There are two parts to this question:

1.) Users in our jira instance can submit issues to us called "New Project", and that's their way of submitting requests to create new projects in our instance, of course. What we want to do as precaution is to make sure whatever name and key they want for their project hasn't already been created.

In the New Project issues, there are two custom fields fields called "Target Name" and "Target Key" that customers use to provide use whatever name and key they want for the new project. We've made use of Scriptrunner behaviours to try and validate the two fields.

Here's what I have so far:

Initialiser

def desc = getFieldById("description")
def defaultValue = """Please consider/address the following questions:

1 - Is there an existing Jira Project you would like to mirror? If not, the project will be created with a universally flexible template
2 - Is there a related Oath Product/Category this project will be associated with?
""".replaceAll(/ /, '')

desc.setFormValue(defaultValue)

Target Key (server-side script)

import com.atlassian.jira.bc.JiraServiceContext
import com.atlassian.jira.bc.JiraServiceContextImpl
import com.atlassian.jira.bc.project.ProjectService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.jql.parser.JqlQueryParser

if(!underlyingIssue) {
def TK = getFieldById(getFieldChanged())
def String TKVal = TK.getValue()

def jqlQueryParser = ComponentAccessor.getComponent(JqlQueryParser)

ProjectService projectService = ComponentAccessor.getComponent(ProjectService.class);
JiraServiceContext jiraServiceContext = new JiraServiceContextImpl(ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser())

if(TKVal.isEmpty())
{
TK.clearError()
} else {
if( projectService.isValidProjectKey( jiraServiceContext, TKVal ) == true )
{
TK.clearError()
}

/*
else if (projectService.isValidProjectKey( jiraServiceContext, TKVal ) == false &&
!jqlSearch("project=\"${TKVal}\""))
{

TK.setError("Project Key has already been taken.")
}

*/

else{

TK.setError("Project Key invalid")
}
}
}

 

I commented out this part of the script:

else if (projectService.isValidProjectKey( jiraServiceContext, TKVal ) == false &&
!jqlSearch("project=\"${TKVal}\""))
{

TK.setError("Project Key has already been taken.")
}

because it ends up breaking the code and the error message never gets pushed out. But when this is commented out, the behaviour seems to work as expected, where the error message "Project Key invalid' will pop up if a user enters a project key that has either already been taken or if it's in an invalid format.

I want to be able to send an error message indicating that the project key is already taken when cases of this happen. I though a jqlSearch method would do the trick, but either it doesn't or I'm missing something.

2.) The second part to this is that I also want to be able to create a validator for the Project Name. The jiraServiceContext class helps in providing the isValidProjectKey method to determine valid or invalid key value, but I'm hoping that there is a way to validate whether a Project Name (whatever value has been put for "Target Name") has already been used or not.

-------------

Any help or tips would be much apprecaited.

1 answer

1 accepted

0 votes
Answer accepted
Viswanathan Ramachandran
Contributor
January 25, 2022

Hi, There is gathering interest within Atlassian for this 

https://jira.atlassian.com/browse/JRASERVER-71636. So I’m afraid there isn’t any. 

Viswanathan Ramachandran
Contributor
January 25, 2022

 

For Jira Cloud, API is available to validate Jira Project Name(GET /rest/api/3/projectvalidate/validProjectName).

Suggest an answer

Log in or Sign up to answer