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

feat: refactor uri parsing to support busybox #247

Merged
merged 11 commits into from
Feb 23, 2023
Merged
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
12 changes: 12 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ jobs:

- run: npm ci

- name: Replace common utilities by busybox counterparts
run: |
path="${PWD}/.busybox"
mkdir -p $path
ln -fs /bin/busybox $path/bash
ln -fs /bin/busybox $path/sh
ln -fs /bin/busybox $path/sed
ln -fs /bin/busybox $path/grep
ln -fs /bin/busybox $path/ls
ln -fs /bin/busybox $path/mktemp
echo "${PWD}/.path" >> $GITHUB_PATH

- run: npm run test
- run: npm run test:e2e

Expand Down
71 changes: 43 additions & 28 deletions helm-git-plugin.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ git_checkout() {
git config core.sparseCheckout true
[ -n "$_git_path" ] && echo "$_git_path/*" >.git/info/sparse-checkout
git pull --quiet --depth 1 origin "$_git_ref" >&2 || \
error "Unable to sparse-checkout. Check your Git ref ($git_ref) and path ($git_path)."
error "Unable to sparse-checkout. Check your Git ref ($_git_ref) and path ($_git_path)."
else
git fetch --quiet --tags origin >&2 || \
error "Unable to fetch remote. Check your Git url."
Expand All @@ -121,7 +121,7 @@ git_checkout() {
fi
# shellcheck disable=SC2010,SC2012
if [ "$(ls -A | grep -v '^.git$' -c)" = "0" ]; then
error "No files have been checked out. Check your Git ref ($git_ref) and path ($git_path)."
error "No files have been checked out. Check your Git ref ($_git_ref) and path ($_git_path)."
fi
}

Expand Down Expand Up @@ -220,7 +220,6 @@ main() {
_raw_uri=$4 # eg: git+https://git.com/user/repo@path/to/charts/index.yaml?ref=master



# If defined, use $HELM_GIT_HELM_BIN as $HELM_BIN.
if [ -n "${HELM_GIT_HELM_BIN:-}" ]
then
Expand All @@ -237,47 +236,63 @@ main() {
fi

# Parse URI

string_starts "$_raw_uri" "$url_prefix" ||
error "Invalid format, got '$_raw_uri'. $error_invalid_prefix"

_raw_uri=$(echo "$_raw_uri" | sed 's/^git+//')

git_proto=$(echo "$_raw_uri" | cut -d':' -f1)
readonly git_proto="$git_proto"
string_contains "$allowed_protocols" "$git_proto" ||
# Thanks goes to https://stackoverflow.com/a/63993578
readonly URI_REGEX='^([^:/?#]+):(//((([^/?#]+)@)?([^/?#]+)?))?(/([^?#]*))(\?([^#]*))?(#(.*))?$'

_uri_scheme=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\1'p")
readonly _uri_scheme
_uri_authority=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\3'p")
readonly _uri_scheme
_uri_path=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\8'p")
readonly _uri_scheme
_uri_query=$(echo "$_raw_uri" | sed -Ene "s'$URI_REGEX'\9'p")
readonly _uri_scheme

git_scheme=$(echo "$_uri_scheme" | sed -e 's/^git+//')
readonly git_scheme="$git_scheme"
string_contains "$allowed_protocols" "$git_scheme" ||
error "$error_invalid_protocol"

git_repo=$(echo "$_raw_uri" | sed -E 's#^([^/]+//[^/]+[^@\?]+)@?[^@\?]+\??.*$#\1#')
readonly git_repo="$git_repo"
# TODO: Validate git_repo
git_repo_path=$(echo "${_uri_path}" | cut -d'@' -f 1)
readonly git_repo_path

git_file_path=$(echo "${_uri_path}" | cut -d'@' -f 2)
readonly git_file_path

helm_dir=$(dirname "${git_file_path}" | sed -r '/^[\.|/]$/d')
aslafy-z marked this conversation as resolved.
Show resolved Hide resolved
readonly helm_dir

git_path=$(echo "$_raw_uri" | sed -E 's#.*@(([^\?]*)\/)?([^\?]*).*(\?.*)?#\1#' | sed -E 's/\/$//')
readonly git_path="$git_path"
# TODO: Validate git_path
helm_file=$(basename "${git_file_path}")
readonly helm_file

helm_file=$(echo "$_raw_uri" | sed -E 's#.*@(([^\?]*)\/)?([^\?]*).*(\?.*)?#\3#')
readonly helm_file="$helm_file"
git_repo="${git_scheme}://${_uri_authority}/${git_repo_path}"
readonly git_repo

git_ref=$(echo "$_raw_uri" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/')
git_ref=$(echo "$_uri_query" | sed '/^.*ref=\([^&#]*\).*$/!d;s//\1/')
# TODO: Validate git_ref
if [ -z "$git_ref" ]; then
warning "git_ref is empty, defaulted to 'master'. Prefer to pin GIT ref in URI."
git_ref="master"
fi
readonly git_ref="$git_ref"
readonly git_ref

git_sparse=$(echo "$_raw_uri" | sed '/^.*sparse=\([^&#]*\).*$/!d;s//\1/')
git_sparse=$(echo "$_uri_query" | sed '/^.*sparse=\([^&#]*\).*$/!d;s//\1/')
[ -z "$git_sparse" ] && git_sparse=1
readonly git_sparse

helm_depupdate=$(echo "$_raw_uri" | sed '/^.*depupdate=\([^&#]*\).*$/!d;s//\1/')
helm_depupdate=$(echo "$_uri_query" | sed '/^.*depupdate=\([^&#]*\).*$/!d;s//\1/')
[ -z "$helm_depupdate" ] && helm_depupdate=1
readonly helm_depupdate

helm_package=$(echo "$_raw_uri" | sed '/^.*package=\([^&#]*\).*$/!d;s//\1/')
helm_package=$(echo "$_uri_query" | sed '/^.*package=\([^&#]*\).*$/!d;s//\1/')
[ -z "$helm_package" ] && helm_package=1
readonly helm_package

debug "repo: $git_repo ref: $git_ref path: $git_path file: $helm_file sparse: $git_sparse depupdate: $helm_depupdate package: $helm_package"
readonly helm_repo_uri="git+$git_repo@$git_path?ref=$git_ref&sparse=$git_sparse&depupdate=$helm_depupdate&package=$helm_package"
debug "repo: ${git_repo} ref: ${git_ref} path: ${helm_dir} file: ${helm_file} sparse: ${git_sparse} depupdate: ${helm_depupdate} package: ${helm_package}"
readonly helm_repo_uri="git+${git_repo}@${helm_dir}?ref=${git_ref}&sparse=${git_sparse}&depupdate=${helm_depupdate}&package=${helm_package}"
debug "helm_repo_uri: $helm_repo_uri"

if ${CACHE_CHARTS}; then
Expand Down Expand Up @@ -306,13 +321,13 @@ main() {

git_root_path="$(mktemp -d "$TMPDIR/helm-git.XXXXXX")"
readonly git_root_path="$git_root_path"
git_sub_path=$(path_join "$git_root_path" "$git_path")
git_sub_path=$(path_join "$git_root_path" "$helm_dir")
readonly git_sub_path="$git_sub_path"
git_checkout "$git_sparse" "$git_root_path" "$git_repo" "$git_ref" "$git_path" ||
git_checkout "$git_sparse" "$git_root_path" "$git_repo" "$git_ref" "$helm_dir" ||
error "Error while git_sparse_checkout"

if [ -f "$git_path/$helm_file" ]; then
cat "$git_path/$helm_file"
if [ -f "$helm_dir/$helm_file" ]; then
cat "$helm_dir/$helm_file"
return
fi

Expand Down
5 changes: 5 additions & 0 deletions tests/04-uri-validation.bats
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ load 'test-helper'
[ $status = 0 ]
}

@test "should success with leading forward slash in ref" {
_run_helm_git "git+https://github.com/jaroslaw-osmanski/helm-git-test@/test-chart/index.yaml?ref=feature/feature-test"
[ $status = 0 ]
}

@test "should success with empty git_path without slash" {
_run_helm_git "git+https://github.com/hashicorp/vault-helm@index.yaml?ref=v0.5.0"
[ $status = 0 ]
Expand Down