diff --git a/.github/workflows/post-pr-comment.yml b/.github/workflows/post-pr-comment.yml index fc0cefbe0d7..3933f55a37a 100644 --- a/.github/workflows/post-pr-comment.yml +++ b/.github/workflows/post-pr-comment.yml @@ -42,24 +42,20 @@ jobs: COMMENT_AUTHOR="github-actions[bot]" PR_NUMBER="$(grep -o '^[0-9]\+$' pr_number.txt)" - # comment_id.txt may be empty if there is no existing comment - if [ -s comment_id.txt ] - then - COMMENT_ID="$(grep -o '^[0-9]\+$' comment_id.txt)" - fi + # If there is no existing comment, comment_id.txt will contain just a + # newline (due to jq & gh behaviour). This will cause grep to fail, so + # we catch that. + COMMENT_ID=$(grep -o '^[0-9]\+$' comment_id.txt || true) - if [ -z "$COMMENT_ID" ] + if [ $COMMENT_ID ] then - # Create new comment - jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --input - - else # Fetch existing comment, and validate: # - comment belongs to the PR with number $PR_NUMBER # - comment starts with the expected prefix ("QHelp previews") # - comment author is github-actions[bot] FILTER="select(.issue_url | test(\"${GITHUB_REPOSITORY}/issues/${PR_NUMBER}$\")) \ | select(.body | test(\"^${COMMENT_PREFIX}\")) \ - | select(.user.login == \"${COMMENT_AUTHOR}\") \ + | select(.user.login == \"${COMMENT_AUTHOR}\") \ | .id" COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" | jq "${FILTER}") if [ $COMMENT_ID ] @@ -69,6 +65,9 @@ jobs: else echo "Comment ${COMMENT_ID} did not pass validations: not editing." fi + else + # Create new comment + jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --input - fi env: GITHUB_TOKEN: ${{ github.token }}