Skip to content

Commit

Permalink
fix(review): add separate version-tag script and some improvemnts
Browse files Browse the repository at this point in the history
  • Loading branch information
Aki0x137 committed Sep 24, 2024
1 parent a3c1632 commit 34fa2b2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ update-version:
echo $$latest_tag > VERSION; \

# Version number
VERSION=$(shell ./tools/image-tag | cut -d, -f 1)
VERSION=$(shell ./tools/version-tag | cut -d, -f 1)

GIT_REVISION := $(shell git rev-parse --short HEAD)
GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD)
Expand Down
3 changes: 2 additions & 1 deletion integration/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,8 @@ func CallBuildinfo(t *testing.T, svc *e2e.HTTPService) {

version, ok := jsonResponse["version"].(string)
require.True(t, ok)
require.Regexp(t, `^v?(\d+\.)?(\d+\.)?(\d+)$`, version)
// regex reference https://semver.org/
require.Regexp(t, `^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$`, version)

defer res.Body.Close()
}
Expand Down
21 changes: 0 additions & 21 deletions tools/image-tag
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@ set -o errexit
set -o nounset
set -o pipefail

REPO_ROOT=$(git rev-parse --show-toplevel)

is_valid_semver() {
local version=$1
# regex taken from https://semver.org/
if [[ $version =~ ^v(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$ ]] ;then
return 1
else
return 0
fi
}

# Check if there is a VERSION file
if [ -f "${REPO_ROOT}/VERSION" ]; then
VERSION=$(cat "${REPO_ROOT}/VERSION" )
if is_valid_semver "$VERSION"; then
echo "$VERSION"
exit 0
fi
fi

WIP=$(git diff --quiet || echo '-WIP')
BRANCH=$(git rev-parse --abbrev-ref HEAD | sed 's#/#-#g')
# When 7 chars are not enough to be unique, git automatically uses more.
Expand Down
42 changes: 42 additions & 0 deletions tools/version-tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

REPO_ROOT=$(git rev-parse --show-toplevel)

is_valid_semver() {
local version=$1
# regex taken from https://semver.org/
if [[ $version =~ ^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-((?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*))?(?:\+([0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*))?$ ]] ;then
return 1
else
return 0
fi
}

# Check if there is a VERSION file
if [ -f "${REPO_ROOT}/VERSION" ]; then
VERSION=$(cat "${REPO_ROOT}/VERSION" )
if is_valid_semver "$VERSION"; then
echo "$VERSION"
exit 0
fi
fi

WIP=$(git diff --quiet || echo '-WIP')
BRANCH=$(git rev-parse --abbrev-ref HEAD | sed 's#/#-#g')
# When 7 chars are not enough to be unique, git automatically uses more.
# We are forcing to 7 here, as we are doing for grafana/grafana as well.
SHA=$(git rev-parse --short=7 HEAD | head -c7)

# If not a tag, use branch-hash else use tag
TAG=$((git describe --exact-match 2> /dev/null || echo "") | sed 's/v//g')
if [ -z "$TAG" ]
then
echo ${BRANCH}-${SHA}${WIP}
else
echo ${TAG}
fi

0 comments on commit 34fa2b2

Please sign in to comment.