This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
codepipeline.ts
151 lines (143 loc) · 4 KB
/
codepipeline.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import { MessageAttachment, WebClient } from '@slack/web-api';
import {
CodePipelineState,
CodePipelineStageState,
CodePipelineActionState,
CodePipelineCloudWatchStageEvent,
CodePipelineCloudWatchActionEvent,
CodePipelineCloudWatchEvent,
CodePipelineCloudWatchPipelineEvent,
} from 'aws-lambda';
import {
Channel,
findMessageForId,
MessageResult,
updateOrAddAttachment,
} from './slack';
const stateColors: {
[K in
| CodePipelineState
| CodePipelineStageState
| CodePipelineActionState]: string;
} = {
CANCELED: 'danger',
FAILED: 'danger',
RESUMED: '#439FE0',
STARTED: '#439FE0',
SUCCEEDED: 'good',
SUPERSEDED: 'warning',
};
const stateText: {
[K in
| CodePipelineState
| CodePipelineStageState
| CodePipelineActionState]: string;
} = {
CANCELED: ':no_entry: cancelled',
FAILED: ':x: failed',
RESUMED: ':building_construction: resumed',
STARTED: ':building_construction: started',
SUCCEEDED: ':white_check_mark: succeeded',
SUPERSEDED: ':x: superseded',
};
// Create Pipeline attachment
export const pipelineAttachment = (
event: CodePipelineCloudWatchPipelineEvent,
): MessageAttachment => {
return {
color: stateColors[event.detail.state],
fallback: `Pipeline ${event.detail.pipeline} ${event.detail.state}`,
footer: event.detail['execution-id'],
text: stateText[event.detail.state],
title: `Pipeline ${event.detail.pipeline}`,
};
};
// Create Stage attachment
export const stageAttachment = (
event: CodePipelineCloudWatchStageEvent,
): MessageAttachment => {
return {
color: stateColors[event.detail.state],
fallback: `Stage ${event.detail.stage} ${event.detail.state}`,
text: stateText[event.detail.state],
title: `Stage ${event.detail.stage}`,
};
};
// Create Action attachment
export const actionAttachment = (
event: CodePipelineCloudWatchActionEvent,
): MessageAttachment => {
return {
color: stateColors[event.detail.state],
fallback: `Stage ${event.detail.stage} ${event.detail.state}`,
text: `${stateText[event.detail.state]} (${event.detail.action})`,
title: `Stage ${event.detail.stage}`,
};
};
// Event handler
export const handleCodePipelineEvent = async (
event: CodePipelineCloudWatchEvent,
slack: WebClient,
channel: Channel,
): Promise<MessageResult | void> => {
const message = await findMessageForId(
slack,
channel.id,
event.detail['execution-id'],
);
switch (event['detail-type']) {
case 'CodePipeline Pipeline Execution State Change': {
const pAttachment = pipelineAttachment(event);
if (message) {
return slack.chat.update({
attachments: updateOrAddAttachment(
message.attachments,
a => a.title === pAttachment.title,
pAttachment,
),
channel: channel.id,
text: '',
ts: message.ts,
}) as Promise<MessageResult>;
}
return slack.chat.postMessage({
attachments: [pipelineAttachment(event)],
channel: channel.id,
text: '',
}) as Promise<MessageResult>;
}
case 'CodePipeline Stage Execution State Change': {
const sAttachment = stageAttachment(event);
if (message) {
return slack.chat.update({
attachments: updateOrAddAttachment(
message.attachments,
a => a.title === sAttachment.title,
sAttachment,
),
channel: channel.id,
text: '',
ts: message.ts,
}) as Promise<MessageResult>;
}
return undefined;
}
case 'CodePipeline Action Execution State Change': {
const aAttachment = actionAttachment(event);
if (message) {
return slack.chat.update({
attachments: updateOrAddAttachment(
message.attachments,
a => a.title === aAttachment.title,
aAttachment,
),
channel: channel.id,
text: '',
ts: message.ts,
}) as Promise<MessageResult>;
}
return undefined;
}
default:
}
};