Forums

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

Restrict Label system field

Emil Naghiyev May 14, 2025

In Project A - switching status to "Send To the Team" displays screen where user choses Project B and Label (system field) I need to do that if they choose labels beside B1, B2, and B3, Jira will not let them to submit the screen.

 

I picked transition to Send To the Team and added validator with Custom ScriptRunner. But seems it does not have affection. Jira shows that script works, but there is logic error somewhere. Maybe someone has successfully implemented this kind of validation?

 

Thank you in advance!

 

 

Here my script:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.label.Label
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

// Get the current issue
def issue = issue as MutableIssue
def labelManager = ComponentAccessor.getComponentOfType(com.atlassian.jira.issue.label.LabelManager)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Get all labels on the issue
def labelSet = labelManager.getLabels(issue.getId())
def labels = labelSet.collect { it.getLabel() }

// Required labels
def requiredLabels = ["B1", "B2", "B3"]

// Check if any required label is present
def hasRequiredLabel = false
for (String label : labels) {
    if (requiredLabels.contains(label)) {
        hasRequiredLabel = true
        break
    }
}

// If none of the required labels are present, revert the transition
if (!hasRequiredLabel) {
    // Get the original status ID (the status before this transition)
    def originalStatusId = issue.getStatusId()
   
    // Create a comment to explain why the transition was reverted
    def commentManager = ComponentAccessor.getCommentManager()
    commentManager.create(issue, user, "This issue was automatically moved back to its previous status because at least one of these labels must be present: B1, B2, or B3", false)
   
    // Move the issue back to previous status
    def previousStatus = ComponentAccessor.getConstantsManager().getStatus(originalStatusId)
    issue.setStatus(previousStatus)
    issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}

 

 

1 answer

0 votes
Alexander Hohmann
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.
May 15, 2025

Could you not use the 'Components' field instead of Label? Components are much easier to control and restrict project by project. You can set the project settings to allow only certain Components. And users cannot create new ones on the fly. Components are a bit like an adult version whereas Labels are like kindergarten.

Or you could provide a custom field like a dropdown list with only the allowed values, and then have a rule or scipt copy the contents into the Label field.

Suggest an answer

Log in or Sign up to answer