diff --git a/workflow-templates/assets/check-go-task/Taskfile.yml b/workflow-templates/assets/check-go-task/Taskfile.yml new file mode 100644 index 00000000..43fd4b71 --- /dev/null +++ b/workflow-templates/assets/check-go-task/Taskfile.yml @@ -0,0 +1,48 @@ +# See: https://taskfile.dev/#/usage +version: "3" + +vars: + DEFAULT_GO_PACKAGES: + sh: echo $(go list ./... | tr '\n' ' ') + +tasks: + # 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 + 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 + 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 + 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 + 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 + cmds: + - go fmt {{default .DEFAULT_GO_PACKAGES .GO_PACKAGES}} diff --git a/workflow-templates/check-go-task.md b/workflow-templates/check-go-task.md new file mode 100644 index 00000000..0ad39f14 --- /dev/null +++ b/workflow-templates/check-go-task.md @@ -0,0 +1,56 @@ +# "Check Go" workflow (Task) + +Workflow file: [check-go-task.yml](check-go-task.yml) + +Lint and check formatting of a [Go](https://golang.org/) module. + +## Assets + +- [`Taskfile.yml`](assets/check-go-task/Taskfile.yml] - Linting and formatting [tasks](https://taskfile.dev/). + - Install to: repository root (or add the tasks into the existing `Taskfile.yml`) + +## Readme badge + +Markdown badge: + +```markdown +[![Check Go status](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/check-go-task.yml/badge.svg)](https://github.com/REPO_OWNER/REPO_NAME/actions/workflows/check-go-task.yml) +``` + +Replace the `REPO_OWNER` and `REPO_NAME` placeholders in the URLs with the final repository owner and name ([example](https://raw.githubusercontent.com/arduino-libraries/ArduinoIoTCloud/master/README.md)). + +--- + +Asciidoc badge: + +```adoc +image:https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-go-task.yml/badge.svg["Check Go status", link="https://github.com/{repository-owner}/{repository-name}/actions/workflows/check-go-task.yml"] +``` + +Define the `{repository-owner}` and `{repository-name}` attributes and use them throughout the readme ([example](https://raw.githubusercontent.com/arduino-libraries/WiFiNINA/master/README.adoc)). + +## Commit message + +``` +Add CI workflow to lint and check formatting of Go code + +On every push and pull request that affects relevant files, and periodically, check the Go module for: + +- Common detectable errors in the code. +- Use of outdated APIs +- Code style violations +- Code formatting inconsistency +- Misconfiguration +``` + +## PR message + +```markdown +On every push and pull request that affects relevant files, and periodically, check the repository's [Go](https://golang.org/) module for: + +- Common detectable errors in the code. +- Use of outdated APIs +- Code style violations +- Code formatting inconsistency +- Misconfiguration +``` diff --git a/workflow-templates/check-go-task.yml b/workflow-templates/check-go-task.yml new file mode 100644 index 00000000..7e74828f --- /dev/null +++ b/workflow-templates/check-go-task.yml @@ -0,0 +1,105 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md +name: Check Go + +# 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 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 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 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 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: Run go mod tidy + run: go mod tidy + + - name: Check whether any tidying was needed + run: git diff --color --exit-code diff --git a/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml b/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml new file mode 100644 index 00000000..7e74828f --- /dev/null +++ b/workflow-templates/dependabot/workflow-template-copies/.github/workflows/check-go-task.yml @@ -0,0 +1,105 @@ +# Source: https://github.com/arduino/tooling-project-assets/blob/main/workflow-templates/check-go-task.md +name: Check Go + +# 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 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 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 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 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: Run go mod tidy + run: go mod tidy + + - name: Check whether any tidying was needed + run: git diff --color --exit-code