Forums

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

Epic name should be same like other epic in the project

Shubhanshu Trivedi February 7, 2022

Hello Team,

Currently, I have noticed that I can create an epic issue type with the same epic name across all the projects.

I want to restrict the same. I want each user to give an epic name differently for each epic issue type.  Jira should not create a duplicate epic if any other epic has the same name epic exist in the project

Can somebody help to restrict the same?

Thanks in Advance.

Shubhanshu

1 answer

1 accepted

1 vote
Answer accepted
Fabio Racobaldo _Herzum_
Community Champion
February 7, 2022

Hi @Shubhanshu Trivedi ,

you can do that using a validator on create issue workflow event.

ScriptRunner allows you to add a scripted validator with that behaviour.

Hope this helps,

Fabio

Shubhanshu Trivedi February 7, 2022

Hello @Fabio Racobaldo _Herzum_ ,

Do you have written any such validator script which I use in my project?

Regards,

Shubhanshu Trivedi

Fabio Racobaldo _Herzum_
Community Champion
February 7, 2022

Hi @Shubhanshu Trivedi ,

 

try this scripted validator :

import java.util.List;

import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.search.SearchException;
import com.atlassian.jira.issue.search.SearchResults;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.web.bean.PagerFilter;
import com.opensymphony.workflow.InvalidInputException;


SearchService searchService = ComponentAccessor.getComponent(SearchService.class);
JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField epicNameCF = (customFieldManager.getCustomFieldObjectsByName("Epic Name")).iterator().next();

String currentEpicName = issue.getCustomFieldValue(epicNameCF).toString();

String query = "project = "+issue.getProjectObject().getKey()+" AND \"Epic Name\" = \""+currentEpicName+"\"";
SearchService.ParseResult parseResult = searchService.parseQuery(authContext.getLoggedInUser(), query);

 

try {
SearchResults results = searchService.search(authContext.getLoggedInUser(), parseResult.getQuery(),
PagerFilter.getUnlimitedFilter());
List<Issue> epics = results.getResults();
if(epics.size()>0){
throw new InvalidInputException("Epic Name already existing!");
}
} catch (SearchException e) {
throw new InvalidInputException("Epic Name already existing!");
}

Please let me know if it works for you.

You can also change the error message at your convenience.

Fabio

Like Shubhanshu Trivedi likes this
Shubhanshu Trivedi February 7, 2022

Hello @Fabio Racobaldo _Herzum_ ,

Thanks a lot. The above code worked successfully in the project. I need one more suggestion from you.

Similar way we have such a script where we are creating a sub-task and updating a summary for the sub-tasks issue.

For example- 

if Parent JIRA Summary = Parent-Task Summary The subtask has Variant=XYZ, Type = ABC, Sub-Task Summary field = Testing123

then the Subtask Summary should be : [XYZ][ABC] - Parent-Task Testing123

Code is like--

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger

Issue issue = issue
Logger logger = log

if (issue.isSubTask()) {
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField variantCF = customFieldManager.getCustomFieldObjectsByName("Variant").first()
CustomField typeCF = customFieldManager.getCustomFieldObjectsByName("Type").first()


List<Option> variant = issue.getCustomFieldValue(variantCF)
StringBuilder variantString = new StringBuilder("[")
variant?.each {
variantString.append("${it.getValue()},")
}

if(variantString.toString().trim().endsWith(",")){
String temp = variantString.toString()
variantString.replace(temp.lastIndexOf(","), temp.lastIndexOf(",") + 1, "" );
}
variantString.append("]")

String summary = variantString.toString()

Option type = issue.getCustomFieldValue(typeCF)
if(type){
summary += "[${type.getValue()}]"
}

logger.info(issue.getParentObject().getKey())
Issue parentIssue = issue.getParentObject()
summary += " - ${parentIssue.getSummary()} "

logger.info(summary)
issue.summary = summary
updateIssue(issue)
}

void updateIssue(Issue issue) {
MutableIssue issueToUpdate = (MutableIssue) issue
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Above code is giving me output - [XYZ][ABC] - Parent-Task

not this 

[XYZ][ABC] - Parent-Task Testing123

can you please help me to find the issue in the above code?

Regards,

Shubhanshu

Fabio Racobaldo _Herzum_
Community Champion
February 7, 2022

Hi @Shubhanshu Trivedi ,

when your script runs, probably parentObject link is not set yet. Please, move that post function after event Issue create originally and try again (you can update post functions orders). 

Fabio

Shubhanshu Trivedi February 7, 2022

Hello @Fabio Racobaldo _Herzum_ ,

I already tested the same. I am getting the same summary in the sub-task issue like [XYZ][ABC] - Parent-Task.

Shubhanshu

Fabio Racobaldo _Herzum_
Community Champion
February 7, 2022

Try this code :

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger

Issue issue = issue
Logger logger = log

if (issue.isSubTask()) {
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField variantCF = customFieldManager.getCustomFieldObjectsByName("Variant").first()
CustomField typeCF = customFieldManager.getCustomFieldObjectsByName("Type").first()


List<Option> variant = issue.getCustomFieldValue(variantCF)
StringBuilder variantString = new StringBuilder("[")
variant?.each {
variantString.append("${it.getValue()},")
}

if(variantString.toString().trim().endsWith(",")){
String temp = variantString.toString()
variantString.replace(temp.lastIndexOf(","), temp.lastIndexOf(",") + 1, "" );
}
variantString.append("]")

String summary = variantString.toString()

Option type = issue.getCustomFieldValue(typeCF)
if(type){
summary += "[${type.getValue()}]"
}

logger.info(issue.getParentObject().getKey())
Issue parentIssue = issue.getParentObject()
summary += " - ${parentIssue.getSummary()} "

logger.info(summary)
MutableIssue mIssue = (MutableIssue) issue;
mIssue.setSummary(summary);
updateIssue(issue)
}

void updateIssue(MutableIssue issueToUpdate) {
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueManager issueManager = ComponentAccessor.getIssueManager()
issueManager.updateIssue(user, issueToUpdate, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Shubhanshu Trivedi February 7, 2022

No @Fabio Racobaldo _Herzum_  It didn't work.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.13.13
TAGS
AUG Leaders

Atlassian Community Events