"use strict";
/*
 * Copyright © 2020 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.sdmGoalStateToGitHubStatusState = exports.setGitHubStatusOnGoalCompletion = exports.createPendingGitHubStatusOnGoalSet = void 0;
const logger_1 = require("@atomist/automation-client/lib/util/logger");
const ghub_1 = require("../../core/util/github/ghub");
const types_1 = require("../../typings/types");
function createPendingGitHubStatusOnGoalSet(sdm) {
    return async (inv) => {
        const { id, credentials } = inv;
        if (inv.goalSet && inv.goalSet.goals && inv.goalSet.goals.length > 0) {
            return ghub_1.createStatus(credentials, id, {
                context: context(sdm),
                description: `${prefix(sdm)} in progress`,
                target_url: link(inv, inv.context),
                state: "pending",
            });
        }
        else {
            return Promise.resolve();
        }
    };
}
exports.createPendingGitHubStatusOnGoalSet = createPendingGitHubStatusOnGoalSet;
function setGitHubStatusOnGoalCompletion(sdm) {
    return async (inv) => {
        const { id, completedGoal, allGoals, credentials } = inv;
        if (completedGoal.state === "failure") {
            logger_1.logger.debug("Setting GitHub status to failed on %s", id.sha);
            return ghub_1.createStatus(credentials, id, {
                context: context(sdm),
                description: `${prefix(sdm)}: ${completedGoal.description}`,
                target_url: link(inv.completedGoal, inv.context),
                state: "failure",
            });
        }
        if (allSuccessful(allGoals)) {
            logger_1.logger.debug("Setting GitHub status to success on %s", id.sha);
            return ghub_1.createStatus(credentials, id, {
                context: context(sdm),
                description: `${prefix(sdm)}: all succeeded`,
                target_url: link(inv.completedGoal, inv.context),
                state: "success",
            });
        }
        return;
    };
}
exports.setGitHubStatusOnGoalCompletion = setGitHubStatusOnGoalCompletion;
function allSuccessful(goals) {
    return !goals.some(g => g.state !== "success");
}
function sdmGoalStateToGitHubStatusState(goalState) {
    switch (goalState) {
        case types_1.SdmGoalState.planned:
        case types_1.SdmGoalState.requested:
        case types_1.SdmGoalState.in_process:
        case types_1.SdmGoalState.waiting_for_approval:
        case types_1.SdmGoalState.waiting_for_pre_approval:
        case types_1.SdmGoalState.approved:
        case types_1.SdmGoalState.pre_approved:
            return "pending";
        case types_1.SdmGoalState.success:
            return "success";
        case types_1.SdmGoalState.failure:
        case types_1.SdmGoalState.skipped:
        case types_1.SdmGoalState.stopped:
        case types_1.SdmGoalState.canceled:
            return "failure";
        default:
            throw new Error("Unknown goal state " + goalState);
    }
}
exports.sdmGoalStateToGitHubStatusState = sdmGoalStateToGitHubStatusState;
function prefix(sdm) {
    return sdm.name && sdm.name.length > 0 ? `${sdm.name} goals` : "Atomist SDM goals";
}
function context(sdm) {
    return `sdm/${sdm.configuration.name.replace("@", "")}`;
}
function link(event, ctx) {
    return `https://app.atomist.com/workspace/${ctx.workspaceId}/goalset/${event.goalSetId}`;
}
//# sourceMappingURL=statusSetters.js.map