bazel: add golangci-lint to //:check target (#1494)

Signed-off-by: Paul Meyer <49727155+katexochen@users.noreply.github.com>
This commit is contained in:
Paul Meyer 2023-03-23 12:27:09 -04:00 committed by GitHub
parent e92c08be31
commit f7713df833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 158 additions and 56 deletions

View File

@ -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 }}

View File

@ -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

View File

@ -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": [

40
bazel/ci/golangci.sh.in Normal file
View File

@ -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}"

View File

@ -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.

View File

@ -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"],
)

View File

@ -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",
)