Skip to content

Commit

Permalink
Merge pull request #275 from sue445/add_inputs_input
Browse files Browse the repository at this point in the history
Support `inputs` context
  • Loading branch information
satterly authored Sep 3, 2024
2 parents f5f5e8d + e438aa5 commit d2d8b23
Show file tree
Hide file tree
Showing 19 changed files with 149 additions and 12 deletions.
3 changes: 3 additions & 0 deletions .github/slack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ fields:
- title: Job Matrix
value: "{{#each jobMatrix}}{{@key}}: {{this}}\n{{/each}}"
short: false
- title: Job Inputs
value: "{{#each jobInputs}}{{@key}}: {{this}}\n{{/each}}"
short: false
- title: Workflow
value: "<{{{workflowUrl}}}|{{workflow}}>"
short: true
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ Parameters for [matrix jobs](https://docs.github.com/en/actions/using-jobs/using

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

#### `inputs` (optional)
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:

with:
status: ${{ job.status }}
inputs: ${{ toJson(inputs) }}

<img src="docs/images/example5.png" width="358" title="Slack Example #5">

<img src="docs/images/example6.png" width="571" title="Slack Example #6">

#### `channel` (optional)

To override the channel or to send the Slack message to an individual
Expand Down
3 changes: 2 additions & 1 deletion __tests__/blocks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const jobSteps = {
}
}
const jobMatrix = {}
const jobInputs = {}
const channel = '#github-ci'

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

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const jobSteps = {
}
}
const jobMatrix = {}
const jobInputs = {}
const channel = '#github-ci'

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

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/inputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const jobSteps = {
}
}
const jobMatrix = {}
const jobInputs = {}
const channel = '#deploy'
let message = 'Successfully deployed to {{ env.ENVIRONMENT }}!'

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

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
97 changes: 97 additions & 0 deletions __tests__/job_inputs.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import * as github from '@actions/github'
import axios from 'axios'
import MockAdapter from 'axios-mock-adapter'
import {send} from '../src/slack'
import {readFileSync} from 'fs'

const url = 'https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX'
const jobName = 'Build and Test'
const jobStatus = 'Success'
const jobSteps = {}
const jobMatrix = {}
const jobInputs = {
environment: 'staging',
logLevel: 'warning',
print_tags: true
}
const channel = '@override'
const message = undefined

// 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.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_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('push 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)

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
username: 'GitHub Actions',
icon_url: 'https://octodex.github.com/images/original.png',
channel: '@override',
timeout: 0,
attachments: [
{
fallback: '[GitHub]: [act10ns/slack] build-test push Success',
color: 'good',
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: '',
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',
title: '',
fields: [
{
title: 'Job Inputs',
value: 'environment: staging\nlogLevel: warning\nprint_tags: true\n',
short: false
}
],
footer: '<https://github.com/act10ns/slack|act10ns/slack> #8',
footer_icon: 'https://github.githubassets.com/favicon.ico',
ts: expect.stringMatching(/[0-9]+/)
}
]
})

mockAxios.resetHistory()
mockAxios.reset()
})
3 changes: 2 additions & 1 deletion __tests__/job_matrix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const jobMatrix = {
name1: 'value1',
name2: 'value2'
}
const jobInputs = {}
const channel = '@override'
const message = undefined

