Skip to content

Commit 66209e8

Browse files
Add CI workflow to lint and check formatting of Go code
On every push and pull request that affects relevant files, check the Go module for: - Common detectable errors in the code. - Use of outdated APIs - Code style violations - Code formatting inconsistency - Misconfiguration
1 parent 17fd16a commit 66209e8

File tree

3 files changed

+300
-0
lines changed

3 files changed

+300
-0
lines changed

.github/workflows/check-go-task.yml

+239
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
2+
name: Check Go
3+
4+
env:
5+
# See: https://github.com/actions/setup-go/tree/main#supported-version-syntax
6+
GO_VERSION: "1.20"
7+
8+
# See: https://docs.github.com/actions/using-workflows/events-that-trigger-workflows
9+
on:
10+
create:
11+
push:
12+
paths:
13+
- ".github/workflows/check-go-task.ya?ml"
14+
- "Taskfile.ya?ml"
15+
- "**/go.mod"
16+
- "**/go.sum"
17+
- "**.go"
18+
pull_request:
19+
paths:
20+
- ".github/workflows/check-go-task.ya?ml"
21+
- "Taskfile.ya?ml"
22+
- "**/go.mod"
23+
- "**/go.sum"
24+
- "**.go"
25+
schedule:
26+
# Run periodically to catch breakage caused by external changes.
27+
- cron: "0 7 * * WED"
28+
workflow_dispatch:
29+
repository_dispatch:
30+
31+
jobs:
32+
run-determination:
33+
runs-on: ubuntu-latest
34+
permissions: {}
35+
outputs:
36+
result: ${{ steps.determination.outputs.result }}
37+
steps:
38+
- name: Determine if the rest of the workflow should run
39+
id: determination
40+
run: |
41+
RELEASE_BRANCH_REGEX="refs/heads/[0-9]+.[0-9]+.x"
42+
# The `create` event trigger doesn't support `branches` filters, so it's necessary to use Bash instead.
43+
if [[
44+
"${{ github.event_name }}" != "create" ||
45+
"${{ github.ref }}" =~ $RELEASE_BRANCH_REGEX
46+
]]; then
47+
# Run the other jobs.
48+
RESULT="true"
49+
else
50+
# There is no need to run the other jobs.
51+
RESULT="false"
52+
fi
53+
54+
echo "result=$RESULT" >> $GITHUB_OUTPUT
55+
56+
check-errors:
57+
name: check-errors (${{ matrix.module.path }})
58+
needs: run-determination
59+
if: needs.run-determination.outputs.result == 'true'
60+
runs-on: ubuntu-latest
61+
permissions:
62+
contents: read
63+
64+
strategy:
65+
fail-fast: false
66+
67+
matrix:
68+
module:
69+
# TODO: add paths of all Go modules here
70+
- path: ./
71+
72+
steps:
73+
- name: Checkout repository
74+
uses: actions/checkout@v3
75+
76+
- name: Install Go
77+
uses: actions/setup-go@v4
78+
with:
79+
go-version: ${{ env.GO_VERSION }}
80+
81+
- name: Install Task
82+
uses: arduino/setup-task@v1
83+
with:
84+
repo-token: ${{ secrets.GITHUB_TOKEN }}
85+
version: 3.x
86+
87+
- name: Check for errors
88+
env:
89+
GO_MODULE_PATH: ${{ matrix.module.path }}
90+
run: task go:vet
91+
92+
check-outdated:
93+
name: check-outdated (${{ matrix.module.path }})
94+
needs: run-determination
95+
if: needs.run-determination.outputs.result == 'true'
96+
runs-on: ubuntu-latest
97+
permissions:
98+
contents: read
99+
100+
strategy:
101+
fail-fast: false
102+
103+
matrix:
104+
module:
105+
# TODO: add paths of all Go modules here
106+
- path: ./
107+
108+
steps:
109+
- name: Checkout repository
110+
uses: actions/checkout@v3
111+
112+
- name: Install Go
113+
uses: actions/setup-go@v4
114+
with:
115+
go-version: ${{ env.GO_VERSION }}
116+
117+
- name: Install Task
118+
uses: arduino/setup-task@v1
119+
with:
120+
repo-token: ${{ secrets.GITHUB_TOKEN }}
121+
version: 3.x
122+
123+
- name: Modernize usages of outdated APIs
124+
env:
125+
GO_MODULE_PATH: ${{ matrix.module.path }}
126+
run: task go:fix
127+
128+
- name: Check if any fixes were needed
129+
run: git diff --color --exit-code
130+
131+
check-style:
132+
name: check-style (${{ matrix.module.path }})
133+
needs: run-determination
134+
if: needs.run-determination.outputs.result == 'true'
135+
runs-on: ubuntu-latest
136+
permissions:
137+
contents: read
138+
139+
strategy:
140+
fail-fast: false
141+
142+
matrix:
143+
module:
144+
# TODO: add paths of all Go modules here
145+
- path: ./
146+
147+
steps:
148+
- name: Checkout repository
149+
uses: actions/checkout@v3
150+
151+
- name: Install Go
152+
uses: actions/setup-go@v4
153+
with:
154+
go-version: ${{ env.GO_VERSION }}
155+
156+
- name: Install Task
157+
uses: arduino/setup-task@v1
158+
with:
159+
repo-token: ${{ secrets.GITHUB_TOKEN }}
160+
version: 3.x
161+
162+
- name: Install golint
163+
run: go install golang.org/x/lint/golint@latest
164+
165+
- name: Check style
166+
env:
167+
GO_MODULE_PATH: ${{ matrix.module.path }}
168+
run: task --silent go:lint
169+
170+
check-formatting:
171+
name: check-formatting (${{ matrix.module.path }})
172+
needs: run-determination
173+
if: needs.run-determination.outputs.result == 'true'
174+
runs-on: ubuntu-latest
175+
permissions:
176+
contents: read
177+
178+
strategy:
179+
fail-fast: false
180+
181+
matrix:
182+
module:
183+
# TODO: add paths of all Go modules here
184+
- path: ./
185+
186+
steps:
187+
- name: Checkout repository
188+
uses: actions/checkout@v3
189+
190+
- name: Install Go
191+
uses: actions/setup-go@v4
192+
with:
193+
go-version: ${{ env.GO_VERSION }}
194+
195+
- name: Install Task
196+
uses: arduino/setup-task@v1
197+
with:
198+
repo-token: ${{ secrets.GITHUB_TOKEN }}
199+
version: 3.x
200+
201+
- name: Format code
202+
env:
203+
GO_MODULE_PATH: ${{ matrix.module.path }}
204+
run: task go:format
205+
206+
- name: Check formatting
207+
run: git diff --color --exit-code
208+
209+
check-config:
210+
name: check-config (${{ matrix.module.path }})
211+
needs: run-determination
212+
if: needs.run-determination.outputs.result == 'true'
213+
runs-on: ubuntu-latest
214+
permissions:
215+
contents: read
216+
217+
strategy:
218+
fail-fast: false
219+
220+
matrix:
221+
module:
222+
# TODO: add paths of all Go modules here
223+
- path: ./
224+
225+
steps:
226+
- name: Checkout repository
227+
uses: actions/checkout@v3
228+
229+
- name: Install Go
230+
uses: actions/setup-go@v4
231+
with:
232+
go-version: ${{ env.GO_VERSION }}
233+
234+
- name: Run go mod tidy
235+
working-directory: ${{ matrix.module.path }}
236+
run: go mod tidy
237+
238+
- name: Check whether any tidying was needed
239+
run: git diff --color --exit-code

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
<!-- NOTE: update the pkg.go.dev badge URL on each major release -->
44

