Skip to content

Commit 6257da8

Browse files
authored
Add Lint and check formatting of a Go module workflow (#742)
* 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 * skip gen/ module and design * fix `task go:lint` * fix `go:format` * install dependencies
1 parent 4372c8e commit 6257da8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2600
-2308
lines changed

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

+231
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
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: Install Dependencies
85+
run: sudo apt update && sudo apt install -y --no-install-recommends build-essential libgtk-3-dev libwebkit2gtk-4.0-dev libappindicator3-dev
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+
98+
strategy:
99+
fail-fast: false
100+
101+
matrix:
102+
module:
103+
# TODO: add paths of all Go modules here
104+
- path: ./
105+
106+
steps:
107+
- name: Checkout repository
108+
uses: actions/checkout@v3
109+
110+
- name: Install Go
111+
uses: actions/setup-go@v3
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+
135+
strategy:
136+
fail-fast: false
137+
138+
matrix:
139+
module:
140+
# TODO: add paths of all Go modules here
141+
- path: ./
142+
143+
steps:
144+
- name: Checkout repository
145+
uses: actions/checkout@v3
146+
147+
- name: Install Go
148+
uses: actions/setup-go@v3
149+
with:
150+
go-version: ${{ env.GO_VERSION }}
151+
152+
- name: Install Task
153+
uses: arduino/setup-task@v1
154+
with:
155+
repo-token: ${{ secrets.GITHUB_TOKEN }}
156+
version: 3.x
157+
158+
- name: Install golint
159+
run: go install golang.org/x/lint/golint@latest
160+
161+
- name: Check style
162+
env:
163+
GO_MODULE_PATH: ${{ matrix.module.path }}
164+
run: task --silent go:lint
165+
166+
check-formatting:
167+
name: check-formatting (${{ matrix.module.path }})
168+
needs: run-determination
169+
if: needs.run-determination.outputs.result == 'true'
170+
runs-on: ubuntu-latest
171+
172+
strategy:
173+
fail-fast: false
174+
175+
matrix:
176+
module:
177+
# TODO: add paths of all Go modules here
178+
- path: ./
179+
180+
steps:
181+
- name: Checkout repository
182+
uses: actions/checkout@v3
183+
184+
- name: Install Go
185+
uses: actions/setup-go@v3
186+
with:
187+
go-version: ${{ env.GO_VERSION }}
188+
189+
- name: Install Task
190+
uses: arduino/setup-task@v1
191+
with:
192+
repo-token: ${{ secrets.GITHUB_TOKEN }}
193+
version: 3.x
194+
195+
- name: Format code
196+
env:
197+
GO_MODULE_PATH: ${{ matrix.module.path }}
198+
run: task go:format
199+
200+
- name: Check formatting
201+
run: git diff --color --exit-code
202+
203+
check-config:
204+
name: check-config (${{ matrix.module.path }})
205+
needs: run-determination
206+
if: needs.run-determination.outputs.result == 'true'
207+
runs-on: ubuntu-latest
208+
209+
strategy:
210+
fail-fast: false
211+
212+
matrix:
213+
module:
214+
# TODO: add paths of all Go modules here
215+
- path: ./
216+
217+
steps:
218+
- name: Checkout repository
219+
uses: actions/checkout@v3
220+
221+
- name: Install Go
222+
uses: actions/setup-go@v3
223+
with:
224+
go-version: ${{ env.GO_VERSION }}
225+
226+
- name: Run go mod tidy
227+
working-directory: ${{ matrix.module.path }}
228+
run: go mod tidy
229+
230+
- name: Check whether any tidying was needed
231+
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

+43-19
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ tasks:
3030
desc: Build the project, to use a specific version use `task build TAG_VERSION=x.x.x`
3131
dir: "{{.DEFAULT_GO_MODULE_PATH}}"
3232
cmds:
33-
- go build -v -i {{default "" .ADDITIONAL_FLAGS}} -o {{default "arduino-create-agent" .APP_NAME}} -ldflags '-X main.version={{default .TAG_TEST .TAG_VERSION}} -X main.git_revision={{.COMMIT}} {{default "" .WIN_FLAGS}}'
33+
- go build -v -i {{default "" .ADDITIONAL_FLAGS}} -o {{default "arduino-create-agent" .APP_NAME}} -ldflags '-X main.version={{default .TAG_TEST .TAG_VERSION}} -X main.commit={{.COMMIT}} {{default "" .WIN_FLAGS}}'
3434
vars:
3535
COMMIT:
3636
sh: git log -n 1 --format=%h
@@ -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"
@@ -123,8 +151,4 @@ vars:
123151
DEFAULT_GO_MODULE_PATH: ./
124152
DEFAULT_GO_PACKAGES:
125153
sh: |
126-
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"
154+
echo $(cd {{default .DEFAULT_GO_MODULE_PATH .GO_MODULE_PATH}} && go list ./... | grep -v 'arduino-create-agent/gen/' | grep -v 'arduino-create-agent/design' | tr '\n' ' ' || echo '"ERROR: Unable to discover Go packages"')

bufferflow.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package main
1717

18+
// Bufferflow interface
1819
type Bufferflow interface {
1920
Init()
2021
OnIncomingData(data string) // implement this method

bufferflow_default.go

+5
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ import (
2121
log "github.com/sirupsen/logrus"
2222
)
2323

24+
// BufferflowDefault is the default bufferflow, whick means no buffering
2425
type BufferflowDefault struct {
2526
port string
2627
output chan<- []byte
2728
input chan string
2829
done chan bool
2930
}
3031

32+
// NewBufferflowDefault create a new default bufferflow
3133
func NewBufferflowDefault(port string, output chan<- []byte) *BufferflowDefault {
3234
return &BufferflowDefault{
3335
port: port,
@@ -37,6 +39,7 @@ func NewBufferflowDefault(port string, output chan<- []byte) *BufferflowDefault
3739
}
3840
}
3941

42+
// Init will initialize the bufferflow
4043
func (b *BufferflowDefault) Init() {
4144
log.Println("Initting default buffer flow (which means no buffering)")
4245
go b.consumeInput()
@@ -57,10 +60,12 @@ Loop:
5760
close(b.input) // close the input channel at the end of the computation
5861
}
5962

63+
// OnIncomingData will forward the data
6064
func (b *BufferflowDefault) OnIncomingData(data string) {
6165
b.input <- data
6266
}
6367

68+
// Close will close the bufferflow
6469
func (b *BufferflowDefault) Close() {
6570
b.done <- true
6671
close(b.done)

0 commit comments

Comments
 (0)