diff options
Diffstat (limited to 'scripts/utils.sh')
-rw-r--r-- | scripts/utils.sh | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/scripts/utils.sh b/scripts/utils.sh index 800b81f1dea..13a051e2b58 100644 --- a/scripts/utils.sh +++ b/scripts/utils.sh @@ -357,3 +357,98 @@ function setup_gcloud() { gcloud auth activate-service-account --key-file="${REVIEW_APPS_GCP_CREDENTIALS}" gcloud config set project "${REVIEW_APPS_GCP_PROJECT}" } + +function download_files() { + base_url_prefix="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/repository/files" + base_url_suffix="raw?ref=${CI_COMMIT_SHA}" + + # Construct the list of files to download with curl + for file in "$@"; do + local url_encoded_filename + url_encoded_filename=$(url_encode "${file}") + local file_url="${base_url_prefix}/${url_encoded_filename}/${base_url_suffix}" + echo "url = ${file_url}" >> urls_outputs.txt + echo "output = ${file}" >> urls_outputs.txt + done + + echo "List of files to download:" + cat urls_outputs.txt + + curl -f --header "Private-Token: ${PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE}" --create-dirs --parallel --config urls_outputs.txt +} + +# Taken from https://gist.github.com/jaytaylor/5a90c49e0976aadfe0726a847ce58736 +# +# It is surprisingly hard to url-encode an URL in shell. shorter alternatives used jq, +# but we would then need to install it wherever we would use this no-clone functionality. +# +# For the purposes of url-encoding filenames, this function should be enough. +function url_encode() { + echo "$@" | sed \ + -e 's/%/%25/g' \ + -e 's/ /%20/g' \ + -e 's/!/%21/g' \ + -e 's/"/%22/g' \ + -e "s/'/%27/g" \ + -e 's/#/%23/g' \ + -e 's/(/%28/g' \ + -e 's/)/%29/g' \ + -e 's/+/%2b/g' \ + -e 's/,/%2c/g' \ + -e 's/-/%2d/g' \ + -e 's/:/%3a/g' \ + -e 's/;/%3b/g' \ + -e 's/?/%3f/g' \ + -e 's/@/%40/g' \ + -e 's/\$/%24/g' \ + -e 's/\&/%26/g' \ + -e 's/\*/%2a/g' \ + -e 's/\./%2e/g' \ + -e 's/\//%2f/g' \ + -e 's/\[/%5b/g' \ + -e 's/\\/%5c/g' \ + -e 's/\]/%5d/g' \ + -e 's/\^/%5e/g' \ + -e 's/_/%5f/g' \ + -e 's/`/%60/g' \ + -e 's/{/%7b/g' \ + -e 's/|/%7c/g' \ + -e 's/}/%7d/g' \ + -e 's/~/%7e/g' +} + +# Download the local gems in `gems` and `vendor/gems` folders from the API. +# +# This is useful if you need to run bundle install while not doing a git clone of the gitlab-org/gitlab repo. +function download_local_gems() { + for folder_path in vendor/gems gems; do + local output="${folder_path}.tar.gz" + + # From https://docs.gitlab.com/ee/api/repositories.html#get-file-archive: + # + # This endpoint can be accessed without authentication if the repository is publicly accessible. + # For GitLab.com users, this endpoint has a rate limit threshold of 5 requests per minute. + # + # We don't want to set a token for public repo (e.g. gitlab-org/gitlab), as 5 requests/minute can + # potentially be reached with many pipelines running in parallel. + local private_token_header="" + if [[ "${CI_PROJECT_VISIBILITY}" != "public" ]]; then + private_token_header="Private-Token: ${PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE}" + fi + + echo "Downloading ${folder_path}" + + url="${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/repository/archive" + curl -f \ + --create-dirs \ + --get \ + --header "${private_token_header}" \ + --output "${output}" \ + --data-urlencode "sha=${CI_COMMIT_SHA}" \ + --data-urlencode "path=${folder_path}" \ + "${url}" + + tar -zxf "${output}" --strip-component 1 + rm "${output}" + done +} |