@@ -42,24 +42,20 @@ jobs:
42
42
COMMENT_AUTHOR="github-actions[bot]"
43
43
PR_NUMBER="$(grep -o '^[0-9]\+$' pr_number.txt)"
44
44
45
- # comment_id.txt may be empty if there is no existing comment
46
- if [ -s comment_id.txt ]
47
- then
48
- COMMENT_ID="$(grep -o '^[0-9]\+$' comment_id.txt)"
49
- fi
45
+ # If there is no existing comment, comment_id.txt will contain just a
46
+ # newline (due to jq & gh behaviour). This will cause grep to fail, so
47
+ # we catch that.
48
+ COMMENT_ID=$(grep -o '^[0-9]\+$' comment_id.txt || true)
50
49
51
- if [ -z " $COMMENT_ID" ]
50
+ if [ $COMMENT_ID ]
52
51
then
53
- # Create new comment
54
- jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --input -
55
- else
56
52
# Fetch existing comment, and validate:
57
53
# - comment belongs to the PR with number $PR_NUMBER
58
54
# - comment starts with the expected prefix ("QHelp previews")
59
55
# - comment author is github-actions[bot]
60
56
FILTER="select(.issue_url | test(\"${GITHUB_REPOSITORY}/issues/${PR_NUMBER}$\")) \
61
57
| select(.body | test(\"^${COMMENT_PREFIX}\")) \
62
- | select(.user.login == \"${COMMENT_AUTHOR}\") \
58
+ | select(.user.login == \"${COMMENT_AUTHOR}\") \
63
59
| .id"
64
60
COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" | jq "${FILTER}")
65
61
if [ $COMMENT_ID ]
69
65
else
70
66
echo "Comment ${COMMENT_ID} did not pass validations: not editing."
71
67
fi
68
+ else
69
+ # Create new comment
70
+ jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --input -
72
71
fi
73
72
env :
74
73
GITHUB_TOKEN : ${{ github.token }}
0 commit comments