diff --git a/.github/workflows/pr-diff.yml b/.github/workflows/pr-diff.yml new file mode 100644 index 000000000000..b73c04889f81 --- /dev/null +++ b/.github/workflows/pr-diff.yml @@ -0,0 +1,82 @@ +name: PR Diff + +on: + issue_comment: + types: [created] + +permissions: + issues: write + pull-requests: write + contents: read + +jobs: + pr_diff: + runs-on: ubuntu-latest + + # Only run when a comment containing `/prdiff` is created + # and the author is a member, collaborator or owner + if: > + ( + github.event_name == 'issue_comment' && + github.event.issue.pull_request && + contains(fromJSON('["MEMBER", "COLLABORATOR", "OWNER"]'), github.event.comment.author_association) && + startsWith(github.event.comment.body, '/prdiff') + ) + + steps: + - name: Checkout repository + uses: actions/checkout@v4 + + - name: Read comment + id: read_comment + env: + COMMENT_BODY: ${{ github.event.comment.body }} + run: | + if [[ "$COMMENT_BODY" =~ ^/prdiff[[:space:]]([^[:space:]]+) ]]; then + CMD_ARG=${BASH_REMATCH[1]} + echo "Using cmd argument: $CMD_ARG" + echo "other_pr=$CMD_ARG" >> $GITHUB_OUTPUT + else + echo "Comment does not match format: '/prdiff ': ignoring" + fi + + - name: Get current PR URL + if: steps.read_comment.outputs.other_pr + id: get_pr_url + run: | + PR_URL="https://github.com/${{ github.repository }}/pull/${{ github.event.issue.number }}" + echo "PR_URL=$PR_URL" >> $GITHUB_OUTPUT + + - name: Obtain diff with the PR provided + if: steps.read_comment.outputs.other_pr + id: run_extension + env: + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + gh extension install samugi/gh-compr + OTHER_PR=${{ steps.read_comment.outputs.other_pr }} + CURRENT_PR=${{ steps.get_pr_url.outputs.PR_URL }} + + set +e + OUTPUT=$(gh compr $OTHER_PR $CURRENT_PR) + EXIT_STATUS=$? + if [ $EXIT_STATUS -ne 0 ]; then + echo "MESSAGE<$OUTPUT\n"$'\n'EOF >> "$GITHUB_OUTPUT" + else + # escape to prepare for assignment to template literal + ESCAPED_OUTPUT=$(echo "$OUTPUT" | sed -e 's/`/\\`/g' -e 's/\$/\\\$/g') + echo "MESSAGE<\nClick to expand\n\n\\\`\\\`\\\`diff\n$ESCAPED_OUTPUT\n\\\`\\\`\\\`\n"$'\n'EOF >> "$GITHUB_OUTPUT" + fi + + - name: Post result as comment in the PR + uses: actions/github-script@v7 + if: steps.run_extension.outputs.MESSAGE + with: + script: | + const commentBody = `${{ steps.run_extension.outputs.MESSAGE }}`; + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: commentBody + })