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

Non-existing ENV variables are reported with ??? instead of empty value

Open jprecuch opened this issue 2 years ago • 4 comments

Description

If a env variable which is in payload is missing, ??? are sent instead of empty value. It would be nice to not put ??? which confuses people but leave it empty (like empty variable).

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

  • [x] bug
  • [ ] enhancement (feature request)
  • [ ] question
  • [ ] documentation related
  • [ ] example code related
  • [ ] 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

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version: latest

Steps to reproduce:

  1. Configure Slack payload json to contain some ENV variable which won't be existing f.e ${{ env.ACTIONS_URL }}
  2. Configure this github action to send notifiation where json contain this ENV
  3. When you check Slack notification it will contain ??? instead of empty variable

Expected result:

Instead of ??? I would expect missing variable to be shown just empty value (bash behavior)

Actual result:

Instead of empty, we get confusing ???

Attachments:

For example:

CVS api-tests: Successful: ??? | Failed: ??? | Total: ???
Metrics api-tests: Successful: ??? | Failed: ??? | Total: ???

jprecuch avatar Jul 03 '23 07:07 jprecuch

Hi @jprecuch, thanks for sharing this. It seems that an underlying module (Markup.js) does it when a given input is undefined (source). Perhaps, to customize the behavior, passing empty string values instead in context can help.

seratch avatar Jul 05 '23 01:07 seratch

I wanted to work on this issue, but I am having some trouble reproducing this behavior. Here's what I have in my workflow:

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the "main" branch
  push:
    branches: ["main"]
  pull_request:
    branches: ["main"]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      - name: Send custom JSON data to Slack workflow
        id: slack
        uses: slackapi/[email protected]
        with:
          payload: |
            {
              "text": "This is an invalid environment variable: ${{ env.SAMPLE_ENV_VAR }}",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "This is an invalid environment variable: ${{ env.SAMPLE_ENV_VAR }}"
                  }
                }
              ]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

SAMPLE_ENV_VAR is the non-existing environment variable but I do not see ??? in the output:

image

Am I misunderstanding something?

arunsathiya avatar Oct 30 '23 00:10 arunsathiya

I wanted to work on this issue, but I am having some trouble reproducing this behavior. ... Am I misunderstanding something?

yes. do it with payload-file-path.

    - name: "Slack Notify: Deployment Complete"
      uses: slackapi/slack-github-action@6c661ce58804a1a20f6dc5fbee7f0381b469e001
      id: slack-notify
      env:
        SLACK_BOT_TOKEN: ${{ inputs.SlackBotToken }}
        DEPLOYRELEASEBRANCH_APPNAME: ${{ inputs.AppName }}
        DEPLOYRELEASEBRANCH_APPVERSION: ${{ inputs.AppVersion }}
        DEPLOYRELEASEBRANCH_ENVIRONMENT: ${{ inputs.Environment }}
        DEPLOYRELEASEBRANCH_JOBNAME: ${{ inputs.JobName }}
        DEPLOYRELEASEBRANCH_JOBURL: ${{ inputs.JobUrl }}
        DEPLOYRELEASEBRANCH_GITHUBACTOR: ${{ inputs.GithubActor }}
      with:
        channel-id: ${{ inputs.SlackChannelId }}
        update-ts: ${{ inputs.MessageId }}
        payload-file-path: ${{ github.action_path }}/slack-notification.json

airtonix avatar Apr 04 '24 06:04 airtonix

Somewhat related, but missing github.* variables in the payload-file-path are also displayed as ???. This specific case is being tracked over in #203!

zimeg avatar Apr 04 '24 21:04 zimeg

👋 I'm finding this can use magic syntax @seratch suggested to default to empty values from the workflow file! With the latest @v2 release, the following step might be useful:

- name: Share an update
  uses: slackapi/[email protected]
  with:
    method: chat.postMessage
    payload-file-path: ./example.json
    payload-templated: true
    token: ${{ secrets.SLACK_BOT_TOKEN }}
  env:
    COMMIT_HASH: ${{ github.sha || '' }}               # This line has the blank default!
    SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }}

With an example.json like so:

{
  "channel": "${{ env.SLACK_CHANNEL_ID }}",
  "text": "A commit landed: ${{ env.COMMIT_HASH }}"
}

If the github event was to not have the matching values, the empty string would be used!

This should also apply to the @v1 tags so I'll close this issue, but wanted to leave a few more notes for updating 📚

  • The payload-templated variable can be used for both types of payloads!
  • The payload-templated variable replaces the payload-file-path-parsed option!
  • The payload-templated variable must be true to replace templated variables!

Finding certain values in the GitHub context can also be a cause of confusion, and options available when replacing templatized variables are available from the default GitHub event.

I'll close this issue with the workaround, but please feel free to comment with other ideas or follow up questions about this. I'm super open to ideas for enhancement here 🙏

zimeg avatar Nov 16 '24 05:11 zimeg