From 93f71bd038c2b55498f8a9ca52c7e7dcb497de36 Mon Sep 17 00:00:00 2001 From: per1234 Date: Thu, 15 Jul 2021 07:26:28 -0700 Subject: [PATCH] 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 --- .github/workflows/check-go-task.yml | 140 ++++++++++++++++++++++++++++ README.md | 1 + Taskfile.yml | 54 +++++++++++ 3 files changed, 195 insertions(+) create mode 100644 .github/workflows/check-go-task.yml diff --git a/.github/workflows/check-go-task.yml b/.github/workflows/check-go-task.yml new file mode 100644 index 00000000..29f9ffc0 --- /dev/null +++ b/.github/workflows/check-go-task.yml @@ -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 diff --git a/README.md b/README.md index d56f16db..04bb93ff 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/Taskfile.yml b/Taskfile.yml index 495a19da..1df78903 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -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