-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from JupiterOne/SRE-1474_promote_actions
SRE-1474 Promote slack_notifier composite action and add tests
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Code QL | ||
|
||
This [composite action](./action.yml) is responsible for sending Github workflow build results to Slack to increase visibilty of build pipeline status. | ||
|
||
## Inputs | ||
|
||
This action takes the following inputs: | ||
|
||
| Name | Type | Default | Required | Description | | ||
| --------------------------- | ------- | ---------------------------- | --------- | --------------------------------------------------------- | | ||
| `slack_bot_token` | String | | True | The Oauth token for the Github Slack Send Slack app. | ||
| `slack_channel_id` | String | | True | The ID of the Slack channel to send notifications to. | ||
|
||
## Outputs | ||
|
||
No outputs provided. | ||
|
||
## Example Usage | ||
|
||
```yaml | ||
steps: | ||
- name: slack_notifier | ||
uses: jupiterone/.github/.github/actions/slack_notifier | ||
# The below condition is required to ensure the action delivers notifications for failed builds (as well as successful ones). | ||
if: always() | ||
with: | ||
slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }} | ||
slack_channel_id: 'C05447SKNH3' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
name: Slack Notifier | ||
description: Send Slack updates to the provided channel upon workflow completion. | ||
|
||
inputs: | ||
slack_bot_token: | ||
type: string | ||
required: true | ||
# This could be refactored to accept a comma-delimited list of IDs if needed. | ||
slack_channel_id: | ||
type: string | ||
required: true | ||
|
||
runs: | ||
using: "composite" | ||
steps: | ||
- name: set_status_emoji | ||
id: setEmoji | ||
shell: bash | ||
run: | | ||
echo "Job status: ${{ job.status }}" | ||
if [ "${{ job.status }}" == 'success' ]; then | ||
echo "status_emoji=:white_check_mark:" >> $GITHUB_OUTPUT | ||
elif [ "${{ job.status }}" == 'failure' ]; then | ||
echo "status_emoji=:x:" >> $GITHUB_OUTPUT | ||
else | ||
echo "status_emoji=:black_square_for_stop:" >> $GITHUB_OUTPUT | ||
fi | ||
- name: prepare_inputs | ||
shell: bash | ||
run: | | ||
echo "slack_bot_token=${{ inputs.slack_bot_token }}" | ||
echo "slack_channel_id=${{ inputs.slack_channel_id }}" | ||
- name: send_workflow_result_to_slack | ||
id: pr | ||
uses: slackapi/[email protected] | ||
with: | ||
channel-id: ${{ inputs.slack_channel_id }} | ||
payload: | | ||
{ | ||
"blocks": [ | ||
{ | ||
"type": "section", | ||
"block_id": "sectionBlockOnlyPlainText", | ||
"text": { | ||
"type": "mrkdwn", | ||
"text": "Github build result for *${{ github.repository }}*: ${{ job.status }} ${{ steps.setEmoji.outputs.status_emoji }}\n${{ github.event.pull_request.html_url || github.event.head_commit.url }}" | ||
} | ||
} | ||
] | ||
} | ||
env: | ||
SLACK_BOT_TOKEN: ${{ inputs.slack_bot_token }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
This file is automatically leveraged when tests are run to determine which | ||
steps should be skipped in the composite action. If these steps were not | ||
mocked, they would break the test. | ||
*/ | ||
export const SLACK_NOTIFIER_RUN_MOCK_STEPS = [ | ||
{ name: 'send_workflow_result_to_slack' }, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { MockGithub } from '@kie/mock-github'; | ||
import { Act } from '@kie/act-js'; | ||
import { getCompositeActionConfig, runCompositeAction } from 'tests/utils/setup'; | ||
import { getTestResult } from 'tests/utils/helpers'; | ||
|
||
const repoName = 'slack_notifier'; | ||
|
||
let mockGithub: MockGithub; | ||
|
||
beforeEach(async () => { | ||
mockGithub = new MockGithub(getCompositeActionConfig({ directory: __dirname, repoName })); | ||
|
||
await mockGithub.setup(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await mockGithub.teardown(); | ||
}); | ||
|
||
const slackBotToken = "mytoken"; | ||
const slackChannelId = "mychannelid"; | ||
|
||
test('Inputs are set correctly', async () => { | ||
const results = await runCompositeAction({ | ||
act: new Act(mockGithub.repo.getPath(repoName)), | ||
repoName, | ||
originDirectory: __dirname | ||
}); | ||
|
||
const result = getTestResult({ | ||
results, | ||
name: 'prepare_inputs' | ||
}); | ||
|
||
const expectedOutput = `slack_bot_token=${slackBotToken}\nslack_channel_id=${slackChannelId}`; | ||
expect(result.output).toEqual(expectedOutput) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
on: [push] | ||
|
||
jobs: | ||
slack_notifier: | ||
runs-on: ubuntu-latest | ||
steps: | ||
# https://github.com/nektos/act#module_not_found | ||
- uses: actions/checkout@v3 | ||
with: | ||
path: slack_notifier | ||
- uses: ./slack_notifier | ||
with: | ||
slack_bot_token: 'mytoken' | ||
slack_channel_id: 'mychannelid' |