-
Notifications
You must be signed in to change notification settings - Fork 3
/
deploymentFreeze.ts
101 lines (92 loc) · 3.15 KB
/
deploymentFreeze.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* Copyright © 2018 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.
*/
import { logger } from "@atomist/automation-client";
import {
allOf,
CommandHandlerRegistration,
executeSendMessageToSlack,
ExtensionPack,
MessageGoal,
metadata,
PushListenerInvocation,
PushTest,
} from "@atomist/sdm";
/**
* Goal to explain a deployment freeze to the user.
* Available after adding deploymentFreeze capability.
* @type {MessageGoal}
*/
export const ExplainDeploymentFreezeGoal = new MessageGoal("deploymentFreeze");
/**
* Implemented by objects that know how to persist deployment freeze status.
*/
export interface DeploymentStatusManager {
setFrozen(flag: boolean);
isFrozen: Promise<boolean>;
}
/**
* Capability to add to an SDM to add deployment freeze.
* Makes the ExplainDeploymentFreezeGoal available.
* @param {DeploymentStatusManager} dsm
* @return {ExtensionPack}
*/
export function deploymentFreeze(dsm: DeploymentStatusManager): ExtensionPack {
return {
...metadata("deployment-freeze"),
configure: sdm => {
sdm.addCommand(
freezeCommand(dsm))
.addCommand(unfreezeCommand(dsm));
sdm.addGoalImplementation("ExplainDeploymentFreezeGoal",
ExplainDeploymentFreezeGoal,
executeSendMessageToSlack("*Attention*: Not deploying as deployment is currently frozen :no_entry:"));
},
};
}
/**
* Return a push test working against the current DeploymentStatusManager.
* Use in SDMs that have enabled deploymentFreeze capability.
* @param {DeploymentStatusManager} dsm
* @return {PushTest}
*/
export function isDeploymentFrozen(dsm: DeploymentStatusManager): PushTest {
return allOf<PushListenerInvocation>(async pu => {
logger.info(`Delivery is frozen for '${pu.push.after.message}' = ${dsm.isFrozen}`);
return dsm.isFrozen;
});
}
function freezeCommand(dsm: DeploymentStatusManager): CommandHandlerRegistration {
return {
listener: async cli => {
dsm.setFrozen(true);
return cli.addressChannels("Deployment is frozen for all services :no_entry:");
},
name: "freeze",
description: "Freeze deployment",
intent: "freeze deployment",
};
}
function unfreezeCommand(dsm: DeploymentStatusManager): CommandHandlerRegistration {
return {
listener: async cli => {
dsm.setFrozen(false);
return cli.addressChannels("Deployment is re-enabled for all services :woman-running:");
},
name: "unfreeze",
description: "Unfreeze deployment",
intent: "unfreeze deployment",
};
}