Skip to content

Commit f07b211

Browse files
committed
Add CI workflow to lint and check formatting of Go code
On every push and pull request that affects relevant files, and periodically, lint and check formatting of the repository's Go module.
1 parent ab8b831 commit f07b211

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed

.github/workflows/check-go.yml

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
name: Check Go
2+
3+
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
4+
on:
5+
push:
6+
paths:
7+
- ".github/workflows/check-go.yml"
8+
- "Taskfile.yml"
9+
- "**.go"
10+
pull_request:
11+
paths:
12+
- ".github/workflows/check-go.yml"
13+
- "Taskfile.yml"
14+
- "**.go"
15+
schedule:
16+
# Run every Tuesday at 8 AM UTC to catch breakage caused by changes to tools.
17+
- cron: "0 8 * * TUE"
18+
workflow_dispatch:
19+
repository_dispatch:
20+
21+
jobs:
22+
check-errors:
23+
runs-on: ubuntu-latest
24+
25+
steps:
26+
- name: Checkout repository
27+
uses: actions/checkout@v2
28+
29+
- name: Install Task
30+
uses: arduino/actions/setup-taskfile@master
31+
with:
32+
repo-token: ${{ secrets.GITHUB_TOKEN }}
33+
version: 3.x
34+
35+
- name: Check for errors
36+
run: task go:vet
37+
38+
check-style:
39+
runs-on: ubuntu-latest
40+
41+
steps:
42+
- name: Checkout repository
43+
uses: actions/checkout@v2
44+
45+
- name: Install Task
46+
uses: arduino/actions/setup-taskfile@master
47+
with:
48+
repo-token: ${{ secrets.GITHUB_TOKEN }}
49+
version: 3.x
50+
51+
- name: Check style
52+
run: task --silent go:lint
53+
54+
check-formatting:
55+
runs-on: ubuntu-latest
56+
57+
steps:
58+
- name: Checkout repository
59+
uses: actions/checkout@v2
60+
61+
- name: Install Task
62+
uses: arduino/actions/setup-taskfile@master
63+
with:
64+
repo-token: ${{ secrets.GITHUB_TOKEN }}
65+
version: 3.x
66+
67+
- name: Format code
68+
run: task go:format
69+
70+
- name: Check formatting
71+
run: git diff --color --exit-code

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![Check Go status](https://github.com/arduino/libraries-repository-engine/actions/workflows/check-go.yml/badge.svg)](https://github.com/arduino/libraries-repository-engine/actions/workflows/check-go.yml)
2+
13
BUILD
24
----------------------------
35

Taskfile.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,34 @@ tasks:
1515
desc: Run unit tests
1616
cmds:
1717
- go test -v -short -run '{{default ".*" .GO_TEST_REGEX}}' {{default "-timeout 10m -coverpkg=./... -covermode=atomic" .GO_TEST_FLAGS}} -coverprofile=coverage_unit.txt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
18+
19+
go:check:
20+
desc: Check for problems with Go code
21+
deps:
22+
- task: go:vet
23+
- task: go:lint
24+
25+
go:vet:
26+
desc: Check for errors in Go code
27+
cmds:
28+
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
29+
30+
go:lint:
31+
desc: Lint Go code
32+
cmds:
33+
- |
34+
PROJECT_PATH="$PWD"
35+
# `go get` and `go list` commands must be run from a temporary folder to avoid polluting go.mod
36+
cd "$(mktemp -d "${TMPDIR-${TMP-/tmp}}/task-temporary-XXXXX")"
37+
go get golang.org/x/lint/golint
38+
GOLINT_PATH="$(go list -f '{{"{{"}}.Target{{"}}"}}' golang.org/x/lint/golint || echo "false")"
39+
# `golint` must be run from the module folder
40+
cd "$PROJECT_PATH"
41+
"$GOLINT_PATH" \
42+
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
43+
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
44+
45+
go:format:
46+
desc: Format Go code
47+
cmds:
48+
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}

0 commit comments

Comments
 (0)