Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 0ec0432

Browse files
committed
introduce GitHub actions to catch bad PRs (#4)
1 parent 5dd996e commit 0ec0432

File tree

5 files changed

+162
-21
lines changed

5 files changed

+162
-21
lines changed

.github/workflows/go.yml

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# This workflow will build a golang project
2+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
3+
4+
name: Go
5+
6+
on:
7+
pull_request:
8+
branches: [main]
9+
10+
jobs:
11+
verify:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
17+
- name: Set up Go
18+
uses: actions/setup-go@v5
19+
with:
20+
go-version: '1.22.3'
21+
22+
- name: Verify Source
23+
run: hack/verify.sh
24+
25+
lint:
26+
runs-on: ubuntu-latest
27+
steps:
28+
- name: Checkout
29+
uses: actions/checkout@v4
30+
31+
- name: Set up Go
32+
uses: actions/setup-go@v5
33+
with:
34+
go-version: '1.22.3'
35+
36+
- name: Run golangci-lint
37+
uses: golangci/golangci-lint-action@v6
38+
with:
39+
version: v1.58

.github/workflows/release.yml

+2-5
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,10 @@ jobs:
2222
runs-on: ubuntu-latest
2323
steps:
2424
- name: Checkout
25-
uses: actions/checkout@v3
26-
27-
- name: Unshallow
28-
run: git fetch --prune --unshallow
25+
uses: actions/checkout@v4
2926

3027
- name: Set up Go
31-
uses: actions/setup-go@v4
28+
uses: actions/setup-go@v5
3229
with:
3330
go-version: '1.22.3'
3431

hack/verify.sh

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
cd $(dirname $0)/..
5+
6+
EXIT_CODE=0
7+
8+
errcho() {
9+
echo $@ >&2
10+
}
11+
12+
try() {
13+
local title="$1"
14+
shift
15+
16+
echo "===================================="
17+
echo "$title"
18+
echo "===================================="
19+
echo
20+
21+
startTime=$(date +%s)
22+
23+
set +e
24+
$@
25+
exitCode=$?
26+
set -e
27+
28+
elapsed=$(($(date +%s) - $startTime))
29+
30+
if [[ $exitCode -eq 0 ]]; then
31+
echo -e "\n[${elapsed}s] SUCCESS :)"
32+
else
33+
echo -e "\n[${elapsed}s] FAILED."
34+
EXIT_CODE=1
35+
fi
36+
37+
git reset --hard --quiet
38+
git clean --force
39+
40+
echo
41+
}
42+
43+
function verify_go_mod_tidy() (
44+
set -e
45+
46+
# bad formatting in go.sum is not automatically fixed by go, for some reason
47+
(set -x; rm go.sum; go mod tidy)
48+
49+
if ! git diff --exit-code; then
50+
echo "::error::Please run go mod tidy."
51+
return 1
52+
fi
53+
54+
echo "go.mod is tidy."
55+
)
56+
57+
function ensure_gimps() {
58+
location=gimps
59+
60+
if ! [ -x "$(command -v gimps)" ]; then
61+
version=0.6.0
62+
arch="$(go env GOARCH)"
63+
os="$(go env GOOS)"
64+
url="https://github.com/xrstf/gimps/releases/download/v${version}/gimps_${version}_${os}_${arch}.tar.gz"
65+
location=/tmp/gimps
66+
67+
errcho "Downloading gimps v$version..."
68+
wget -qO- "$url" | tar xzOf - "gimps_${version}_${os}_${arch}/gimps" > $location
69+
chmod +x $location
70+
fi
71+
72+
echo "$location"
73+
}
74+
75+
function verify_go_imports() (
76+
set -e
77+
78+
gimps="$(ensure_gimps)"
79+
80+
(set -x; $gimps .)
81+
82+
if ! git diff --exit-code; then
83+
echo "::error::Some import statements are not properly grouped. Please run https://github.com/xrstf/gimps or sort them manually."
84+
return 1
85+
fi
86+
87+
echo "Go import statements are properly sorted."
88+
)
89+
90+
function verify_go_build() (
91+
set -e
92+
93+
if ! (set -x; make build); then
94+
echo "::error::Code does not compile."
95+
return 1
96+
fi
97+
98+
echo "Code compiles."
99+
)
100+
101+
function verify_go_lint() (
102+
set -e
103+
104+
if ! (set -x; golangci-lint run ./...); then
105+
echo "::error::Computer says no."
106+
return 1
107+
fi
108+
109+
echo "Code looks sane."
110+
)
111+
112+
try "go.mod tidy?" verify_go_mod_tidy
113+
try "gimpsed?" verify_go_imports
114+
try "Go code builds?" verify_go_build
115+
116+
exit $EXIT_CODE

main.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -147,13 +147,13 @@ func main() {
147147
}
148148

149149
if args[0] == "-" {
150-
watchStdin(rootCtx, log, os.Stdin, printer)
150+
watchStdin(log, os.Stdin, printer)
151151
} else {
152152
watchKubernetes(rootCtx, log, args, &opt, printer)
153153
}
154154
}
155155

156-
func watchStdin(ctx context.Context, log logrus.FieldLogger, input io.Reader, printer *diff.Printer) {
156+
func watchStdin(log logrus.FieldLogger, input io.Reader, printer *diff.Printer) {
157157
decoder := yamlutil.NewYAMLOrJSONDecoder(input, 1024)
158158

159159
for {
@@ -213,10 +213,13 @@ func watchKubernetes(ctx context.Context, log logrus.FieldLogger, args []string,
213213
if err != nil {
214214
log.Fatalf("Unknown resource kind %q: %v", resourceKind, err)
215215
}
216+
217+
//nolint:staticcheck
216218
if parsed == nil {
217219
log.Fatalf("Unknown resource kind %q", resourceKind)
218220
}
219221

222+
//nolint:staticcheck
220223
gvk := parsed.GroupVersionKind
221224
kinds[gvk.String()] = gvk
222225

pkg/maputil/util.go

-14
This file was deleted.

0 commit comments

Comments
 (0)