Search code examples
githubgithub-actionscicd

toJSON in message in github action


I'm using the slack-api for every pr, but I noticed that sometimes when I post a pr on github, the title contains substitute characters(�), which causes an invalid json error(passed in payload was invalid JSON Error: Error: Need to provide valid JSON payload). It works when I add toJSON, but the problem is when there is a message with it.

The following code is what I solved in another github yml.

      - name: slack notification
        id: slack
        uses: slackapi/[email protected]
        with:
          channel-id: ""
          payload: |
            {
             "text": ${{ toJSON(github.event.head_commit.message) }},
             "blocks": [
                {
                  "type": "section",
                  "fields":[
                    {
                      "type": "mrkdwn",
                      "text": "${{ github.ref }}"
                    }
                  ]
                }
              ]
            }

However, I'm having problems with the following cases: As you can see, the messages are enclosed.

      - name: Post to a Slack channel
        id: slack
        uses: slackapi/[email protected]
        with:
          channel-id: ""
          payload: |
            {
             "text": "GitHub Action build result: ${{ job.status }}\n${{ github.event.pull_request.html_url }}",
             "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "new PR! :pull_request: \n <${{ github.event.pull_request.html_url }}|${{ toJSON(github.event.pull_request.title) }}>"
                  }
                }
              ]
             }

I'm still getting invalid json errors because the quotes are inside the quotes. I thought about replacing them all with quotes, but I don't think that's a good idea. What should I do?

Is that the only way to use single quotes?


Solution

  • The easiest solution it to convert these non UTF-8 strings to UTF-8 and eliminate substitute characters (�) altogether.

    You can perform this conversion in a shell step before invoking your Slack action. The example below uses iconv to convert the text from Windows-1252 to UTF-8. After conversion, the corrected title is saved as an output and then accessed as ${{ steps.utf.outputs.title }} in inside the message. This process ensures that all substitute characters are correctly converted back into their proper letters and saves you using toJson() that adds extra double quotes that caused an error you described.

    - name: Convert to UTF-8
      id: utf
      run: |
        title=$(echo -n '${{ github.event.pull_request.title }}' | iconv -f WINDOWS-1252 -t UTF-8)
        echo title="$title" >> $GITHUB_OUTPUT
    - name: Post to a Slack channel
      id: slack
      uses: slackapi/[email protected]
      with:
        channel-id: ""
        payload: |
          {
           "text": "GitHub Action build result: ${{ job.status }}\n${{ github.event.pull_request.html_url }}",
           "blocks": [
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "new PR! :pull_request: \n <${{ github.event.pull_request.html_url }}|${{ steps.utf.outputs.title }}>"
                }
              }
            ]
           }