Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multilabel #7

Open
wants to merge 3 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/commands/get-largest-tag.yml
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion src/commands/get-last-tag.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
73 changes: 47 additions & 26 deletions src/scripts/export-tag.sh
Original file line number Diff line number Diff line change
@@ -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]?\
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think here we are missing the closing parenthesis for this group?

Copy link
Member

@anairamzap-mobomo anairamzap-mobomo Feb 24, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, and also in here: I think we can replace that hardcoded "vV" with the $PREFIX env var that we are sending to this step

  - run:
      name: PR Semantic Versioning
      environment:
        SHA: $CIRCLE_SHA1
        USER: << parameters.git-username >>
        PREFIX: << parameters.tag-prefix >>
      command: <<include(scripts/export-tag.sh)>>

So we would endup with something like:

SEMVER_REGEX="\
^(${PREFIX}?)\
($NAT)\\.($NAT)\\.($NAT)$"

($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 | grep -Ei "^(major|minor|patch)$" | tr '[:upper:]' '[:lower:]' | tr "\n" " ")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here the github enpoint I think we are missing the repo owner. https://docs.github.com/en/rest/reference/repos#get-a-repository like:

https://api.github.com/repos/"$REPO_OWNER"/"$REPO_NAME"/issues/"$PR_NUMBER"/labels

We can get the repository owner/organization with the cicrcleci env var: $CIRCLE_PROJECT_USERNAME


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 {
Expand All @@ -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
Expand All @@ -49,23 +50,43 @@ 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 "$commands" ]; 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 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
if [ "$command" = "major" ]; then
new="$((major + 1)).0.0"
has_major=1
elif [ "$command" = "minor" ] && [ $has_major -eq 0 ]; then
new="${major}.$((minor + 1)).0"
has_minor=1
elif [ "$command" = "patch" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then
new="${major}.${minor}.$((patch + 1))"
elif [ "$command" = "wip" ] && [ $has_major -eq 0 ] && [ $has_minor -eq 0 ]; then
new="${major}.${minor}.$((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
Expand Down