Skip to content

Commit de74341

Browse files
committed
Add more validations to workflow inputs
1 parent 815c6f4 commit de74341

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

.github/workflows/post-pr-comment.yml

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ jobs:
3838

3939
- name: Create or update comment
4040
run: |
41+
COMMENT_PREFIX="QHelp previews"
42+
COMMENT_AUTHOR="github-actions[bot]"
4143
PR_NUMBER="$(grep -o '^[0-9]\+$' pr_number.txt)"
44+
4245
# comment_id.txt may be empty if there is no existing comment
4346
if [ -s comment_id.txt ]
4447
then
@@ -50,8 +53,22 @@ jobs:
5053
# Create new comment
5154
jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/${PR_NUMBER}/comments" -X POST --input -
5255
else
53-
# Update existing comment
54-
jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" -X PATCH --input -
56+
# Fetch existing comment, and validate:
57+
# - comment belongs to the PR with number $PR_NUMBER
58+
# - comment starts with the expected prefix ("QHelp previews")
59+
# - comment author is github-actions[bot]
60+
FILTER="select(.issue_url | test(\"${GITHUB_REPOSITORY}/issues/${PR_NUMBER}$\")) \
61+
| select(.body | test(\"^${COMMENT_PREFIX}\")) \
62+
| select(.user.login == \"${COMMENT_AUTHOR}\") \
63+
| .id"
64+
COMMENT_ID=$(gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" | jq "${FILTER}")
65+
if [ $COMMENT_ID ]
66+
then
67+
# Update existing comment
68+
jq --rawfile body comment_body.txt '{"body":$body}' -n | gh api "repos/${GITHUB_REPOSITORY}/issues/comments/${COMMENT_ID}" -X PATCH --input -
69+
else
70+
echo "Comment ${COMMENT_ID} did not pass validations: not editing."
71+
fi
5572
fi
5673
env:
5774
GITHUB_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)