Skip to content

Commit bd856df

Browse files
authored
Merge pull request #1 from deckhouse/add-lint-and-ci
Signed-off-by: Nikolay Mitrofanov <nikolay.mitrofanov@flant.com>
2 parents 3f07443 + 6af6e5f commit bd856df

File tree

7 files changed

+329
-0
lines changed

7 files changed

+329
-0
lines changed

.github/CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* @name212
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2025 Flant JSC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
name: Check pull request
16+
on:
17+
pull_request_target:
18+
types:
19+
- opened
20+
- synchronize
21+
- reopened
22+
23+
jobs:
24+
run_tests:
25+
name: "Run unit tests"
26+
runs-on: ubuntu-latest
27+
28+
steps:
29+
- &checkout_step
30+
name: Checkout
31+
uses: actions/checkout@v6.0.1
32+
with:
33+
fetch-depth: 0
34+
ref: ${{ github.event.pull_request.head.sha }}
35+
36+
- name: Run tests
37+
run: make test
38+
39+
license_validation:
40+
needs: [run_tests]
41+
name: "Validate that license added to all files"
42+
runs-on: ubuntu-latest
43+
44+
steps:
45+
- *checkout_step
46+
- name: Setup Go
47+
uses: actions/setup-go@v6.1.0
48+
with:
49+
go-version: '1.25.x'
50+
- name: Run validation
51+
run: make validation/license
52+
53+
lint:
54+
needs: [run_tests, license_validation]
55+
name: "Lint golang code"
56+
runs-on: ubuntu-latest
57+
58+
steps:
59+
- *checkout_step
60+
- name: Run lint
61+
run: make lint

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,6 @@ go.work.sum
3030
# Editor/IDE
3131
# .idea/
3232
# .vscode/
33+
34+
bin/
35+
validation/

.golangci.yaml

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
version: "2"
2+
run:
3+
timeout: 10m
4+
5+
linters:
6+
default: none
7+
enable:
8+
- copyloopvar
9+
- dogsled
10+
- errcheck
11+
- gocritic
12+
- govet
13+
- ineffassign
14+
- misspell
15+
- musttag
16+
- nonamedreturns
17+
- prealloc
18+
- revive
19+
- sloglint
20+
- staticcheck
21+
- unconvert
22+
- unparam
23+
- whitespace
24+
- unused
25+
- usestdlibvars
26+
settings:
27+
errcheck:
28+
exclude-functions:
29+
- fmt:.*[rR]ead|[wW]rite|[cC]lose
30+
- io:Copy
31+
nonamedreturns:
32+
report-error-in-defer: false
33+
sloglint:
34+
no-mixed-args: true
35+
# Enforce using key-value pairs only (overrides no-mixed-args, incompatible with attr-only).
36+
kv-only: false
37+
# Enforce using attributes only (overrides no-mixed-args, incompatible with kv-only).
38+
attr-only: false
39+
# Enforce not using global loggers.
40+
no-global: ""
41+
# Enforce using methods that accept a context.
42+
context: ""
43+
# Enforce using static values for log messages.
44+
static-msg: false
45+
# Enforce using constants instead of raw keys.
46+
no-raw-keys: false
47+
# Enforce a single key naming convention.
48+
key-naming-case: "snake"
49+
# Enforce not using specific keys.
50+
forbidden-keys:
51+
- level
52+
- msg
53+
- logger
54+
- source
55+
- stacktrace
56+
- time
57+
# Enforce putting arguments on separate lines.
58+
args-on-sep-lines: false
59+
staticcheck:
60+
checks:
61+
- all
62+
- '-ST1003' # waiting for package name will be fixed (underscores)
63+
- '-QF1008' # not need to fix; we understand how to call nested structs
64+
revive:
65+
rules:
66+
- name: var-naming # waiting for package name will be fixed (incorrect naming)
67+
severity: warning
68+
disabled: false
69+
exclude: [""]
70+
arguments:
71+
- ["ID"] # AllowList
72+
- ["VM"] # DenyList
73+
- - skip-initialism-name-checks: false
74+
upper-case-const: false
75+
skip-package-name-checks: true
76+
skip-package-name-collision-with-go-std: false
77+
extra-bad-package-names: []
78+
exclusions:
79+
generated: lax
80+
presets:
81+
- comments
82+
- common-false-positives
83+
- legacy
84+
- std-error-handling
85+
rules:
86+
- path: (.+)\.go$
87+
text: ST1005.*
88+
- path: (.+)\.go$
89+
text: should not use dot imports
90+
- path: (.+)\.go$
91+
text: don't use an underscore in package name
92+
- path: (.+)\.go$
93+
text: 'exported: .*'
94+
paths:
95+
- validation/
96+
formatters:
97+
enable:
98+
- gci
99+
- gofmt
100+
- goimports
101+
settings:
102+
gci:
103+
sections:
104+
- standard
105+
- default
106+
- prefix(github.com/deckhouse/)
107+
- localmodule
108+
goimports:
109+
local-prefixes:
110+
- github.com/deckhouse/

