Skip to content

Commit 2e04456

Browse files
authored
PHP-95866: Private devcontainer fixes (#258)
* PHP-95866: Private devcontainer fixes 1. Surface git clone errors 2. Fix check if repo private
1 parent e229c44 commit 2e04456

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

startupscript/butane/004-git-clone-devcontainer.sh

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,14 @@ if [[ -d "${LOCAL_REPO}/.git" ]]; then
3434
fi
3535

3636
PRIVATE_DEVCONTAINER_ENABLED="$(get_metadata_value "private-devcontainer-enabled" "")"
37-
# Check if repo is private by attempting to list files
38-
if [[ "${PRIVATE_DEVCONTAINER_ENABLED}" = "TRUE" ]] && ! git ls-remote "${REPO_SRC}" &> /dev/null; then
37+
# Replace ssh URL with HTTPS URL
38+
https_url="${REPO_SRC/git@github.com:/https://github.com/}"
39+
# Create GitHub API URL
40+
api_url="https://api.github.com/repos/${https_url/https:\/\/github.com\//}"
41+
api_url="${api_url%.git}"
42+
# Check if repo is private
43+
private_status=$(curl --retry 5 -s "${api_url}" | jq -r ".status")
44+
if [[ "${PRIVATE_DEVCONTAINER_ENABLED}" == "TRUE" && "${private_status}" == 404 ]]; then
3945
# disable logs to not expose access token
4046
set +o xtrace
4147

@@ -57,11 +63,13 @@ if [[ "${PRIVATE_DEVCONTAINER_ENABLED}" = "TRUE" ]] && ! git ls-remote "${REPO_S
5763

5864
token=$(echo "${response}" | head -n1)
5965
# Insert token into url
60-
repo_auth_url=$(echo "${REPO_SRC}" | sed "s/:\/\//:\/\/${token}@/")
66+
repo_auth_url=$(echo "${https_url}" | sed "s/:\/\//:\/\/${token}@/")
6167

6268
# Clone the private repo
69+
set +o errexit
6370
response=$(git clone "${repo_auth_url}" "${LOCAL_REPO}" 2>&1)
6471
git_status=$?
72+
set -o errexit
6573
if [[ ${git_status} -ne 0 ]]; then
6674
set_metadata "startup_script/status" "ERROR"
6775
set_metadata "startup_script/message" "Failed to clone the devcontainer GitHub repo. ERROR: ${response}"
@@ -72,8 +80,10 @@ if [[ "${PRIVATE_DEVCONTAINER_ENABLED}" = "TRUE" ]] && ! git ls-remote "${REPO_S
7280
set -o xtrace
7381
else
7482
# GitHub repo is public
83+
set +o errexit
7584
response=$(git clone "${REPO_SRC}" "${LOCAL_REPO}" 2>&1)
7685
git_status=$?
86+
set -o errexit
7787
if [[ ${git_status} -ne 0 ]]; then
7888
set_metadata "startup_script/status" "ERROR"
7989
set_metadata "startup_script/message" "Failed to clone the devcontainer GitHub repo. ERROR: ${response}"

startupscript/butane/devcontainer-failure-handler.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ if [[ "${num_retries}" -ge "${RETRY_COUNT}" ]]; then
3232
# Log failure
3333
source /home/core/metadata-utils.sh
3434
set_metadata "startup_script/status" "ERROR"
35-
set_metadata "startup_script/message" "There was an error launching your custom container on the VM. Please try recreating the VM."
36-
35+
error_message=$(get_guest_attribute "startup_script/message" "")
36+
if [[ -z "${error_message}" ]]; then
37+
set_metadata "startup_script/message" "There was an error launching your custom container on the VM. Please try recreating the VM."
38+
fi
3739
# Stop the service
3840
systemctl stop devcontainer.service
3941

0 commit comments

Comments
 (0)