-
Notifications
You must be signed in to change notification settings - Fork 56
/
inputs.test.ts
160 lines (150 loc) · 4.76 KB
/
inputs.test.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
152
153
154
155
156
157
158
159
160
import * as github from '@actions/github'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import {send, ConfigOptions} from '../src/slack'
import {readFileSync} from 'fs'
import * as yaml from 'js-yaml'
const url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
const jobName = 'CI Tests'
const jobStatus = 'in progress'
const jobSteps = {
'install-deps': {
outputs: {},
outcome: 'success',
conclusion: 'success'
},
hooks: {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
lint: {
outputs: {},
outcome: 'failure',
conclusion: 'failure'
},
types: {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
'unit-test': {
outputs: {},
outcome: 'skipped',
conclusion: 'skipped'
},
'integration-test': {
outputs: {},
outcome: 'failure',
conclusion: 'failure'
}
}
const channel = '#deploy'
let message = 'Successfully deployed to {{ env.ENVIRONMENT }}!'
// mock github context
const dump = JSON.parse(readFileSync('./__tests__/fixtures/push.json', 'utf-8'))
github.context.payload = dump.event
github.context.eventName = dump.event_name
github.context.sha = dump.sha
github.context.ref = dump.ref
github.context.workflow = dump.workflow
github.context.action = dump.action
github.context.actor = dump.actor
process.env.ENVIRONMENT = 'dev'
process.env.CI = 'true'
process.env.GITHUB_WORKFLOW = 'build-test'
process.env.GITHUB_RUN_ID = '100143423'
process.env.GITHUB_RUN_NUMBER = '8'
process.env.GITHUB_ACTION = 'self2'
process.env.GITHUB_ACTIONS = 'true'
process.env.GITHUB_ACTOR = 'satterly'
process.env.GITHUB_REPOSITORY = 'act10ns/slack'
process.env.GITHUB_EVENT_NAME = 'push'
process.env.GITHUB_EVENT_PATH = '/home/runner/work/_temp/_github_workflow/event.json'
process.env.GITHUB_WORKSPACE = '/home/runner/work/slack/slack'
process.env.GITHUB_SHA = '68d48876e0794fba714cb331a1624af6b20942d8'
process.env.GITHUB_REF = 'refs/heads/master'
process.env.GITHUB_REF_TYPE = 'branch'
process.env.GITHUB_REF_NAME = 'master'
process.env.GITHUB_HEAD_REF = ''
process.env.GITHUB_BASE_REF = ''
process.env.GITHUB_SERVER_URL = 'https://github.com'
process.env.GITHUB_API_URL = 'https://github.com'
process.env.GITHUB_GRAPHQL_URL = 'https://api.github.com/graphql'
test('custom config of slack action', async () => {
const mockAxios = new MockAdapter(axios, {delayResponse: 200})
mockAxios
.onPost()
.reply(config => {
console.log(config.data)
return [200, {status: 'ok'}]
})
.onAny()
.reply(500)
let config = yaml.load(readFileSync('./__tests__/fixtures/slack.yml', 'utf-8'), {
schema: yaml.FAILSAFE_SCHEMA
}) as ConfigOptions
let res = await send(url, jobName, jobStatus, jobSteps, channel, message, config)
await expect(res).toStrictEqual({text: {status: 'ok'}})
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
username: 'GitHub-CI',
icon_url: 'https://octodex.github.com/images/mona-the-rivetertocat.png',
channel: '#deploy',
attachments: [
{
fallback: '[GitHub] build-test #8 CI Tests is in progress',
color: '#7D3C98',
author_name: 'satterly',
author_link: 'https://github.com/satterly',
author_icon: 'https://avatars0.githubusercontent.com/u/615057?v=4',
mrkdwn_in: ['pretext', 'text', 'fields'],
pretext: 'Triggered via push by satterly action master `68d48876`',
text: 'Successfully deployed to dev!',
title: 'GitHub Actions',
title_link: 'https://support.github.com',
fields: [
{
short: false,
title: 'Job Steps',
value: ':white_check_mark: install-deps\n:grimacing: lint\n:grimacing: integration-test\n'
},
{
short: true,
title: 'Workflow',
value: '<https://github.com/act10ns/slack/actions?query=build-test|build-test>'
},
{
short: true,
title: 'Git Ref',
value: 'master (branch)'
},
{
short: true,
title: 'Run ID',
value: '<https://github.com/act10ns/slack/actions/runs/100143423|100143423>'
},
{
short: true,
title: 'Run Number',
value: '8'
},
{
short: true,
title: 'Actor',
value: 'satterly'
},
{
short: true,
title: 'Job Status',
value: 'in progress'
}
],
footer: '<https://github.com/act10ns/slack|act10ns/slack> build-test #8',
footer_icon: 'https://github.githubassets.com/favicon.ico',
ts: expect.stringMatching(/[0-9]+/)
}
]
})
mockAxios.resetHistory()
mockAxios.reset()
})