-
Notifications
You must be signed in to change notification settings - Fork 1
86 lines (76 loc) · 2.57 KB
/
format.yml
File metadata and controls
86 lines (76 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
name: Code Format Check
on:
push:
branches:
- master
- main
- develop
pull_request:
branches:
- master
- main
- develop
permissions:
contents: write
pull-requests: write
jobs:
format:
name: Check & Fix Formatting
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.24'
- name: Run gofmt
id: fmt
run: |
gofmt -w .
if [ -n "$(git diff)" ]; then
echo "needs_format=true" >> $GITHUB_OUTPUT
git diff --name-only
else
echo "needs_format=false" >> $GITHUB_OUTPUT
fi
- name: Run goimports
id: imports
run: |
go install golang.org/x/tools/cmd/goimports@latest
goimports -w .
if [ -n "$(git diff)" ]; then
echo "needs_imports=true" >> $GITHUB_OUTPUT
git diff --name-only
else
echo "needs_imports=false" >> $GITHUB_OUTPUT
fi
- name: Comment if formatting needed
if: github.event_name == 'pull_request' && (steps.fmt.outputs.needs_format == 'true' || steps.imports.outputs.needs_imports == 'true')
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: '❌ Code formatting issues detected\n\nRun the following locally:\n```bash\ngofmt -w .\ngo install golang.org/x/tools/cmd/goimports@latest\ngoimports -w .\n```\n\nOr commit the auto-fixed changes from this workflow.'
})
- name: Commit changes if needed (push)
if: |
github.event_name == 'push' &&
(steps.fmt.outputs.needs_format == 'true' || steps.imports.outputs.needs_imports == 'true')
run: |
git config --local user.email "bot@nodebyte.dev"
git config --local user.name "NodeByte Bot"
git add -A
git commit -m "style: auto-format code with gofmt and goimports"
git push
- name: Fail if PR has formatting issues
if: |
github.event_name == 'pull_request' &&
(steps.fmt.outputs.needs_format == 'true' || steps.imports.outputs.needs_imports == 'true')
run: |
echo "❌ Code has formatting issues"
echo "Run gofmt and goimports locally and push the changes"
exit 1