Skip to content

Commit 1530efa

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 dc3cfbf commit 1530efa

File tree

3 files changed

+296
-1
lines changed

3 files changed

+296
-1
lines changed

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

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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+
- path: ./
70+
71+
steps:
72+
- name: Checkout repository
73+
uses: actions/checkout@v3
74+
75+
- name: Install Go
76+
uses: actions/setup-go@v4
77+
with:
78+
go-version: ${{ env.GO_VERSION }}
79+
80+
- name: Install Task
81+
uses: arduino/setup-task@v1
82+
with:
83+
repo-token: ${{ secrets.GITHUB_TOKEN }}
84+
version: 3.x
85+
86+
- name: Check for errors
87+
env:
88+
GO_MODULE_PATH: ${{ matrix.module.path }}
89+
run: task go:vet
90+
91+
check-outdated:
92+
name: check-outdated (${{ matrix.module.path }})
93+
needs: run-determination
94+
if: needs.run-determination.outputs.result == 'true'
95+
runs-on: ubuntu-latest
96+
permissions:
97+
contents: read
98+
99+
strategy:
100+
fail-fast: false
101+
102+
matrix:
103+
module:
104+
- path: ./
105+
106+
steps:
107+
- name: Checkout repository
108+
uses: actions/checkout@v3
109+
110+
- name: Install Go
111+
uses: actions/setup-go@v4
112+
with:
113+
go-version: ${{ env.GO_VERSION }}
114+
115+
- name: Install Task
116+
uses: arduino/setup-task@v1
117+
with:
118+
repo-token: ${{ secrets.GITHUB_TOKEN }}
119+
version: 3.x
120+
121+
- name: Modernize usages of outdated APIs
122+
env:
123+
GO_MODULE_PATH: ${{ matrix.module.path }}
124+
run: task go:fix
125+
126+
- name: Check if any fixes were needed
127+
run: git diff --color --exit-code
128+
129+
check-style:
130+
name: check-style (${{ matrix.module.path }})
131+
needs: run-determination
132+
if: needs.run-determination.outputs.result == 'true'
133+
runs-on: ubuntu-latest
134+
permissions:
135+
contents: read
136+
137+
strategy:
138+
fail-fast: false
139+
140+
matrix:
141+
module:
142+
- path: ./
143+
144+
steps:
145+
- name: Checkout repository
146+
uses: actions/checkout@v3
147+
148+
- name: Install Go
149+
uses: actions/setup-go@v4
150+
with:
151+
go-version: ${{ env.GO_VERSION }}
152+
153+
- name: Install Task
154+
uses: arduino/setup-task@v1
155+
with:
156+
repo-token: ${{ secrets.GITHUB_TOKEN }}
157+
version: 3.x
158+
159+
- name: Install golint
160+
run: go install golang.org/x/lint/golint@latest
161+
162+
- name: Check style
163+
env:
164+
GO_MODULE_PATH: ${{ matrix.module.path }}
165+
run: task --silent go:lint
166+
167+
check-formatting:
168+
name: check-formatting (${{ matrix.module.path }})
169+
needs: run-determination
170+
if: needs.run-determination.outputs.result == 'true'
171+
runs-on: ubuntu-latest
172+
permissions:
173+
contents: read
174+
175+
strategy:
176+
fail-fast: false
177+
178+
matrix:
179+
module:
180+
- path: ./
181+
182+
steps:
183+
- name: Checkout repository
184+
uses: actions/checkout@v3
185+
186+
- name: Install Go
187+
uses: actions/setup-go@v4
188+
with:
189+
go-version: ${{ env.GO_VERSION }}
190+
191+
- name: Install Task
192+
uses: arduino/setup-task@v1
193+
with:
194+
repo-token: ${{ secrets.GITHUB_TOKEN }}
195+
version: 3.x
196+
197+
- name: Format code
198+
env:
199+
GO_MODULE_PATH: ${{ matrix.module.path }}
200+
run: task go:format
201+
202+
- name: Check formatting
203+
run: git diff --color --exit-code
204+
205+
check-config:
206+
name: check-config (${{ matrix.module.path }})
207+
needs: run-determination
208+
if: needs.run-determination.outputs.result == 'true'
209+
runs-on: ubuntu-latest
210+
permissions:
211+
contents: read
212+
213+
strategy:
214+
fail-fast: false
215+
216+
matrix:
217+
module:
218+
- path: ./
219+
220+
steps:
221+
- name: Checkout repository
222+
uses: actions/checkout@v3
223+
224+
- name: Install Go
225+
uses: actions/setup-go@v4
226+
with:
227+
go-version: ${{ env.GO_VERSION }}
228+
229+
- name: Run go mod tidy
230+
working-directory: ${{ matrix.module.path }}
231+
run: go mod tidy
232+
233+
- name: Check whether any tidying was needed
234+
run: git diff --color --exit-code

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
# portenta-c33-fwuploader-plugin
1+
# portenta-c33-fwuploader-plugin
2+
3+
[![Check Go status](https://github.com/arduino/portenta-c33-fwuploader-plugin/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/portenta-c33-fwuploader-plugin/actions/workflows/check-go-task.yml)

Taskfile.yml

Lines changed: 59 additions & 0 deletions
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)