<commit_before>package uk.org.sappho.jira.workflow.approvals;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.opensymphony.workflow.WorkflowException;

public class ApproveAction extends DecideAction {

    private static final Logger log = Logger.getLogger(ApproveAction.class);

    @SuppressWarnings("unchecked")
    @Override
    protected void bumpWorkflow(MutableIssue approvalIssue, Map params) throws WorkflowException {

        ApprovalsConfiguration approvalsConfiguration = ApprovalsConfiguration.getInstance();
        MutableIssue parentIssue = (MutableIssue) approvalIssue.getParentObject();
        String project = parentIssue.getProjectObject().getKey();
        String approvalType = (String) params.get(ApprovalTypeFactory.approvalTypeKey);
        boolean isApproved = true;
        for (MutableIssue subTask : parentIssue.getSubTaskObjects())
            if (approvalsConfiguration.isIssueType(project, approvalType, subTask.getIssueTypeObject().getName())
                    && !approvalsConfiguration.isRegexMatch(project, "statuses.approved", subTask.getStatusObject()
                            .getName())) {
                isApproved = false;
                break;
            }
        if (isApproved) {
            List<String> workflowNames =
                    approvalsConfiguration.getPropertyList(project, "auto.transition.workflow." + approvalType);
            List<String> workflowActions =
                    approvalsConfiguration.getPropertyList(project, "auto.transition.action." + approvalType);
            ComponentManager componentManager = ComponentManager.getInstance();
            String workflowName = componentManager.getWorkflowManager().getWorkflow(parentIssue).getName();
            int transitionActionId = Integer.parseInt(approvalsConfiguration.getProperty(project,
                    "auto.transition.action." + approvalType, "-999"));
            log.warn("Running workflow transition action id " + transitionActionId + " on " + parentIssue.getKey());
            WorkflowTransitionUtil workflowTransitionUtil = JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
            workflowTransitionUtil.setIssue(parentIssue);
            workflowTransitionUtil.setUsername(componentManager.getJiraAuthenticationContext().getUser().getName());
            workflowTransitionUtil.setAction(transitionActionId);
            ErrorCollection errors = workflowTransitionUtil.validate();
            if (errors.hasAnyErrors())
                throw new WorkflowException("Unable to validate transition " + parentIssue.getKey() + "! Caused by "
                        + errors);
            errors = workflowTransitionUtil.progress();<commit_msg>SAPPHO-1: Link configured workflow actions to actual workflows used by parent issue.<commit_after>package uk.org.sappho.jira.workflow.approvals;

import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.util.ErrorCollection;
import com.atlassian.jira.util.JiraUtils;
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.opensymphony.workflow.WorkflowException;

public class ApproveAction extends DecideAction {

    private static final Logger log = Logger.getLogger(ApproveAction.class);

    @SuppressWarnings("unchecked")
    @Override
    protected void bumpWorkflow(MutableIssue approvalIssue, Map params) throws WorkflowException {

        ApprovalsConfiguration approvalsConfiguration = ApprovalsConfiguration.getInstance();
        MutableIssue parentIssue = (MutableIssue) approvalIssue.getParentObject();
        String project = parentIssue.getProjectObject().getKey();
        String approvalType = (String) params.get(ApprovalTypeFactory.approvalTypeKey);
        boolean isApproved = true;
        for (MutableIssue subTask : parentIssue.getSubTaskObjects())
            if (approvalsConfiguration.isIssueType(project, approvalType, subTask.getIssueTypeObject().getName())
                    && !approvalsConfiguration.isRegexMatch(project, "statuses.approved", subTask.getStatusObject()
                            .getName())) {
                isApproved = false;
                break;
            }
        if (isApproved) {
            List<String> workflowNames =
                    approvalsConfiguration.getPropertyList(project, "auto.transition.workflow.name." + approvalType);
            List<String> workflowActions =
                    approvalsConfiguration.getPropertyList(project, "auto.transition.workflow.action.id."
                            + approvalType);
            ComponentManager componentManager = ComponentManager.getInstance();
            String issueWorkflowName = componentManager.getWorkflowManager().getWorkflow(parentIssue).getName();
            log.warn("Found workflow " + issueWorkflowName + " on " + parentIssue.getKey());
            int transitionActionId = 0;
            boolean workflowFound = false;
            for (String workflowName : workflowNames) {
                if (workflowName.equals(issueWorkflowName)) {
                    try {
                        transitionActionId = Integer.parseInt(workflowActions.get(transitionActionId));
                    } catch (Throwable t) {
                        break;
                    }
                    workflowFound = true;
                    break;
                }
                transitionActionId++;
            }
            if (!workflowFound)
                throw new WorkflowException("Auto-transition for workflow " + issueWorkflowName + " and approval type "
                        + approvalType + " is not correctly configured");
            log.warn("Running workflow transition action id " + transitionActionId + " on " + parentIssue.getKey());
            WorkflowTransitionUtil workflowTransitionUtil = JiraUtils.loadComponent(WorkflowTransitionUtilImpl.class);
            workflowTransitionUtil.setIssue(parentIssue);
            workflowTransitionUtil.setUsername(componentManager.getJiraAuthenticationContext().getUser().getName());
            workflowTransitionUtil.setAction(transitionActionId);
            ErrorCollection errors = workflowTransitionUtil.validate();
            if (errors.hasAnyErrors())
                throw new WorkflowException("Unable to validate transition " + parentIssue.getKey() + "! Caused by "
                        + errors);
            errors = workflowTransitionUtil.progress();
{'fraction_non_alphanumeric': 0.08737584568878652, 'fraction_numerical': 0.0010076291924571758, 'mean_word_length': 2.9343148357870894, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7060781', 'n_tokens_mistral': 1719, 'n_tokens_neox': 1626, 'n_words': 308}