From f10eb1d25971679141eef46156b7c6a6a0e2b0ab Mon Sep 17 00:00:00 2001 From: Todd Short Date: Mon, 16 Sep 2024 10:26:00 -0400 Subject: [PATCH] Add CI to check if golang version updated (#1264) Signed-off-by: Todd Short --- .github/workflows/go-verdiff.yaml | 16 +++++++ Makefile | 4 +- hack/tools/check-go-version.sh | 72 +++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/go-verdiff.yaml create mode 100755 hack/tools/check-go-version.sh diff --git a/.github/workflows/go-verdiff.yaml b/.github/workflows/go-verdiff.yaml new file mode 100644 index 000000000..4008a7cec --- /dev/null +++ b/.github/workflows/go-verdiff.yaml @@ -0,0 +1,16 @@ +name: go-verdiff +on: + pull_request: + paths: + - '**.mod' + branches: + - main +jobs: + go-verdiff: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Check golang version + run: hack/tools/check-go-version.sh "${{ github.event.pull_request.base.sha }}" diff --git a/Makefile b/Makefile index 2b07b3ab2..ffc8b7b6a 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,7 @@ SHELL := /usr/bin/env bash -o pipefail .SHELLFLAGS := -ec export ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST)))) +GOLANG_VERSION := $(shell sed -En 's/^go (.*)$$/\1/p' "go.mod") # Image URL to use all building/pushing image targets ifeq ($(origin IMAGE_REPO), undefined) IMAGE_REPO := quay.io/operator-framework/operator-controller @@ -95,7 +96,8 @@ lint: $(GOLANGCI_LINT) #HELP Run golangci linter. .PHONY: tidy tidy: #HELP Update dependencies. - $(Q)go mod tidy + # Force tidy to use the version already in go.mod + $(Q)go mod tidy -go=$(GOLANG_VERSION) .PHONY: manifests manifests: $(CONTROLLER_GEN) #EXHELP Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects. diff --git a/hack/tools/check-go-version.sh b/hack/tools/check-go-version.sh new file mode 100755 index 000000000..7de77b60d --- /dev/null +++ b/hack/tools/check-go-version.sh @@ -0,0 +1,72 @@ +#!/bin/bash + +BASE_REF=${1:-main} +GO_VER=$(sed -En 's/^go (.*)$/\1/p' "go.mod") +OLDIFS="${IFS}" +IFS='.' MAX_VER=(${GO_VER}) +IFS="${OLDIFS}" + +if [ ${#MAX_VER[*]} -ne 3 -a ${#MAX_VER[*]} -ne 2 ]; then + echo "Invalid go version: ${GO_VER}" + exit 1 +fi + +GO_MAJOR=${MAX_VER[0]} +GO_MINOR=${MAX_VER[1]} +GO_PATCH=${MAX_VER[2]} + +RETCODE=0 + +check_version () { + local whole=$1 + local file=$2 + OLDIFS="${IFS}" + IFS='.' ver=(${whole}) + IFS="${OLDIFS}" + + if [ ${ver[0]} -gt ${GO_MAJOR} ]; then + echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)" + return 1 + fi + if [ ${ver[1]} -gt ${GO_MINOR} ]; then + echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)" + return 1 + fi + + if [ ${#ver[*]} -eq 2 ] ; then + return 0 + fi + if [ ${#ver[*]} -ne 3 ] ; then + echo "${file}: ${whole}: Badly formatted golang version" + return 1 + fi + + if [ ${ver[1]} -eq ${GO_MINOR} -a ${ver[2]} -gt ${GO_PATCH} ]; then + echo "${file}: ${whole}: Bad golang version (expected ${GO_VER} or less)" + return 1 + fi + return 0 +} + +echo "Found golang version: ${GO_VER}" + +for f in $(find . -name "*.mod"); do + v=$(sed -En 's/^go (.*)$/\1/p' ${f}) + if [ -z ${v} ]; then + echo "${f}: Skipping, no version found" + continue + fi + if ! check_version ${v} ${f}; then + RETCODE=1 + fi + old=$(git grep -ohP '^go .*$' "${BASE_REF}" -- "${f}") + old=${old#go } + new=$(git grep -ohP '^go .*$' "${f}") + new=${new#go } + if [ "${new}" != "${old}" ]; then + echo "${f}: ${v}: Updated golang version from ${old}" + RETCODE=1 + fi +done + +exit ${RETCODE}