Skip to content

Multi-line JSON arrives as a single line #377

@dbeilin

Description

@dbeilin

Description

I'm trying to send a message by adding a JSON to the markdown block. The JSON itself is a multi-line JSON, but the message arrives as a single-line

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

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

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

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

I have this composite action to send Slack messages:

name: "Slack Notification"
description: "Send Slack notifications for GitHub Actions workflows"

inputs:
  slack_bot_token:
    description: "Slack bot token"
    required: true
  channel_id:
    description: "Slack channel id"
    required: false
    default: "channel"
  message:
    description: "Message to send to Slack"
    required: true
  blocks:
    description: "Optional Slack blocks for advanced message formatting"
    required: false

runs:
  using: "composite"
  steps:
    - name: Send Slack notification
      uses: slackapi/slack-github-action@v2.0
      with:
        method: chat.postMessage
        token: ${{ inputs.slack_bot_token }}
        payload: |
          channel: "${{ inputs.channel_id }}"
          text: "${{ inputs.message }}"
          blocks:
          ${{ inputs.blocks }}

Which I call in this example workflow:

name: send-slack

on:
  workflow_dispatch:
  push:
    branches:
      - main

jobs:
  send-slack:
    name: send-slack
    runs-on: ubuntu-latest

    steps:
      - name: Create JSON file
        run: |
          echo '{ "added": [], "removed": [] }' > diff.json

      - name: Set output
        id: vars
        run: |
          {
            echo 'DIFF_CONTENT<<EOF'
            jq '.' diff.json
            echo 'EOF'
          } >> $GITHUB_OUTPUT

      - name: echo (debug)
        run: |
          echo "${{ steps.vars.outputs.DIFF_CONTENT }}"

      - name: Notification
        uses: dbeilin/orangutan/composite/send-slack-notification@main
        with:
          slack_bot_token: ${{ secrets.SLACK_BOT_TOKEN }}
          channel_id: "@user"
          message: "message"
          blocks: |
            - type: "section"
              text:
                type: "mrkdwn"
                text: '```${{ steps.vars.outputs.DIFF_CONTENT }}```'

My echo step shows a multi-line JSON:
image

But the message arrives as a single line:
image

It might be related to quotes, because when I simply copy and pasted the following payload into the block kit playground it seems to work:

{
	"blocks": [
		{
			"type": "section",
			"text": {
				"text": "```{\n  \"added\": [],\n  \"removed\": []\n}```",
				"type": "mrkdwn"
			}
		}
	]
}

image

  • I tried adding toJSON() in my markdown block, but it didn't work.
  • I tried removing the quotes in the composite: text: "${{ inputs.message }}", but it didn't work as well.

Feel like any other change I make just breaks the payload.

Any help on this would be appreciated :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions