slackapi / slack-github-action

Send data into Slack using this GitHub Action!
MIT License
938 stars 148 forks source link

Sending formatted release to slack as payload? #304

Closed dingo-d closed 5 months ago

dingo-d commented 5 months ago

Description

I'm trying to send release notes to Slack channel when I create a release on GH using this workflow:

name: "Post release notes to Slack"

on:
  release:
    types: ["published"]

jobs:
  slack-notify:
    name: "Post release notes to Slack"
    runs-on: "ubuntu-latest"
    steps:
      - name: "Send release notes to Slack"
        uses: "slackapi/slack-github-action@v1.25.0"
        with:
          channel-id: "releases"
          payload: |
            {
              "text": "Release notes",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "# Release notes - ${{ github.event.release.tag_name }}"
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "${{ github.event.release.body }}"
                  }
                },
              ]
            }
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}

But I keep getting error: Error: Need to provide valid JSON payload.

I've tried to wrap the notes in toJSON, but got an error. Exposing it as env variable, still got the error. The only way this worked was when I tried:

slack-message: "${{ github.event.release.body }}"

instead of payload, but in that case I get unstyled markdown in my channel.

Is it possible to send the markdown to the channel in Slack and for it to be properly formatted? I am using the action to send the notification about deployment successes or failures on GH, and it works great (also as a payload with a markdown).

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

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

Fluorz commented 5 months ago

Yo exactly the same

zimeg commented 5 months ago

Hey @dingo-d 👋 The trailing comma in your payload might be causing problems! Perhaps it's something similar for you @Fluorz? Please let me know if this fixes things!

          payload: |
            {
              "text": "Release notes",
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "# Release notes - ${{ github.event.release.tag_name }}"
                  }
                },
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "${{ github.event.release.body }}"
                  }
-               },
+               }
              ]
            }
dingo-d commented 5 months ago

I'll try 👍🏼 Totally didn't notice the trailing comma 🙈

EDIT: Nope, still got the failure. The action details look like this:

Run slackapi/slack-github-action@v1.25.0
  with:
    channel-id: releases
    payload: {
    "text": "Release notes",
    "blocks": [
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": "# Release notes - 1.87.6"
        }
      },
      {
        "type": "section",
        "text": {
          "type": "mrkdwn",
          "text": "## What's Changed

  * Attempt to fix the release notes action by @dingo-d in https://github.com/pull-request/number

  Note: This won't be deployed in a bugfix, as it's just a process fix (attempt?).

  **Full Changelog**: https://github.com/repo-name/compare/1.87.5...1.87.6"
        }
      }
    ]
  }

  env:
    SLACK_BOT_TOKEN: ***
zimeg commented 5 months ago

@dingo-d thanks for sharing a full example, it's super helpful! Multiple lines aren't supported without proper escaping as JSON input, so this might require some preprocessing. If you can use the github.event.release.body then this change might be useful:

                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
-                   "text": "${{ github.event.release.body }}"
+                   "text": ${{ toJSON(github.event.release.body) }}
                  }
                }
dingo-d commented 5 months ago

Ok, so this works. Kinda 😅. I'm still getting non-formatted results. Or maybe slack only allows specific subsection of markdown syntax.

In an action where I'm using your actions for deployment notification, the bold works. Here I'm using the heading syntax # or ## but these are staying the same. Also, list items are not being formatted.

I guess this is a limitation of slack formatting 🤷🏼‍♂️

EDIT: Seems like not all formatting is allowed 🥲

https://www.markdownguide.org/tools/slack/