UNPKG

@atomist/sdm-pack-fingerprints

Version:

an Atomist SDM Extension Pack for fingerprinting code

165 lines (164 loc) 7.54 kB
"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. */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); const automation_client_1 = require("@atomist/automation-client"); const sdm_1 = require("@atomist/sdm"); const slack_messages_1 = require("@atomist/slack-messages"); const preferences_1 = require("../adhoc/preferences"); const applyFingerprint_1 = require("../handlers/commands/applyFingerprint"); const updateTarget_1 = require("../handlers/commands/updateTarget"); const Aspects_1 = require("../machine/Aspects"); const messages_1 = require("../support/messages"); const util_1 = require("../support/util"); /** * Message for one case where a Fingerprint target is different from what's in the latest Push. * Offer two choices: 'apply' or 'change target' * * @param params * @param vote */ function oneFingerprint(params, vote) { return sdm_1.slackInfoMessage(util_1.orDefault(() => vote.summary.title, "Policy Update"), util_1.orDefault(() => vote.summary.description, vote.text), { actions: [ automation_client_1.buttonForCommand({ text: "Apply" }, applyFingerprint_1.ApplyTargetFingerprintName, { msgId: params.msgId, targetfingerprint: preferences_1.toName(vote.fpTarget.type, vote.fpTarget.name), title: messages_1.applyFingerprintTitle(vote.fpTarget, params.aspects), branch: vote.diff.branch, body: messages_1.prBody(vote, params.aspects), targets: { owner: vote.diff.owner, repo: vote.diff.repo, branch: vote.diff.branch, }, }), automation_client_1.buttonForCommand({ text: "Set Policy" }, updateTarget_1.UpdateTargetFingerprintName, { msgId: params.msgId, targetfingerprint: preferences_1.toName(vote.fpTarget.type, vote.fpTarget.name), sha: vote.fingerprint.sha, broadcast: true, }), ], }).attachments[0]; } exports.IgnoreCommandName = "IgnoreFingerprintDiff"; function ignoreCommand(aspects) { return { name: exports.IgnoreCommandName, parameters: { msgId: { required: false }, fingerprints: { required: false } }, listener: (i) => __awaiter(this, void 0, void 0, function* () { const fingerprints = i.parameters.fingerprints.split(",").map(f => { const { type, name } = preferences_1.fromName(f); const aspect = Aspects_1.aspectOf({ type }, aspects); if (!!aspect && !!aspect.toDisplayableFingerprintName) { return `${slack_messages_1.italic(aspect.displayName)} ${slack_messages_1.codeLine(aspect.toDisplayableFingerprintName(name))}`; } else { return slack_messages_1.codeLine(f); } }); const msg = sdm_1.slackInfoMessage("Policy Updates", `Dismissed policy updates for: ${fingerprints.join("\n")}`); // collapse the message yield i.addressChannels(msg, { id: i.parameters.msgId }); }), }; } exports.ignoreCommand = ignoreCommand; function ignoreButton(params) { return sdm_1.actionableButton({ text: "Dismiss" }, exports.IgnoreCommandName, { msgId: params.msgId, fingerprints: params.voteResults.failedVotes .map(vote => `${vote.fpTarget.type}::${vote.fpTarget.name}`).join(","), }); } /** */ function applyAll(params) { const fingerprints = params.voteResults.failedVotes.map(vote => { const aspect = Aspects_1.aspectOf({ type: vote.fpTarget.type }, params.aspects); if (!!aspect && !!aspect.toDisplayableFingerprintName) { return aspect.toDisplayableFingerprintName(vote.fpTarget.name); } return vote.fpTarget.name; }); return { color: "#D7B958", fallback: "Apply Policies", actions: [ automation_client_1.buttonForCommand({ text: "Apply All" }, applyFingerprint_1.ApplyAllFingerprintsName, { msgId: params.msgId, fingerprints: params.voteResults.failedVotes.map(vote => preferences_1.toName(vote.fpTarget.type, vote.fpTarget.name)).join(","), title: `Apply all of ${fingerprints.join(", ")}`, body: params.voteResults.failedVotes.map(v => messages_1.prBody(v, params.aspects)).join("\n---\n"), targets: { owner: params.coord.owner, repo: params.coord.repo, branch: params.coord.branch, }, }), ignoreButton(params), ], }; } /** * Default Message Maker for target fingerprint impact handler * * @param params */ exports.messageMaker = (params) => __awaiter(this, void 0, void 0, function* () { const message = { attachments: [ { text: `Differences from set policy detected on ${slack_messages_1.bold(`${params.coord.owner}/${params.coord.repo}/${params.coord.branch}`)}`, fallback: "Policy differences", }, ...params.voteResults.failedVotes.map(vote => oneFingerprint(params, vote)), ], }; if (params.voteResults.failedVotes.length > 1) { message.attachments.push(applyAll(params)); } else { const lastAttachment = message.attachments[message.attachments.length - 1]; lastAttachment.actions = [ ...(lastAttachment.actions || []), ignoreButton(params), ]; } message.attachments[message.attachments.length - 1].footer = sdm_1.slackFooter(); message.attachments[message.attachments.length - 1].ts = sdm_1.slackTs(); if (!!params.channel) { return params.ctx.messageClient.send(message, yield automation_client_1.addressSlackChannelsFromContext(params.ctx, params.channel), // {id: params.msgId} if you want to update messages if the target goal has not changed { id: params.msgId }); } else { return params.ctx.messageClient.send(message, automation_client_1.addressWeb(), // {id: params.msgId} if you want to update messages if the target goal has not changed { id: params.msgId }); } }); //# sourceMappingURL=messageMaker.js.map