Skip to content

Commit a8d7811

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 0681fed commit a8d7811

File tree

2 files changed

+187
-9
lines changed

2 files changed

+187
-9
lines changed

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

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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/v2#readme
6+
GO_VERSION: "1.16"
7+
8+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
9+
on:
10+
push:
11+
paths:
12+
- ".github/workflows/check-go-task.ya?ml"
13+
- "Taskfile.ya?ml"
14+
- "go.mod"
15+
- "go.sum"
16+
- "**.go"
17+
pull_request:
18+
paths:
19+
- ".github/workflows/check-go-task.ya?ml"
20+
- "Taskfile.ya?ml"
21+
- "go.mod"
22+
- "go.sum"
23+
- "**.go"
24+
workflow_dispatch:
25+
repository_dispatch:
26+
27+
jobs:
28+
check-errors:
29+
runs-on: ubuntu-latest
30+
31+
steps:
32+
- name: Checkout repository
33+
uses: actions/checkout@v2
34+
35+
- name: Install Go
36+
uses: actions/setup-go@v2
37+
with:
38+
go-version: ${{ env.GO_VERSION }}
39+
40+
- name: Install Task
41+
uses: arduino/setup-task@v1
42+
with:
43+
repo-token: ${{ secrets.GITHUB_TOKEN }}
44+
version: 3.x
45+
46+
- name: Check for errors
47+
run: task go:vet
48+
49+
check-outdated:
50+
runs-on: ubuntu-latest
51+
52+
steps:
53+
- name: Checkout repository
54+
uses: actions/checkout@v2
55+
56+
- name: Install Go
57+
uses: actions/setup-go@v2
58+
with:
59+
go-version: ${{ env.GO_VERSION }}
60+
61+
- name: Install Task
62+
uses: arduino/setup-task@v1
63+
with:
64+
repo-token: ${{ secrets.GITHUB_TOKEN }}
65+
version: 3.x
66+
67+
- name: Modernize usages of outdated APIs
68+
run: task go:fix
69+
70+
- name: Check if any fixes were needed
71+
run: git diff --color --exit-code
72+
73+
check-style:
74+
runs-on: ubuntu-latest
75+
76+
steps:
77+
- name: Checkout repository
78+
uses: actions/checkout@v2
79+
80+
- name: Install Go
81+
uses: actions/setup-go@v2
82+
with:
83+
go-version: ${{ env.GO_VERSION }}
84+
85+
- name: Install Task
86+
uses: arduino/setup-task@v1
87+
with:
88+
repo-token: ${{ secrets.GITHUB_TOKEN }}
89+
version: 3.x
90+
91+
- name: Install golint
92+
run: go install golang.org/x/lint/golint@latest
93+
94+
- name: Check style
95+
run: task --silent go:lint
96+
97+
check-formatting:
98+
runs-on: ubuntu-latest
99+
100+
steps:
101+
- name: Checkout repository
102+
uses: actions/checkout@v2
103+
104+
- name: Install Go
105+
uses: actions/setup-go@v2
106+
with:
107+
go-version: ${{ env.GO_VERSION }}
108+
109+
- name: Install Task
110+
uses: arduino/setup-task@v1
111+
with:
112+
repo-token: ${{ secrets.GITHUB_TOKEN }}
113+
version: 3.x
114+
115+
- name: Format code
116+
run: task go:format
117+
118+
- name: Check formatting
119+
run: git diff --color --exit-code
120+
121+
check-config:
122+
runs-on: ubuntu-latest
123+
124+
steps:
125+
- name: Checkout repository
126+
uses: actions/checkout@v2
127+
128+
- name: Install Go
129+
uses: actions/setup-go@v2
130+
with:
131+
go-version: ${{ env.GO_VERSION }}
132+
133+
- name: Run go mod tidy
134+
run: go mod tidy
135+
136+
- name: Check whether any tidying was needed
137+
run: git diff --color --exit-code

Taskfile.yml

Lines changed: 50 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
version: "3"
22

3+
vars:
4+
DIST_DIR: "dist"
5+
DUMMY_DISCOVERY_VERSION:
6+
sh: echo "$(git describe --tags --dirty --broken)"
7+
DUMMY_DISCOVERY_TIMESTAMP:
8+
sh: echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
9+
DUMMY_DISCOVERY_LDFLAGS: >
10+
-X github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args.Tag={{.DUMMY_DISCOVERY_VERSION}}
11+
-X github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args.Timestamp={{.DUMMY_DISCOVERY_TIMESTAMP}}
12+
DEFAULT_GO_PACKAGES:
13+
sh: echo $(go list ./... | tr '\n' ' ')
14+
315
tasks:
416
build-dummy-discovery:
517
desc: Build the dummy-discovery client example
@@ -73,12 +85,41 @@ tasks:
7385
cmds:
7486
- poetry update
7587

76-
vars:
77-
DIST_DIR: "dist"
78-
DUMMY_DISCOVERY_VERSION:
79-
sh: echo "$(git describe --tags --dirty --broken)"
80-
DUMMY_DISCOVERY_TIMESTAMP:
81-
sh: echo "$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
82-
DUMMY_DISCOVERY_LDFLAGS: >
83-
-X github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args.Tag={{.DUMMY_DISCOVERY_VERSION}}
84-
-X github.com/arduino/pluggable-discovery-protocol-handler/dummy-discovery/args.Timestamp={{.DUMMY_DISCOVERY_TIMESTAMP}}
88+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
89+
go:check:
90+
desc: Check for problems with Go code
91+
deps:
92+
- task: go:vet
93+
- task: go:lint
94+
95+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
96+
go:vet:
97+
desc: Check for errors in Go code
98+
cmds:
99+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
100+
101+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
102+
go:fix:
103+
desc: Modernize usages of outdated APIs
104+
cmds:
105+
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
106+
107+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
108+
go:lint:
109+
desc: Lint Go code
110+
cmds:
111+
- |
112+
if ! which golint &>/dev/null; then
113+
echo "golint not installed or not in PATH. Please install: https://github.com/golang/lint#installation"
114+
exit 1
115+
fi
116+
- |
117+
golint \
118+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
119+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
120+
121+
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
122+
go:format:
123+
desc: Format Go code
124+
cmds:
125+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

0 commit comments

Comments
 (0)