Conditions 'translated' into groovy script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager

// Get the current user
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
// Get the changed field
def exampleField = getFieldById(getFieldChanged())

if (currentUser == underlyingIssue.reporterUser && currentUser == underlyingIssue.assignee) { (1)
    exampleField.setHidden(true)
} else {
    exampleField.setHidden(false)
}

if (ComponentAccessor.getGroupManager().getGroupsForUser(currentUser)?.find { it.name == "jira-administrators" }) { (2)
    exampleField.setAllowInlineEdit(true)
} else {
    exampleField.setAllowInlineEdit(false)
}

def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def devProjectRole = projectRoleManager.getProjectRole("Developers")
if (currentUser.key == "luke" || projectRoleManager.isUserInProjectRole(currentUser, devProjectRole, underlyingIssue.getProjectObject())) { (3)
    exampleField.setError("You are either Luke or you belong to Developers project role and you cannot edit the field")
    exampleField.setReadOnly(true)
} else {
    exampleField.clearError()
    exampleField.setReadOnly(false)
}

if (currentUser == underlyingIssue.projectObject.getProjectLead()) { (4)
    exampleField.setDescription("Current User is project lead")
} else {
    exampleField.clearHelpText()
}

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Multi User Picker CF")
if (currentUser == underlyingIssue.getCustomFieldValue(cf) || currentUser in underlyingIssue.getCustomFieldValue(cf)) { (5)
    getFieldById("description").setFormValue("Current user is member of the Multi User Picker custom field")
} else {
    getFieldById("description").setFormValue("Current user is not member of the Multi User Picker custom field")
}

def cf2 = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Single Group Picker CF")
def groupsToCompare = underlyingIssue.getCustomFieldValue(cf2) as ArrayList
if (ComponentAccessor.getGroupManager().getGroupsForUser(currentUser.key)?.disjoint(groupsToCompare)) { (6)
    exampleField.setRequired(true)
} else {
    exampleField.setRequired(false)
}

if (getActionName() == "Start Progress" || getDestinationStepName() == "Done") { (7)
    exampleField.setRequired(true)
} else {
    exampleField.setRequired(false)
}
1 If current user is reporter AND current user is current assignee make the changed field hidden, else shown
2 If current user in group 'jira-administrators' allow the inline edit of the changed field
3 If current user is 'luke' or current user in project role 'Developers' make the changed field read-only and show an error message, else clear the error and make the field editable again
4 If current user is Project Lead add a description at the changed field, else clear the description
5 If current user in user custom field value add a default value in the description
6 If current user in group custom field value then make the changed field required, else optional.
7 If workflow Action is 'Start Progress' or workflow Step is 'In Progress' then make the changed field required, else optional.

Have questions? Visit the Atlassian Community to connect, share, and learn with other Atlassian users and experts, including Adaptavist staff.

Ask a question about ScriptRunner for JIRA, Bitbucket Server, or Confluence.

Want to learn more? Check out courses on Adaptavist Learn, an online platform to onboard and train new users for Atlassian solutions.