Skip to content

Commit 1127bc6

Browse files
feat: add helpers to parse git ref names (related to #5)
1 parent ffbc28f commit 1127bc6

File tree

3 files changed

+131
-54
lines changed

3 files changed

+131
-54
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
### Added
1111

1212
- support for mongo 8 (`install_mongo8`)
13+
- added `get_flavor_from_git_ref` `get_version_from_git_ref` `get_custom_from_git_ref` helpers to parse git ref names (tag or branch names).
1314

1415
### Removed
1516

kash.sh

+103-54
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ begin_group() {
700700
echo "travis_fold:start:$TITLE"
701701
fi
702702
else
703-
echo "%< --- $TITLE ------"
703+
echo "${KASH_TXT_BOLD}%< --- $TITLE ------${KASK_TXT_RESET}"
704704
fi
705705
}
706706

@@ -720,7 +720,7 @@ end_group() {
720720
echo "travis_fold:end:$TITLE"
721721
fi
722722
else
723-
echo "------ $TITLE --- >%"
723+
echo "${KASH_TXT_BOLD}------ $TITLE --- >%${KASH_TXT_RESET}"
724724
fi
725725
}
726726

@@ -904,28 +904,82 @@ load_value_files() {
904904
### Kalisio
905905
###
906906

907+
# Returns the kalisio flavor based on the git ref (tag or branch name).
908+
# Known flavors are 'dev', 'test' and 'prod'.
909+
# Expected args:
910+
# 1. the git ref name
911+
get_flavor_from_git_ref() {
912+
local GIT_REF=$1
913+
914+
case "$GIT_REF" in
915+
# Will match anything beginning with 'prod-'
916+
prod-*)
917+
printf "prod"
918+
;;
919+
# Will match single 'test' or anything beginning with 'test-'
920+
test | test-*)
921+
printf "test"
922+
;;
923+
# Anything else is 'dev' flavor
924+
*)
925+
printf "dev"
926+
;;
927+
esac
928+
}
929+
930+
# Returns the kalisio version based on the git ref (tag or branch name).
931+
# Version may be MAJOR.MINOR (eg. with 'test' flavors) or MAJOR.MINOR.PATCH (eg. with 'prod' flavors)
932+
# Expected args:
933+
# 1. the git ref name
934+
get_version_from_git_ref() {
935+
local GIT_REF=$1
936+
937+
local VERSION_REGEX="-v([0-9]+\.[0-9]+(\.[0-9]+)?)"
938+
if [[ "$GIT_REF" =~ $VERSION_REGEX ]]; then
939+
printf "%s" "${BASH_REMATCH[1]}"
940+
fi
941+
}
942+
943+
# Returns the kalisio custom field based on the git ref (tag or branch name).
944+
# Expected args:
945+
# 1. the git ref name
946+
get_custom_from_git_ref() {
947+
local GIT_REF=$1
948+
949+
local CUSTOM_REGEX="(^|-)([a-zA-Z0-9]+)$"
950+
if [[ "$GIT_REF" =~ $CUSTOM_REGEX ]]; then
951+
if [[ "${BASH_REMATCH[1]}" == "" ]]; then
952+
# If first capture group is empty => that's probably a 'dev' flavor.
953+
# The branch name = the custom field (except for master and main)
954+
#
955+
case "${BASH_REMATCH[2]}" in
956+
# Also exclude 'test' as it's a valid branch name for test flavor
957+
master | main | test)
958+
;;
959+
*)
960+
printf "%s" "${BASH_REMATCH[2]}"
961+
;;
962+
esac
963+
else
964+
# Otherwise, that's probably a test or prod flavor,
965+
printf "%s" "${BASH_REMATCH[2]}"
966+
fi
967+
fi
968+
}
969+
907970
# Returns the kalisio flavor (prod, test, dev) according to current branch/tag name
908971
# Expected args:
909972
# 1. the repository root folder
910973
get_flavor_from_git() {
911974
local REPO_DIR=$1
912-
# Matches 'test' but also 'test-v1.2'
913-
local TEST_FLAVOR_REGEX="^test(-v[0-9]+\.[0-9]+)?$"
914-
# Only matches 'prod-v1.2.3'
915-
local PROD_FLAVOR_REGEX="^prod-v[0-9]+\.[0-9]+\.[0-9]+$"
916975

917976
local GIT_TAG
918977
GIT_TAG=$(get_git_tag "$REPO_DIR")
919978
local GIT_BRANCH
920979
GIT_BRANCH=$(get_git_branch "$REPO_DIR")
980+
local GIT_REF="${GIT_TAG:-$GIT_BRANCH}"
921981

922-
if [[ "$GIT_TAG" =~ $PROD_FLAVOR_REGEX ]]; then
923-
printf "prod"
924-
elif [[ "$GIT_BRANCH" =~ $TEST_FLAVOR_REGEX ]]; then
925-
printf "test"
926-
else
927-
printf "dev"
928-
fi
982+
get_flavor_from_git_ref "$GIT_REF"
929983
}
930984

