From 81a1dc0cd9125fcd4ebb978042f69ebdf636faf4 Mon Sep 17 00:00:00 2001 From: Fil Maj Date: Wed, 17 May 2023 14:53:56 -0600 Subject: [PATCH] Fixed to be able to use env of Step (#200) Use `event` github context object property rather than payload for repository and commit payload file integration test. Tweak repository and commit fields in payload file test. Tweak author field in payload file test. Drop message, link to user profile for actor field. Co-authored-by: hideki narimiya --- .github/resources/payload-notification.json | 36 ++++++++++++++------- .github/workflows/main.yml | 9 ++++-- README.md | 3 ++ src/slack-send.js | 4 +-- 4 files changed, 36 insertions(+), 16 deletions(-) diff --git a/.github/resources/payload-notification.json b/.github/resources/payload-notification.json index 13fdef46..83908bdf 100644 --- a/.github/resources/payload-notification.json +++ b/.github/resources/payload-notification.json @@ -1,13 +1,27 @@ { - "text": "Incoming Webhook test for slack send", - "blocks": [ - { - "type": "section", - "text": { - "type": "plain_text", - "text": "Content of the event name: {{ github.event_name }}", - "emoji": true - } - } - ] + "text": "A GitHub Action Event ${{ github.eventName }} has ${{ env.JOB_STATUS }}", + "attachments": [{ + "color": "${{ env.ATTACHMENT_COLOR }}", + "fields": [{ + "title": "Repository", + "short": false, + "value": "<${{ github.payload.repository.html_url }}|${{ github.payload.repository.full_name }}>" + }, { + "title": "Ref", + "short": false, + "value": "${{ github.ref }}" + }, { + "title": "Commit", + "short": false, + "value": "<${{ github.payload.repository.html_url }}/commit/${{ github.sha }}|${{ github.sha }}>" + }, { + "title": "Author", + "short": false, + "value": "" + }, { + "title": "Workflow", + "short": false, + "value": "<${{ github.payload.repository.html_url }}/actions/runs/${{ github.runId }}|${{ github.workflow }}>" + }] + }] } diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8d7a6efd..98459f28 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -36,7 +36,7 @@ jobs: id: slackThreadResponse uses: ./ with: - channel-id: ${{ secrets.SLACK_CHANNEL_ID }} + channel-id: ${{ secrets.SLACK_CHANNEL_ID }} payload: | { "text": "This message should be posted as a response in thread", @@ -72,7 +72,7 @@ jobs: payload: "{\"author\":\"${{ github.event.sender.login }}\",\"url\":\"${{ env.URL}}\", \"repoName\":\"${{ github.event.repository.full_name }}\", \"status\":\"${{ job.status }}\"}" env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} - + # Use the output from the `slackWorkflow` step - name: Check Action output is not empty @@ -103,7 +103,8 @@ jobs: steps: - uses: actions/checkout@v3 - run: npm ci && npm run build - - run: echo "${{ github.event_name }}" + - name: Dump out GitHub Context + run: echo '${{ toJSON(github) }}' - name: Post message to Slack with Payload path id: slackPayloadFile uses: ./ @@ -112,6 +113,8 @@ jobs: env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_INCOMING_WEBHOOK_URL }} SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK + JOB_STATUS: ${{ job.status }} + ATTACHMENT_COLOR: ${{ (job.status == 'success' && 'good') || (job.status == 'failure' && 'danger') || 'warning' }} # Use the output from the `slackIncoming` step - name: Check Action output is not empty diff --git a/README.md b/README.md index 21ac2ec4..168627d4 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,9 @@ or SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} ``` +> **Warning** +> [github actions contexts](https://docs.github.com/en/actions/learn-github-actions/contexts) is not available in payload-file-path. Instead, you can use the context from [@actions/github](https://github.com/actions/toolkit/tree/main/packages/github) or the env defined in the step. + ## Technique 2: Slack App By creating a new Slack app or using an existing one, this approach allows your GitHub Actions job to post a message in a Slack channel or direct message by utilizing the [chat.postMessage](https://api.slack.com/methods/chat.postMessage) API method. Using this approach you can instantly post a message without setting up Slack workflows. diff --git a/src/slack-send.js b/src/slack-send.js index ed350e87..86c99ac4 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -39,8 +39,8 @@ module.exports = async function slackSend(core) { try { payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8'); // parse github context variables - const context = { github: github.context }; - const payloadString = payload.replace('$', ''); + const context = { github: github.context, env: process.env }; + const payloadString = payload.replaceAll('${{', '{{'); payload = markup.up(payloadString, context); } catch (error) { // passed in payload file path was invalid