Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JIRA process approval. MyGroovy + Custom select. All process in 1 workflow status.
import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonSlurper
cfNeedApproval = ComponentAccessor.customFieldManager.getCustomFieldObject(10001)//User Picker (multiple users)
cfApprovedBy = ComponentAccessor.customFieldManager.getCustomFieldObject(10002)//User Picker (multiple users)
cfStep = ComponentAccessor.customFieldManager.getCustomFieldObject(10003)//Text Field (single line)
cfCustomSelect = ComponentAccessor.customFieldManager.getCustomFieldObject(10004)//Custom Select List https://marketplace.atlassian.com/apps/1218105/custom-select-list
def customSelectValue = issue.getCustomFieldValue(cfCustomSelect)
/*get data from custom select option:
{
"steps": [{
"name": "step1",
"approvers": ["user1"]
},
{
"name": "step2",
"approvers": ["user2", "user3"]
},
{
"name": "step3",
"approvers": ["user4"]
},
{
"name": "done"
}
]
}
*/
def processApproval = parseText(customSelectValue.getData())
def currentStepName = issue.getCustomFieldValue(cfStep)//get step from data
def currentStep
if(currentStepName){
currentStep = processApproval.steps.find{it.name == currentStepName}
} else {
initStep(processApproval.steps.first())
return
}
//set approve
def approvedBy = issue.getCustomFieldValue(cfApprovedBy) ?: []
approvedBy += currentUser
issue.setCustomFieldValue(cfApprovedBy, approvedBy)
def needApproval = issue.getCustomFieldValue(cfNeedApproval)?:[]
needApproval -= currentUser
issue.setCustomFieldValue(cfNeedApproval, needApproval)
def nextStep = getNextStep(currentStep, processApproval)
if(nextStep){
initStep(nextStep)
} else {
//todo set customField/transition... end.
}
def initStep(step){
issue.setCustomFieldValue(cfStep, step.name)
issue.setCustomFieldValue(cfNeedApproval, step.approvers.collect{getUserByName(it)})
}
def getNextStep(currentStep, processApproval){
int indexCurrentStep = processApproval.steps.lastIndexOf(currentStep)
int nextIndex = indexCurrentStep + 1
if(nextIndex >= processApproval.steps.size()){
return null
} else {
return processApproval.steps.get(nextIndex)
}
}
def getUserByName(name){
return ComponentAccessor.getUserManager().getUserByName(name)
}
def parseText(text) {
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(text)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.