Search code examples
githubyamlgithub-actionsgithub-issues

Github action create an issue with body a table


- name: Create Issue
  if: steps.check_issue.outputs.issue_exists == 'false'
  run: |
       issue_body=$(echo '{
        "title": "Titolo ${{  steps.check_repo.outputs.code }}",
        "body": "${{  steps.check_repo.outputs.table }}"
       }')
       curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -d "$issue_body" https://api.github.com/repos/${GITHUB_REPOSITORY}/issues

enter image description here

I'm trying to create an issue automatically via github action with body a table as seen below, but it's not working, I'm having the following problems that can be seen in the image.

The table instead of wrapping the elements, they should all be on a single line but with a \n instead of wrapping, I tried | tr '\n' '\\n' I couldn't do it, can you help me.


Solution

  • You can not use multiline strings in JSON. You have to use "\n" to encode multiline. This works for me:

    issue_body='{
      "title": "Titolo",
      "body": "|a|b|\n|-|-|\n|1|2|"
    }'
    
    curl -X POST -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" -d "$issue_body" https://api.github.com/repos/${GITHUB_REPOSITORY}/issues
    

    The following complete workflow works for me in converting a multiline string sent via an output to an encoded multiline and JSON payload. awk is used to read input line by line and output each line followed by 2 characters "" and "n". awk removes LF character '\n' when reading input lines.

    name: multiline-body
    
    on:
      workflow_dispatch
    
    jobs:
      create-issue:
        runs-on: ubuntu-latest
        steps:
          - name: Output multiline table
            id: check_repo
            run: |
              {
              echo "table<<EOF"
              echo '| a | b |
              |---|---|
              | 1 | 2 |
              '
              echo EOF
              } >> $GITHUB_OUTPUT
          - name: Create table environment
            run: |
              echo -n "table=${{ steps.check_repo.outputs.table }}" | awk '{printf "%s\\n", $0}' >> $GITHUB_ENV
    
          - name: Create Issue
            run: |
              issue_body='{
                "title": "Titolo",
                "body": "${{ env.table }}"
              }'
    
              curl -X POST -H "Authorization: token ${{ secrets.PAT }}" -d "$issue_body" https://api.github.com/repos/${GITHUB_REPOSITORY}/issues