Skip to content

Commit af76233

Browse files
Add go-version check to avoid introduces an version that we cannot support yet (#1657)
1 parent 61353b8 commit af76233

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

.github/workflows/go-verdiff.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: go-verdiff
2+
on:
3+
pull_request:
4+
paths:
5+
- '**.mod'
6+
- '.github/workflows/go-verdiff.yaml'
7+
branches:
8+
- main
9+
jobs:
10+
go-verdiff:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v4
14+
with:
15+
fetch-depth: 0
16+
- name: Check golang version
17+
run: hack/tools/check-go-version.sh "${{ github.event.pull_request.base.sha }}"

hack/tools/check-go-version.sh

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!/bin/bash
2+
3+
###########################################
4+
# Check Go version in go.mod files
5+
# and ensure it is not greater than the
6+
# version in the main go.mod file.
7+
# Also check if the version in the main
8+
# go.mod file is updated in the
9+
# submodules.
10+
# This script is intended to be run
11+
# as part of the CI pipeline to ensure
12+
# that the version of Go that we can use
13+
# is not accidentally upgraded.
14+
# Source: https://github.com/operator-framework/operator-controller/blob/main/hack/tools/check-go-version.sh
15+
#
16+
# PS: We have the intention to centralize
17+
# this implementation in the future.
18+
###########################################
19+
20+
BASE_REF=${1:-main}
21+
GO_VER=$(sed -En 's/^go (.*)$/\1/p' "go.mod")
22+
OLDIFS="${IFS}"
23+
IFS='.' MAX_VER=(${GO_VER})
24+
IFS="${OLDIFS}"
25+
26+
if [ ${#MAX_VER[*]} -ne 3 -a ${#MAX_VER[*]} -ne 2 ]; then
27+
echo "Invalid go version: ${GO_VER}"
28+
exit 1
29+
fi
30+
31+
GO_MAJOR=${MAX_VER[0]}
32+
GO_MINOR=${MAX_VER[1]}
33+
GO_PATCH=${MAX_VER[2]}
34+
35+
RETCODE=0
36+
37+
check_version () {
38+
local whole=$1
39+
local file=$2
40+
OLDIFS="${IFS}"
41+
IFS='.' ver=(${whole})
42+
IFS="${OLDIFS}"
43+
44+
if [ ${ver[0]} -gt ${GO_MAJOR} ]; then
45+
echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)"
46+
return 1
47+
fi
48+
if [ ${ver[1]} -gt ${GO_MINOR} ]; then
49+
echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)"
50+
return 1
51+
fi
52+
53+
if [ ${#ver[*]} -eq 2 ] ; then
54+
return 0
55+
fi
56+
if [ ${#ver[*]} -ne 3 ] ; then
57+
echo "${file}: ${whole}: Badly formatted golang version"
58+
return 1
59+
fi
60+
61+
if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then
62+
echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)"
63+
return 1
64+
fi
65+
return 0
66+
}
67+
68+
echo "Found golang version: ${GO_VER}"
69+
70+
for f in $(find . -name "*.mod"); do
71+
v=$(sed -En 's/^go (.*)$/\1/p' ${f})
72+
if [ -z ${v} ]; then
73+
echo "${f}: Skipping, no version found"
74+
continue
75+
fi
76+
if ! check_version ${v} ${f}; then
77+
RETCODE=1
78+
fi
79+
old=$(git grep -ohP '^go .*$' "${BASE_REF}" -- "${f}")
80+
old=${old#go }
81+
new=$(git grep -ohP '^go .*$' "${f}")
82+
new=${new#go }
83+
# If ${old} is empty, it means this is a new .mod file
84+
if [ -z "${old}" ]; then
85+
continue
86+
fi
87+
# Check if patch version remains 0: X.x.0 <-> X.x
88+
if [ "${new}.0" == "${old}" -o "${new}" == "${old}.0" ]; then
89+
continue
90+
fi
91+
if [ "${new}" != "${old}" ]; then
92+
echo "${f}: ${v}: Updated golang version from ${old}"
93+
RETCODE=1
94+
fi
95+
done
96+
97+
exit ${RETCODE}

0 commit comments

Comments
 (0)