|
| 1 | +#!/bin/bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +# Migrate devcontainer.json from local builds to a prebuilt GHCR image. |
| 5 | +# Also attempts to make the GHCR package public. |
| 6 | + |
| 7 | +DEVCONTAINER_JSON=".devcontainer/devcontainer.json" |
| 8 | + |
| 9 | +# --- Check prerequisites --- |
| 10 | +if ! command -v gh >/dev/null 2>&1; then |
| 11 | + echo "WARNING: GitHub CLI (gh) not found. Will skip making the package public." >&2 |
| 12 | + GH_AVAILABLE=false |
| 13 | +else |
| 14 | + GH_AVAILABLE=true |
| 15 | +fi |
| 16 | + |
| 17 | +# --- Resolve GitHub owner/repo from git remote --- |
| 18 | +REMOTE_URL=$(git remote get-url origin 2>/dev/null) || { |
| 19 | + echo "ERROR: No git remote 'origin' found." >&2 |
| 20 | + exit 1 |
| 21 | +} |
| 22 | + |
| 23 | +# Handle common GitHub remote URL formats |
| 24 | +REPO=$(echo "$REMOTE_URL" | sed -E 's#(ssh://git@github\.com/|git@github\.com:|https?://github\.com/|git://github\.com/)##; s/\.git$//') |
| 25 | + |
| 26 | +if [[ -z "$REPO" || "$REPO" != */* ]]; then |
| 27 | + echo "ERROR: Could not parse owner/repo from remote URL: $REMOTE_URL" >&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | + |
| 31 | +IMAGE="ghcr.io/${REPO}/devcontainer:latest" |
| 32 | +echo "Repository: $REPO" |
| 33 | +echo "Image: $IMAGE" |
| 34 | + |
| 35 | +# --- Check current state: look for an uncommented "image": line --- |
| 36 | +if grep -q '^[[:space:]]*"image"[[:space:]]*:' "$DEVCONTAINER_JSON"; then |
| 37 | + echo "Already using a prebuilt image. Nothing to do." |
| 38 | + exit 0 |
| 39 | +fi |
| 40 | + |
| 41 | +# --- Replace the file using awk for reliable block manipulation --- |
| 42 | +awk -v image="$IMAGE" ' |
| 43 | + # Comment out uncommented "build" block (4-space indent open/close). |
| 44 | + # NOTE: assumes no nested {} within these blocks. |
| 45 | + /^ "build": \{/ { in_build=1 } |
| 46 | + in_build { |
| 47 | + sub(/^ /, " // ") |
| 48 | + if (/^ \/\/ \}/) in_build=0 |
| 49 | + print; next |
| 50 | + } |
| 51 | +
|
| 52 | + # Comment out uncommented "features" block (4-space indent open/close). |
| 53 | + # NOTE: assumes no nested {} within these blocks. |
| 54 | + /^ "features": \{/ { in_features=1 } |
| 55 | + in_features { |
| 56 | + sub(/^ /, " // ") |
| 57 | + if (/^ \/\/ \}/) in_features=0 |
| 58 | + print; next |
| 59 | + } |
| 60 | +
|
| 61 | + # Uncomment the image line and set the correct reference |
| 62 | + /^ \/\/ "image":/ { |
| 63 | + printf " \"image\": \"%s\",\n", image |
| 64 | + next |
| 65 | + } |
| 66 | +
|
| 67 | + { print } |
| 68 | +' "$DEVCONTAINER_JSON" > "${DEVCONTAINER_JSON}.tmp" && mv "${DEVCONTAINER_JSON}.tmp" "$DEVCONTAINER_JSON" |
| 69 | + |
| 70 | +echo "" |
| 71 | +echo "Updated $DEVCONTAINER_JSON to use prebuilt image." |
| 72 | + |
| 73 | +# --- Try to make the GHCR package public --- |
| 74 | +echo "" |
| 75 | +echo "Attempting to make GHCR package public..." |
| 76 | + |
| 77 | +OWNER=$(echo "$REPO" | cut -d'/' -f1) |
| 78 | +PACKAGE_NAME=$(echo "$REPO" | cut -d'/' -f2) |
| 79 | +ENCODED_PACKAGE="${PACKAGE_NAME}%2Fdevcontainer" |
| 80 | + |
| 81 | +if ! $GH_AVAILABLE; then |
| 82 | + echo "Skipping: gh CLI not available." |
| 83 | + echo "" |
| 84 | + echo "To make the package public, install gh and run:" |
| 85 | + echo " gh auth refresh -s write:packages" |
| 86 | + echo " gh api --method PATCH /user/packages/container/${ENCODED_PACKAGE} -f visibility=public" |
| 87 | + exit 0 |
| 88 | +fi |
| 89 | + |
| 90 | +# Determine if owner is an org or a user |
| 91 | +IS_ORG=false |
| 92 | +if gh api "/orgs/${OWNER}" >/dev/null 2>&1; then |
| 93 | + IS_ORG=true |
| 94 | +fi |
| 95 | + |
| 96 | +if $IS_ORG; then |
| 97 | + API_PATH="/orgs/${OWNER}/packages/container/${ENCODED_PACKAGE}" |
| 98 | + SETTINGS_URL="https://github.com/orgs/${OWNER}/packages/container/${ENCODED_PACKAGE}/settings" |
| 99 | +else |
| 100 | + API_PATH="/user/packages/container/${ENCODED_PACKAGE}" |
| 101 | + SETTINGS_URL="https://github.com/users/${OWNER}/packages/container/${ENCODED_PACKAGE}/settings" |
| 102 | +fi |
| 103 | + |
| 104 | +if API_OUTPUT=$(gh api --method PATCH "$API_PATH" -f visibility=public 2>&1); then |
| 105 | + echo "GHCR package is now public." |
| 106 | +else |
| 107 | + echo "Could not set package visibility automatically." |
| 108 | + echo "API response: $API_OUTPUT" |
| 109 | + echo "" |
| 110 | + echo "To make the package public manually, either:" |
| 111 | + echo "" |
| 112 | + echo " 1. Visit: $SETTINGS_URL" |
| 113 | + echo " -> Danger Zone -> Change visibility -> Public" |
| 114 | + echo "" |
| 115 | + echo " 2. Run:" |
| 116 | + echo " gh auth refresh -s write:packages" |
| 117 | + echo " gh api --method PATCH $API_PATH -f visibility=public" |
| 118 | +fi |
0 commit comments