From a1e610bff77cdc3d348da6458d87e1568c5cb770 Mon Sep 17 00:00:00 2001 From: Terry Yanko Date: Mon, 27 Nov 2023 11:57:17 -0500 Subject: [PATCH 1/7] add slack_notifier composite action with tests --- .github/actions/slack_notifier/README.md | 29 +++++++++++ .github/actions/slack_notifier/action.yml | 52 +++++++++++++++++++ .github/actions/slack_notifier/mocks.ts | 8 +++ .../slack_notifier/test/action.test.ts | 37 +++++++++++++ .../slack_notifier/test/action_test.yml | 11 ++++ 5 files changed, 137 insertions(+) create mode 100644 .github/actions/slack_notifier/README.md create mode 100644 .github/actions/slack_notifier/action.yml create mode 100644 .github/actions/slack_notifier/mocks.ts create mode 100644 .github/actions/slack_notifier/test/action.test.ts create mode 100644 .github/actions/slack_notifier/test/action_test.yml diff --git a/.github/actions/slack_notifier/README.md b/.github/actions/slack_notifier/README.md new file mode 100644 index 00000000..c7323fe1 --- /dev/null +++ b/.github/actions/slack_notifier/README.md @@ -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: ./.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' +``` diff --git a/.github/actions/slack_notifier/action.yml b/.github/actions/slack_notifier/action.yml new file mode 100644 index 00000000..cb742790 --- /dev/null +++ b/.github/actions/slack_notifier/action.yml @@ -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/slack-github-action@v1.24.0 + 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 }} diff --git a/.github/actions/slack_notifier/mocks.ts b/.github/actions/slack_notifier/mocks.ts new file mode 100644 index 00000000..3b665ddb --- /dev/null +++ b/.github/actions/slack_notifier/mocks.ts @@ -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' }, +]; diff --git a/.github/actions/slack_notifier/test/action.test.ts b/.github/actions/slack_notifier/test/action.test.ts new file mode 100644 index 00000000..36a049ed --- /dev/null +++ b/.github/actions/slack_notifier/test/action.test.ts @@ -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'; +import { SLACK_NOTIFIER_RUN_MOCK_STEPS } from '../mocks'; + +const repoName = 'slack_notifier'; + +let mockGithub: MockGithub; + +afterEach(async () => { + await mockGithub.teardown(); +}); + +const token = "mytoken"; + +test('Inputs are set correctly', async () => { + mockGithub = new MockGithub(getCompositeActionConfig({ + directory: __dirname, + repoName, + })); + + await mockGithub.setup(); + + const results = await runCompositeAction({ + act: new Act(mockGithub.repo.getPath(repoName)), + repoName, + originDirectory: __dirname, + }); + + const result = getTestResult({ + results, + name: 'prepare_inputs' + }); + + expect(result).toEqual(token); +}); diff --git a/.github/actions/slack_notifier/test/action_test.yml b/.github/actions/slack_notifier/test/action_test.yml new file mode 100644 index 00000000..9a5b4142 --- /dev/null +++ b/.github/actions/slack_notifier/test/action_test.yml @@ -0,0 +1,11 @@ +on: [push] + +jobs: + slack_notify: + runs-on: ubuntu-latest + steps: + - uses: ./slack_notifier + if: always() + with: + slack-bot-token: 'mytoken' + slack-channel-id: 'mychannelid' \ No newline at end of file From e2672a1d6f2965d231d9e1dab786eb3e42cc7dde Mon Sep 17 00:00:00 2001 From: Terry Yanko Date: Mon, 27 Nov 2023 12:08:00 -0500 Subject: [PATCH 2/7] test that result is defined --- .github/actions/slack_notifier/test/action.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/slack_notifier/test/action.test.ts b/.github/actions/slack_notifier/test/action.test.ts index 36a049ed..9f60fa5b 100644 --- a/.github/actions/slack_notifier/test/action.test.ts +++ b/.github/actions/slack_notifier/test/action.test.ts @@ -33,5 +33,5 @@ test('Inputs are set correctly', async () => { name: 'prepare_inputs' }); - expect(result).toEqual(token); + expect(result).toBeDefined(); }); From fcea9255f704c07e45ed854fbf0a174a684c6348 Mon Sep 17 00:00:00 2001 From: "noah.jablonski" <1964654+jablonnc@users.noreply.github.com> Date: Mon, 27 Nov 2023 16:14:09 -0500 Subject: [PATCH 3/7] Fix setup issue --- .../actions/slack_notifier/test/action.test.ts | 17 ++++++++--------- .../actions/slack_notifier/test/action_test.yml | 7 +++++-- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.github/actions/slack_notifier/test/action.test.ts b/.github/actions/slack_notifier/test/action.test.ts index 9f60fa5b..bf775896 100644 --- a/.github/actions/slack_notifier/test/action.test.ts +++ b/.github/actions/slack_notifier/test/action.test.ts @@ -8,24 +8,23 @@ 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 token = "mytoken"; -test('Inputs are set correctly', async () => { - mockGithub = new MockGithub(getCompositeActionConfig({ - directory: __dirname, - repoName, - })); - - await mockGithub.setup(); - +test('output of test_passed is true when cypress_run is successful', async () => { const results = await runCompositeAction({ act: new Act(mockGithub.repo.getPath(repoName)), repoName, - originDirectory: __dirname, + originDirectory: __dirname }); const result = getTestResult({ diff --git a/.github/actions/slack_notifier/test/action_test.yml b/.github/actions/slack_notifier/test/action_test.yml index 9a5b4142..746e9193 100644 --- a/.github/actions/slack_notifier/test/action_test.yml +++ b/.github/actions/slack_notifier/test/action_test.yml @@ -1,11 +1,14 @@ on: [push] jobs: - slack_notify: + 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 - if: always() with: slack-bot-token: 'mytoken' slack-channel-id: 'mychannelid' \ No newline at end of file From a44a51222a6869449709860e5f381ba7e6af1906 Mon Sep 17 00:00:00 2001 From: Terry Yanko Date: Mon, 27 Nov 2023 17:38:51 -0500 Subject: [PATCH 4/7] capture and compare expected output --- .github/actions/slack_notifier/test/action.test.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/.github/actions/slack_notifier/test/action.test.ts b/.github/actions/slack_notifier/test/action.test.ts index bf775896..1616b624 100644 --- a/.github/actions/slack_notifier/test/action.test.ts +++ b/.github/actions/slack_notifier/test/action.test.ts @@ -2,7 +2,6 @@ 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'; -import { SLACK_NOTIFIER_RUN_MOCK_STEPS } from '../mocks'; const repoName = 'slack_notifier'; @@ -10,7 +9,7 @@ let mockGithub: MockGithub; beforeEach(async () => { mockGithub = new MockGithub(getCompositeActionConfig({ directory: __dirname, repoName })); - + await mockGithub.setup(); }); @@ -18,9 +17,10 @@ afterEach(async () => { await mockGithub.teardown(); }); -const token = "mytoken"; +const slackBotToken = "mytoken"; +const slackChannelId = "mychannelid"; -test('output of test_passed is true when cypress_run is successful', async () => { +test('Inputs are set correctly', async () => { const results = await runCompositeAction({ act: new Act(mockGithub.repo.getPath(repoName)), repoName, @@ -32,5 +32,7 @@ test('output of test_passed is true when cypress_run is successful', async () => name: 'prepare_inputs' }); - expect(result).toBeDefined(); + // console.log(JSON.stringify(result.output)); + const expectedOutput = `slack-bot-token=${slackBotToken}\nslack-channel-id=${slackChannelId}`; + expect(result.output).toEqual(expectedOutput) }); From 521d93b4ec54ef2957c4d416a4593081184b8c57 Mon Sep 17 00:00:00 2001 From: Terry Yanko Date: Mon, 27 Nov 2023 17:40:59 -0500 Subject: [PATCH 5/7] cleanup --- .github/actions/slack_notifier/test/action.test.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/actions/slack_notifier/test/action.test.ts b/.github/actions/slack_notifier/test/action.test.ts index 1616b624..ce519f80 100644 --- a/.github/actions/slack_notifier/test/action.test.ts +++ b/.github/actions/slack_notifier/test/action.test.ts @@ -32,7 +32,6 @@ test('Inputs are set correctly', async () => { name: 'prepare_inputs' }); - // console.log(JSON.stringify(result.output)); const expectedOutput = `slack-bot-token=${slackBotToken}\nslack-channel-id=${slackChannelId}`; expect(result.output).toEqual(expectedOutput) }); From bdfdf49ef879931ec52963d3418f9e78a84bccb8 Mon Sep 17 00:00:00 2001 From: Terry Yanko Date: Mon, 27 Nov 2023 17:42:37 -0500 Subject: [PATCH 6/7] fix readme --- .github/actions/slack_notifier/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/actions/slack_notifier/README.md b/.github/actions/slack_notifier/README.md index c7323fe1..814a6053 100644 --- a/.github/actions/slack_notifier/README.md +++ b/.github/actions/slack_notifier/README.md @@ -20,7 +20,7 @@ No outputs provided. ```yaml steps: - name: slack_notifier - uses: ./.github/actions/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: From 216082c3d5ee681f18804974f2eda62f81746fed Mon Sep 17 00:00:00 2001 From: Terry Yanko Date: Tue, 28 Nov 2023 09:32:26 -0500 Subject: [PATCH 7/7] use snake case --- .github/actions/slack_notifier/README.md | 8 ++++---- .github/actions/slack_notifier/action.yml | 12 ++++++------ .github/actions/slack_notifier/test/action.test.ts | 2 +- .github/actions/slack_notifier/test/action_test.yml | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/.github/actions/slack_notifier/README.md b/.github/actions/slack_notifier/README.md index 814a6053..c08e33d9 100644 --- a/.github/actions/slack_notifier/README.md +++ b/.github/actions/slack_notifier/README.md @@ -8,8 +8,8 @@ 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. +| `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 @@ -24,6 +24,6 @@ steps: # 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' + slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }} + slack_channel_id: 'C05447SKNH3' ``` diff --git a/.github/actions/slack_notifier/action.yml b/.github/actions/slack_notifier/action.yml index cb742790..cb5f9bbe 100644 --- a/.github/actions/slack_notifier/action.yml +++ b/.github/actions/slack_notifier/action.yml @@ -2,11 +2,11 @@ name: Slack Notifier description: Send Slack updates to the provided channel upon workflow completion. inputs: - slack-bot-token: + slack_bot_token: type: string required: true # This could be refactored to accept a comma-delimited list of IDs if needed. - slack-channel-id: + slack_channel_id: type: string required: true @@ -28,13 +28,13 @@ runs: - name: prepare_inputs shell: bash run: | - echo "slack-bot-token=${{ inputs.slack-bot-token }}" - echo "slack-channel-id=${{ inputs.slack-channel-id }}" + 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/slack-github-action@v1.24.0 with: - channel-id: ${{ inputs.slack-channel-id }} + channel-id: ${{ inputs.slack_channel_id }} payload: | { "blocks": [ @@ -49,4 +49,4 @@ runs: ] } env: - SLACK_BOT_TOKEN: ${{ inputs.slack-bot-token }} + SLACK_BOT_TOKEN: ${{ inputs.slack_bot_token }} diff --git a/.github/actions/slack_notifier/test/action.test.ts b/.github/actions/slack_notifier/test/action.test.ts index ce519f80..7ee63ded 100644 --- a/.github/actions/slack_notifier/test/action.test.ts +++ b/.github/actions/slack_notifier/test/action.test.ts @@ -32,6 +32,6 @@ test('Inputs are set correctly', async () => { name: 'prepare_inputs' }); - const expectedOutput = `slack-bot-token=${slackBotToken}\nslack-channel-id=${slackChannelId}`; + const expectedOutput = `slack_bot_token=${slackBotToken}\nslack_channel_id=${slackChannelId}`; expect(result.output).toEqual(expectedOutput) }); diff --git a/.github/actions/slack_notifier/test/action_test.yml b/.github/actions/slack_notifier/test/action_test.yml index 746e9193..abeef043 100644 --- a/.github/actions/slack_notifier/test/action_test.yml +++ b/.github/actions/slack_notifier/test/action_test.yml @@ -10,5 +10,5 @@ jobs: path: slack_notifier - uses: ./slack_notifier with: - slack-bot-token: 'mytoken' - slack-channel-id: 'mychannelid' \ No newline at end of file + slack_bot_token: 'mytoken' + slack_channel_id: 'mychannelid' \ No newline at end of file