slack-github-action icon indicating copy to clipboard operation
slack-github-action copied to clipboard

Github context is not properly referenced within the JSON payload template

Open ylkhayat opened this issue 3 years ago • 4 comments

Description

Github context is not properly referenced when using the workflow_run

What type of issue is this? (place an x in one of the [ ])

  • [x] bug
  • [ ] enhancement (feature request)
  • [ ] question
  • [ ] documentation related
  • [ ] example code related
  • [x] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Create a workflow_run github action

Reproducible in:

package version: v1.18.0 node version: v16.13.0 OS version(s):

Steps to reproduce:

  1. create a Github Action running on the workflow_run events
  2. add a json template that uses ${{github.event.workflow_run.name}} for example
  3. fire the event, and the json template will not properly replace the github context

Example of my JSON template

{
  "text": "[Codebase News] - Pipeline is 🔴",
  "attachments": [
    {
      "color": "#000",
      "blocks": [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "Pipeline is 🔴 on `master`."
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "Workflow `${{github.event.workflow_run.name}}` requires extra attention. :warning:"
          }
        },
        {
          "type": "context",
          "elements": [
            {
              "type": "mrkdwn",
              "text": "More info here ${{github.server_url}}/${{github.repository}}/actions/runs/${{github.github.event.workflow_run.run_id}}"
            }
          ]
        }
      ]
    }
  ]
}

Expected result:

I would expect for the JSON file to have in hand the entire provided github context corresponding to the workflow_run event triggers.

Actual result:

As you can see ${{github.event.workflow_run.name}} did not work as expected image Also the regular github context did not work as well, here image

Side Node:

I looked into this issue https://github.com/slackapi/slack-github-action/issues/51, and was aware that the problem got fixed, but I tested it multiple times, it was not properly fixed.

ylkhayat avatar Apr 23 '22 20:04 ylkhayat

Currently facing the same problem.

FredericoIRodrigues avatar May 17 '22 12:05 FredericoIRodrigues

@yousseftarekkh i was able to see github context variables message data in my slack message . i have done following changes in payload json by adding spaces between context variables

{
  "text": "[Codebase News] - Pipeline is 🔴",
  "attachments": [
    {
      "color": "#000",
      "blocks": [
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "Pipeline is 🔴 on `master`."
          }
        },
        {
          "type": "section",
          "text": {
            "type": "mrkdwn",
            "text": "Workflow `${{ github.event.workflow_run.name }}` requires extra attention. :warning:"
          }
        },
        {
          "type": "context",
          "elements": [
            {
              "type": "mrkdwn",
              "text": "More info here ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.event.workflow_run.run_id }}"
            }
          ]
        }
      ]
    }
  ]
}

i have received below message in slack image regarding github.event.workflow_run.name that is bug .

nitintecg avatar Jun 04 '22 15:06 nitintecg

When using a payload template, slack-action gets the github.context from actions/toolkit and replaces the variables in the template. You will find that there is no github.event.* and github.repository in the toolkit, you can get github.serverUrl but not github.server_url.

taofang2021 avatar Jul 20 '22 09:07 taofang2021

I have the same problem with the following snippet in the payload file:

        {
          "type": "button",
          "text": {
            "type": "plain_text",
            "text": "▶️ Check running actions"
          },
          "style": "primary",
          "url": "https://github.com/my-company/platform/actions?query=branch%3A${{ env.BRANCH_NAME }}"
        },
        {
          "type": "button",
          "text": {
            "type": "plain_text",
            "text": "📑 Open PR"
          },
          "url": "https://github.com/my-company/platform/pull/${{ steps.head-ref-match.outputs.group1 }}"
        },

Neither my custom env variable in {{ env.BRANCH_NAME }} nor referencing another step output with ${{ steps.head-ref-match.outputs.group1 }} seems to work. If I use them in payload text within the workflow file directly it works.

Any workaround for this?

magnattic avatar Aug 18 '22 17:08 magnattic

This bug its horrible please release a bugfix image

The most basic and important thing of this app (sending messages with github info) not working

ivorsmorenburg avatar Nov 04 '22 13:11 ivorsmorenburg

This makes me want to switch off from Slack

ivorsmorenburg avatar Nov 04 '22 13:11 ivorsmorenburg

I am seeing the same issue here. The payload-file-path template does not replace the env vars in the JSON template.

It looks like razor54 has a fix ready to go for this. Seems like the payload-file-path functionality is basically broken until this gets resolved.

Workflow code

      - name: Workflow init slack message
        id: slack-init-msg
        uses: slackapi/[email protected]
        env:
          STATUS: "Success"
          BRANCH: "DMZ-000_Test_stuff"
          APP_NAME: "My_sweet_app"
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
          WORKFLOW_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
        with:
          channel-id: "my-test-channel"
          payload-file-path: ./.github/slack-msg-payloads/init-msg.json

Template JSON file

{
    "blocks": [
    {
        "type": "header",
        "text": {
        "type": "plain_text",
        "text": "Deployment of ${{ env.APP_NAME }} to ${{ env.ENVIRO }} started",
        "emoji": true
        }
    },
    {
        "type": "context",
        "elements": [
        {
            "type": "mrkdwn",
            "text": "*Workflow URL:* `${{ env.WORKFLOW_URL }}`\n*BRANCH:* `${{ env.BRANCH }}`\n*Triggered by:* `${{ github.actor }}`"
        }
        ]
    },
    {
        "type": "divider"
    }
    ],
    "attachments": [
    {
        "color": "#0328fc",
        "blocks": [
        {
            "type": "section",
            "text": {
            "type": "mrkdwn",
            "text": "This area will be updated with the workflow progress, stay tuned!\n*Workflow status:* ${{ env.STATUS }}"
            }
        }
        ]
    }
    ]
}

Output in Slack Screen Shot 2022-12-07 at 8 23 57 PM

jr-surfline avatar Dec 08 '22 04:12 jr-surfline

any news on this? experiencing the same issue using a slack webhook and a json payload in a template file

rd-florian-stagliano avatar May 16 '23 15:05 rd-florian-stagliano

We fixed this in #200; assigning to the 1.24 milestone, which we will release shortly.

filmaj avatar May 17 '23 21:05 filmaj

Release v1.24.0 is out!

filmaj avatar May 17 '23 21:05 filmaj