Skip to content

Instantly share code, notes, and snippets.

@chuikoaleksandr
Last active November 13, 2018 14:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuikoaleksandr/737c57d71c5e1565c6ba0db8031a54fa to your computer and use it in GitHub Desktop.
Save chuikoaleksandr/737c57d71c5e1565c6ba0db8031a54fa to your computer and use it in GitHub Desktop.
JIRA process approval. MyGroovy + Custom select. All process in 1 workflow status. https://atlasteam.ru/?p=1746
import com.atlassian.jira.component.ComponentAccessor
import groovy.json.JsonSlurper
cfStep = ComponentAccessor.customFieldManager.getCustomFieldObject(10003)//Text Field (single line)
cfCustomSelect = ComponentAccessor.customFieldManager.getCustomFieldObject(10004)//Custom Select List
def customSelectValue = issue.getCustomFieldValue(cfCustomSelect)
if(!customSelectValue){
return false
}
def processApproval = parseText(customSelectValue.getData())
def currentStepName = issue.getCustomFieldValue(cfStep)
def currentStep
if(currentStepName){
currentStep = processApproval.steps.find{it.name == currentStepName}
} else {
return false
}
def nextStep = getNextStep(currentStep, processApproval)
if(nextStep){
return false
}
return true
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 parseText(text) {
def jsonSlurper = new JsonSlurper()
return jsonSlurper.parseText(text)
}
import com.atlassian.jira.component.ComponentAccessor
cfNeedApproval = ComponentAccessor.customFieldManager.getCustomFieldObject(10001)//User Picker (multiple users)
return (issue.getCustomFieldValue(cfNeedApproval)?:[]).contains(currentUser)
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)
if(!customSelectValue){
return
}
/*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)
}
import com.atlassian.jira.component.ComponentAccessor
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)
issue.setCustomFieldValue(cfNeedApproval, null)
issue.setCustomFieldValue(cfApprovedBy, null)
issue.setCustomFieldValue(cfStep, null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment