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?
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)
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.