Skip to content

Commit d2d8b23

Browse files
authored
Merge pull request #275 from sue445/add_inputs_input
Support `inputs` context
2 parents f5f5e8d + e438aa5 commit d2d8b23

19 files changed

Lines changed: 149 additions & 12 deletions

.github/slack.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ fields:
2525
- title: Job Matrix
2626
value: "{{#each jobMatrix}}{{@key}}: {{this}}\n{{/each}}"
2727
short: false
28+
- title: Job Inputs
29+
value: "{{#each jobInputs}}{{@key}}: {{this}}\n{{/each}}"
30+
short: false
2831
- title: Workflow
2932
value: "<{{{workflowUrl}}}|{{workflow}}>"
3033
short: true

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,17 @@ Parameters for [matrix jobs](https://docs.github.com/en/actions/using-jobs/using
6969

7070
<img src="./docs/images/example4.png" width="505" title="Slack Example #4">
7171

72+
#### `inputs` (optional)
73+
Parameters for [`workflow_call`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_call) or [`workflow_dispatch`](https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#workflow_dispatch) events can be included in Slack messages:
74+
75+
with:
76+
status: ${{ job.status }}
77+
inputs: ${{ toJson(inputs) }}
78+
79+
<img src="docs/images/example5.png" width="358" title="Slack Example #5">
80+
81+
<img src="docs/images/example6.png" width="571" title="Slack Example #6">
82+
7283
#### `channel` (optional)
7384

7485
To override the channel or to send the Slack message to an individual

__tests__/blocks.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const jobSteps = {
4141
}
4242
}
4343
const jobMatrix = {}
44+
const jobInputs = {}
4445
const channel = '#github-ci'
4546

4647
// mock github context
@@ -93,7 +94,7 @@ test('custom config of slack action using legacy and blocks', async () => {
9394
schema: yaml.FAILSAFE_SCHEMA
9495
}) as ConfigOptions
9596

96-
let res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message, config)
97+
let res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message, config)
9798
await expect(res).toStrictEqual({text: {status: 'ok'}})
9899

99100
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

__tests__/config.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const jobSteps = {
4141
}
4242
}
4343
const jobMatrix = {}
44+
const jobInputs = {}
4445
const channel = '#github-ci'
4546

4647
// mock github context
@@ -93,7 +94,7 @@ test('custom config of slack action using legacy attachments', async () => {
9394
schema: yaml.FAILSAFE_SCHEMA
9495
}) as ConfigOptions
9596

96-
let res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message, config)
97+
let res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message, config)
9798
await expect(res).toStrictEqual({text: {status: 'ok'}})
9899

99100
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

__tests__/inputs.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const jobSteps = {
4141
}
4242
}
4343
const jobMatrix = {}
44+
const jobInputs = {}
4445
const channel = '#deploy'
4546
let message = 'Successfully deployed to {{ env.ENVIRONMENT }}!'
4647

@@ -93,7 +94,7 @@ test('custom config of slack action using inputs for channel and message', async
9394
schema: yaml.FAILSAFE_SCHEMA
9495
}) as ConfigOptions
9596

96-
let res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message, config)
97+
let res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message, config)
9798
await expect(res).toStrictEqual({text: {status: 'ok'}})
9899

99100
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

