From f7713df8330317a79fc5f1d88b11851822123e82 Mon Sep 17 00:00:00 2001 From: Paul Meyer <49727155+katexochen@users.noreply.github.com> Date: Thu, 23 Mar 2023 12:27:09 -0400 Subject: [PATCH] bazel: add golangci-lint to //:check target (#1494) Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com> --- .github/workflows/test-lint.yml | 53 --------------------------- .github/workflows/test-tidy.yml | 20 ++++++++-- bazel/ci/BUILD.bazel | 24 ++++++++++++ bazel/ci/golangci.sh.in | 40 ++++++++++++++++++++ bazel/sh/def.bzl | 30 +++++++++++++++ bazel/toolchains/BUILD.golangci.bazel | 8 ++++ bazel/toolchains/ci_deps.bzl | 39 ++++++++++++++++++++ 7 files changed, 158 insertions(+), 56 deletions(-) delete mode 100644 .github/workflows/test-lint.yml create mode 100644 bazel/ci/golangci.sh.in create mode 100644 bazel/toolchains/BUILD.golangci.bazel diff --git a/.github/workflows/test-lint.yml b/.github/workflows/test-lint.yml deleted file mode 100644 index 2f271bb0a..000000000 --- a/.github/workflows/test-lint.yml +++ /dev/null @@ -1,53 +0,0 @@ -name: Golangci-lint - -on: - workflow_dispatch: - push: - branches: - - main - - "release/**" - paths: - - "**.go" - - "**/go.mod" - - "**/go.sum" - - ".github/workflows/test-lint.yml" - pull_request: - paths: - - "**.go" - - "**/go.mod" - - "**/go.sum" - - ".github/workflows/test-lint.yml" - -jobs: - golangci: - name: lint - runs-on: ubuntu-22.04 - steps: - - name: Checkout - uses: actions/checkout@24cb9080177205b6e8c946b17badbe402adc938f # v3.4.0 - with: - ref: ${{ !github.event.pull_request.head.repo.fork && github.head_ref || '' }} - - - name: Install Dependencies - run: sudo apt-get update && sudo apt-get -y install libcryptsetup-dev libvirt-dev - - - name: Setup Go environment - uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 - with: - go-version: "1.20.2" - cache: true - - - name: Get Go submodules - id: submods - shell: bash - run: | - mods=$(go list -f '{{.Dir}}/...' -m | xargs) - echo "Found mods: $mods" - echo "submods=${mods}" >> "$GITHUB_OUTPUT" - - - name: golangci-lint - uses: golangci/golangci-lint-action@08e2f20817b15149a52b5b3ebe7de50aff2ba8c5 # v3.4.0 - with: - skip-pkg-cache: true - skip-build-cache: true - args: --timeout=15m ${{ steps.submods.outputs.submods }} diff --git a/.github/workflows/test-tidy.yml b/.github/workflows/test-tidy.yml index 77b4764ab..fed826956 100644 --- a/.github/workflows/test-tidy.yml +++ b/.github/workflows/test-tidy.yml @@ -20,6 +20,17 @@ jobs: # No token available for forks, so we can't push changes token: ${{ !github.event.pull_request.head.repo.fork && secrets.CI_COMMIT_PUSH_PR || '' }} + - name: Install Dependencies + run: | + echo "::group::Install Dependencies" + sudo apt-get update && sudo apt-get -y install libcryptsetup-dev libvirt-dev + echo "::endgroup::" + + - name: Setup Go environment + uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # v3.5.0 + with: + go-version: "1.20.2" + - name: Run Bazel tidy shell: bash run: bazelisk run //:tidy @@ -31,11 +42,13 @@ jobs: diff=$(git diff) if [[ -z "$diff" ]]; then echo "Everything is tidy." - echo "untidy=false" >> "$GITHUB_OUTPUT" + echo "untidy=false" | tee -a "$GITHUB_OUTPUT" exit 0 fi echo "Detected changes after tidy" - echo "untidy=true" >> "$GITHUB_OUTPUT" + echo "untidy=true" | tee -a "$GITHUB_OUTPUT" + diffsum=$(echo "$diff" | sha256sum) + echo "diffsum=${diffsum}" | tee -a "$GITHUB_OUTPUT" - name: Run Bazel generate shell: bash @@ -46,7 +59,8 @@ jobs: shell: bash run: | diff=$(git diff) - if [[ -z "$diff" ]]; then + diffsum=$(echo "$diff" | sha256sum) + if [[ "${{ steps.untidy.outputs.diffsum }}" != "$diffsum" ]]; then echo "Everything is tidy." echo "ungenerated=false" >> "$GITHUB_OUTPUT" exit 0 diff --git a/bazel/ci/BUILD.bazel b/bazel/ci/BUILD.bazel index c11513166..1f1826b99 100644 --- a/bazel/ci/BUILD.bazel +++ b/bazel/ci/BUILD.bazel @@ -213,6 +213,29 @@ sh_template( template = "tf.sh.in", ) +alias( + name = "com_github_golangci_golangci_lint", + actual = select({ + "@io_bazel_rules_go//go/platform:darwin_amd64": "@com_github_golangci_golangci_lint_darwin_amd64//:golangci_lint_bin", + "@io_bazel_rules_go//go/platform:darwin_arm64": "@com_github_golangci_golangci_lint_darwin_arm64//:golangci_lint_bin", + "@io_bazel_rules_go//go/platform:linux_amd64": "@com_github_golangci_golangci_lint_linux_amd64//:golangci_lint_bin", + "@io_bazel_rules_go//go/platform:linux_arm64": "@com_github_golangci_golangci_lint_linux_arm64//:golangci_lint_bin", + }), +) + +sh_template( + name = "golangci_lint", + data = [ + ":com_github_golangci_golangci_lint", + "@go_sdk//:bin/go", + ], + substitutions = { + "@@GO@@": "$(rootpath @go_sdk//:bin/go)", + "@@GOLANGCI-LINT@@": "$(rootpath :com_github_golangci_golangci_lint)", + }, + template = "golangci.sh.in", +) + multirun( name = "tidy", commands = [ @@ -234,6 +257,7 @@ multirun( commands = [ ":gazelle_check", ":buildifier_check", + ":golangci_lint", ":terraform_check", ] + select({ "@io_bazel_rules_go//go/platform:darwin_arm64": [ diff --git a/bazel/ci/golangci.sh.in b/bazel/ci/golangci.sh.in new file mode 100644 index 000000000..bc4cd105b --- /dev/null +++ b/bazel/ci/golangci.sh.in @@ -0,0 +1,40 @@ +#!/usr/bin/env bash + +lib=$(realpath @@BASE_LIB@@) || exit 1 +golangcilint=$(realpath @@GOLANGCI-LINT@@) || exit 1 +go=$(realpath @@GO@@) || exit 1 + +# shellcheck source=../sh/lib.bash +if ! source "${lib}"; then + echo "Error: could not find import" + exit 1 +fi + +cd "${BUILD_WORKSPACE_DIRECTORY}" || exit 1 + +readarray -t <<< "$(${go} list -f '{{.Dir}}' -m)" +modules=("${MAPFILE[@]}") + +excludeMods=( + "hack/tools" +) + +echo "The following Go modules are excluded and won't be linted with golangci-lint:" +for exclude in "${excludeMods[@]}"; do + for i in "${!modules[@]}"; do + if [[ ${modules[i]} == "${BUILD_WORKSPACE_DIRECTORY}/${exclude}" ]]; then + echo " ${modules[i]}" + unset 'modules[i]' + fi + done +done + +statuscode=0 + +echo "Linting the following Go modules with golangci-lint:" +for mod in "${modules[@]}"; do + echo " ${mod}" + GOROOT=$(${go} env GOROOT) ${golangcilint} run --timeout=15m "${mod}/..." || statuscode=$? +done + +exit "${statuscode}" diff --git a/bazel/sh/def.bzl b/bazel/sh/def.bzl index 884e244b9..6376a7704 100644 --- a/bazel/sh/def.bzl +++ b/bazel/sh/def.bzl @@ -65,6 +65,36 @@ def sh_template(name, **kwargs): **kwargs ) +def sh_test_template(name, **kwargs): + """Build a sh_test from a template + + Args: + name: name + **kwargs: **kwargs + """ + script_name = name + "-script" + + tags = kwargs.get("tags", []) + data = kwargs.get("data", []) + data.append("//bazel/sh:base_lib") + substitutions = kwargs.pop("substitutions", []) + substitutions["@@BASE_LIB@@"] = "$(rootpath //bazel/sh:base_lib)" + template = kwargs.pop("template", []) + + _sh_template( + name = script_name, + tags = tags, + data = data, + substitutions = substitutions, + template = template, + ) + + native.sh_test( + name = name, + srcs = [script_name], + **kwargs + ) + def repo_command(name, **kwargs): """Build a sh_binary that executes a single command. diff --git a/bazel/toolchains/BUILD.golangci.bazel b/bazel/toolchains/BUILD.golangci.bazel new file mode 100644 index 000000000..aeca1fc05 --- /dev/null +++ b/bazel/toolchains/BUILD.golangci.bazel @@ -0,0 +1,8 @@ +genrule( + name = "golangci_lint_bin", + srcs = ["golangci-lint"], + outs = ["golangci_lint_bin_out"], + cmd = "cp $< $@", # Copy the binary to the output directory. + executable = True, + visibility = ["//visibility:public"], +) diff --git a/bazel/toolchains/ci_deps.bzl b/bazel/toolchains/ci_deps.bzl index 6628298fa..95347d4d2 100644 --- a/bazel/toolchains/ci_deps.bzl +++ b/bazel/toolchains/ci_deps.bzl @@ -9,6 +9,7 @@ def ci_deps(): _actionlint_deps() _gofumpt_deps() _tfsec_deps() + _golangci_lint_deps() def _shellcheck_deps(): http_archive( @@ -174,3 +175,41 @@ def _tfsec_deps(): ], sha256 = "6d664dcdd37e2809d1b4f14b310ccda0973b4a29e4624e902286e4964d101e22", ) + +def _golangci_lint_deps(): + http_archive( + name = "com_github_golangci_golangci_lint_linux_amd64", + build_file = "//bazel/toolchains:BUILD.golangci.bazel", + urls = [ + "https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-linux-amd64.tar.gz", + ], + strip_prefix = "golangci-lint-1.51.2-linux-amd64", + sha256 = "4de479eb9d9bc29da51aec1834e7c255b333723d38dbd56781c68e5dddc6a90b", + ) + http_archive( + name = "com_github_golangci_golangci_lint_linux_arm64", + build_file = "//bazel/toolchains:BUILD.golangci.bazel", + urls = [ + "https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-linux-arm64.tar.gz", + ], + strip_prefix = "golangci-lint-1.51.2-linux-arm64", + sha256 = "9e03c47b7628d49f950445d74881a0e3cb3a1e6b3c5ac3b67672d600124c1b08", + ) + http_archive( + name = "com_github_golangci_golangci_lint_darwin_amd64", + build_file = "//bazel/toolchains:BUILD.golangci.bazel", + urls = [ + "https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-darwin-amd64.tar.gz", + ], + strip_prefix = "golangci-lint-1.51.2-darwin-amd64", + sha256 = "0549cbaa2df451cf3a2011a9d73a9cb127784d26749d9cd14c9f4818af104d44", + ) + http_archive( + name = "com_github_golangci_golangci_lint_darwin_arm64", + build_file = "//bazel/toolchains:BUILD.golangci.bazel", + urls = [ + "https://github.com/golangci/golangci-lint/releases/download/v1.51.2/golangci-lint-1.51.2-darwin-arm64.tar.gz", + ], + strip_prefix = "golangci-lint-1.51.2-darwin-arm64", + sha256 = "36e69882205a0e42a63ad57ec3015639c11051e03f0beb9cf7949c6451408960", + )