-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #209 from paoloredis/feature/add-autocomment-workflow
Add workflow for autocommenting pull requests with staging links
- Loading branch information
Showing
1 changed file
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
name: Auto Comment on PR | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- 'content/**' | ||
types: | ||
- opened | ||
- synchronize | ||
permissions: | ||
pull-requests: write | ||
|
||
jobs: | ||
auto-comment: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: 'Install jq' | ||
run: sudo apt-get install jq | ||
|
||
- name: Create Comment | ||
run: | | ||
set -e | ||
PR_NUMBER=${{ github.event.pull_request.number }} | ||
BRANCH_NAME=${{ github.head_ref }} | ||
GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} | ||
CREATE_COMMENT_URL="https://api.github.com/repos/${{ github.repository }}/issues/${PR_NUMBER}/comments" | ||
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" | ||
# Get all changed md files | ||
COMMENT="Staging links:<br> $(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||' \ | ||
| uniq \ | ||
| xargs \ | ||
| sed 's/ /<br>/g')" | ||
printf '%s %s\n' 'Writing comment: ' "$COMMENT" | ||
generate_post_data() | ||
{ | ||
cat <<EOF | ||
{ | ||
"body": "$COMMENT" | ||
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 | ||
if [[ "$COMMENT" != "$EXISTING_COMMENT" ]] | ||
then | ||
curl -Ls \ | ||
-X PATCH \ | ||
-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}" \ | ||
--data "$(generate_post_data)" 1>/dev/null | ||
printf '%s\n' 'Comment updated!' | ||
else | ||
printf '%s\n' 'Nothing changed!' | ||
fi | ||
else | ||
# Write comment on pull request | ||
curl -Ls \ | ||
-X POST \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
-H "Authorization: token ${GITHUB_TOKEN}" \ | ||
$CREATE_COMMENT_URL \ | ||
--data "$(generate_post_data)" 1>/dev/null | ||
printf '%s\n' 'Comment written!' | ||
fi |