"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.announceTransformResults = void 0;
const editModes_1 = require("@atomist/automation-client/lib/operations/edit/editModes");
const messages_1 = require("../../misc/slack/messages");
/**
 * This is a useful function to pass to CodeTransformRegistration.onTransformResults.
 * It sends a message per repository describing any errors that occurred while saving the transforms,
 * and also where the transform was applied. It gives you the branch name.
 * @param trs results of transforms
 * @param cli original command invocation
 */
async function announceTransformResults(trs, cli) {
    const messages = trs.map(tr => {
        const projectId = tr.target.id;
        const title = `${projectId.owner}/${projectId.repo}`;
        if (tr.error) {
            return messages_1.slackErrorMessage(title, "Failure in " + cli.commandName + "\n```\n" + tr.error.message + "\n```", cli.context, {
                author_name: title,
                author_link: tr.target.id.url,
            });
        }
        if (tr.edited) {
            return messages_1.slackSuccessMessage(title, "Successfully applied " + cli.commandName, {
                fields: fromEditMode(tr.editMode),
                author_name: title,
                author_link: tr.target.id.url,
            });
        }
        return messages_1.slackInfoMessage(title, "No changes made by " + cli.commandName, {
            author_name: title,
            author_link: tr.target.id.url,
        });
    });
    await asyncForEach(messages, message => cli.addressChannels(message));
}
exports.announceTransformResults = announceTransformResults;
async function asyncForEach(array, fn) {
    return Promise.all(array.map(fn));
}
function fromEditMode(editMode) {
    if (!editMode) {
        return [];
    }
    const fields = [];
    if (editModes_1.isBranchCommit(editMode)) {
        fields.push({
            title: "branch",
            value: editMode.branch,
            short: false,
        });
    }
    if (editModes_1.isPullRequest(editMode)) {
        fields.push({
            title: "Pull Request title",
            value: editMode.title,
            short: false,
        });
        if (!!editMode.targetBranch) {
            fields.push({
                title: "target branch",
                value: editMode.targetBranch,
                short: false,
            });
        }
        if (!!editMode.autoMerge) {
            fields.push({
                title: "AutoMerge mode",
                value: editMode.autoMerge.mode,
                short: false,
            });
            if (!!editMode.autoMerge.method) {
                fields.push({
                    title: "AutoMerge method",
                    value: editMode.autoMerge.method,
                    short: false,
                });
            }
        }
    }
    return fields;
}
//# sourceMappingURL=onTransformResult.js.map