Skip to content

Commit b5a24a3

Browse files
committed
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 31b0a9d commit b5a24a3

File tree

4 files changed

+270
-20
lines changed

4 files changed

+270
-20
lines changed

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

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

.github/workflows/release.yml

-3
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ jobs:
8686
version: '3.x'
8787
repo-token: ${{ secrets.GITHUB_TOKEN }}
8888

89-
- name: Check the code is good
90-
run: task check
91-
9289
- name: Build the Agent for linux
9390
run: task go:build
9491
if: matrix.os == 'ubuntu-20.04'

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[![Test Integration status](https://github.com/arduino/arduino-create-agent/actions/workflows/test-go-integration-task.yml/badge.svg)](https://github.com/arduino/arduino-create-agent/actions/workflows/test-go-integration-task.yml)
55
[![Check License status](https://github.com/arduino/arduino-create-agent/actions/workflows/check-license.yml/badge.svg)](https://github.com/arduino/arduino-create-agent/actions/workflows/check-license.yml)
66
[![Check Go Dependencies status](https://github.com/arduino/arduino-create-agent/actions/workflows/check-go-dependencies-task.yml/badge.svg)](https://github.com/arduino/arduino-create-agent/actions/workflows/check-go-dependencies-task.yml)
7+
[![Check Go status](https://github.com/arduino/arduino-create-agent/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/arduino-create-agent/actions/workflows/check-go-task.yml)
78

89
arduino-create-agent
910
====================

Taskfile.yml

+41-17
Original file line numberDiff line numberDiff line change
@@ -96,22 +96,50 @@ tasks:
9696
cmds:
9797
- poetry update
9898

99+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
100+
go:fix:
101+
desc: Modernize usages of outdated APIs
102+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
103+
cmds:
104+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
105+
106+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
107+
go:format:
108+
desc: Format Go code
109+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
110+
cmds:
111+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
112+
113+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
114+
go:lint:
115+
desc: Lint Go code
116+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
117+
cmds:
118+
- |
119+
if ! which golint &>/dev/null; then
120+
echo "golint not installed or not in PATH. Please install: https://github.com/golang/lint#installation"
121+
exit 1
122+
fi
123+
- |
124+
golint \
125+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
126+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
127+
128+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
129+
go:vet:
130+
desc: Check for errors in Go code
131+
dir: "{{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}}"
132+
cmds:
133+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
134+
99135
check:
100136
desc: Check fmt and lint
101137
cmds:
102-
- go version
103-
- go fmt {{ default .DEFAULT_TARGETS .TARGETS }}
104-
- test -z $(go fmt {{ default .DEFAULT_TARGETS .TARGETS }})
105-
- echo 'test ok'
106-
- go vet {{ default .DEFAULT_TARGETS .TARGETS }}
107-
- echo 'vet ok'
108-
# FIXME: too many suggestions are failing the check, I'll fix these one in
109-
# another PR.
110-
# - "'{{.GOLINTBIN}}' {{.GOLINTFLAGS}} {{ default .DEFAULT_TARGETS .TARGETS }}"
111-
# - task: i18n:check
112-
# - task: python:check
113-
# - task: docs:check
114-
# - task: config:check
138+
- task: go:vet
139+
- task: go:lint
140+
- task: python:lint
141+
- task: protoc:check
142+
115143

116144
vars:
117145
TAG_TEST: "0.0.0-dev"
@@ -124,7 +152,3 @@ vars:
124152
DEFAULT_GO_PACKAGES:
125153
sh: |
126154
echo $(cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} && go list ./... | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')
127-
# check-lint vars
128-
GOLINTBIN:
129-
sh: go list -f {{"{{"}}".Target{{"}}"}}" golang.org/x/lint/golint
130-
GOLINTFLAGS: "-min_confidence 0.8 -set_exit_status"

0 commit comments

Comments
 (0)