Skip to content

Commit

Permalink
Update autocomment workflow (#220)
Browse files Browse the repository at this point in the history
* Delete comment if there are no longer changed files in the PR

* Fix generated links for commands pages
  • Loading branch information
paoloredis authored May 15, 2024
1 parent 9e9d702 commit 163a5ff
Showing 1 changed file with 35 additions and 14 deletions.
49 changes: 35 additions & 14 deletions .github/workflows/autocomment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,49 @@ jobs:
UPDATE_COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/comments"
FILES_URL="https://api.github.com/repos/${{ github.repository }}/pulls/${PR_NUMBER}/files"
# Check if comment already exists
EXISTING_COMMENT_JSON=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$CREATE_COMMENT_URL \
| jq -r '.[] | select(.user.login == "github-actions[bot]")'
)
EXISTING_COMMENT_ID=$(jq -r '.id' <<<"$EXISTING_COMMENT_JSON")
EXISTING_COMMENT=$(jq -r '.body' <<<"$EXISTING_COMMENT_JSON")
# Get all changed md files
COMMENT="Staging links:<br> $(curl -Ls \
CHANGED_FILES=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$FILES_URL \
| jq -r --arg prefix $BRANCH_NAME/ '.[] | select(.filename | test("content\/.+\\.md")) | ($prefix + .filename)' \
| sed -E -e 's|(^[^/]+/)([^/]+/)|\1|' -e 's|^|https://redis.io/docs/staging/|' -e 's|(_index)?\.md||' \
| sed -E -e 's|(^[^/]+/)([^/]+/)|\1|' -e 's|^|https://redis.io/docs/staging/|' -e 's|(_?index)?\.md||' \
| uniq \
| xargs \
| sed 's/ /<br>/g')"
| sed 's/ /<br>/g')
if [[ -z "$CHANGED_FILES" ]]
then
if [[ -z "$EXISTING_COMMENT_ID" ]]
then
printf '%s\n' 'Nothing to do!'
exit 0
else
# If a comment already exists but there are no changed files in the PR anymore, delete the comment
curl -Ls \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: token ${GITHUB_TOKEN}" \
"${UPDATE_COMMENT_URL}/${EXISTING_COMMENT_ID}"
printf '%s\n' 'Comment deleted!'
exit 0
fi
fi
COMMENT="Staging links:<br> $CHANGED_FILES"
printf '%s %s\n' 'Writing comment: ' "$COMMENT"
Expand All @@ -51,17 +83,6 @@ jobs:
EOF
}
# Check if comment already exists
EXISTING_COMMENT_JSON=$(curl -Ls \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
$CREATE_COMMENT_URL \
| jq -r '.[] | select(.user.login == "github-actions[bot]")'
)
EXISTING_COMMENT_ID=$(jq -r '.id' <<<"$EXISTING_COMMENT_JSON")
EXISTING_COMMENT=$(jq -r '.body' <<<"$EXISTING_COMMENT_JSON")
if [[ -n "$EXISTING_COMMENT_ID" ]]
then
# Update comment on pull request if comment is actually different
Expand Down

0 comments on commit 163a5ff

Please sign in to comment.