Skip to content

Commit c5e55ac

Browse files
committed
Added a make check-generate target to the makefile
1 parent 95884c4 commit c5e55ac

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

.ci/check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ echo "Running golangci-lint..."
4949
# golangci-lint can't be run from outside the directory
5050
(cd ${SOURCE_PATH} && golangci-lint run -c .golangci.yaml --timeout 10m)
5151

52+
echo "Running check-generate..."
53+
make check-generate
54+
5255
# Run Static Application Security Testing (SAST) using gosec
56+
echo "Running SAST..."
5357
make sast-report

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ generate: $(VGOPATH) $(DEEPCOPY_GEN) $(DEFAULTER_GEN) $(CONVERSION_GEN) $(OPENAP
176176
@./hack/generate-code
177177
@./hack/api-reference/generate-spec-doc.sh
178178

179+
.PHONY: check-generate
180+
check-generate:
181+
@./hack/check-generate.sh
182+
179183
.PHONY: add-license-headers
180184
add-license-headers: $(GO_ADD_LICENSE)
181185
@./hack/add_license_headers.sh

hack/check-generate.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env bash
2+
#
3+
# SPDX-FileCopyrightText: SAP SE or an SAP affiliate company and Gardener contributors
4+
#
5+
# SPDX-License-Identifier: Apache-2.0
6+
7+
set -e
8+
9+
echo "> Generate"
10+
11+
check_branch="__check"
12+
stashed=false
13+
checked_out=false
14+
generated=false
15+
16+
function delete-check-branch {
17+
git rev-parse --verify "$check_branch" &>/dev/null && git branch -q -D "$check_branch" || :
18+
}
19+
20+
function cleanup {
21+
if [[ "$generated" == true ]]; then
22+
if ! clean_err="$(make clean && git reset --hard -q && git clean -qdf)"; then
23+
echo "Could not clean: $clean_err"
24+
fi
25+
fi
26+
27+
if [[ "$checked_out" == true ]]; then
28+
if ! checkout_err="$(git checkout -q -)"; then
29+
echo "Could not checkout to previous branch: $checkout_err"
30+
fi
31+
fi
32+
33+
if [[ "$stashed" == true ]]; then
34+
if ! stash_err="$(git stash pop -q)"; then
35+
echo "Could not pop stash: $stash_err"
36+
fi
37+
fi
38+
39+
delete-check-branch
40+
}
41+
42+
trap cleanup EXIT SIGINT SIGTERM
43+
44+
if which git &>/dev/null; then
45+
git config --global user.name 'Gardener'
46+
git config --global user.email 'gardener@cloud'
47+
if ! git rev-parse --git-dir &>/dev/null; then
48+
echo "Not a git repo, aborting"
49+
exit 1
50+
fi
51+
52+
if [[ "$(git rev-parse --abbrev-ref HEAD)" == "$check_branch" ]]; then
53+
echo "Already on check branch, aborting"
54+
exit 1
55+
fi
56+
delete-check-branch
57+
58+
if [[ "$(git status -s)" != "" ]]; then
59+
stashed=true
60+
git stash --include-untracked -q
61+
git stash apply -q &>/dev/null
62+
fi
63+
64+
checked_out=true
65+
git checkout -q -b "$check_branch"
66+
git add --all
67+
git commit -q --allow-empty -m 'checkpoint'
68+
69+
old_status="$(git status -s)"
70+
71+
if ! out=$(rm -rf hack/tools/bin/ 2>&1); then
72+
echo "Error while cleaning hack/tools/bin/: $out"
73+
exit 1
74+
fi
75+
76+
echo ">> make generate"
77+
generated=true
78+
if ! out=$(make generate 2>&1); then
79+
echo "Error during calling make generate: $out"
80+
exit 1
81+
fi
82+
new_status="$(git status -s)"
83+
84+
if [[ "$old_status" != "$new_status" ]]; then
85+
echo "make generate needs to be run:"
86+
echo "$new_status"
87+
exit 1
88+
fi
89+
90+
echo ">> make tidy"
91+
if ! out=$(make tidy 2>&1); then
92+
echo "Error during calling make tidy: $out"
93+
exit 1
94+
fi
95+
new_status="$(git status -s)"
96+
97+
if [[ "$old_status" != "$new_status" ]]; then
98+
echo "make tidy needs to be run:"
99+
echo "$new_status"
100+
exit 1
101+
fi
102+
else
103+
echo "No git detected, cannot run make check-generate"
104+
fi
105+
exit 0

0 commit comments

Comments
 (0)