/
workflow_run.test.ts
141 lines (129 loc) · 5.4 KB
/
workflow_run.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
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'
// mock github context
const dump = JSON.parse(readFileSync('./__tests__/fixtures/workflow_run.json', 'utf-8'))
github.context.payload = dump
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.CI = 'true'
process.env.GITHUB_WORKFLOW = 'workflow-run'
process.env.GITHUB_JOB = 'on-success'
process.env.GITHUB_RUN_ID = '1452345894'
process.env.GITHUB_RUN_NUMBER = '4'
process.env.GITHUB_ACTION = '__act10ns_slack'
process.env.GITHUB_ACTIONS = 'true'
process.env.GITHUB_ACTOR = 'satterly'
process.env.GITHUB_REPOSITORY = 'act10ns/slack'
process.env.GITHUB_EVENT_NAME = 'workflow_run'
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 = '0d05b90e3bf469738248c462d36be1a78520a02e'
process.env.GITHUB_REF = 'refs/heads/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'
process.env.INPUT_CHANNEL = '#actions'
process.env.INPUT_CONFIG = '__tests__/fixtures/slack-workflow.yml'
process.env.INPUT_STATUS = 'success'
process.env.INPUT_STEPS = ''
process.env.SLACK_WEBHOOK_URL = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
const url = process.env.SLACK_WEBHOOK_URL as string
const jobName = process.env.GITHUB_JOB as string
const jobStatus = (process.env.INPUT_STATUS as string).toUpperCase()
const jobSteps = process.env.INPUT_STEPS || {}
const channel = process.env.INPUT_CHANNEL as string
const message = process.env.INPUT_MESSAGE as string
test('workflow_run event to slack', 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-workflow.yml', 'utf-8'), {
schema: yaml.FAILSAFE_SCHEMA
}) as ConfigOptions
const 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/femalecodertocat.png',
channel: '#actions',
attachments: [
{
mrkdwn_in: ['pretext', 'text', 'fields'],
color: '#5DADE2',
pretext: 'Triggered via workflow_run by satterly __act10ns_slack master `0d05b90e`',
author_name: 'satterly',
author_link: 'https://github.com/satterly',
author_icon: 'https://avatars.githubusercontent.com/u/615057?v=4',
title: 'GitHub Actions',
title_link: 'https://support.github.com',
text: '*<https://github.com/act10ns/slack/actions?query=workflow:workflow-run|Workflow _workflow-run_ job _on-success_ triggered by _workflow_run_ is _SUCCESS_>* for <https://github.com/act10ns/slack/commits/master|`master`>\n',
fields: [],
fallback: '[GitHub] workflow-run #4 is SUCCESS',
footer: '<https://github.com/act10ns/slack|act10ns/slack> workflow-run #4',
footer_icon: 'https://github.githubassets.com/favicon.ico',
ts: expect.stringMatching(/[0-9]+/)
},
{
color: '#5DADE2',
fallback: '[GitHub] workflow-run #4 is SUCCESS',
blocks: [
{
type: 'context',
elements: [
{type: 'image', image_url: 'https://avatars.githubusercontent.com/u/615057?v=4', alt_text: 'satterly'},
{type: 'mrkdwn', text: '*<https://github.com/satterly|satterly>*'}
]
},
{
type: 'section',
text: {type: 'mrkdwn', text: 'Workflow build-test completed with success after 1 attempt'},
accessory: {
type: 'button',
text: {type: 'plain_text', text: 'View'},
value: 'workflow_run_1237076',
url: 'https://github.com/act10ns/slack/actions/runs/1452342609',
action_id: 'button-action'
}
},
{
type: 'section',
fields: [
{type: 'mrkdwn', text: '*Jobs*\nhttps://api.github.com/repos/act10ns/slack/actions/runs/1452342609/jobs'},
{type: 'mrkdwn', text: '*Logs*\nhttps://api.github.com/repos/act10ns/slack/actions/runs/1452342609/logs'}
]
},
{
type: 'context',
elements: [
{type: 'image', image_url: 'https://github.githubassets.com/favicon.ico', alt_text: 'github'},
{
type: 'mrkdwn',
text: expect.stringMatching(
/<https:\/\/github.com\/act10ns\/slack|act10ns\/slack> build-test #8 | <!date^[0-9]+^{date_short_pretty} at {time}|[0-9]+>/
)
}
]
}
]
}
]
})
mockAxios.resetHistory()
mockAxios.reset()
})