Skip to content

Commit 983e410

Browse files
🌱 Add go-version check to avoid upgrade versions for those that we cannot support yet (#3561)
* Add go-version check to avoid upgrade versions for those that we cannot support yet * Update .github/workflows/go-verdiff.yaml Co-authored-by: Per Goncalves da Silva <[email protected]> --------- Co-authored-by: Per Goncalves da Silva <[email protected]>
1 parent c6ef935 commit 983e410

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

‎.github/workflows/go-verdiff.yaml

+17
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+
- master
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

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
21+
BASE_REF=${1:-main}
22+
GO_VER=$(sed -En 's/^go (.*)$/\1/p' "go.mod")
23+
OLDIFS="${IFS}"
24+
IFS='.' MAX_VER=(${GO_VER})
25+
IFS="${OLDIFS}"
26+
27+
if [ ${#MAX_VER[*]} -ne 3 -a ${#MAX_VER[*]} -ne 2 ]; then
28+
echo "Invalid go version: ${GO_VER}"
29+
exit 1
30+
fi
31+
32+
GO_MAJOR=${MAX_VER[0]}
33+
GO_MINOR=${MAX_VER[1]}
34+
GO_PATCH=${MAX_VER[2]}
35+
36+
RETCODE=0
37+
38+
check_version () {
39+
local whole=$1
40+
local file=$2
41+
OLDIFS="${IFS}"
42+
IFS='.' ver=(${whole})
43+
IFS="${OLDIFS}"
44+
45+
if [ ${ver[0]} -gt ${GO_MAJOR} ]; then
46+
echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)"
47+
return 1
48+
fi
49+
if [ ${ver[1]} -gt ${GO_MINOR} ]; then
50+
echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)"
51+
return 1
52+
fi
53+
54+
if [ ${#ver[*]} -eq 2 ] ; then
55+
return 0
56+
fi
57+
if [ ${#ver[*]} -ne 3 ] ; then
58+
echo "${file}: ${whole}: Badly formatted golang version"
59+
return 1
60+
fi
61+
62+
if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then
63+
echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)"
64+
return 1
65+
fi
66+
return 0
67+
}
68+
69+
echo "Found golang version: ${GO_VER}"
70+
71+
for f in $(find . -name "*.mod"); do
72+
v=$(sed -En 's/^go (.*)$/\1/p' ${f})
73+
if [ -z ${v} ]; then
74+
echo "${f}: Skipping, no version found"
75+
continue
76+
fi
77+
if ! check_version ${v} ${f}; then
78+
RETCODE=1
79+
fi
80+
old=$(git grep -ohP '^go .*$' "${BASE_REF}" -- "${f}")
81+
old=${old#go }
82+
new=$(git grep -ohP '^go .*$' "${f}")
83+
new=${new#go }
84+
# If ${old} is empty, it means this is a new .mod file
85+
if [ -z "${old}" ]; then
86+
continue
87+
fi
88+
# Check if patch version remains 0: X.x.0 <-> X.x
89+
if [ "${new}.0" == "${old}" -o "${new}" == "${old}.0" ]; then
90+
continue
91+
fi
92+
if [ "${new}" != "${old}" ]; then
93+
echo "${f}: ${v}: Updated golang version from ${old}"
94+
RETCODE=1
95+
fi
96+
done
97+
98+
exit ${RETCODE}

0 commit comments

Comments
 (0)