931985
# Returns the git ref that produced the given container tag.
@@ -1048,26 +1102,20 @@ setup_app_workspace() {
10481102
GIT_REF="$7"
10491103
fi
10501104

1051-
if [ -z "$KLI_BASE" ]; then
1052-
KLI_BASE="$DEVELOPMENT_DIR/$APP_NAME"
1053-
else
1054-
KLI_BASE="$DEVELOPMENT_DIR/$KLI_BASE/$APP_NAME"
1055-
fi
1105+
local APP_FLAVOR
1106+
APP_FLAVOR=$(get_flavor_from_git_ref "$GIT_REF")
1107+
local APP_VERSION
1108+
APP_VERSION=$(get_version_from_git_ref "$GIT_REF")
1109+
local APP_CUSTOM
1110+
APP_CUSTOM=$(get_custom_from_git_ref "$GIT_REF")
10561111

10571112
# determine associated kli file
1058-
local KLI_FILE
1059-
local PROD_REGEX="^prod-v([0-9]+\.[0-9]+\.[0-9]+)$"
1060-
local TEST_REGEX="^test-v([0-9]+\.[0-9]+)$"
1061-
if [[ "$GIT_REF" =~ $PROD_REGEX ]]; then
1062-
KLI_FILE="$KLI_BASE/prod/$APP_NAME-${BASH_REMATCH[1]}.js"
1063-
elif [[ "$GIT_REF" =~ $TEST_REGEX ]]; then
1064-
KLI_FILE="$KLI_BASE/test/$APP_NAME-${BASH_REMATCH[1]}.js"
1065-
else
1066-
KLI_FILE="$KLI_BASE/dev/$APP_NAME-$GIT_REF.js"
1067-
if [ ! -f "$KLI_FILE" ]; then
1068-
KLI_FILE="$KLI_BASE/dev/$APP_NAME.js"
1069-
fi
1070-
fi
1113+
local KLI_FILE="$DEVELOPMENT_DIR"
1114+
[[ "$KLI_BASE" != "" ]] && KLI_FILE="$KLI_FILE/$KLI_BASE"
1115+
KLI_FILE="$KLI_FILE/$APP_NAME/$APP_FLAVOR/$APP_NAME"
1116+
[[ "$APP_VERSION" != "" ]] && KLI_FILE="$KLI_FILE-$APP_VERSION"
1117+
[[ "$APP_CUSTOM" != "" ]] && KLI_FILE="$KLI_FILE-$APP_CUSTOM"
1118+
KLI_FILE="$KLI_FILE.js"
10711119

10721120
# run kli !
10731121
if [ "$KLI_RUN" = kli ] || [ "$KLI_RUN" = klifull ]; then
@@ -1096,34 +1144,29 @@ init_app_infos() {
10961144
local APP_VERSION
10971145
APP_VERSION=$(yq --output-format=yaml '.version' "$REPO_ROOT/package.json")
10981146

1099-
KLI_BASE="$KLI_BASE/$APP_NAME"
1100-
11011147
local GIT_TAG
11021148
GIT_TAG=$(get_git_tag "$REPO_ROOT")
11031149
local GIT_BRANCH
11041150
GIT_BRANCH=$(get_git_branch "$REPO_ROOT")
1105-
11061151
local GIT_REF="${GIT_TAG:-$GIT_BRANCH}"
11071152

11081153
local APP_FLAVOR
1109-
local KLI_FILE
1110-
local PROD_REGEX="^prod-v([0-9]+\.[0-9]+\.[0-9]+)$"
1111-
local TEST_REGEX="^test-v([0-9]+\.[0-9]+)$"
1112-
if [[ "$GIT_REF" =~ $PROD_REGEX ]]; then
1113-
APP_FLAVOR="prod"
1114-
KLI_FILE="$KLI_BASE/$APP_FLAVOR/$APP_NAME-${BASH_REMATCH[1]}.js"
1115-
elif [[ "$GIT_REF" =~ $TEST_REGEX ]]; then
1116-
APP_FLAVOR="test"
1117-
KLI_FILE="$KLI_BASE/$APP_FLAVOR/$APP_NAME-${BASH_REMATCH[1]}.js"
1118-
else
1119-
APP_FLAVOR="dev"
1120-
KLI_FILE="$KLI_BASE/$APP_FLAVOR/$APP_NAME-$GIT_REF.js"
1121-
if [ ! -f "$KLI_FILE" ]; then
1122-
KLI_FILE="$KLI_BASE/$APP_FLAVOR/$APP_NAME.js"
1123-
fi
1124-
fi
1154+
APP_FLAVOR=$(get_flavor_from_git_ref "$GIT_REF")
1155+
local APP_CUSTOM
1156+
APP_CUSTOM=$(get_custom_from_git_ref "$GIT_REF")
1157+
1158+
local KLI_FILE="$KLI_BASE/$APP_NAME/$APP_FLAVOR/$APP_NAME"
1159+
case "$APP_FLAVOR" in
1160+
prod | test)
1161+
KLI_FILE="$KLI_FILE-$APP_VERSION"
1162+
;;
1163+
*)
1164+
;;
1165+
esac
1166+
[[ "$APP_CUSTOM" != "" ]] && KLI_FILE="$KLI_FILE-$APP_CUSTOM"
1167+
KLI_FILE="$KLI_FILE.js"
11251168