__tests__/job_inputs.test.ts

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import * as github from '@actions/github'
2+
import axios from 'axios'
3+
import MockAdapter from 'axios-mock-adapter'
4+
import {send} from '../src/slack'
5+
import {readFileSync} from 'fs'
6+
7+
const url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
8+
const jobName = 'Build and Test'
9+
const jobStatus = 'Success'
10+
const jobSteps = {}
11+
const jobMatrix = {}
12+
const jobInputs = {
13+
environment: 'staging',
14+
logLevel: 'warning',
15+
print_tags: true
16+
}
17+
const channel = '@override'
18+
const message = undefined
19+
20+
// mock github context
21+
const dump = JSON.parse(readFileSync('./__tests__/fixtures/push.json', 'utf-8'))
22+
23+
github.context.payload = dump.event
24+
github.context.eventName = dump.event_name
25+
github.context.sha = dump.sha
26+
github.context.ref = dump.ref
27+
github.context.workflow = dump.workflow
28+
github.context.action = dump.action
29+
github.context.actor = dump.actor
30+
31+
process.env.CI = 'true'
32+
process.env.GITHUB_WORKFLOW = 'build-test'
33+
process.env.GITHUB_RUN_ID = '100143423'
34+
process.env.GITHUB_RUN_NUMBER = '8'
35+
process.env.GITHUB_ACTION = 'self2'
36+
process.env.GITHUB_ACTIONS = 'true'
37+
process.env.GITHUB_ACTOR = 'satterly'
38+
process.env.GITHUB_REPOSITORY = 'act10ns/slack'
39+
process.env.GITHUB_EVENT_NAME = 'push'
40+
process.env.GITHUB_EVENT_PATH = '/home/runner/work/_temp/_github_workflow/event.json'
41+
process.env.GITHUB_WORKSPACE = '/home/runner/work/slack/slack'
42+
process.env.GITHUB_SHA = '68d48876e0794fba714cb331a1624af6b20942d8'
43+
process.env.GITHUB_REF = 'refs/heads/master'
44+
process.env.GITHUB_HEAD_REF = ''
45+
process.env.GITHUB_BASE_REF = ''
46+
process.env.GITHUB_SERVER_URL = 'https://github.com'
47+
process.env.GITHUB_API_URL = 'https://github.com'
48+
process.env.GITHUB_GRAPHQL_URL = 'https://api.github.com/graphql'
49+
50+
test('push event to slack', async () => {
51+
const mockAxios = new MockAdapter(axios, {delayResponse: 200})
52+
53+
mockAxios
54+
.onPost()
55+
.reply(config => {
56+
console.log(config.data)
57+
return [200, {status: 'ok'}]
58+
})
59+
.onAny()
60+
.reply(500)
61+
62+
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message)
63+
await expect(res).toStrictEqual({text: {status: 'ok'}})
64+
65+
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
66+
username: 'GitHub Actions',
67+
icon_url: 'https://octodex.github.com/images/original.png',
68+
channel: '@override',
69+
timeout: 0,
70+
attachments: [
71+
{
72+
fallback: '[GitHub]: [act10ns/slack] build-test push Success',
73+
color: 'good',
74+
author_name: 'satterly',
75+
author_link: 'https://github.com/satterly',
76+
author_icon: 'https://avatars0.githubusercontent.com/u/615057?v=4',
77+
mrkdwn_in: ['pretext', 'text', 'fields'],
78+
pretext: '',
79+
text: '*<https://github.com/act10ns/slack/actions?query=workflow:%22build-test%22|Workflow _build-test_ job _Build and Test_ triggered by _push_ is _Success_>* for <https://github.com/act10ns/slack/commits/master|`master`>\n<https://github.com/act10ns/slack/compare/db9fe60430a6...68d48876e079|`68d48876`> - 4 commits',
80+
title: '',
81+
fields: [
82+
{
83+
title: 'Job Inputs',
84+
value: 'environment: staging\nlogLevel: warning\nprint_tags: true\n',
85+
short: false
86+
}
87+
],
88+
footer: '<https://github.com/act10ns/slack|act10ns/slack> #8',
89+
footer_icon: 'https://github.githubassets.com/favicon.ico',
90+
ts: expect.stringMatching(/[0-9]+/)
91+
}
92+
]
93+
})
94+
95+
mockAxios.resetHistory()
96+
mockAxios.reset()
97+
})

__tests__/job_matrix.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const jobMatrix = {
1212
name1: 'value1',
1313
name2: 'value2'
1414
}
15+
const jobInputs = {}
1516
const channel = '@override'
1617
const message = undefined
1718

@@ -57,7 +58,7 @@ test('push event to slack', async () => {
5758
.onAny()
5859
.reply(500)
5960

60-
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message)
61+
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message)
6162
await expect(res).toStrictEqual({text: {status: 'ok'}})
6263

6364
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

__tests__/job_status.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ const jobSteps = {
4040
}
4141
}
4242
const jobMatrix = {}
43+
const jobInputs = {}
4344
const channel = '#github-ci'
4445
const message = undefined
4546

@@ -87,7 +88,7 @@ test('push event to slack', async () => {
8788

8889
const config: ConfigOptions = {}
8990

90-
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message, config)
91+
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message, config)
9192
await expect(res).toStrictEqual({text: {status: 'ok'}})
9293

9394
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

__tests__/pull_request.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const jobName = 'Build and Test'
99
const jobStatus = 'Success'
1010
const jobSteps = {}
1111
const jobMatrix = {}
12+
const jobInputs = {}
1213
const channel = '@override'
1314
const message = undefined
1415

@@ -54,7 +55,7 @@ test('pull request event to slack', async () => {
5455
.onAny()
5556
.reply(500)
5657

57-
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message)
58+
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message)
5859
await expect(res).toStrictEqual({text: {status: 'ok'}})
5960

6061
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

__tests__/push.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const jobName = 'Build and Test'
99
const jobStatus = 'Success'
1010
const jobSteps = {}
1111
const jobMatrix = {}
12+
const jobInputs = {}
1213
const channel = '@override'
1314
const message = undefined
1415

@@ -54,7 +55,7 @@ test('push event to slack', async () => {
5455
.onAny()
5556
.reply(500)
5657

57-
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message)
58+
const res = await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message)
5859
await expect(res).toStrictEqual({text: {status: 'ok'}})
5960

6061
expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({

0 commit comments

Comments
 (0)