From bd524b5f908dfa33d5e8116b2b6bae08c71f6986 Mon Sep 17 00:00:00 2001 From: tyler Date: Wed, 22 Nov 2023 10:05:00 +0000 Subject: [PATCH] Add PR Tidying (demo) --- .github/workflows/pr-tidying.yml | 155 +++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 .github/workflows/pr-tidying.yml diff --git a/.github/workflows/pr-tidying.yml b/.github/workflows/pr-tidying.yml new file mode 100644 index 000000000..f2eb476f0 --- /dev/null +++ b/.github/workflows/pr-tidying.yml @@ -0,0 +1,155 @@ +name: Pull Request Tidying + +on: + # ====== 👇 WORKAROUND FOR TESTING 👇 (DO NOT MERGE OR APPROVE) ====== + # pull_request_target: + # types: + # - opened + # - reopened + # - ready_for_review + pull_request: { types: [ opened, reopened, ready_for_review ] } # REMOVE BEFORE MERGING! + # ====== 👆 WORKAROUND FOR TESTING 👆 (DO NOT MERGE OR APPROVE) ====== + +permissions: + contents: read + pull-requests: write + +jobs: + default-assignee: + name: Assign contributor + continue-on-error: true + permissions: + pull-requests: write + runs-on: ubuntu-latest + outputs: + check-passed: ${{ steps.check.outputs.result == 'passed' }} + fixed: ${{ steps.fix.outputs.result == 'fixed' }} + steps: + - name: Check if PR already has assignee + id: check + run: ([[ -n $CURRENT_ASSIGNEE ]] && echo "result=passed") >> $GITHUB_OUTPUT + env: + CURRENT_ASSIGNEE: ${{ github.event.pull_request.assignee || '' }} + - name: Add assignee + id: fix + run: | + level=$(gh api /repos/$REPO/collaborators/$AUTHOR/permission --jq .permission) + if [[ $level = "write" || $level = "admin" ]]; then + gh pr edit "$PR_NUMBER" --repo "$REPO" --add-assignee "$AUTHOR" + echo "result=fixed" >> $GITHUB_OUTPUT + fi + env: + REPO: ${{ github.repository }} + AUTHOR: ${{ github.actor }} + PR_NUMBER: ${{ github.event.pull_request.number }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + issue-in-title: + name: Add issue references to title + continue-on-error: true + runs-on: ubuntu-latest + outputs: + check-passed: ${{ steps.check.outputs.result == 'passed' }} + fixed: ${{ steps.fix.outputs.result == 'fixed' }} + steps: + - name: Check the current PR title + id: check + run: | + regex='\s\([#][0-9]+\s?\)$' + if [[ $PR_TITLE =~ $regex ]]; then + echo "result=passed" >> $GITHUB_OUTPUT + else + echo "result=failed" >> $GITHUB_OUTPUT + fi + - name: Parse issue(s) from Ticket heading + id: parse + continue-on-error: true + run: | + regex='^[#][#][#] Ticket (([#][0-9]+\s?)+)' + line=$(gh pr view "$PR_NUMBER" --json body --jq .body | head -n 1) + issues='' + if [[ $line =~ $regex ]]; then + issues="${BASH_REMATCH[1]}" + fi + echo "issue-numbers=$(echo $ISSUES | tr -d '[:blank:]')" >> $GITHUB_OUTPUT + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Update PR title + id: fix + if: steps.parse.outputs.issue-numbers != '' + continue-on-error: true + run: | + gh pr edit "$PR_NUMBER" --title "$ORIGINAL_TITLE (issue $ISSUE_NUMBERS)" + echo "result=fixed" >> $GITHUB_OUTPUT + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + ORIGINAL_TITLE: ${{ github.event.pull_request.title }} + ISSUE_NUMBERS: ${{ steps.parse.outputs.issue-numbers }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + comment: + name: Create/update PR comment + if: github.actor != 'dependabot[bot]' + needs: + - default-assignee + - issue-in-title + runs-on: ubuntu-latest + steps: + - name: Create comment file + run: | + COMMENT_FILE=$(mktemp -t comment.md.XXXXX) + echo "COMMENT_FILE=$COMMENT_FILE" >> $GITHUB_ENV + cat >> $COMMENT_FILE << 'ENDOFCOMMENT' + ## PR Helper Bot 🤖 + + **Thanks for your pull request!** + My job is to check for any missing conventions and automatically tidy things up (if I can). + You'll find the results below, along with any suggested actions for you to take. + + *These checks normally only run once, so you won't see any updates after you make changes.* + +
+ Result key + ✅ = Already passing + 🛠️ = Fixed (now passing) + ⚠️ = Consider fixing + + + | Convention | Result | + |:-------------------------------------|:------:| + | PR has assignee (defaults to author) | ${{ env.DEFAULT_ASSIGNEE }} | + | PR title ends with `(#issue)` | ${{ env.ISSUE_IN_TITLE }} | + + *Pusher: @${{ env.GH_ACTOR }}, Action: `${{ env.GH_ACTION }}`, Workflow: [`${{ env.GH_WORKFLOW }}`](${{ env.GH_SERVER}}/${{ env.GH_REPO }}/actions/runs/${{ env.GH_RUN_ID }})* + ENDOFCOMMENT + env: + DEFAULT_ASSIGNEE: ${{ (needs.default-assignee.outputs.check-passed && '✅' ) || (needs.default-assignee.outputs.fixed && '🛠️') || '⚠️' }} + ISSUE_IN_TITLE: ${{ (needs.issue-in-title.outputs.check-passed && '✅' ) || (needs.issue-in-title.outputs.fixed && '🛠️') || '⚠️' }} + GH_ACTOR: ${{ github.actor }} + GH_ACTION: ${{ github.event_name }} + GH_WORKFLOW: ${{ github.workflow }} + GH_SERVER: ${{ github.server_url }} + GH_REPO: ${{ github.repository }} + GH_RUN_ID: ${{ github.run_id }} + - name: Write the comment body + id: comment-body + run: | + CONTENT=$(cat $COMMENT_FILE) + echo "COMMENT_CONTENT<> $GITHUB_OUTPUT + echo "$CONTENT" >> $GITHUB_OUTPUT + echo "ENDOFREPORT" >> $GITHUB_OUTPUT + - name: Find previous report comment + id: find-comment + uses: peter-evans/find-comment@a54c31d7fa095754bfef525c0c8e5e5674c4b4b1 # v2.4.0 + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: 'PR Helper Bot 🤖' + - name: Create or update comment + uses: peter-evans/create-or-update-comment@23ff15729ef2fc348714a3bb66d2f655ca9066f2 # v3.1.0 + with: + comment-id: ${{ steps.find-comment.outputs.comment-id }} + issue-number: ${{ github.event.pull_request.number }} + body: ${{ steps.comment-body.outputs.COMMENT_CONTENT }} + edit-mode: replace