UNPKG

3.58 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright © 2019 Atomist, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.announceTransformResults = void 0;
19const editModes_1 = require("@atomist/automation-client/lib/operations/edit/editModes");
20const messages_1 = require("../../misc/slack/messages");
21/**
22 * This is a useful function to pass to CodeTransformRegistration.onTransformResults.
23 * It sends a message per repository describing any errors that occurred while saving the transforms,
24 * and also where the transform was applied. It gives you the branch name.
25 * @param trs results of transforms
26 * @param cli original command invocation
27 */
28async function announceTransformResults(trs, cli) {
29 const messages = trs.map(tr => {
30 const projectId = tr.target.id;
31 const title = `${projectId.owner}/${projectId.repo}`;
32 if (tr.error) {
33 return messages_1.slackErrorMessage(title, "Failure in " + cli.commandName + "\n```\n" + tr.error.message + "\n```", cli.context, {
34 author_name: title,
35 author_link: tr.target.id.url,
36 });
37 }
38 if (tr.edited) {
39 return messages_1.slackSuccessMessage(title, "Successfully applied " + cli.commandName, {
40 fields: fromEditMode(tr.editMode),
41 author_name: title,
42 author_link: tr.target.id.url,
43 });
44 }
45 return messages_1.slackInfoMessage(title, "No changes made by " + cli.commandName, {
46 author_name: title,
47 author_link: tr.target.id.url,
48 });
49 });
50 await asyncForEach(messages, message => cli.addressChannels(message));
51}
52exports.announceTransformResults = announceTransformResults;
53async function asyncForEach(array, fn) {
54 return Promise.all(array.map(fn));
55}
56function fromEditMode(editMode) {
57 if (!editMode) {
58 return [];
59 }
60 const fields = [];
61 if (editModes_1.isBranchCommit(editMode)) {
62 fields.push({
63 title: "branch",
64 value: editMode.branch,
65 short: false,
66 });
67 }
68 if (editModes_1.isPullRequest(editMode)) {
69 fields.push({
70 title: "Pull Request title",
71 value: editMode.title,
72 short: false,
73 });
74 if (!!editMode.targetBranch) {
75 fields.push({
76 title: "target branch",
77 value: editMode.targetBranch,
78 short: false,
79 });
80 }
81 if (!!editMode.autoMerge) {
82 fields.push({
83 title: "AutoMerge mode",
84 value: editMode.autoMerge.mode,
85 short: false,
86 });
87 if (!!editMode.autoMerge.method) {
88 fields.push({
89 title: "AutoMerge method",
90 value: editMode.autoMerge.method,
91 short: false,
92 });
93 }
94 }
95 }
96 return fields;
97}
98//# sourceMappingURL=onTransformResult.js.map
\No newline at end of file