Skip to content

Commit

Permalink
Use double quotes to preserve adjacent spaces correctly
Browse files Browse the repository at this point in the history
I used this action as part of a workflow to add code formatted blocks.
Using the guide in the readme I, adjacent spaces where not correctly
preserved, i.e. the following
```
      - name: yolo
        id: get-comment-body
        run: |
          echo "\`\`\`" > tmp/comment.md
          echo "+--------------+" >> tmp/comment.md
          echo "|This is a test|" >> tmp/comment.md
          echo "+--------------+" >> tmp/comment.md
          echo "|     yolo     |" >> tmp/comment.md
          echo "+--------------+" >> tmp/comment.md
          echo "\`\`\`" >> tmp/comment.md

          cat tmp/comment.md

          # https://github.com/peter-evans/create-or-update-comment#setting-the-comment-body-from-a-file
          body="$(cat tmp/comment.md)"
          body="${body//'%'/'%25'}"
          body="${body//$'\n'/'%0A'}"
          body="${body//$'\r'/'%0D'}"
          echo ::set-output name=body::$body

      - name: Create or update comment
        uses: peter-evans/create-or-update-comment@v1
        with:
          body: ${{ steps.get-comment-body.outputs.body }}
          issue-number: ${{ github.event.pull_request.number }}
```
produced this
```
+--------------+
|This is a test|
+--------------+
| yolo |
+--------------+
```
Adding the double quotes around `set-output` fixed this:
```
+--------------+
|This is a test|
+--------------+
|     yolo     |
+--------------+
```
Further I added double quotes also around the initial `cat`, because of
this comment https://github.community/t/set-output-truncates-multiline-strings/16852/5
> Here are a few observation. First, it is important to suppress word-splitting upon expansion in bash. This is easiest done by enclosing with double quotes:
  • Loading branch information
mfn committed Oct 26, 2021
1 parent dfd809e commit 6484e4d
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -164,11 +164,11 @@ The content must be [escaped to preserve newlines](https://github.community/t/se
```yml
- id: get-comment-body
run: |
body=$(cat comment-body.txt)
body="$(cat comment-body.txt)"
body="${body//'%'/'%25'}"
body="${body//$'\n'/'%0A'}"
body="${body//$'\r'/'%0D'}"
echo ::set-output name=body::$body
echo "::set-output name=body::$body"
- name: Create comment
uses: peter-evans/create-or-update-comment@v1
Expand Down

0 comments on commit 6484e4d

Please sign in to comment.