-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcleanup.test.js
202 lines (193 loc) · 6.07 KB
/
cleanup.test.js
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
jest.mock('@actions/core');
jest.mock('@slack/web-api')
jest.mock('@actions/github')
const core = require('@actions/core');
const { WebClient, postMessage } = require('@slack/web-api');
const { slack } = require('./src/slack');
const github = require('@actions/github')
describe('slack', () => {
let client;
beforeAll(() => {
client = new WebClient();
process.env.SLACK_BOT_TOKEN = "my-token"
process.env.GITHUB_REF = "abcdef"
process.env.GITHUB_RUN_ID = "535677"
});
beforeEach(() => {
core.clearInputs()
core.setFailed.mockClear()
core.info.mockClear()
postMessage.mockClear()
})
test('does not send a message if cancelled', async () => {
core.setInputs({
slack_channel: 'my-channel',
job_status: 'cancelled',
slack_always: false
})
await slack();
expect(postMessage).not.toHaveBeenCalled();
expect(core.info).toHaveBeenCalledWith("Job cancelled, skipping notification")
})
test('does not send a message with no Slack channel', async() => {
core.setInputs({
job_status: 'failure',
slack_always: false
})
await slack();
expect(postMessage).not.toHaveBeenCalled();
expect(core.info).toHaveBeenCalledWith("No Slack channel provided - skipping Slack notification")
})
test('does not send a message on success without slack_always', async() => {
core.setInputs({
slack_channel: 'my-channel',
job_status: 'success',
slack_always: false
})
await slack();
expect(postMessage).not.toHaveBeenCalled();
expect(core.info).toHaveBeenCalledWith("Slack only sending on failure and job succeeded - skipping notification")
})
test('does not send a message if channel cannot be found', async () => {
core.setInputs({
slack_channel: 'not-my-channel',
job_status: 'failure',
slack_always: false
})
await slack();
expect(postMessage).not.toHaveBeenCalled();
expect(core.setFailed).toHaveBeenCalledWith("Slack channel #not-my-channel could not be found.")
})
test('sends a message on failure', async () => {
github.setPullRequest(false)
core.setInputs({
slack_channel: 'my-channel',
job_status: 'failure',
slack_always: false
})
await slack();
expect(postMessage).toHaveBeenCalledWith(expect.objectContaining({
attachments: [
{
color: "danger",
fields: [
{
short: true,
title: "Job",
value: "<https://github.com/wishabi/my-repo/actions/runs/535677 | my-job>"
},
{
short: true,
title: "Repo",
value: "<https://github.com/wishabi/my-repo | wishabi/my-repo>"
},
{
short: true,
title: "Branch",
value: "<https://github.com/wishabi/my-repo/commit/abc123 | feature-branch>"
},
{
short: true,
title: "Event",
value: "push"
}
],
footer: "<https://github.com/wishabi/my-repo | wishabi/my-repo>",
footer_icon: "https://github.githubassets.com/favicon.ico",
ts: expect.any(Number)
}
],
channel: 5,
text: "GitHub Action build failed!"
}));
expect(core.info).toHaveBeenCalledWith("Sent message to channel my-channel (5)")
})
test('sends a message for pull requests', async () => {
github.setPullRequest(true)
core.setInputs({
slack_channel: 'my-channel',
job_status: 'failure',
slack_always: false
})
await slack();
expect(postMessage).toHaveBeenCalledWith(expect.objectContaining({
attachments: [
{
color: "danger",
fields: [
{
short: true,
title: "Job",
value: "<https://github.com/wishabi/my-repo/actions/runs/535677 | my-job>"
},
{
short: true,
title: "Repo",
value: "<https://github.com/wishabi/my-repo | wishabi/my-repo>"
},
{
short: true,
title: "Pull Request",
value: "<https://github.com/wishabi/my-repo/pull/1 | My Pull Request>"
},
{
short: true,
title: "Event",
value: "pull_request"
}
],
footer: "<https://github.com/wishabi/my-repo | wishabi/my-repo>",
footer_icon: "https://github.githubassets.com/favicon.ico",
ts: expect.any(Number)
}
],
channel: 5,
text: "GitHub Action build failed!"
}));
expect(core.info).toHaveBeenCalledWith("Sent message to channel my-channel (5)")
})
test('sends message for success', async () => {
github.setPullRequest(false)
core.setInputs({
slack_channel: 'my-channel',
job_status: 'success',
slack_always: true
})
await slack();
expect(postMessage).toHaveBeenCalledWith(expect.objectContaining({
attachments: [
{
color: "good",
fields: [
{
short: true,
title: "Job",
value: "<https://github.com/wishabi/my-repo/actions/runs/535677 | my-job>"
},
{
short: true,
title: "Repo",
value: "<https://github.com/wishabi/my-repo | wishabi/my-repo>"
},
{
short: true,
title: "Branch",
value: "<https://github.com/wishabi/my-repo/commit/abc123 | feature-branch>"
},
{
short: true,
title: "Event",
value: "push"
}
],
footer: "<https://github.com/wishabi/my-repo | wishabi/my-repo>",
footer_icon: "https://github.githubassets.com/favicon.ico",
ts: expect.any(Number)
}
],
channel: 5,
text: "GitHub Action build succeeded!"
}));
expect(core.info).toHaveBeenCalledWith("Sent message to channel my-channel (5)")
})
})