Last active
April 2, 2019 17:02
-
-
Save evanwinter/fe01b1969dbe4d22c587f7f226e1954d to your computer and use it in GitHub Desktop.
Adds initial Reporter as a Participant, and sets the user in custom field "Primary User" as the new Reporter. For use in JIRA Service Desk.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.atlassian.jira.component.ComponentAccessor | |
import com.atlassian.jira.event.type.EventDispatchOption | |
import com.atlassian.jira.issue.CustomFieldManager | |
import com.atlassian.jira.issue.MutableIssue | |
import com.atlassian.jira.issue.IssueManager | |
import java.util.ArrayList | |
import com.atlassian.jira.user.ApplicationUser | |
import org.apache.log4j.Logger | |
def log = Logger.getLogger("com.acme.XXX") | |
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() | |
def REQUEST_PARTICIPANTS_FIELD = "customfield_10600" | |
def PRIMARY_USER_FIELD = "customfield_10913" | |
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager() | |
IssueManager issueManager = ComponentAccessor.getIssueManager() | |
MutableIssue myIssue = issue | |
// For testing in the Script Console, hard-code an issue object. | |
// MutableIssue myIssue = issueManager.getIssueObject("ATW-37") | |
// Get custom field objects. | |
def requestParticipantsField = customFieldManager.getCustomFieldObject(REQUEST_PARTICIPANTS_FIELD) | |
def primaryUserField = customFieldManager.getCustomFieldObject(PRIMARY_USER_FIELD) | |
// Get initial Reporter. | |
ApplicationUser reporter = myIssue.getReporter() | |
// Add initial Reporter to array. | |
ArrayList<ApplicationUser> participants = [] | |
participants.add(reporter) | |
// Get initial Primary User. | |
ApplicationUser primaryUser = myIssue.getCustomFieldValue(primaryUserField) as ApplicationUser | |
if (primaryUser) { | |
// Update the Request Participants field. | |
myIssue.setCustomFieldValue( | |
requestParticipantsField, | |
participants | |
) | |
// Update the Reporter field. | |
myIssue.setReporter(primaryUser) | |
// Clear the Primary User field. | |
myIssue.setCustomFieldValue( | |
primaryUserField, | |
null | |
) | |
// Store to database. | |
try { | |
issueManager.updateIssue( | |
currentUser, | |
myIssue, | |
EventDispatchOption.ISSUE_UPDATED, | |
false | |
) | |
} catch (Exception e) { | |
log.debug "Exception: " + e | |
} | |
} else { | |
log.debug "No Primary User specified." | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment