-
Notifications
You must be signed in to change notification settings - Fork 31
247 lines (217 loc) · 9.87 KB
/
go-lint-check.yml
File metadata and controls
247 lines (217 loc) · 9.87 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
name: Go linters check and auto-fix
on:
workflow_dispatch:
inputs:
Update:
description: 'Update go version'
default: false
type: boolean
Version:
description: 'Version for Go'
required: false
default: '1.25'
Modules:
description: 'Update dependencies in modules'
default: false
type: boolean
Golangci:
description: 'Golangci linters check'
default: false
type: boolean
Fix:
description: 'Golangci linters fix'
default: false
type: boolean
PR:
description: 'Create Pull Request'
default: false
type: boolean
Gocrit:
description: 'Go critical linters check'
default: false
type: boolean
Gosec:
description: 'Go security linters check'
default: false
type: boolean
jobs:
lint:
name: Linters check
permissions:
contents: write
pull-requests: write
models: read
runs-on: ubuntu-latest
env:
APP_VERSION: "latest"
steps:
- name: Checkout repository (main branch and 1 last commits)
uses: actions/checkout@v6
with:
fetch-depth: 1
ref: main
- name: Install Go
uses: actions/setup-go@v6
with:
go-version: 1.25
- name: Install dependencies
run: |
go fmt ./...
go vet ./...
go get ./...
go mod tidy
go mod verify
go build -v ./...
- name: Get build parameters to summary
shell: bash
run: |
echo "### Parameters" >> $GITHUB_STEP_SUMMARY
echo "- **Update go version**: ${{ github.event.inputs.Update == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Update dependencies in** `go.mod`: ${{ github.event.inputs.Modules == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Golangci linters check**: ${{ github.event.inputs.Golangci == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Golangci linters auto-fix in** `main.go`: ${{ github.event.inputs.Fix == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Create Pull Request**: ${{ github.event.inputs.PR == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Go critical linters check**: ${{ github.event.inputs.Gocrit == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
echo "- **Go security linters check**: ${{ github.event.inputs.Gosec == 'true' && '✅' || '❌' }}" >> $GITHUB_STEP_SUMMARY
- name: Get app version in env
run: |
APP_VERSION=$(go run main.go -v)
echo "APP_VERSION=$APP_VERSION" >> $GITHUB_ENV
- name: Update Go version
if: ${{ github.event.inputs.Update == 'true' && github.event.inputs.Version != '' }}
run: |
go mod edit -go=${{ github.event.inputs.Version }}
# go mod edit -toolchain=go${{ github.event.inputs.Version }}
go mod tidy
- name: Reinstall Go on new version
if: ${{ github.event.inputs.Update == 'true' && github.event.inputs.Version != '' }}
uses: actions/setup-go@v6
with:
go-version: ${{ github.event.inputs.Version }}
cache: true
- name: Update dependencies in modules
if: ${{ github.event.inputs.Modules == 'true' }}
run: go get -u ./...
- name: Create Pull Request for update go version amd dependencies
if: ${{ github.event.inputs.Modules == 'true' && github.event.inputs.PR == 'true' }}
uses: peter-evans/create-pull-request@v8
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "update go version and package dependencies"
branch: "fix/${{ env.APP_VERSION }}-update"
title: "Updated dependencies"
body: "Updated go version and modules in `go.mod` and `go.sum` files."
add-paths: |
go.mod
go.sum
- name: Install golangci
if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }}
run: go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v2.8.0
- name: Golangci check config
id: configCheck
if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }}
run: golangci-lint config verify --verbose
- name: Get used linters from config and parse in Markdown
if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }}
run: |
golangci-lint --version
version=$(golangci-lint --version | grep -Eo "[0-9.]+" | head -n 1)
echo "### Configuration" >> $GITHUB_STEP_SUMMARY
echo "Golangci version: $version" >> $GITHUB_STEP_SUMMARY
echo "Check config status: ${{ steps.configCheck.outcome }}" >> $GITHUB_STEP_SUMMARY
golangci-lint linters
golangci-lint linters > linters.md
sed "s/Enabled by your configuration linters:/## Enabled linters/" -i linters.md
sed "s/Disabled by your configuration linters:/## Disabled linters/" -i linters.md
sed "s/ \[deprecated\]//g" -i linters.md
sed "s/. \[(.+)\]/ (**$1**)./g" -i linters.md
sed -E "s/\. \[(.+)\]/ (**\1**)./g" -i linters.md
linters=$(cat linters.md | awk -F ":" '/:/ {print "- [" $1"](https://golangci-lint.run/docs/linters/configuration/#"$1") -" $2":"$3; next} {print $0}')
echo "$linters" > linters.md
- name: Golangci linters check using Action
id: golangci
if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }}
continue-on-error: true
uses: golangci/golangci-lint-action@v9
with:
version: v2.8.0
args: ./main.go
- name: Get issues result from linters check to summary
if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }}
continue-on-error: true
run: |
echo "### Linters check results" >> $GITHUB_STEP_SUMMARY
golangci-lint run ./main.go > results.md || true
sed -n '/issues:/,$p' -i results.md
cat results.md >> $GITHUB_STEP_SUMMARY
- name: Golangci linters fix in main.go
if: ${{ github.event.inputs.Fix == 'true' }}
continue-on-error: true
run: golangci-lint run --fix ./main.go
- name: Golangci linters check after fix
if: ${{ github.event.inputs.Fix == 'true' }}
continue-on-error: true
uses: golangci/golangci-lint-action@v9
with:
version: v2.8.0
args: ./main.go
- name: Get issues result from linters check to summary after fix
if: ${{ github.event.inputs.Fix == 'true' }}
continue-on-error: true
run: |
echo "### Linters check results after fix" >> $GITHUB_STEP_SUMMARY
golangci-lint run ./main.go > results.md || true
sed -n '/issues:/,$p' -i results.md
cat results.md >> $GITHUB_STEP_SUMMARY
- name: Public used linters from config to summary
if: ${{ github.event.inputs.golangci == 'true' || github.event.inputs.Fix == 'true' }}
continue-on-error: true
run: cat linters.md >> $GITHUB_STEP_SUMMARY
- name: Create Pull Request for fix lint
if: ${{ github.event.inputs.Fix == 'true' && github.event.inputs.PR == 'true' }}
uses: peter-evans/create-pull-request@v8
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "autofix linters using golangci"
branch: "fix/${{ env.APP_VERSION }}-lint"
title: "Fixing linters"
body: "Automatically fixing linters with golangci from GitHub Actions."
add-paths: |
main.go
- name: Go critical linters check
id: gocrit
if: ${{ github.event.inputs.gocrit == 'true' }}
continue-on-error: true
run: |
go install github.com/go-critic/go-critic/cmd/gocritic@latest
gocritic check -v -enableAll ./main.go
- name: Go security linters check
id: gosec
if: ${{ github.event.inputs.gosec == 'true' }}
continue-on-error: true
run: |
go install github.com/securego/gosec/v2/cmd/gosec@latest
gosec -severity=high ./...
- name: Send report to Telegram
uses: appleboy/telegram-action@master
with:
token: ${{ secrets.TELEGRAM_API_TOKEN }}
to: ${{ secrets.TELEGRAM_CHANNEL_ID }}
debug: true
format: markdown
message: |
🔔 **Action**: linters check and fix
📁 **Repository**: ${{ github.repository }}
👤 **User**: ${{ github.actor }}
Parameters:
**Update go version**: ${{ github.event.inputs.Update == 'true' && '✅' || '❌' }}
**Update dependencies in** `go.mod`: ${{ github.event.inputs.Modules == 'true' && '✅' || '❌' }}
**Golangci linters check**: ${{ github.event.inputs.Golangci == 'true' && '✅' || '❌' }}
**Golangci linters fix in** `main.go`: ${{ github.event.inputs.Fix == 'true' && '✅' || '❌' }}
**Create Pull Request**: ${{ github.event.inputs.PR == 'true' && '✅' || '❌' }}
**Go critical linters check**: ${{ github.event.inputs.Gocrit == 'true' && '✅' || '❌' }}
**Go security linters check**: ${{ github.event.inputs.Gosec == 'true' && '✅' || '❌' }}
Results:
${{ steps.golangci.outcome == 'failure' && '❌' || '✅' }} **Golangci linters check**: ${{ steps.golangci.outcome }}
${{ steps.gocrit.outcome == 'failure' && '❌' || '✅' }} **Go critical linters check**: ${{ steps.gocrit.outcome }}
${{ steps.gosec.outcome == 'failure' && '❌' || '✅' }} **Go security linters check**: ${{ steps.gosec.outcome }}