Expand Down Expand Up @@ -57,7 +58,7 @@ test('push event to slack', async () => {
.onAny()
.reply(500)

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/job_status.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const jobSteps = {
}
}
const jobMatrix = {}
const jobInputs = {}
const channel = '#github-ci'
const message = undefined

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

const config: ConfigOptions = {}

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/pull_request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const jobName = 'Build and Test'
const jobStatus = 'Success'
const jobSteps = {}
const jobMatrix = {}
const jobInputs = {}
const channel = '@override'
const message = undefined

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

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const jobName = 'Build and Test'
const jobStatus = 'Success'
const jobSteps = {}
const jobMatrix = {}
const jobInputs = {}
const channel = '@override'
const message = undefined

Expand Down Expand Up @@ -54,7 +55,7 @@ test('push event to slack', async () => {
.onAny()
.reply(500)

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/release.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const jobName = 'Build and Test'
const jobStatus = 'Success'
const jobSteps = {}
const jobMatrix = {}
const jobInputs = {}
const channel = '@override'
const message = undefined

Expand Down Expand Up @@ -57,7 +58,7 @@ test('release event to slack', async () => {
.onAny()
.reply(500)

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/schedule.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const jobName = 'Build and Test'
const jobStatus = 'Success'
const jobSteps = {}
const jobMatrix = {}
const jobInputs = {}
const channel = '@override'
const message = undefined

Expand Down Expand Up @@ -50,7 +51,7 @@ test('schedule event to slack', async () => {
.onAny()
.reply(500)

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/workflow_dispatch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const jobName = 'Build and Test'
const jobStatus = 'Success'
const jobSteps = {}
const jobMatrix = {}
const jobInputs = {}
const channel = '@override'
const message = undefined

Expand Down Expand Up @@ -55,7 +56,7 @@ test('workflow_dispatch event to slack', async () => {
.onAny()
.reply(500)

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 2 additions & 1 deletion __tests__/workflow_run.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const jobName = process.env.GITHUB_JOB as string
const jobStatus = (process.env.INPUT_STATUS as string).toUpperCase()
const jobSteps = process.env.INPUT_STEPS || {}
const jobMatrix = {}
const jobInputs = {}
const channel = process.env.INPUT_CHANNEL as string
const message = process.env.INPUT_MESSAGE as string

Expand All @@ -67,7 +68,7 @@ test('workflow_run event to slack', async () => {
schema: yaml.FAILSAFE_SCHEMA
}) as ConfigOptions

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

expect(JSON.parse(mockAxios.history.post[0].data)).toStrictEqual({
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ inputs:
matrix:
description: matrix properties
required: false
inputs:
description: Report input values passed by workflow_call or workflow_dispatch events
required: false
channel:
description: Override default channel with different channel or username
required: false
Expand Down
Binary file added docs/images/example5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/images/example6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 3 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,16 @@ async function run(): Promise<void> {
const jobStatus = core.getInput('status', {required: true}).toUpperCase()
const jobSteps = JSON.parse(core.getInput('steps', {required: false}) || '{}')
const jobMatrix = JSON.parse(core.getInput('matrix', {required: false}) || '{}')
const jobInputs = JSON.parse(core.getInput('inputs', {required: false}) || '{}')
const channel = core.getInput('channel', {required: false})
const message = core.getInput('message', {required: false})
core.debug(`jobName: ${jobName}, jobStatus: ${jobStatus}`)
core.debug(`channel: ${channel}, message: ${message}`)
core.debug(`jobMatrix: ${JSON.stringify(jobMatrix)}`)
core.debug(`jobInputs: ${JSON.stringify(jobInputs)}`)

if (url) {
await send(url, jobName, jobStatus, jobSteps, jobMatrix, channel, message, config)
await send(url, jobName, jobStatus, jobSteps, jobMatrix, jobInputs, channel, message, config)
core.info(`Sent ${jobName} status of ${jobStatus} to Slack!`)
} else {
core.warning('No "SLACK_WEBHOOK_URL"s env or "webhook-url" input configured. Skip.')
Expand Down
10 changes: 10 additions & 0 deletions src/slack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export async function send(
jobStatus: string,
jobSteps: object,
jobMatrix: object,
jobInputs: object,
channel?: string,
message?: string,
opts?: ConfigOptions
Expand Down Expand Up @@ -259,6 +260,14 @@ export async function send(
if: 'always()'
})
}
if (Object.entries(jobInputs).length) {
defaultFields.push({
title: 'Job Inputs',
value: '{{#each jobInputs}}{{@key}}: {{this}}\n{{~/each}}',
short: false,
if: 'always()'
})
}

const filteredFields: object[] = []
for (const field of opts?.fields || defaultFields) {
Expand All @@ -283,6 +292,7 @@ export async function send(
jobStatus,
jobSteps,
jobMatrix,
jobInputs,
eventName,
workflow,
workflowUrl,
Expand Down

0 comments on commit d2d8b23

Please sign in to comment.