UNPKG

2.69 kBPlain TextView Raw
1/*
2 * Copyright © 2019 Atomist, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {
18 buttonForCommand,
19 failure,
20 guid,
21 RepoRef,
22 Success,
23} from "@atomist/automation-client";
24import {
25 CommandHandlerRegistration,
26 CommandListener,
27 isDeployEnabled,
28} from "@atomist/sdm";
29import {
30 bold,
31 SlackMessage,
32} from "@atomist/slack-messages";
33import { SetDeployEnablementParameters } from "./SetDeployEnablement";
34
35const displayDeployEnablement: CommandListener<SetDeployEnablementParameters> =
36 async cli => {
37 const enabled = await isDeployEnabled({ context: cli.context, id: cli.parameters as any as RepoRef });
38 const msgId = guid();
39 return cli.context.messageClient.respond(
40 reportDeployEnablement(cli.parameters, enabled, msgId), { id: cli.parameters.msgId })
41 .then(() => Success, failure);
42 };
43
44export function reportDeployEnablement(params: SetDeployEnablementParameters,
45 enabled: boolean,
46 msgId: string): SlackMessage {
47 const text = `Deploy is currently ${enabled ? "enabled" : "disabled"} on ${bold(`${params.owner}/${params.repo}`)}`;
48 const actions =
49 [buttonForCommand({ text: enabled ? "Disable" : "Enable" },
50 enabled ? "DisableDeploy" : "EnableDeploy",
51 { ...params, msgId })];
52 const msg: SlackMessage = {
53 attachments: [{
54 author_icon: `https://images.atomist.com/rug/check-circle.gif?gif=${guid()}`,
55 author_name: "Deploy Enablement",
56 text,
57 fallback: text,
58 color: enabled ? "#37A745" : "#B5B5B5",
59 mrkdwn_in: ["text"],
60 actions,
61 footer: `${params.name}:${params.version}`,
62 }],
63 };
64 return msg;
65}
66
67export const DisplayDeployEnablement: CommandHandlerRegistration<SetDeployEnablementParameters> = {
68 name: "DisplayDeployEnablement",
69 description: "Display whether deployment via Atomist SDM in enabled",
70 intent: "is deploy enabled?",
71 paramsMaker: SetDeployEnablementParameters,
72 listener: displayDeployEnablement,
73};