55
[![Go Reference](https://pkg.go.dev/badge/github.com/arduino/fwuploader-plugin-helper.svg)](https://pkg.go.dev/github.com/arduino/fwuploader-plugin-helper)
6+
[![Check Go status](https://github.com/arduino/fwuploader-plugin-helper/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/fwuploader-plugin-helper/actions/workflows/check-go-task.yml)
7+
68
<!--[![Test Go status](https://github.com/arduino/fwuploader-plugin-helper/actions/workflows/test-go-task.yml/badge.svg)](https://github.com/arduino/fwuploader-plugin-helper/actions/workflows/test-go-task.yml)-->
79
<!--[![Codecov](https://codecov.io/gh/arduino/fwuploader-plugin-helper/branch/main/graph/badge.svg)](https://codecov.io/gh/arduino/fwuploader-plugin-helper)-->
810

Taskfile.yml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# See: https://taskfile.dev/#/usage
2+
version: "3"
3+
4+
vars:
5+
# Path of the project's primary Go module:
6+
DEFAULT_GO_MODULE_PATH: ./
7+
DEFAULT_GO_PACKAGES:
8+
sh: |
9+
echo $(
10+
cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} &&
11+
go list ./... | tr '\n' ' ' ||
12+
echo '"ERROR: Unable to discover Go packages"'
13+
)
14+
# `-ldflags` flag to use for `go build` command
15+
LDFLAGS:
16+
17+
tasks:
18+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/go-task/Taskfile.yml
19+
go:build:
20+
desc: Build the Go code
21+
dir: "{{.DEFAULT_GO_MODULE_PATH}}"
22+
cmds:
23+
- go build -v {{.LDFLAGS}}
24+
25+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
26+
go:fix:
27+
desc: Modernize usages of outdated APIs
28+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
29+
cmds:
30+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
31+
32+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
33+
go:format:
34+
desc: Format Go code
35+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
36+
cmds:
37+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
38+
39+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
40+
go:lint:
41+
desc: Lint Go code
42+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
43+
cmds:
44+
- |
45+
if ! which golint &>/dev/null; then
46+
echo "golint not installed or not in PATH. Please install: https://github.com/golang/lint#installation"
47+
exit 1
48+
fi
49+
- |
50+
golint \
51+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
52+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
53+
54+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
55+
go:vet:
56+
desc: Check for errors in Go code
57+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
58+
cmds:
59+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

0 commit comments

Comments
 (0)