Simplify shell script

This commit is contained in:
Harry Maclean
2022-04-05 14:03:29 +12:00
parent de743418e2
commit 342bb17fb6

View File

@@ -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 }}