From d87e852929f2dbf0ce8e0c5de62e72c04da58190 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 09:47:03 +0800 Subject: [PATCH 1/8] ci: run gofmt workflow only for style label --- .github/workflows/gofmt.yml | 52 +++---------------------------------- 1 file changed, 3 insertions(+), 49 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index 09fedf86..a694c371 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -2,10 +2,7 @@ name: Go Fmt on: pull_request: - types: [opened, synchronize, reopened, labeled] - push: - branches: - - main + types: [labeled] permissions: contents: write @@ -13,7 +10,7 @@ permissions: jobs: gofmt: - if: github.event.action == 'labeled' && contains(github.event.pull_request.labels.*.name, 'style') + if: github.event.label.name == 'style' strategy: matrix: go-version: [1.25.1] @@ -25,7 +22,7 @@ jobs: - name: Checkout code uses: actions/checkout@v4 with: - ref: ${{ github.event_name == 'pull_request' && github.head_ref || github.ref }} + ref: ${{ github.head_ref }} fetch-depth: 0 - name: Install Go @@ -37,13 +34,11 @@ jobs: run: gofmt -w -s . - name: Check for formatting changes (dry run) - if: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'testing') }} run: | git diff --exit-code || echo "::warning::Formatting changes would be applied in normal mode" continue-on-error: true - name: Commit Changes to PR - if: ${{ github.event_name == 'pull_request' && contains(github.event.pull_request.labels.*.name, 'testing') }} uses: stefanzweifel/git-auto-commit-action@v6.0.1 with: commit_message: apply coding style fixes @@ -54,44 +49,3 @@ jobs: commit_user_email: ${{ secrets.GUS_GH_EMAIL }} commit_author: gocanto branch: ${{ github.head_ref }} - - - name: Create branch for formatting changes - if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} - run: | - # Check if there are any changes after formatting - if ! git diff --quiet; then - # Create a new branch with timestamp - BRANCH_NAME="gofmt-fixes-$(date +%Y%m%d-%H%M%S)" - git checkout -b $BRANCH_NAME - - # Configure git - git config user.name "GitHub Actions" - git config user.email "${{ secrets.GUS_GH_EMAIL }}" - - # Commit changes - git add . - git commit -m "Apply Go formatting fixes" - - # Push the branch - git push origin $BRANCH_NAME - - # Set output for next step - echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV - echo "HAS_CHANGES=true" >> $GITHUB_ENV - else - echo "No formatting changes needed" - echo "HAS_CHANGES=false" >> $GITHUB_ENV - fi - - - name: Create Pull Request - if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && env.HAS_CHANGES == 'true' }} - uses: peter-evans/create-pull-request@v6 - with: - token: ${{ secrets.GITHUB_TOKEN }} - commit-message: Apply Go formatting fixes - title: "Auto: Apply Go formatting fixes" - body: | - This PR was automatically created by the Go Fmt GitHub Action. - It applies Go formatting standards to the codebase. - branch: ${{ env.BRANCH_NAME }} - base: main From a2a95c1f74e711875073e40f74405339f083a9e7 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 09:57:11 +0800 Subject: [PATCH 2/8] ci: ignore git dir for gofmt --- .github/workflows/gofmt.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index a694c371..8b34785a 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -31,7 +31,7 @@ jobs: go-version: ${{ matrix.go-version }} - name: Run Formatter - run: gofmt -w -s . + run: git ls-files '*.go' | xargs gofmt -w -s - name: Check for formatting changes (dry run) run: | From fd1e31cd4c5bf7c25237a2d2694c299bdfb098e2 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 10:10:28 +0800 Subject: [PATCH 3/8] ci: checkout head repo and skip commits on forks --- .github/workflows/gofmt.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index 8b34785a..da9173f6 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -22,7 +22,8 @@ jobs: - name: Checkout code uses: actions/checkout@v4 with: - ref: ${{ github.head_ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.event.pull_request.head.ref }} fetch-depth: 0 - name: Install Go @@ -39,6 +40,7 @@ jobs: continue-on-error: true - name: Commit Changes to PR + if: github.event.pull_request.head.repo.full_name == github.repository uses: stefanzweifel/git-auto-commit-action@v6.0.1 with: commit_message: apply coding style fixes @@ -48,4 +50,4 @@ jobs: commit_user_name: GitHub Actions commit_user_email: ${{ secrets.GUS_GH_EMAIL }} commit_author: gocanto - branch: ${{ github.head_ref }} + branch: ${{ github.event.pull_request.head.ref }} From b780c6ac7a26156445c2f3226c36570ed6714423 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 11:21:07 +0800 Subject: [PATCH 4/8] ci: run gofmt workflow after merging to master --- .github/workflows/gofmt.yml | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index da9173f6..1e58835e 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -2,7 +2,8 @@ name: Go Fmt on: pull_request: - types: [labeled] + branches: [master] + types: [closed] permissions: contents: write @@ -10,7 +11,7 @@ permissions: jobs: gofmt: - if: github.event.label.name == 'style' + if: github.event.pull_request.merged == true strategy: matrix: go-version: [1.25.1] @@ -22,8 +23,7 @@ jobs: - name: Checkout code uses: actions/checkout@v4 with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.event.pull_request.head.ref }} + ref: ${{ github.event.pull_request.base.ref }} fetch-depth: 0 - name: Install Go @@ -39,8 +39,7 @@ jobs: git diff --exit-code || echo "::warning::Formatting changes would be applied in normal mode" continue-on-error: true - - name: Commit Changes to PR - if: github.event.pull_request.head.repo.full_name == github.repository + - name: Commit Changes uses: stefanzweifel/git-auto-commit-action@v6.0.1 with: commit_message: apply coding style fixes @@ -50,4 +49,4 @@ jobs: commit_user_name: GitHub Actions commit_user_email: ${{ secrets.GUS_GH_EMAIL }} commit_author: gocanto - branch: ${{ github.event.pull_request.head.ref }} + branch: ${{ github.event.pull_request.base.ref }} From 297ef8d27d1bc5fde83ac060d0d1dd2121e01ebb Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 11:21:15 +0800 Subject: [PATCH 5/8] ci: run gofmt workflow after merging to main --- .github/workflows/gofmt.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index 1e58835e..cb506c57 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -2,7 +2,7 @@ name: Go Fmt on: pull_request: - branches: [master] + branches: [main] types: [closed] permissions: From 667bec614aa303146291a8969fe7cf3bdbf15361 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 11:27:20 +0800 Subject: [PATCH 6/8] ci: restore formatting branch PR workflow --- .github/workflows/gofmt.yml | 127 +++++++++++++++++++++++------------- 1 file changed, 83 insertions(+), 44 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index cb506c57..880a9f9a 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -2,51 +2,90 @@ name: Go Fmt on: pull_request: - branches: [main] - types: [closed] + types: [labeled] + push: + branches: + - main permissions: - contents: write - pull-requests: write + contents: write + pull-requests: write jobs: - gofmt: - if: github.event.pull_request.merged == true - strategy: - matrix: - go-version: [1.25.1] - os: [ubuntu-latest] - - runs-on: ${{ matrix.os }} - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.base.ref }} - fetch-depth: 0 - - - name: Install Go - uses: actions/setup-go@v5 - with: - go-version: ${{ matrix.go-version }} - - - name: Run Formatter - run: git ls-files '*.go' | xargs gofmt -w -s - - - name: Check for formatting changes (dry run) - run: | - git diff --exit-code || echo "::warning::Formatting changes would be applied in normal mode" - continue-on-error: true - - - name: Commit Changes - uses: stefanzweifel/git-auto-commit-action@v6.0.1 - with: - commit_message: apply coding style fixes - commit_options: '--no-verify' - file_pattern: . - repository: . - commit_user_name: GitHub Actions - commit_user_email: ${{ secrets.GUS_GH_EMAIL }} - commit_author: gocanto - branch: ${{ github.event.pull_request.base.ref }} + gofmt: + # Run when a PR is labeled 'style' or on pushes to main + if: > + (github.event_name == 'pull_request' && github.event.action == 'labeled' && github.event.label.name == 'style') || + (github.event_name == 'push' && github.ref == 'refs/heads/main') + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [1.25.1] + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + repository: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name || github.repository }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }} + fetch-depth: 0 + + - name: Install Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: Run Formatter + run: git ls-files '*.go' | xargs gofmt -w -s + + - name: Check for formatting changes (dry run) + if: github.event_name == 'pull_request' + run: | + git diff --exit-code || echo "::warning::Formatting changes would be applied in normal mode" + continue-on-error: true + + - name: Commit Changes + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository + uses: stefanzweifel/git-auto-commit-action@v6.0.1 + with: + commit_message: apply coding style fixes + commit_options: '--no-verify' + file_pattern: . + repository: . + commit_user_name: GitHub Actions + commit_user_email: ${{ secrets.GUS_GH_EMAIL }} + commit_author: gocanto + branch: ${{ github.event.pull_request.head.ref }} + + - name: Create branch for formatting changes + if: github.event_name == 'push' && github.ref == 'refs/heads/main' + run: | + if ! git diff --quiet; then + BRANCH_NAME="gofmt-fixes-$(date +%Y%m%d-%H%M%S)" + git checkout -b "$BRANCH_NAME" + + git config user.name "GitHub Actions" + git config user.email "${{ secrets.GUS_GH_EMAIL }}" + + git add . + git commit -m "Apply Go formatting fixes" + git push origin "$BRANCH_NAME" + + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV + echo "HAS_CHANGES=true" >> $GITHUB_ENV + else + echo "HAS_CHANGES=false" >> $GITHUB_ENV + fi + + - name: Create Pull Request + if: github.event_name == 'push' && github.ref == 'refs/heads/main' && env.HAS_CHANGES == 'true' + uses: peter-evans/create-pull-request@v6 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: Apply Go formatting fixes + title: "Auto: Apply Go formatting fixes" + body: | + This PR was automatically created by the Go Fmt GitHub Action. + It applies Go formatting standards to the codebase. + branch: ${{ env.BRANCH_NAME }} + base: main From cc31fee192c3ea202c5ecfa3e49bfa940a948a00 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 11:35:17 +0800 Subject: [PATCH 7/8] ci: limit gofmt auto-commit to draft style PRs --- .github/workflows/gofmt.yml | 52 ++++++------------------------------- 1 file changed, 8 insertions(+), 44 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index 880a9f9a..db587d63 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -3,9 +3,6 @@ name: Go Fmt on: pull_request: types: [labeled] - push: - branches: - - main permissions: contents: write @@ -13,10 +10,8 @@ permissions: jobs: gofmt: - # Run when a PR is labeled 'style' or on pushes to main - if: > - (github.event_name == 'pull_request' && github.event.action == 'labeled' && github.event.label.name == 'style') || - (github.event_name == 'push' && github.ref == 'refs/heads/main') + # Run only when a PR is labeled 'style' + if: github.event.action == 'labeled' && github.event.label.name == 'style' runs-on: ubuntu-latest strategy: matrix: @@ -26,8 +21,8 @@ jobs: - name: Checkout code uses: actions/checkout@v4 with: - repository: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name || github.repository }} - ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + ref: ${{ github.event.pull_request.head.ref }} fetch-depth: 0 - name: Install Go @@ -39,13 +34,15 @@ jobs: run: git ls-files '*.go' | xargs gofmt -w -s - name: Check for formatting changes (dry run) - if: github.event_name == 'pull_request' run: | git diff --exit-code || echo "::warning::Formatting changes would be applied in normal mode" continue-on-error: true - name: Commit Changes - if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository + if: > + github.event.pull_request.draft && + github.event.pull_request.head.repo.full_name == github.repository && + contains(github.event.pull_request.labels.*.name, 'style') uses: stefanzweifel/git-auto-commit-action@v6.0.1 with: commit_message: apply coding style fixes @@ -56,36 +53,3 @@ jobs: commit_user_email: ${{ secrets.GUS_GH_EMAIL }} commit_author: gocanto branch: ${{ github.event.pull_request.head.ref }} - - - name: Create branch for formatting changes - if: github.event_name == 'push' && github.ref == 'refs/heads/main' - run: | - if ! git diff --quiet; then - BRANCH_NAME="gofmt-fixes-$(date +%Y%m%d-%H%M%S)" - git checkout -b "$BRANCH_NAME" - - git config user.name "GitHub Actions" - git config user.email "${{ secrets.GUS_GH_EMAIL }}" - - git add . - git commit -m "Apply Go formatting fixes" - git push origin "$BRANCH_NAME" - - echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV - echo "HAS_CHANGES=true" >> $GITHUB_ENV - else - echo "HAS_CHANGES=false" >> $GITHUB_ENV - fi - - - name: Create Pull Request - if: github.event_name == 'push' && github.ref == 'refs/heads/main' && env.HAS_CHANGES == 'true' - uses: peter-evans/create-pull-request@v6 - with: - token: ${{ secrets.GITHUB_TOKEN }} - commit-message: Apply Go formatting fixes - title: "Auto: Apply Go formatting fixes" - body: | - This PR was automatically created by the Go Fmt GitHub Action. - It applies Go formatting standards to the codebase. - branch: ${{ env.BRANCH_NAME }} - base: main From 88829b81ef43d96f3834bc6a0d74b3a4d7c9a483 Mon Sep 17 00:00:00 2001 From: Gus Date: Wed, 10 Sep 2025 11:40:57 +0800 Subject: [PATCH 8/8] ci: restore PR creation step --- .github/workflows/gofmt.yml | 45 +++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gofmt.yml b/.github/workflows/gofmt.yml index db587d63..a929252e 100644 --- a/.github/workflows/gofmt.yml +++ b/.github/workflows/gofmt.yml @@ -3,6 +3,9 @@ name: Go Fmt on: pull_request: types: [labeled] + push: + branches: + - main permissions: contents: write @@ -10,8 +13,11 @@ permissions: jobs: gofmt: - # Run only when a PR is labeled 'style' - if: github.event.action == 'labeled' && github.event.label.name == 'style' + if: > + github.event_name == 'push' || + (github.event_name == 'pull_request' && + github.event.action == 'labeled' && + github.event.label.name == 'style') runs-on: ubuntu-latest strategy: matrix: @@ -21,8 +27,8 @@ jobs: - name: Checkout code uses: actions/checkout@v4 with: - repository: ${{ github.event.pull_request.head.repo.full_name }} - ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name || github.repository }} + ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.ref || github.ref }} fetch-depth: 0 - name: Install Go @@ -53,3 +59,34 @@ jobs: commit_user_email: ${{ secrets.GUS_GH_EMAIL }} commit_author: gocanto branch: ${{ github.event.pull_request.head.ref }} + + - name: Create branch for formatting changes + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' }} + run: | + if ! git diff --quiet; then + BRANCH_NAME="gofmt-fixes-$(date +%Y%m%d-%H%M%S)" + git checkout -b $BRANCH_NAME + git config user.name "GitHub Actions" + git config user.email "${{ secrets.GUS_GH_EMAIL }}" + git add . + git commit -m "Apply Go formatting fixes" + git push origin $BRANCH_NAME + echo "BRANCH_NAME=$BRANCH_NAME" >> $GITHUB_ENV + echo "HAS_CHANGES=true" >> $GITHUB_ENV + else + echo "No formatting changes needed" + echo "HAS_CHANGES=false" >> $GITHUB_ENV + fi + + - name: Create Pull Request + if: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && env.HAS_CHANGES == 'true' }} + uses: peter-evans/create-pull-request@v6 + with: + token: ${{ secrets.GITHUB_TOKEN }} + commit-message: Apply Go formatting fixes + title: "Auto: Apply Go formatting fixes" + body: | + This PR was automatically created by the Go Fmt GitHub Action. + It applies Go formatting standards to the codebase. + branch: ${{ env.BRANCH_NAME }} + base: main