From f8149115600182298946a68d552122d423d251eb Mon Sep 17 00:00:00 2001 From: paoloredis Date: Mon, 13 May 2024 10:15:10 +0200 Subject: [PATCH] Add workflow for autocommenting pull requests with staging links --- .github/workflows/autocomment.yaml | 93 ++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 .github/workflows/autocomment.yaml diff --git a/.github/workflows/autocomment.yaml b/.github/workflows/autocomment.yaml new file mode 100644 index 000000000..15ce4072a --- /dev/null +++ b/.github/workflows/autocomment.yaml @@ -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:
$(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/ /
/g')" + + printf '%s %s\n' 'Writing comment: ' "$COMMENT" + + generate_post_data() + { + cat </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 \ No newline at end of file