Skip to content

Commit 7f8ca15

Browse files
committed
fix: update CI workflow for format checking
- Format check now only runs on pull requests - Comments on PR with unformatted files and tags author - Removes auto-commit behavior that conflicted with branch protection - Main branch pushes only run build/test jobs
1 parent 00829b0 commit 7f8ca15

File tree

1 file changed

+28
-16
lines changed

1 file changed

+28
-16
lines changed

.github/workflows/ci-cd.yml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,46 @@ jobs:
4343
name: coverage-report
4444
path: coverage.out
4545

46-
format:
47-
name: Auto-format with gofmt (Linux only)
46+
format-check:
47+
name: Go Format Check
4848
runs-on: ubuntu-latest
49-
if: github.event_name == 'push' && github.actor != 'github-actions[bot]'
49+
if: github.event_name == 'pull_request'
5050
permissions:
51-
contents: write
51+
contents: read
52+
pull-requests: write
5253

5354
steps:
5455
- name: Checkout code
5556
uses: actions/checkout@v4
56-
with:
57-
token: ${{ secrets.GITHUB_TOKEN }}
5857

5958
- name: Set up Go
6059
uses: actions/setup-go@v5
6160
with:
6261
go-version: '1.24.4'
6362

64-
- name: Run gofmt and commit changes if needed
63+
- name: Check Go formatting and comment on PR
6564
run: |
66-
gofmt -w .
67-
if [ -n "$(git status --porcelain)" ]; then
68-
echo "Code was not formatted. Committing changes..."
69-
git config user.name "github-actions[bot]"
70-
git config user.email "github-actions[bot]@users.noreply.github.com"
71-
git add .
72-
git commit -m "chore: auto-format Go code via gofmt"
73-
git push
65+
unformatted=$(gofmt -l .)
66+
if [ -n "$unformatted" ]; then
67+
echo "The following files need formatting:"
68+
echo "$unformatted"
69+
70+
# Get PR author
71+
pr_author="${{ github.event.pull_request.user.login }}"
72+
73+
# Create PR comment
74+
comment_body="@$pr_author Go formatting is required for the following files:
75+
76+
\`\`\`
77+
$unformatted
78+
\`\`\`
79+
80+
Please run \`go fmt ./...\` to format your code and push the changes."
81+
82+
gh pr comment ${{ github.event.number }} --body "$comment_body"
83+
exit 1
7484
else
75-
echo "Code already properly formatted."
85+
echo "All Go code is properly formatted"
7686
fi
87+
env:
88+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)