actions / github-script

Write workflows scripting the GitHub API in JavaScript
MIT License
4.13k stars 410 forks source link

The step is failing with the error: `SyntaxError: Unexpected number` #423

Closed LucasMMota closed 10 months ago

LucasMMota commented 11 months ago

Describe the bug The step is failing with the error: SyntaxError: Unexpected number

Run actions/github-script@v6
  with:
    script: github.rest.issues.createComment({
    issue_number: context.issue.number,
    owner: context.repo.owner,
    repo: context.repo.repo,
    body: ':x: **Job run failed**. Approvals dismissed. Please check'
  })
    github-token: ***
    debug: false
    user-agent: actions/github-script
    result-encoding: json
    retries: 0
    retry-exempt-status-codes: 400,401,403,404,422
  env:
    pythonLocation: /opt/hostedtoolcache/Python/3.9.18/x64
    PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.9.18/x64/lib/pkgconfig
    Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.9.18/x64
    Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.9.18/x64
    Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.9.18/x64
    LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.9.18/x64/lib
SyntaxError: Unexpected number
    at new AsyncFunction (<anonymous>)
Error: Unhandled error: SyntaxError: Unexpected number
    at callAsyncFunction (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15143:16)
    at main (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15236:26)
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15217:1
    at /home/runner/work/_actions/actions/github-script/v6/dist/index.js:15268:3
    at Object.<anonymous> (/home/runner/work/_actions/actions/github-script/v6/dist/index.js:15271:12)
    at Module._compile (node:internal/modules/cjs/loader:1198:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
    at Module.load (node:internal/modules/cjs/loader:1076:32)
    at Function.Module._load (node:internal/modules/cjs/loader:911:12)

To Reproduce I'm not sure this is intermittent or not

Expected behavior post comment on PR

joshmgross commented 11 months ago

@LucasMMota can you share your workflow file?

LucasMMota commented 11 months ago

@joshmgross

name: Run CI job on dbt Cloud

on:
  pull_request_review: # triggers on PR approval
    types:
      - submitted

jobs:
  run-job:
    name: job name
    runs-on: ubuntu-latest

    steps:
      - name: Checkout
        uses: "actions/checkout@v3"
        with:
          ref: ${{ github.event.pull_request.head.sha }}

      - name: Python set up
        uses: "actions/setup-python@v4"
        with:
          python-version: "3.9"

      ... other steps removed here

      - name: Comment failure in PR
        uses: "actions/github-script@v6"
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: ':x: **Job run failed**. Approvals dismissed. Please check
            })
joshmgross commented 11 months ago

@LucasMMota it looks like you have an unclosed string in your body argument:

body: ':x: **Job run failed**. Approvals dismissed. Please check

Could you try body: ':x: **Job run failed**. Approvals dismissed. Please check' and see if that fixes your issue?

LucasMMota commented 11 months ago

Sorry, I updated the code just to exemplify, that'd be the real step (it has no error in this sense):

      - name: Comment failure in PR
        if: steps.monitor_job.outputs.run_succeeded != 'true'
        uses: "actions/github-script@v6"
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: ':x: **Job run failed**. Approvals dismissed. Please check: ${{ steps.trigger_job.outputs.run_status_link }}'
            })
joshmgross commented 11 months ago

It's possible something in that output isn't being escaped and breaking the JS syntax, could you try:

- name: Comment failure in PR
  if: steps.monitor_job.outputs.run_succeeded != 'true'
  uses: "actions/github-script@v6"
  env:
    RUN_STATUS_LINK: ${{ steps.trigger_job.outputs.run_status_link }}
  with:
    script: |
      github.rest.issues.createComment({
        issue_number: context.issue.number,
        owner: context.repo.owner,
        repo: context.repo.repo,
        body: ':x: **Job run failed**. Approvals dismissed. Please check: ${ process.env.RUN_STATUS_LINK }'
      })
LucasMMota commented 11 months ago

I updated and it's like this, but same error continues happening 🤔 (it worths it to mention that it runs gracefully in the first time and break in the second:

First step (ok):

      - name: Comment link in PR
        uses: "actions/github-script@v6"
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: ':link: **Job run in dbt cloud:** ${{ steps.trigger_job.outputs.run_status_link }}'
            })

Second step (failing):


      - name: Comment failure in PR
        if: steps.monitor_job.outputs.run_succeeded != 'true'
        uses: "actions/github-script@v6"
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: ':x: **Job run failed**. Approvals dismissed. Please check run in the dbt cloud.'
            })
            exit 1
LucasMMota commented 11 months ago

Just checked that other workflows that run once, post the comment successfully. Only this one that posts twice is failing. It seems something on the "actions/github-script@v6" on the second post.

joshmgross commented 11 months ago

@LucasMMota that exit 1 in your second step is not valid JavaScript

The closest analogue in Node would be process.exit(1)

The actions/core library providers a wrapper around that with core.SetFailed which also allows you to pass a message:


      - name: Comment failure in PR
        if: steps.monitor_job.outputs.run_succeeded != 'true'
        uses: "actions/github-script@v6"
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: ':x: **Job run failed**. Approvals dismissed. Please check run in the dbt cloud.'
            })
            core.setFailed('Job run failed')
joaopaulobiffi commented 10 months ago

thank you @joshmgross, your solution solved the problem