Dear Team,
As a part of process improvement, we are planning to instruct all the project team to use the pre-defined releases versions(fixversions,affect versions) in a specified format.To achieve this we are trying to block the project admin roles users to block the creation of new versions(fixversions,affect versions) while creating an issue in a project and updating any existing issue.
Written the the below script and placed in listeners but always allowing the user who is in project admin roles of a project is able to create the new releases.
Below is the script and please help me if there is any logic error in the below script.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.event.type.EventDispatchOption
def log = log // Use the log object provided by ScriptRunner
def issue = event.issue as MutableIssue
def user = event.user
def project = issue.getProjectObject()
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
// Check if the user is in the project admin role for this project
def projectAdminRole = projectRoleManager.getProjectRole("Administrators")
def isUserInProjectAdminRole = projectAdminRole && projectRoleManager.isUserInProjectRole(user, projectAdminRole, project)
if (isUserInProjectAdminRole) {
log.warn("Blocking field updates for project admins.")
// Block updates to fixversions field, including adding new versions
def currentFixVersions = issue.getFixVersions()
issue.setFixVersions(currentFixVersions)
log.warn("fixversions field has been blocked from update for project admins. Current fix versions: ${currentFixVersions}")
// Block updates to affected versions field, including adding new versions
def currentAffectedVersions = issue.getAffectedVersions()
issue.setAffectedVersions(currentAffectedVersions)
log.warn("affected versions field has been blocked from update for project admins. Current affected versions: ${currentAffectedVersions}")
// Save the issue without triggering another update event
ComponentAccessor.getIssueManager().updateIssue(
user, issue, EventDispatchOption.DO_NOT_DISPATCH, false
)
} else {
log.info("Allowing field updates for non-project admins.")
}
Thanks and Regards,
Ramesh