Skip to content

Commit

Permalink
Merge pull request #151 from JupiterOne/SRE-1474_promote_actions
Browse files Browse the repository at this point in the history
SRE-1474 Promote slack_notifier composite action and add tests
  • Loading branch information
tyanko1 authored Nov 28, 2023
2 parents 12a39bc + 216082c commit b12a5fa
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/actions/slack_notifier/README.md
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'
```
52 changes: 52 additions & 0 deletions .github/actions/slack_notifier/action.yml
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 }}
8 changes: 8 additions & 0 deletions .github/actions/slack_notifier/mocks.ts
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' },
];
37 changes: 37 additions & 0 deletions .github/actions/slack_notifier/test/action.test.ts
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)
});
14 changes: 14 additions & 0 deletions .github/actions/slack_notifier/test/action_test.yml
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'

0 comments on commit b12a5fa

Please sign in to comment.