diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 451e5d2..cbfbe66 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -4,7 +4,7 @@ on: push: branches: [main] pull_request: - branches: [main] + types: [opened, synchronize, reopened, labeled] permissions: contents: write @@ -51,11 +51,14 @@ jobs: version: v2.11 go-generate: - name: Run all Generates & PR + name: Run all Generates & Commit runs-on: ubuntu-latest needs: [vet_build_test] steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref || github.ref_name }} + token: ${{ secrets.GITHUB_TOKEN }} - uses: actions/setup-go@v5 with: @@ -84,8 +87,31 @@ jobs: echo "changed=true" >> "$GITHUB_OUTPUT" fi + - name: Commit and push to PR branch + if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request' + id: push_to_pr + continue-on-error: true + run: | + git config user.name "github-actions[bot]" + git config user.email "github-actions[bot]@users.noreply.github.com" + git add -A + git commit -m "chore: run go generate" + git push + + - name: Comment on PR if push failed + if: steps.diff.outputs.changed == 'true' && github.event_name == 'pull_request' && steps.push_to_pr.outcome == 'failure' + uses: actions/github-script@v7 + with: + script: | + github.rest.issues.createComment({ + issue_number: context.issue.number, + owner: context.repo.owner, + repo: context.repo.repo, + body: '`go generate` produced file changes but could not be pushed automatically (likely a fork PR). Please run `go generate ./... && gofmt -w .` locally and push the changes.' + }) + - name: Create Pull Request - if: steps.diff.outputs.changed == 'true' + if: steps.diff.outputs.changed == 'true' && github.event_name == 'push' uses: peter-evans/create-pull-request@v7 with: token: ${{ secrets.GITHUB_TOKEN }}