Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,368,035
Community Members
 
Community Events
168
Community Groups

Automatically change a workflow transition based on checklist field (Behaviours)

Hey,

I have a simple three layer role approval based workflow and i have added a checklist field which populated the users based on other field. Now, the logic here is until and unless all the users in the checklist approves it, the transition is disabled and workflow remains same. This again makes a manual process to changing status from 'Pending'to 'Approved'. This seems  can be done using behaviors. Can someone share the behaviour script?

Thanks,

Swapan

1 answer

I did this using below Listener:

 

import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.atlassian.sal.api.ApplicationProperties
import com.atlassian.sal.api.UrlMode
import com.atlassian.jira.component.ComponentAccessor;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.customfields.option.Option;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.event.type.EventDispatchOption;

MutableIssue issue = event.getIssue() as MutableIssue;

def Approval = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_');
log.debug(issue.status.name);
if(issue.status.name.equals("CFV") && issue.issueType.name.equals("Change Control Request")){
    def cfType = Approval.getCustomFieldType();
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def checklistDoR = Approval.getValue(issue) as List
    if(checklistDoR?.size()!=0){
         if(isAllChecked(checklistDoR, issue)){
            log.debug("All options are checked,Trying to make the transition to 'statusname'")
            transitionToxyzstatus(issue);
        }else{
            log.debug("All options are not checked")
        }        
    } else {
       
}

boolean isAllChecked(List checklistDoR, MutableIssue issue){
    boolean flag=true;
    for (checklistItem in checklistDoR) {
        log.info checklistItem
        log.info checklistItem.toString()
        Boolean isChecked = invokeMethodByReflection(checklistItem, "isChecked")
        if(!isChecked){
            flag=false;
            autoAssign(checklistItem,issue);
            break;
        }
    }
    return flag;
}


void transitionToxyx(MutableIssue issue){
    def actionId = (define your status id);
    def user = ComponentAccessor.getUserManager().getUserByName("RestUser");
    def issueService = ComponentAccessor.getIssueService();
    def issueInputParams = ComponentAccessor.issueService.newIssueInputParameters();
    issueInputParams.setSkipScreenCheck(true);
    def transitionValidationResult = issueService.validateTransition(user,issue.id,actionId,issueInputParams);
    if(transitionValidationResult.isValid()){
        def transitionResult = issueService.transition(user, transitionValidationResult);
        if (!transitionResult.isValid()){
            log.warn transitionResult.errorCollection.errorMessages;
        }else{
            log.debug("Transition Sucessfull")
        }
       
    } else {
        log.warn transitionValidationResult.errorCollection.errorMessages;
    }
}

void autoAssign(def checklistItem,MutableIssue issue){
    if(event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == 'CF name}){
        String[] splits = checklistItem?.toString()?.split(' PM - ')
        if(splits.length>1){
            String dpm = splits[1];
           
            List<ApplicationUser> consumingPMs = ComponentAccessor.issueManager.getWatchersFor(issue);
            ApplicationUser nextAssignee = null;
            for(pm in cpm){
                if(pm.displayName.equals(dpm) && pm.id != issue.assignee.id){
                    nextAssignee = pm;
                }
            }
            if(nextAssignee){
                log.info "Next Assignee is:"+nextAssignee
                issue.assignee = nextAssignee;
                ComponentAccessor.issueManager.updateIssue(ComponentAccessor.getUserManager().getUserByName("RestUser"),issue,EventDispatchOption.DO_NOT_DISPATCH,false);
            }
        }
    }
}


Object invokeMethodByReflection(Object object, String methodName) {
 Method method;
 
 try {
 method = object.getClass().getMethod(methodName);
 } catch (SecurityException e) {
 // doSomething
 } catch (NoSuchMethodException e) {
 // doSomething
 }
 
 try {
 return method.invoke(object);
 } catch (IllegalArgumentException e) {
 // doSomething
 } catch (IllegalAccessException e) {
 // doSomething
 } catch (InvocationTargetException e) {
 // doSomething
 }
}

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events