"use strict";
/*
 * Copyright © 2019 Atomist, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
Object.defineProperty(exports, "__esModule", { value: true });
exports.executePushImpact = void 0;
const HandlerResult_1 = require("@atomist/automation-client/lib/HandlerResult");
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const PushImpactListenerRegistration_1 = require("../../api/registration/PushImpactListenerRegistration");
const minimalClone_1 = require("../goal/minimalClone");
const createPushImpactListenerInvocation_1 = require("./createPushImpactListenerInvocation");
const relevantCodeActions_1 = require("./relevantCodeActions");
/**
 * Execute arbitrary code reactions against a codebase
 * @param {PushImpactListenerRegistration[]} registrations
 * @return {ExecuteGoal}
 */
function executePushImpact(registrations) {
    return async (goalInvocation) => {
        if (registrations.length === 0) {
            return HandlerResult_1.Success;
        }
        const { goalEvent, configuration, credentials, id, context } = goalInvocation;
        return configuration.sdm.projectLoader.doWithProject({
            credentials,
            id,
            context,
            readOnly: true,
            cloneOptions: minimalClone_1.minimalClone(goalEvent.push, { detachHead: true }),
        }, async (project) => {
            const cri = await createPushImpactListenerInvocation_1.createPushImpactListenerInvocation(goalInvocation, project);
            const regs = registrations.map(PushImpactListenerRegistration_1.toPushReactionRegistration);
            const relevantCodeReactions = await relevantCodeActions_1.relevantCodeActions(regs, cri);
            logger_1.logger.debug("Will invoke %d eligible code reactions of %d to %j: [%s] of [%s]", relevantCodeReactions.length, registrations.length, cri.id, relevantCodeReactions.map(a => a.name).join(), regs.map(a => a.name).join());
            const allReactions = await Promise.all(relevantCodeReactions
                .map(reactionReg => reactionReg.action(cri)));
            const result = {
                code: allReactions.includes(PushImpactListenerRegistration_1.PushImpactResponse.failGoals) ? 1 : 0,
                requireApproval: allReactions.includes(PushImpactListenerRegistration_1.PushImpactResponse.requireApprovalToProceed),
            };
            logger_1.logger.debug("PushReaction responses are %j, result=%j", allReactions, result);
            return result;
        });
    };
}
exports.executePushImpact = executePushImpact;
//# sourceMappingURL=executePushImpact.js.map