UNPKG

3.4 kBPlain TextView Raw
1/*
2 * Copyright © 2018 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 addressEvent,
19 failure,
20 HandlerError,
21 HandlerResult,
22 MappedParameter,
23 MappedParameters,
24 Parameter,
25 Parameters,
26 Success,
27 Value,
28} from "@atomist/automation-client";
29import {
30 CommandHandlerRegistration,
31 CommandListenerInvocation,
32 slackSuccessMessage,
33} from "@atomist/sdm";
34import { bold } from "@atomist/slack-messages";
35import {
36 DeployEnablementRootType,
37 SdmDeployEnablement,
38} from "../../ingesters/sdmDeployEnablement";
39
40@Parameters()
41export class SetDeployEnablementParameters {
42
43 @Parameter({ required: false, displayable: false })
44 public msgId?: string;
45
46 @MappedParameter(MappedParameters.GitHubOwner)
47 public owner: string;
48
49 @MappedParameter(MappedParameters.GitHubRepository)
50 public repo: string;
51
52 @MappedParameter(MappedParameters.GitHubRepositoryProvider)
53 public providerId: string;
54
55 @Value("name")
56 public name: string;
57
58 @Value("version")
59 public version: string;
60}
61
62/**
63 * Command to set deploy enablement on the currently mapped repo
64 * @param {CommandListenerInvocation} cli
65 * @param {boolean} enable
66 * @returns {Promise<HandlerResult | HandlerError>}
67 */
68export function setDeployEnablement(cli: CommandListenerInvocation<SetDeployEnablementParameters>,
69 enable: boolean): Promise<HandlerResult | HandlerError> {
70 const deployEnablement: SdmDeployEnablement = {
71 state: enable ? "requested" : "disabled",
72 owner: cli.parameters.owner,
73 repo: cli.parameters.repo,
74 providerId: cli.parameters.providerId,
75 };
76 return cli.context.messageClient.send(deployEnablement, addressEvent(DeployEnablementRootType))
77 .then(() => cli.context.messageClient.respond(
78 slackSuccessMessage(
79 "Deploy Enablement",
80 `Successfully ${enable ? "enabled" : "disabled"} deployment of ${
81 bold(`${cli.parameters.owner}/${cli.parameters.repo}`)}`,
82 {
83 footer: `${cli.parameters.name}:${cli.parameters.version}`,
84 }), { id: cli.parameters.msgId }))
85 .then(() => Success, failure);
86}
87
88export const EnableDeploy: CommandHandlerRegistration<SetDeployEnablementParameters> = {
89 name: "EnableDeploy",
90 intent: "enable deploy",
91 description: "Enable deployment via Atomist SDM",
92 paramsMaker: SetDeployEnablementParameters,
93 listener: async cli => setDeployEnablement(cli, true),
94};
95
96export const DisableDeploy: CommandHandlerRegistration<SetDeployEnablementParameters> = {
97 name: "DisableDeploy",
98 intent: "disable deploy",
99 description: "Disable deployment via Atomist SDM",
100 paramsMaker: SetDeployEnablementParameters,
101 listener: async cli => setDeployEnablement(cli, false),
102};
103
\No newline at end of file