1126-
APP_INFOS=("$APP_NAME" "$APP_VERSION" "$APP_FLAVOR" "$GIT_TAG" "$GIT_BRANCH" "$KLI_FILE")
1169+
APP_INFOS=("$APP_NAME" "$APP_VERSION" "$APP_FLAVOR" "$APP_CUSTOM" "$GIT_TAG" "$GIT_BRANCH" "$KLI_FILE")
11271170
}
11281171

11291172
# Extract app name from app infos
@@ -1144,22 +1187,28 @@ get_app_flavor() {
11441187
echo "${APP_INFOS[2]}"
11451188
}
11461189

1190+
# Extract app flavor from app infos
1191+
# NOTE: requires a call to init_app_infos first
1192+
get_app_custom() {
1193+
echo "${APP_INFOS[3]}"
1194+
}
1195+
11471196
# Extract app tag from app infos
11481197
# NOTE: requires a call to init_app_infos first
11491198
get_app_tag() {
1150-
echo "${APP_INFOS[3]}"
1199+
echo "${APP_INFOS[4]}"
11511200
}
11521201

11531202
# Extract app branch from app infos
11541203
# NOTE: requires a call to init_app_infos first
11551204
get_app_branch() {
1156-
echo "${APP_INFOS[4]}"
1205+
echo "${APP_INFOS[5]}"
11571206
}
11581207

11591208
# Extract app kli file from app infos
11601209
# NOTE: requires a call to init_app_infos first
11611210
get_app_kli_file() {
1162-
echo "${APP_INFOS[5]}"
1211+
echo "${APP_INFOS[6]}"
11631212
}
11641213

11651214
# Run backend tests for the given app.

scripts/run_tests.sh

+27
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ git_shallow_clone https://github.com/kalisio/kApp.git "$TMP_DIR/kApp.v1.3.0" pro
9191

9292
## Kalisio helpers
9393

94+
[[ "$(get_flavor_from_git_ref "master")" != "dev" ]] && exit 1
95+
[[ "$(get_flavor_from_git_ref "main")" != "dev" ]] && exit 1
96+
[[ "$(get_flavor_from_git_ref "foo")" != "dev" ]] && exit 1
97+
[[ "$(get_flavor_from_git_ref "test")" != "test" ]] && exit 1
98+
[[ "$(get_flavor_from_git_ref "test-v4.3")" != "test" ]] && exit 1
99+
[[ "$(get_flavor_from_git_ref "test-v4.3-blabla")" != "test" ]] && exit 1
100+
[[ "$(get_flavor_from_git_ref "prod-v4.5.3")" != "prod" ]] && exit 1
101+
[[ "$(get_flavor_from_git_ref "prod-v4.5.3-custom")" != "prod" ]] && exit 1
102+
103+
[[ "$(get_version_from_git_ref "master")" != "" ]] && exit 1
104+
[[ "$(get_version_from_git_ref "main")" != "" ]] && exit 1
105+
[[ "$(get_version_from_git_ref "foo")" != "" ]] && exit 1
106+
[[ "$(get_version_from_git_ref "test")" != "" ]] && exit 1
107+
[[ "$(get_version_from_git_ref "test-v4.3")" != "4.3" ]] && exit 1
108+
[[ "$(get_version_from_git_ref "test-v4.3-blabla")" != "4.3" ]] && exit 1
109+
[[ "$(get_version_from_git_ref "prod-v4.5.3")" != "4.5.3" ]] && exit 1
110+
[[ "$(get_version_from_git_ref "prod-v4.5.3-custom")" != "4.5.3" ]] && exit 1
111+
112+
[[ "$(get_custom_from_git_ref "master")" != "" ]] && exit 1
113+
[[ "$(get_custom_from_git_ref "main")" != "" ]] && exit 1
114+
[[ "$(get_custom_from_git_ref "foo")" != "foo" ]] && exit 1
115+
[[ "$(get_custom_from_git_ref "test")" != "" ]] && exit 1
116+
[[ "$(get_custom_from_git_ref "test-v4.3")" != "" ]] && exit 1
117+
[[ "$(get_custom_from_git_ref "test-v4.3-blabla")" != "blabla" ]] && exit 1
118+
[[ "$(get_custom_from_git_ref "prod-v4.5.3")" != "" ]] && exit 1
119+
[[ "$(get_custom_from_git_ref "prod-v4.5.3-custom")" != "custom" ]] && exit 1
120+
94121
# Setup a fake workspace with additional dependencies
95122
mkdir -p "$TMP_DIR/fake"
96123
setup_workspace "$TMP_DIR/fake" "https://github.com/kalisio/kApp.git" \

0 commit comments

Comments
 (0)