CircleCI-Public / slack-orb

Create custom Slack notifications for CircleCI job statuses
https://circleci.com/developer/orbs/orb/circleci/slack
MIT License
214 stars 206 forks source link

parse error: Invalid numeric literal #450

Closed juvarabrera closed 2 weeks ago

juvarabrera commented 4 months ago

Orb version:

4.9.3

What happened:

I'm trying to pass a custom message to slack orb. The custom message is already in json format

Expected behavior:

Should send the message in the Slack channel

Additional Information:

.circleci/config.yml

commands:
  notify_on_e2e_fail:
    description: Send slack notification on fail
    parameters:
      channel:
        type: string
      branch_pattern:
        type: string
        default: ".+"
      message:
        type: string
        default: ""
    steps:
      - slack/notify:
          event: fail
          channel: << parameters.channel >>
          custom: << parameters.message >>
          branch_pattern: << parameters.branch_pattern >>

. . .

jobs:
  myjob:
    steps:
      . . . # some steps that creates message.txt
      - notify_on_e2e_fail:
          channel: << parameters.slack_channel >>
          message: $(cat message.txt)

This is the value of message.txt

{"blocks": [{"type": "header","text": {"type": "plain_text","text": ":x: E2E Test Failed","emoji": true}},{"type": "section","text": {"type": "mrkdwn","text": "*Branch*: my/branch\n\n1 tests | ✅ 0 passed | :x: 1 failures | :x: 0 errors | 🔃 0 skipped"}},{"type": "divider"},{"type": "section","text": {"type": "mrkdwn","text": "*1. Test name*"}},{"type": "section","fields": [{"type": "mrkdwn","text": "*File*: path/to/file:123\n\n*Author*: Name <email>"},{"type": "mrkdwn","text": "*View Commit*: https://github.com/org_slug/repo/commit/abcdefg\n\n*Last updated*: 2024-01-01 00:00:00 +0800 (99 days ago)"}]},{"type": "section","fields": [{"type": "mrkdwn","text": "*File*: path/to/file:123\n\n*Author*: Name <email>"},{"type": "mrkdwn","text": "*View Commit*: https://github.com/org_slug/repo/commit/abcdefg\n\n*Last updated*: 2024-01-01 00:00:00 +0800 (99 days ago)"}]},{"type": "actions","elements": [{"type": "button","text": {"type": "plain_text","text": "View CircleCI Job","emoji": true},"value": "https://circleci.com/gh/org_slug/********/1234","action_id": "actionId-0"}]}]}

There are no issues in the Slack Block Kit Builder

This is the output of Slack orb:

BASH_ENV file: /tmp/.bash_env-663dc3f01fdefa2f8dbddf81-0-build
Does Not Exist. Skipping file execution
Posting Status
Checking For JQ + CURL
parse error: Invalid numeric literal at line 1, column 6
parse error: Invalid numeric literal at line 1, column 6

Exited with code exit status 4

Is this even possible? Because I tried to pass the json string manually like this and it worked.

      - notify_on_e2e_fail:
          channel: << parameters.slack_channel >>
          message: "{ ... }"

I feel like it has something to do with this specific line

message: $(cat message.txt)

I also tried

message: |
  $(cat message.txt)

Thank you for checking 🙇

marboledacci commented 3 weeks ago

You cannot use bash in the parameters, the $(cat message.txt) is managed as a string and the orb is not designed to eval this. You can get the value from a file though, you have two options:

  1. Use a step before to export the content of your file to an environment variable. More information here
  2. Use include, custom: <<include(message.txt)>>. More information here
juvarabrera commented 2 weeks ago

Sorry, I forgot about this issue. I've fixed it using the following

...
      - run:
          name: Generate E2E Error Message
          when: on_fail
          command: |
            ...
            echo 'export MESSAGE="$(cat message.json)"' >> $BASH_ENV
      - notify_on_e2e_fail: # my own command
          channel: << parameters.slack_channel >>
          template: MESSAGE
...

https://discuss.circleci.com/t/exporting-environment-variables-from-sourced-scripts/4564/4