Repository navigation
Expand file tree
/
Copy path.pre-commit-config.yaml
More file actions
278 lines (273 loc) · 15.8 KB
/
Copy path.pre-commit-config.yaml
File metadata and controls
278 lines (273 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# DO NOT EDIT. Generated with:
#
# devctl
#
# https://github.com/giantswarm/devctl/blob/84caccc8334479d1752d924494e76942d6ec1188/pkg/gen/input/precommit/internal/file/pre-commit-config.yaml.template
#
minimum_pre_commit_version: '2.17'
default_install_hook_types: [pre-commit, commit-msg]
# Protobuf-generated code (protoc, buf: *_pb.ts, *_pb.js, *_pb2.py,
# *_pb2_grpc.py, *.pb.go, *_grpc.pb.go) belongs to its generator: a hook that
# rewrote it (end-of-file-fixer, go-imports, ruff --fix) would be undone by
# the next generation, so no hook reads it.
exclude: "(_pb2?|\\.pb)(_grpc)?\\.[^/]+$"
repos:
# base hooks
#
# helm/<chart>/charts/ holds vendored upstream subcharts (vendir-synced).
# Those files are not repo-authored: the auto-fixing hooks would rewrite
# them on every vendir bump and the next vendir sync would revert the fix,
# so the whitespace/shebang/private-key hooks skip the vendored subtree.
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
hooks:
- id: check-merge-conflict
- id: check-shebang-scripts-are-executable
exclude: "^helm/[^/]+/charts/.*"
- id: detect-private-key
# Upstream charts document key formats with placeholder PEM headers
# (redis-ha's restore.ssh.key example) that the hook cannot tell from
# a key; gitleaks stays the scanner of record for the vendored subtree.
exclude: "^helm/[^/]+/charts/.*"
- id: end-of-file-fixer
exclude: "(.*testdata/.*|^\\.yarn/.*|^helm/[^/]+/charts/.*|.*.patch)"
- id: mixed-line-ending
exclude: "(^helm/[^/]+/charts/.*|.*.patch)"
- id: trailing-whitespace
exclude: "(.*testdata/.*|^\\.yarn/.*|^helm/[^/]+/charts/.*|.*.patch)"
- repo: https://github.com/compilerla/conventional-pre-commit
rev: v4.5.0
hooks:
- id: conventional-pre-commit
stages: [commit-msg]
args: [--strict]
# Go hooks run the tools on the PATH: gofmt and go from the Go toolchain,
# goimports and golangci-lint at the versions zz_generated.pre-commit.yaml installs.
# They replace the archived dnephin/pre-commit-golang and keep its behaviour.
- repo: local
hooks:
- id: go-fmt
name: go fmt
language: system
files: '\.go$'
entry: >-
bash -c 'command -v gofmt > /dev/null || { echo "gofmt not installed or available in the PATH, see https://go.dev/doc/install"; exit 1; };
out="$(gofmt -l -w "$@")"; rc=$?;
[ -z "$out" ] || echo "$out";
[ $rc -eq 0 ] && [ -z "$out" ]' --
- id: go-mod-tidy
name: go mod tidy
language: system
files: '\.go$'
pass_filenames: false
entry: >-
bash -c 'if go mod tidy -v 2>&1 | grep -q "updates to go.mod needed"; then exit 1; fi;
git diff --exit-code go.* > /dev/null 2>&1;
if [ $? -eq 1 ]; then echo "go.mod or go.sum differs, please re-add it to your commit"; exit 1; fi' --
- id: golangci-lint
name: golangci-lint
language: system
types: [go]
pass_filenames: false
entry: golangci-lint run
args:
# Let independent sessions and worktrees lint at the same time.
- --allow-parallel-runners
# No --timeout: golangci-lint v2 has none by default and a flag would
# override a repo's `run.timeout` in .golangci.yml, its place to set one.
# List all issues found
- --max-same-issues=0
- --max-issues-per-linter=0
- id: go-imports
name: go imports
language: system
files: '\.go$'
entry: >-
bash -c 'command -v goimports > /dev/null || { echo "goimports not installed or available in the PATH, see https://pkg.go.dev/golang.org/x/tools/cmd/goimports"; exit 1; };
out="$(goimports -l -w "$@")"; rc=$?;
[ -z "$out" ] || echo "$out";
[ $rc -eq 0 ] && [ -z "$out" ]' --
args: [ -local, github.com/giantswarm/muster/v5 ]
# Renovate's gomod manager reads a require line's trailing comment as one
# word (`// indirect`); a line whose comment has more words is skipped
# without a PR or a dashboard entry, so the dependency silently stops being
# updated. The explanation goes on a comment line above the require instead.
- repo: local
hooks:
- id: go-mod-require-comment
name: "go.mod require comment is one word (Renovate skips the line otherwise; move the explanation to a comment line above it)"
language: pygrep
files: (^|/)go\.mod$
entry: '^\s*(require\s+)?[A-Za-z0-9][^\s,]*[./][^\s,]*\s+v[0-9]\S*\s*//\s*\S+\s+\S'
# Helm chart schema for muster: verify helm/muster/values.schema.json is up to date.
#
# `devctl gen precommit` now generates and writes helm/muster/values.schema.json itself
# (running the same generate -> fix $ref -> normalize pipeline in-process, see
# giantswarm/devctl#2195), so this hook is READ-ONLY: it reproduces that pipeline to a
# scratch file and diffs it against the committed one, but never writes the committed
# file. A hook that regenerated files in place used to fight `schemalint-verify` and
# itself over key ordering, because pre-commit runs each hook independently against the
# file on disk and fails the run if any hook modifies it (giantswarm/giantswarm#37267);
# a read-only check has no resting format to fight over.
#
# The `$ref` step repairs a JSON Schema 2020-12 defect: `additionalProperties: false` as
# a sibling of a `$ref`. `additionalProperties` only sees sibling
# `properties`/`patternProperties` -- NOT properties pulled in through `$ref` -- so such
# a node rejects every field the referenced schema defines (e.g. `helm template` fails
# with "additional properties 'podAntiAffinity' not allowed" for a k8s Affinity). The
# correct keyword is `unevaluatedProperties`, which DOES consider $ref-evaluated
# properties. So for every object that has both `$ref` and `additionalProperties: false`
# we drop `additionalProperties` and set `unevaluatedProperties: false`.
#
# The generator itself no longer produces this. It did until v2.6.0, which shipped
# https://github.com/losisin/helm-values-schema-json/pull/378 (fixing #317): at draft
# 2019 or later, with `noAdditionalProperties: true`, it now closes applicator-bearing
# nodes with `unevaluatedProperties` instead. devctl sets `draft: 2020`, so that holds
# here. Under v2.6.0 the step is a no-op on generated output -- measured over every
# repository that DECLARES the `helmchart` pre-commit flavour. The hook reaches more
# repositories than that: `helmchart` is also the default flavour for any repository
# with a `helm/` directory, so re-measure over those too before you act on the number.
#
# It stays for schemas the generator BUNDLES rather than generates. `bundle: true`
# inlines a `# @schema $ref: <file>` target verbatim, defect included, and the tool
# cannot be expected to rewrite third-party content. One repo depends on this today:
# security-profiles-operator-app vendors the upstream chart's own `values.schema.json`,
# whose `daemon.tolerations` items close a `$ref`ed k8s Toleration with
# `additionalProperties: false` and so reject `value` and `tolerationSeconds`.
# Before removing this step, re-run that measurement -- do not assume it is dead.
#
# BOTH binaries are installed and pinned by the hook itself (`additional_dependencies`,
# hence `language: golang`), at the exact versions devctl's own go.mod requires (stamped
# in by the generator, not hardcoded here), so this reproduction runs the same versions
# devctl used to write the committed file.
#
# `helm-values-schema-json` is the module behind the `helm schema` plugin -- the very
# same binary, called directly, so nothing here depends on a helm plugin. It used to:
# the hook called a bare `helm schema`, resolved from whatever the developer had in
# their global helm plugin dir, while CI pinned a version in a different file. The old
# guard only checked that *a* plugin existed, never which version, so a wrong version
# printed "Passed" locally while rewriting the committed schema (v2.6.0 alone strips 41
# lines from hello-world-app's) and CI then rejected it.
- repo: local
hooks:
- id: helm-schema-muster
name: "verify helm/muster/values.schema.json is up to date"
language: golang
additional_dependencies: ['github.com/giantswarm/schemalint/v2@v2.6.3', 'github.com/losisin/helm-values-schema-json/v2@v2.6.1']
files: ^helm/muster/(values\.yaml|\.schema\.yaml|values\.schema\.json|zz_generated\.app-platform\.values\.yaml)$
pass_filenames: false
require_serial: true
entry: sh -c
args:
- |
set -e
tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT
helm-values-schema-json --config helm/muster/.schema.yaml -o "$tmp"
python3 -c 'import json,sys; h=lambda o: {**{k:v for k,v in o.items() if k!="additionalProperties"},"unevaluatedProperties":False} if ("$ref" in o and o.get("additionalProperties") is False) else o; p=sys.argv[1]; f=open(p,encoding="utf-8"); d=json.load(f,object_hook=h); f.close(); f=open(p,"w",encoding="utf-8"); json.dump(d,f); f.close()' "$tmp"
schemalint normalize "$tmp" -o "$tmp" --force
if ! diff -u helm/muster/values.schema.json "$tmp"; then
printf '%s\n' "helm/muster/values.schema.json is out of date; run from the repository root and commit the result:" 'devctl gen precommit --language='"'"'go'"'"' --repo-name='"'"'github.com/giantswarm/muster/v5'"'"' --flavors='"'"'helmchart'"'"' --k8s-schema-version='"'"'v1.33.1'"'"'' >&2
exit 1
fi
- repo: https://github.com/giantswarm/schemalint
rev: v2.6.3
hooks:
- id: schemalint-verify
pass_filenames: false
args:
- helm/muster/values.schema.json
# Helm chart schema for muster-crds: verify helm/muster-crds/values.schema.json is up to date.
#
# `devctl gen precommit` now generates and writes helm/muster-crds/values.schema.json itself
# (running the same generate -> fix $ref -> normalize pipeline in-process, see
# giantswarm/devctl#2195), so this hook is READ-ONLY: it reproduces that pipeline to a
# scratch file and diffs it against the committed one, but never writes the committed
# file. A hook that regenerated files in place used to fight `schemalint-verify` and
# itself over key ordering, because pre-commit runs each hook independently against the
# file on disk and fails the run if any hook modifies it (giantswarm/giantswarm#37267);
# a read-only check has no resting format to fight over.
#
# The `$ref` step repairs a JSON Schema 2020-12 defect: `additionalProperties: false` as
# a sibling of a `$ref`. `additionalProperties` only sees sibling
# `properties`/`patternProperties` -- NOT properties pulled in through `$ref` -- so such
# a node rejects every field the referenced schema defines (e.g. `helm template` fails
# with "additional properties 'podAntiAffinity' not allowed" for a k8s Affinity). The
# correct keyword is `unevaluatedProperties`, which DOES consider $ref-evaluated
# properties. So for every object that has both `$ref` and `additionalProperties: false`
# we drop `additionalProperties` and set `unevaluatedProperties: false`.
#
# The generator itself no longer produces this. It did until v2.6.0, which shipped
# https://github.com/losisin/helm-values-schema-json/pull/378 (fixing #317): at draft
# 2019 or later, with `noAdditionalProperties: true`, it now closes applicator-bearing
# nodes with `unevaluatedProperties` instead. devctl sets `draft: 2020`, so that holds
# here. Under v2.6.0 the step is a no-op on generated output -- measured over every
# repository that DECLARES the `helmchart` pre-commit flavour. The hook reaches more
# repositories than that: `helmchart` is also the default flavour for any repository
# with a `helm/` directory, so re-measure over those too before you act on the number.
#
# It stays for schemas the generator BUNDLES rather than generates. `bundle: true`
# inlines a `# @schema $ref: <file>` target verbatim, defect included, and the tool
# cannot be expected to rewrite third-party content. One repo depends on this today:
# security-profiles-operator-app vendors the upstream chart's own `values.schema.json`,
# whose `daemon.tolerations` items close a `$ref`ed k8s Toleration with
# `additionalProperties: false` and so reject `value` and `tolerationSeconds`.
# Before removing this step, re-run that measurement -- do not assume it is dead.
#
# BOTH binaries are installed and pinned by the hook itself (`additional_dependencies`,
# hence `language: golang`), at the exact versions devctl's own go.mod requires (stamped
# in by the generator, not hardcoded here), so this reproduction runs the same versions
# devctl used to write the committed file.
#
# `helm-values-schema-json` is the module behind the `helm schema` plugin -- the very
# same binary, called directly, so nothing here depends on a helm plugin. It used to:
# the hook called a bare `helm schema`, resolved from whatever the developer had in
# their global helm plugin dir, while CI pinned a version in a different file. The old
# guard only checked that *a* plugin existed, never which version, so a wrong version
# printed "Passed" locally while rewriting the committed schema (v2.6.0 alone strips 41
# lines from hello-world-app's) and CI then rejected it.
- repo: local
hooks:
- id: helm-schema-muster-crds
name: "verify helm/muster-crds/values.schema.json is up to date"
language: golang
additional_dependencies: ['github.com/giantswarm/schemalint/v2@v2.6.3', 'github.com/losisin/helm-values-schema-json/v2@v2.6.1']
files: ^helm/muster-crds/(values\.yaml|\.schema\.yaml|values\.schema\.json|zz_generated\.app-platform\.values\.yaml)$
pass_filenames: false
require_serial: true
entry: sh -c
args:
- |
set -e
tmp=$(mktemp)
trap 'rm -f "$tmp"' EXIT
helm-values-schema-json --config helm/muster-crds/.schema.yaml -o "$tmp"
python3 -c 'import json,sys; h=lambda o: {**{k:v for k,v in o.items() if k!="additionalProperties"},"unevaluatedProperties":False} if ("$ref" in o and o.get("additionalProperties") is False) else o; p=sys.argv[1]; f=open(p,encoding="utf-8"); d=json.load(f,object_hook=h); f.close(); f=open(p,"w",encoding="utf-8"); json.dump(d,f); f.close()' "$tmp"
schemalint normalize "$tmp" -o "$tmp" --force
if ! diff -u helm/muster-crds/values.schema.json "$tmp"; then
printf '%s\n' "helm/muster-crds/values.schema.json is out of date; run from the repository root and commit the result:" 'devctl gen precommit --language='"'"'go'"'"' --repo-name='"'"'github.com/giantswarm/muster/v5'"'"' --flavors='"'"'helmchart'"'"' --k8s-schema-version='"'"'v1.33.1'"'"'' >&2
exit 1
fi
- repo: https://github.com/giantswarm/schemalint
rev: v2.6.3
hooks:
- id: schemalint-verify
pass_filenames: false
args:
- helm/muster-crds/values.schema.json
# helm-docs finds every chart below helm/, the vendored subcharts under
# helm/<chart>/charts/ included; --chart-to-generate limits the README it
# writes to the repo's own charts, so the vendored subtree stays untouched
# like it does for the base hooks above.
- repo: https://github.com/norwoodj/helm-docs
rev: "v1.14.2"
hooks:
- id: helm-docs
# The upstream script hook uses helm-docs from PATH. Build this rev
# in pre-commit's isolated Go environment so local and CI runs agree.
language: golang
entry: helm-docs
args:
- --chart-search-root=helm
- --chart-to-generate=helm/muster,helm/muster-crds
- --sort-values-order=file