From a9355ab5ee6eae544a666896c0144ca639582c9a Mon Sep 17 00:00:00 2001 From: Valeriy Svydenko Date: Wed, 22 Jan 2025 12:59:29 +0200 Subject: [PATCH] chore: use git clone instead of curl to get airgap samples Signed-off-by: Valeriy Svydenko --- build/dockerfiles/Dockerfile | 2 +- scripts/airgap.sh | 82 +++++++++++++++++------------------- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/build/dockerfiles/Dockerfile b/build/dockerfiles/Dockerfile index cd0e62387..fac1e8b24 100644 --- a/build/dockerfiles/Dockerfile +++ b/build/dockerfiles/Dockerfile @@ -11,7 +11,7 @@ FROM docker.io/node:18.19.1-alpine3.19 as builder # hadolint ignore=DL3018 -RUN apk add --no-cache python3 py3-pip make g++ jq curl +RUN apk add --no-cache python3 py3-pip make g++ jq curl git zip RUN pip3 install yq --break-system-packages COPY .yarn/releases /dashboard/.yarn/releases/ diff --git a/scripts/airgap.sh b/scripts/airgap.sh index 5310f58b3..2cf56b157 100755 --- a/scripts/airgap.sh +++ b/scripts/airgap.sh @@ -1,6 +1,6 @@ #!/bin/sh # -# Copyright (c) 2021-2024 Red Hat, Inc. +# Copyright (c) 2021-2025 Red Hat, Inc. # This program and the accompanying materials are made # available under the terms of the Eclipse Public License 2.0 # which is available at https://www.eclipse.org/legal/epl-2.0/ @@ -28,8 +28,8 @@ init() { if [ -z "${SRC_INDEX_JSON_PATH}" ]; then - usage - exit + usage + exit fi if [ -z "${OUTPUT_DIR}" ]; then @@ -65,26 +65,22 @@ run() { strippedURL="${url#https://github.com/}" organization="$(echo "${strippedURL}" | cut -d '/' -f 1)" repository="$(echo "${strippedURL}" | cut -d '/' -f 2)" - ref="$(echo "${strippedURL}" | cut -d '/' -f 4)" + branch="$(echo "${strippedURL}" | cut -d '/' -f 4)" - if [ -n "${ref}" ]; then - archiveFileName="${organization}-${repository}-${ref}.zip" - devfileFileName="${organization}-${repository}-${ref}-devfile.yaml" - projectDownloadLink="https://api.github.com/repos/${organization}/${repository}/zipball/${ref}" - devfileDownloadLink="https://api.github.com/repos/${organization}/${repository}/contents/devfile.yaml?ref=${ref}" + if [ -n "${branch}" ]; then + archiveFileName="${organization}-${repository}-${branch}.zip" + devfileFileName="${organization}-${repository}-${branch}-devfile.yaml" else archiveFileName="${organization}-${repository}.zip" devfileFileName="${organization}-${repository}-devfile.yaml" - projectDownloadLink="https://api.github.com/repos/${organization}/${repository}/zipball" - devfileDownloadLink="https://api.github.com/repos/${organization}/${repository}/contents/devfile.yaml" fi - echo "[INFO] Downloading ${url} into ${archiveFileName}" + echo "[INFO] Processing ${url}" processSample \ "${archiveFileName}" \ "${devfileFileName}" \ - "${projectDownloadLink}" \ - "${devfileDownloadLink}" \ + "${url}" \ + "${branch}" \ "${sampleId}" \ "${repository}" fi @@ -96,41 +92,41 @@ run() { processSample() { archiveFileName=$1 devfileFileName=$2 - projectDownloadLink=$3 - devfileDownloadLink=$4 + repoURL=$3 + branch=$4 sampleId=$5 repository=$6 - if [ -z ${GITHUB_TOKEN} ]; then - curl -L \ - -H "Accept: application/vnd.github.raw+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "${devfileDownloadLink}" \ - -o "${OUTPUT_DIR}/${devfileFileName}" - - curl -L \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "${projectDownloadLink}" \ - -o "${OUTPUT_DIR}/${archiveFileName}" + # Remove '/tree/' from the URL if present + cleanRepoURL=$(echo "${repoURL}" | sed -E 's/\/tree\/[^/]+$//') + + tempDir="${OUTPUT_DIR}/${repository}-clone" + + echo "[INFO] Cloning ${cleanRepoURL} branch ${branch} into ${tempDir}" + if [ -n "${branch}" ]; then + git clone --branch "${branch}" --depth 1 "${cleanRepoURL}.git" "${tempDir}" else - curl -L \ - -H "Authorization: token ${GITHUB_TOKEN}" \ - -H "Accept: application/vnd.github.raw+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "${devfileDownloadLink}" \ - -o "${OUTPUT_DIR}/${devfileFileName}" - - curl -L \ - -H "Authorization: token ${GITHUB_TOKEN}" \ - -H "Accept: application/vnd.github+json" \ - -H "X-GitHub-Api-Version: 2022-11-28" \ - "${projectDownloadLink}" \ - -o "${OUTPUT_DIR}/${archiveFileName}" + git clone --depth 1 "${cleanRepoURL}.git" "${tempDir}" fi - # CHE_DASHBOARD_INTERNAL_URL is a placeholder that will be replaced - # by the actual URL in entrypoint.sh + # Archive the repository + echo "[INFO] Zipping cloned repository into ${archiveFileName}" + # Remove the .git and .github folders + rm -rf "${tempDir}/.git" "${tempDir}/.github" + (cd "${tempDir}" && zip -rq "${OUTPUT_DIR}/${archiveFileName}" .) + + # Copy devfile.yaml if it exists + if [ -f "${tempDir}/devfile.yaml" ]; then + echo "[INFO] Found devfile.yaml. Copying to ${devfileFileName}" + cp "${tempDir}/devfile.yaml" "${OUTPUT_DIR}/${devfileFileName}" + else + echo "[WARN] devfile.yaml not found in ${tempDir}" + fi + + # Cleanup + rm -rf "${tempDir}" + + # Update the index.json devfileLink="CHE_DASHBOARD_INTERNAL_URL/dashboard/api/airgap-sample/devfile/download?id=${sampleId}" projectLink="CHE_DASHBOARD_INTERNAL_URL/dashboard/api/airgap-sample/project/download?id=${sampleId}"