Makefile

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
# Copyright 2025 Flant JSC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
SHELL = /usr/bin/env bash
16+
17+
GOLANGCI_VERSION = 2.7.2
18+
GOFUMPT_VERSION=0.9.2
19+
JQ_VERSION=1.7.1
20+
21+
PLATFORM_NAME := $(shell uname -m)
22+
23+
OS_NAME := $(shell uname)
24+
ifndef OS
25+
ifeq ($(UNAME), Linux)
26+
OS = linux
27+
else ifeq ($(UNAME), Darwin)
28+
OS = darwin
29+
endif
30+
endif
31+
32+
# Set platform for deps
33+
ifeq ($(OS_NAME), Linux)
34+
GOFUMPT_PLATFORM = linux
35+
JQ_PLATFORM = linux
36+
else ifeq ($(OS_NAME), Darwin)
37+
GOFUMPT_PLATFORM = darwin
38+
JQ_PLATFORM = macos
39+
endif
40+
41+
# Set arch for deps
42+
ifeq ($(PLATFORM_NAME), x86_64)
43+
GOFUMPT_ARCH = amd64
44+
JQ_PLATFORM_ARCH = $(JQ_PLATFORM)-amd64
45+
else ifeq ($(PLATFORM_NAME), arm64)
46+
GOFUMPT_ARCH = arm64
47+
JQ_PLATFORM_ARCH = $(JQ_PLATFORM)-arm64
48+
endif
49+
50+
.PHONY: bin/jq bin/gofumpt bin/golangci-lint clean validation/license/download
51+
52+
bin:
53+
mkdir -p bin
54+
55+
curl-installed:
56+
command -v curl > /dev/null
57+
58+
go-installed:
59+
command -v go
60+
go version
61+
62+
bin/jq: curl-installed bin
63+
curl -sSfL https://github.com/jqlang/jq/releases/download/jq-$(JQ_VERSION)/jq-$(JQ_PLATFORM_ARCH) -o ./bin/jq
64+
@chmod +x "./bin/jq"
65+
66+
bin/golangci-lint: curl-installed bin
67+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | BINARY=golangci-lint bash -s -- v${GOLANGCI_VERSION}
68+
@chmod +x "./bin/golangci-lint"
69+
70+
bin/gofumpt: curl-installed bin
71+
curl -sSfLo "bin/gofumpt" https://github.com/mvdan/gofumpt/releases/download/v$(GOFUMPT_VERSION)/gofumpt_v$(GOFUMPT_VERSION)_$(GOFUMPT_PLATFORM)_$(GOFUMPT_ARCH)
72+
@chmod +x "./bin/gofumpt"
73+
74+
deps: bin bin/jq bin/golangci-lint bin/gofumpt
75+
76+
test: go-installed
77+
./hack/run_tests.sh
78+
79+
lint: bin/golangci-lint
80+
./bin/golangci-lint run ./... -c .golangci.yaml
81+
82+
lint/fix: bin/golangci-lint
83+
./bin/golangci-lint run ./... -c .golangci.yaml --fix
84+
85+
fmt: bin/gofumpt
86+
find . -type f -name '*.go' -not -path "./validation*" -print0 | xargs -0 ./bin/gofumpt -l
87+
88+
validation/license/dir:
89+
mkdir -p validation
90+
91+
validation/license/download: curl-installed validation/license/dir bin/jq
92+
set -o pipefail; \
93+
curl -sL https://api.github.com/repos/deckhouse/deckhouse/contents/tools/validation \
94+
| ./bin/jq 'map(select(.type == "file")) | map(select(.name | test(".*.go")))' \
95+
| ./bin/jq -r '.[] | ["curl -sSfLo \"validation/\(.name)\" \"\(.download_url)\""] | join("\n")' \
96+
| while IFS= read -r command_to_run; do echo "run: $$command_to_run"; $(SHELL) -c "$$command_to_run"; done
97+
98+
validation/license: go-installed validation/license/download
99+
cd ./validation; declare -a validation_deps=("github.com/tidwall/gjson" "gopkg.in/yaml.v2"); \
100+
for i in "${validation_deps[@]}"; do \
101+
go get "$$i"; \
102+
done
103+
go run ./validation/{main,messages,diff,copyright,no_cyrillic,doc_changes,grafana_dashboard,release_requirements}.go -type copyright
104+
# prevent goland ide errors
105+
rm -f ./validation/go.mod ./validation/go.sum
106+
107+
all: bin deps validation/license fmt lint test
108+
109+
clean:
110+
rm -rf ./bin
111+
rm -rf ./validation

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/deckhouse/lib-ssh
2+
3+
go 1.25.5

hack/run_tests.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 Flant JSC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
run_tests=""
18+
19+
if [ -n "$RUN_TEST" ]; then
20+
echo "Found RUN_TEST env. Run only $RUN_TEST test"
21+
run_tests="-run $RUN_TEST"
22+
fi
23+
24+
run_dir="$(pwd)"
25+
packages="$(go list ./... | grep -v /validation/)"
26+
prefix="$(grep -oP 'module .*$' go.mod | sed 's|module ||')"
27+
28+
echo "Found packages: ${packages[@]} in $run_dir with module $prefix"
29+
30+
while IFS= read -r p; do
31+
pkg_dir="${p#$prefix}"
32+
if [ -z "$pkg_dir" ]; then
33+
echo "Package $p cannot have dir after trim $prefix"
34+
exit 1
35+
fi
36+
full_pkg_path="${run_dir}${pkg_dir}"
37+
echo "Run tests in $full_pkg_path"
38+
cd "$full_pkg_path"
39+
echo "test -v -p 1 $run_tests" | xargs go
40+
done <<< "$packages"

0 commit comments

Comments
 (0)