gitpod/dev/preview/util/preview-name-from-branch.sh
ArthurSens ed0e8262f0 Introduce CLI for interactions with Preview environments.
Following our [internal RFC](https://www.notion.so/gitpod/A-Go-based-CLI-for-interacting-with-Preview-Environments-1834aa90bc104a0b836dd523e22f9e93), the work done in this CLI was to make sure it is easily extendable with new commands and that the architecture makes it easy to test.

It also introduces the first command asked in Milestone 1 of that same RFC. Which makes it possible to install the kube-context of different preview environments.

Signed-off-by: ArthurSens <arthursens2005@gmail.com>
2022-05-31 20:39:49 +05:30

31 lines
923 B
Bash
Executable File

#!/usr/bin/env bash
#
# Based on the name of the current branch this will compute the name of the associated
# preview environment.
#
# NOTE: This needs to produce the same result as the function in .werft/util/preview.ts
# See the file for implementation notes.
#
function preview-name-from-branch {
set +u
local BRANCH="$1"
if [ "$BRANCH" == "" ]; then
branch_name=$(git symbolic-ref HEAD 2>&1) || error "Cannot get current branch"
else
branch_name=$BRANCH
fi
sanitizedd_branch_name=$(echo "$branch_name" | awk '{ sub(/^refs\/heads\//, ""); $0 = tolower($0); gsub(/[^-a-z0-9]/, "-"); print }')
length=$(echo -n "$sanitizedd_branch_name" | wc -c)
if [ "$length" -gt 20 ]; then
hashed=$(echo -n "${sanitizedd_branch_name}" | sha256sum)
echo "${sanitizedd_branch_name:0:10}${hashed:0:10}"
else
echo "${sanitizedd_branch_name}"
fi
}