Skip to content

commit-suggest.yaml

commit-suggest.yaml #1

name: commit-suggest.yaml
on:
workflow_run:
workflows: ["rcc"]
types:
- completed
permissions:
contents: write
pull-requests: write
jobs:
commit-suggest:
runs-on: ubuntu-latest
if: github.event.workflow_run.event == 'pull_request'
steps:
- name: Show event payload
run: |
echo '${{ toJson(github.event) }}' | jq .
shell: bash
- name: Checkout PR
uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha }}
- name: Download artifact
uses: actions/download-artifact@v6
with:
name: changes-patch
github-token: ${{ secrets.GITHUB_TOKEN }}
run-id: ${{ github.event.workflow_run.id }}
continue-on-error: true
- name: Check if artifact exists
id: check-artifact
run: |
if [ -f changes.patch ]; then
echo "has_diff=true" >> $GITHUB_OUTPUT
else
echo "has_diff=false" >> $GITHUB_OUTPUT
echo "No changes-patch artifact found"
fi
shell: bash
- name: Find PR number for branch from correct head repository
id: find-pr
env:
GITHUB_TOKEN: ${{ github.token }}
run: |
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 "")
echo "pr_number=$PR_NUMBER" >> $GITHUB_OUTPUT
shell: bash
- name: Generate comment body
if: steps.check-artifact.outputs.has_diff == 'true'
id: comment-body
run: |
cat << 'EOF' > comment.md
## Formatting suggestions available
A patch file with formatting suggestions has been generated. You can apply it using one of these methods:
### Method 1: Apply via gh CLI
```bash
# Download and apply the patch directly
gh run download ${{ github.event.workflow_run.id }} --repo ${{ github.repository }} --name changes-patch && patch -p1 < changes.patch && rm changes.patch
```
### Method 2: View the patch
<details>
<summary>Click to see the patch contents</summary>
```diff
EOF
cat changes.patch >> comment.md
cat << 'EOF' >> comment.md
```
</details>
---
*This comment was automatically generated by the commit-suggester workflow.*
EOF
shell: bash
- name: Post or update comment
if: steps.check-artifact.outputs.has_diff == 'true'
uses: thollander/actions-comment-pull-request@v3
with:
pr-number: ${{ steps.find-pr.outputs.pr_number }}
file-path: comment.md
comment-tag: formatting-suggestions
mode: recreate