From dc0aa7da336de777b639f18a5a40e290a3874678 Mon Sep 17 00:00:00 2001 From: ednark Date: Tue, 25 Jan 2022 12:58:56 -0500 Subject: [PATCH 1/3] add mulit-label behaviour --- src/commands/get-largest-tag.yml | 8 ++++ src/commands/get-last-tag.yml | 2 +- src/scripts/export-tag.sh | 78 +++++++++++++++++++++----------- 3 files changed, 61 insertions(+), 27 deletions(-) create mode 100755 src/commands/get-largest-tag.yml diff --git a/src/commands/get-largest-tag.yml b/src/commands/get-largest-tag.yml new file mode 100755 index 0000000..1ef5bd9 --- /dev/null +++ b/src/commands/get-largest-tag.yml @@ -0,0 +1,8 @@ +description: > + Exports the largest tag in the repo as an Env Var. +steps: + - checkout + - run: + command: | + LARGEST_TAG=$(git tag | grep -E ^([vV])\(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)$ | sort -r --version-sort | head -n1) + echo "export LARGEST_TAG=${LARGEST_TAG}" >> "$BASH_ENV" diff --git a/src/commands/get-last-tag.yml b/src/commands/get-last-tag.yml index 8b0a3b9..566e9b6 100755 --- a/src/commands/get-last-tag.yml +++ b/src/commands/get-last-tag.yml @@ -1,5 +1,5 @@ description: > - Exports the last tag as an Env Var. + Exports the last tag of this branch as an Env Var. steps: - checkout - run: diff --git a/src/scripts/export-tag.sh b/src/scripts/export-tag.sh index 979ce41..8314e55 100644 --- a/src/scripts/export-tag.sh +++ b/src/scripts/export-tag.sh @@ -1,25 +1,25 @@ COMMIT_SHA=$(eval echo "$SHA") echo "Commit hash: $COMMIT_SHA" + +# valid semvar for consideration must contain a "v" prefix and no postfix NAT='0|[1-9][0-9]*' SEMVER_REGEX="\ -^[vV]?\ +^([vV])\ ($NAT)\\.($NAT)\\.($NAT)$" REPO_NAME="${CIRCLE_PROJECT_REPONAME}" -PR_NUMBER=$(curl -s -X GET -u "$USER":"$GIT_USER_TOKEN" https://api.github.com/search/issues?q="$COMMIT_SHA" | jq .items[0].number) - -LABEL=$(curl -s -X GET -u "$USER":"$GIT_USER_TOKEN" https://api.github.com/repos/"$REPO_NAME"/issues/"$PR_NUMBER"/labels | jq .[0].name -r) +# choose the first/most recent PR that this commit is part of - might have issues picking between multiple prs +PR_NUMBER=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api.github.com/search/issues?q="$COMMIT_SHA" | jq .items[0].number) -if [ "$LABEL" == null ] || [ "$LABEL" == "WIP" ] -then - LABEL="patch" -fi +# select all the labels of this PR +LABELS=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api.github.com/repos/"$REPO_NAME"/issues/"$PR_NUMBER"/labels | jq .[].name -r) -LAST_TAG=$(git describe --tags --abbrev=0 | sed -e "s/^$PREFIX//") +# to ensure uniqueness, find the highest semvar of any tag in the repo +LARGEST_TAG=$(git tag | grep -E $SEMVER_REGEX | sort -r --version-sort | head -n1) -echo "Last Tag: $LAST_TAG" -echo "Semver part to update: $LABEL" +echo "Largest Tag: $LARGEST_TAG" +echo "Labels: $LABELS" # Show error message. function error { @@ -33,10 +33,11 @@ function validate_version { echo "$version" if [[ "$version" =~ $SEMVER_REGEX ]]; then if [ "$#" -eq "2" ]; then - local major=${BASH_REMATCH[1]} - local minor=${BASH_REMATCH[2]} - local patch=${BASH_REMATCH[3]} - eval "$2=(\"$major\" \"$minor\" \"$patch\")" + local prefix=${BASH_REMATCH[1]} + local major=${BASH_REMATCH[2]} + local minor=${BASH_REMATCH[3]} + local patch=${BASH_REMATCH[4]} + eval "$2=(\"$prefix\" \"$major\" \"$minor\" \"$patch\")" else echo "$version" fi @@ -49,23 +50,48 @@ function validate_version { function increment { local new; local version; local command; - command=$LABEL - version=$LAST_TAG + commands=$LABELS + + # no labels count as "patch" + if [ "$commands" == null ] || [ -z "$commnands" ]; then + commands="patch" + fi + + version=$LARGEST_TAG validate_version "$version" parts + # shellcheck disable=SC2154 - local major="${parts[0]}" - local minor="${parts[1]}" - local patch="${parts[2]}" + local prefix="${parts[0]}" + local major="${parts[1]}" + local minor="${parts[2]}" + local patch="${parts[3]}" + + local has_major=0; + local has_minor=0; + local has_patch=0; + local new="$version" - case "$command" in - major) new="$((major + 1)).0.0";; - minor) new="${major}.$((minor + 1)).0";; - patch) new="${major}.${minor}.$((patch + 1))";; - esac + for command in $commands; do + lcommand=$(echo "$command" | tr '[:upper:]' '[:lower:]') + echo "Checking '$lcommand'" + if [ "$lcommand" = "major" ]; then + new="$((major + 1)).0.0" + has_major=1 + elif [ "$lcommand" = "minor" ] && [ $has_major -eq 0 ]; then + new="${major}.$((minor + 1)).0" + has_minor=1 + elif [ "$lcommand" = "patch" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then + new="${major}.${minor}.$((patch + 1))" + has_patch=1 + elif [ "$lcommand" = "wip" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then + new="${major}.${minor}.$((patch + 1))" + has_patch=1 + fi + done echo "$new" - echo "export NEW_SEMVER_TAG=${PREFIX}${new}" >> "$BASH_ENV" + echo "export NEW_SEMVER_TAG=${prefix}${new}" >> "$BASH_ENV" if [ -z "$NEW_SEMVER_TAG" ] then From 9f19751d6379274e1d313eeea886dbdd287dbf76 Mon Sep 17 00:00:00 2001 From: ednark Date: Tue, 25 Jan 2022 15:57:48 -0500 Subject: [PATCH 2/3] shellcheck fixes --- src/scripts/export-tag.sh | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/scripts/export-tag.sh b/src/scripts/export-tag.sh index 8314e55..411b728 100644 --- a/src/scripts/export-tag.sh +++ b/src/scripts/export-tag.sh @@ -16,7 +16,7 @@ PR_NUMBER=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api LABELS=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api.github.com/repos/"$REPO_NAME"/issues/"$PR_NUMBER"/labels | jq .[].name -r) # to ensure uniqueness, find the highest semvar of any tag in the repo -LARGEST_TAG=$(git tag | grep -E $SEMVER_REGEX | sort -r --version-sort | head -n1) +LARGEST_TAG=$(git tag | grep -E "$SEMVER_REGEX" | sort -r --version-sort | head -n1) echo "Largest Tag: $LARGEST_TAG" echo "Labels: $LABELS" @@ -53,7 +53,7 @@ function increment { commands=$LABELS # no labels count as "patch" - if [ "$commands" == null ] || [ -z "$commnands" ]; then + if [ "$commands" == null ] || [ -z "$commands" ]; then commands="patch" fi @@ -69,7 +69,6 @@ function increment { local has_major=0; local has_minor=0; - local has_patch=0; local new="$version" for command in $commands; do @@ -83,10 +82,8 @@ function increment { has_minor=1 elif [ "$lcommand" = "patch" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then new="${major}.${minor}.$((patch + 1))" - has_patch=1 elif [ "$lcommand" = "wip" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then new="${major}.${minor}.$((patch + 1))" - has_patch=1 fi done From d5c9b676cf303d4ceb342d05f56aedd998192dc0 Mon Sep 17 00:00:00 2001 From: ednark Date: Wed, 26 Jan 2022 16:11:36 -0500 Subject: [PATCH 3/3] restrict labels and lowercase everything from the get go --- src/scripts/export-tag.sh | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/src/scripts/export-tag.sh b/src/scripts/export-tag.sh index 411b728..7a9c974 100644 --- a/src/scripts/export-tag.sh +++ b/src/scripts/export-tag.sh @@ -4,7 +4,7 @@ echo "Commit hash: $COMMIT_SHA" # valid semvar for consideration must contain a "v" prefix and no postfix NAT='0|[1-9][0-9]*' SEMVER_REGEX="\ -^([vV])\ +^([vV]?\ ($NAT)\\.($NAT)\\.($NAT)$" REPO_NAME="${CIRCLE_PROJECT_REPONAME}" @@ -13,7 +13,7 @@ REPO_NAME="${CIRCLE_PROJECT_REPONAME}" PR_NUMBER=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api.github.com/search/issues?q="$COMMIT_SHA" | jq .items[0].number) # select all the labels of this PR -LABELS=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api.github.com/repos/"$REPO_NAME"/issues/"$PR_NUMBER"/labels | jq .[].name -r) +LABELS=$(curl -s -X GET -H "Authorization: token $GIT_USER_TOKEN" https://api.github.com/repos/"$REPO_NAME"/issues/"$PR_NUMBER"/labels | jq .[].name -r | grep -Ei "^(major|minor|patch)$" | tr '[:upper:]' '[:lower:]' | tr "\n" " ") # to ensure uniqueness, find the highest semvar of any tag in the repo LARGEST_TAG=$(git tag | grep -E "$SEMVER_REGEX" | sort -r --version-sort | head -n1) @@ -72,17 +72,15 @@ function increment { local new="$version" for command in $commands; do - lcommand=$(echo "$command" | tr '[:upper:]' '[:lower:]') - echo "Checking '$lcommand'" - if [ "$lcommand" = "major" ]; then + if [ "$command" = "major" ]; then new="$((major + 1)).0.0" has_major=1 - elif [ "$lcommand" = "minor" ] && [ $has_major -eq 0 ]; then + elif [ "$command" = "minor" ] && [ $has_major -eq 0 ]; then new="${major}.$((minor + 1)).0" has_minor=1 - elif [ "$lcommand" = "patch" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then + elif [ "$command" = "patch" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then new="${major}.${minor}.$((patch + 1))" - elif [ "$lcommand" = "wip" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then + elif [ "$command" = "wip" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then new="${major}.${minor}.$((patch + 1))" fi done