1
0
mirror of https://github.com/arduino/library-registry.git synced 2025-07-29 14:01:15 +03:00

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
This commit is contained in:
per1234
2021-07-15 07:26:28 -07:00
parent 71ea0c6d07
commit 93f71bd038
3 changed files with 195 additions and 0 deletions

140
.github/workflows/check-go-task.yml vendored Normal file
View File

@ -0,0 +1,140 @@
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md
name: Check Go
env:
# See: https://github.com/actions/setup-go/tree/v2#readme
GO_VERSION: "1.14"
# See: https://docs.github.com/en/actions/reference/events-that-trigger-workflows
on:
push:
paths:
- ".github/workflows/check-go-task.ya?ml"
- "Taskfile.ya?ml"
- "**/go.mod"
- "**/go.sum"
- "**.go"
pull_request:
paths:
- ".github/workflows/check-go-task.ya?ml"
- "Taskfile.ya?ml"
- "**/go.mod"
- "**/go.sum"
- "**.go"
workflow_dispatch:
repository_dispatch:
jobs:
check-errors:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x
- name: Check for errors
run: task go:vet
check-outdated:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x
- name: Modernize usages of outdated APIs
run: task go:fix
- name: Check if any fixes were needed
run: git diff --color --exit-code
check-style:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x
- name: Check style
run: task --silent go:lint
check-formatting:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x
- name: Format code
run: task go:format
- name: Check formatting
run: git diff --color --exit-code
check-config:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ env.GO_VERSION }}
- name: Install Task
uses: arduino/setup-task@v1
with:
repo-token: ${{ secrets.GITHUB_TOKEN }}
version: 3.x
- name: Run go mod tidy
run: task go:tidy
- name: Check whether any tidying was needed
run: git diff --color --exit-code

View File

@ -2,6 +2,7 @@
[![Check Registry status](https://github.com/arduino/library-registry/actions/workflows/check-registry.yml/badge.svg)](https://github.com/arduino/library-registry/actions/workflows/check-registry.yml)
[![Test Integration status](https://github.com/arduino/library-registry/actions/workflows/test-go-integration-task.yml/badge.svg)](https://github.com/arduino/library-registry/actions/workflows/test-go-integration-task.yml)
[![Check Go status](https://github.com/arduino/library-registry/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/arduino/library-registry/actions/workflows/check-go-task.yml)
[![Check Python status](https://github.com/arduino/library-registry/actions/workflows/check-python-task.yml/badge.svg)](https://github.com/arduino/library-registry/actions/workflows/check-python-task.yml)
[![Check General Formatting status](https://github.com/arduino/library-registry/actions/workflows/check-general-formatting-task.yml/badge.svg)](https://github.com/arduino/library-registry/actions/workflows/check-general-formatting-task.yml)

View File

@ -4,6 +4,8 @@ version: "3"
vars:
GO_PROJECT_PATH: .github/workflows/assets/validate-registry
LDFLAGS:
DEFAULT_GO_PACKAGES:
sh: cd "{{.GO_PROJECT_PATH}}" && echo $(go list ./... | tr '\n' ' ')
tasks:
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-general-formatting-task/Taskfile.yml
@ -24,6 +26,58 @@ tasks:
cmds:
- go build -v {{.LDFLAGS}}
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
go:check:
desc: Check for problems with Go code
dir: "{{.GO_PROJECT_PATH}}"
deps:
- task: go:vet
- task: go:lint
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
go:vet:
desc: Check for errors in Go code
dir: "{{.GO_PROJECT_PATH}}"
cmds:
- go vet {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
go:fix:
desc: Modernize usages of outdated APIs
dir: "{{.GO_PROJECT_PATH}}"
cmds:
- go fix {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
go:lint:
desc: Lint Go code
dir: "{{.GO_PROJECT_PATH}}"
cmds:
- |
PROJECT_PATH="$PWD"
# `go get` and `go list` commands must be run from a temporary folder to avoid polluting go.mod
cd "$(mktemp -d "${TMPDIR-${TMP-/tmp}}/task-temporary-XXXXX")"
go get golang.org/x/lint/golint
GOLINT_PATH="$(go list -f '{{"{{"}}.Target{{"}}"}}' golang.org/x/lint/golint || echo "false")"
# `golint` must be run from the module folder
cd "$PROJECT_PATH"
"$GOLINT_PATH" \
{{default "-min_confidence 0.8 -set_exit_status" .GO_LINT_FLAGS}} \
{{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/check-go-task/Taskfile.yml
go:format:
desc: Format Go code
dir: "{{.GO_PROJECT_PATH}}"
cmds:
- go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}}
go:tidy:
desc: Run go mod tidy
dir: "{{.GO_PROJECT_PATH}}"
cmds:
- go mod tidy
# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/assets/test-go-integration-task/Taskfile.yml
go:test-integration:
desc: Run integration tests