|
| 1 | +name: commit-suggest.yaml |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_run: |
| 5 | + workflows: ["rcc"] |
| 6 | + types: |
| 7 | + - completed |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: write |
| 11 | + pull-requests: write |
| 12 | + |
| 13 | +jobs: |
| 14 | + commit-suggest: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + if: github.event.workflow_run.event == 'pull_request' |
| 17 | + |
| 18 | + steps: |
| 19 | + - name: Show event payload |
| 20 | + run: | |
| 21 | + echo '${{ toJson(github.event) }}' | jq . |
| 22 | + shell: bash |
| 23 | + |
| 24 | + - name: Checkout PR |
| 25 | + uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + ref: ${{ github.event.workflow_run.head_sha }} |
| 28 | + |
| 29 | + - name: Download artifact |
| 30 | + uses: actions/download-artifact@v6 |
| 31 | + with: |
| 32 | + name: changes-patch |
| 33 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 34 | + run-id: ${{ github.event.workflow_run.id }} |
| 35 | + continue-on-error: true |
| 36 | + |
| 37 | + - name: Check if artifact exists |
| 38 | + id: check-artifact |
| 39 | + run: | |
| 40 | + if [ -f changes.patch ]; then |
| 41 | + echo "has_diff=true" >> $GITHUB_OUTPUT |
| 42 | + else |
| 43 | + echo "has_diff=false" >> $GITHUB_OUTPUT |
| 44 | + echo "No changes-patch artifact found" |
| 45 | + fi |
| 46 | + shell: bash |
| 47 | + |
| 48 | + - name: Find PR number for branch from correct head repository |
| 49 | + id: find-pr |
| 50 | + env: |
| 51 | + GITHUB_TOKEN: ${{ github.token }} |
| 52 | + run: | |
| 53 | + PR_NUMBER=$(gh pr list --head ${{ github.event.workflow_run.head_branch }} --state open --json number,headRepositoryOwner --jq '.[] | select(.headRepositoryOwner.login == "${{ github.event.workflow_run.head_repository.owner.login }}") | .number' || echo "") |
| 54 | + echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT |
| 55 | + shell: bash |
| 56 | + |
| 57 | + - name: Generate comment body |
| 58 | + if: steps.check-artifact.outputs.has_diff == 'true' |
| 59 | + id: comment-body |
| 60 | + run: | |
| 61 | + cat << 'EOF' > comment.md |
| 62 | + ## Formatting suggestions available |
| 63 | +
|
| 64 | + A patch file with formatting suggestions has been generated. You can apply it using one of these methods: |
| 65 | +
|
| 66 | + ### Method 1: Apply via gh CLI |
| 67 | +
|
| 68 | + ```bash |
| 69 | + # Download and apply the patch directly |
| 70 | + gh run download ${{ github.event.workflow_run.id }} --repo ${{ github.repository }} --name changes-patch && patch -p1 < changes.patch && rm changes.patch |
| 71 | + ``` |
| 72 | +
|
| 73 | + ### Method 2: View the patch |
| 74 | +
|
| 75 | + <details> |
| 76 | + <summary>Click to see the patch contents</summary> |
| 77 | +
|
| 78 | + ```diff |
| 79 | + EOF |
| 80 | +
|
| 81 | + cat changes.patch >> comment.md |
| 82 | +
|
| 83 | + cat << 'EOF' >> comment.md |
| 84 | + ``` |
| 85 | +
|
| 86 | + </details> |
| 87 | +
|
| 88 | + --- |
| 89 | + *This comment was automatically generated by the commit-suggester workflow.* |
| 90 | + EOF |
| 91 | + shell: bash |
| 92 | + |
| 93 | + - name: Post or update comment |
| 94 | + if: steps.check-artifact.outputs.has_diff == 'true' |
| 95 | + uses: thollander/actions-comment-pull-request@v3 |
| 96 | + with: |
| 97 | + pr-number: ${{ steps.find-pr.outputs.pr_number }} |
| 98 | + file-path: comment.md |
| 99 | + comment-tag: formatting-suggestions |
| 100 | + mode: recreate |
0 commit comments