From 91080890a0ee66ceac715651faab2b2978aaf01c Mon Sep 17 00:00:00 2001 From: Josh Johanning Date: Wed, 30 Jul 2025 12:07:37 -0500 Subject: [PATCH 1/2] feat: enhance migration scripts with detailed error handling and usage instructions --- ...most-recent-migration-id-for-repository.sh | 44 +++++++++++++++++- gh-cli/unlock-repository-migration-by-id.sh | 45 +++++++++++++++++-- gh-cli/unlock-repository-migration.sh | 15 ++++++- 3 files changed, 98 insertions(+), 6 deletions(-) diff --git a/gh-cli/get-most-recent-migration-id-for-repository.sh b/gh-cli/get-most-recent-migration-id-for-repository.sh index b983e4d..f64503f 100755 --- a/gh-cli/get-most-recent-migration-id-for-repository.sh +++ b/gh-cli/get-most-recent-migration-id-for-repository.sh @@ -13,7 +13,38 @@ org="$1" repo="$2" return_only_id=${3:-false} -migrations=$(gh api -X GET /orgs/$org/migrations -F per_page=100 --jq '.[] | {id: .id, repositories: .repositories.[].full_name, state: .state, created_at: .created_at}') +# Make the API call with error handling +migrations=$(gh api -X GET /orgs/$org/migrations -F per_page=100 --jq '.[] | {id: .id, repositories: .repositories.[].full_name, state: .state, created_at: .created_at}' 2>&1) +exit_code=$? + +if [ $exit_code -ne 0 ]; then + if echo "$migrations" | grep -q "Authorization failed\|HTTP 403"; then + echo "Error: Authorization failed (HTTP 403)" >&2 + echo "" >&2 + echo "This endpoint requires a Personal Access Token (PAT) instead of OAuth CLI token." >&2 + echo "Please export your PAT as GH_TOKEN:" >&2 + echo " export GH_TOKEN=your_personal_access_token" >&2 + echo "" >&2 + echo "For more information, visit:" >&2 + echo "https://docs.github.com/migrations/using-ghe-migrator/exporting-migration-data-from-githubcom" >&2 + exit 1 + elif echo "$migrations" | grep -q "Not Found\|HTTP 404"; then + echo "Error: Organization not found or no access to migrations (HTTP 404)" >&2 + echo "" >&2 + echo "This could mean:" >&2 + echo "- The organization '$org' doesn't exist" >&2 + echo "- You don't have access to view migrations for this organization" >&2 + echo "" >&2 + echo "Note: This endpoint requires a Personal Access Token (PAT)." >&2 + echo "Make sure you have exported your PAT as GH_TOKEN:" >&2 + echo " export GH_TOKEN=your_personal_access_token" >&2 + exit 1 + else + echo "Error: Failed to retrieve migrations" >&2 + echo "$migrations" >&2 + exit 1 + fi +fi if [ "$return_only_id" = "false" ]; then most_recent_migration=$(echo "$migrations" | jq -s -r --arg repo "$org/$repo" 'map(select(.repositories == $repo)) | sort_by(.created_at) | last') @@ -21,4 +52,15 @@ else most_recent_migration=$(echo "$migrations" | jq -s -r --arg repo "$org/$repo" 'map(select(.repositories == $repo)) | sort_by(.created_at) | last | .id') fi +# Check if we found a migration for this repository +if [ "$most_recent_migration" = "null" ] || [ -z "$most_recent_migration" ]; then + echo "Error: No migrations found for repository $org/$repo" >&2 + echo "" >&2 + echo "This could mean:" >&2 + echo "- No migrations exist for this repository" >&2 + echo "- The repository name is incorrect" >&2 + echo "- You don't have access to migrations for this repository" >&2 + exit 1 +fi + echo "$most_recent_migration" diff --git a/gh-cli/unlock-repository-migration-by-id.sh b/gh-cli/unlock-repository-migration-by-id.sh index 1f86f7e..8333a5e 100755 --- a/gh-cli/unlock-repository-migration-by-id.sh +++ b/gh-cli/unlock-repository-migration-by-id.sh @@ -4,10 +4,10 @@ # you need to get the migration id first; ./get-most-recent-migration-id-for-repository.sh joshjohanning-org test-repo-export # Note: Use this other script to automatically look up the migration id for the repository: -# ./unlock-migrated-repository.sh joshjohanning-org test-repo-export +# ./unlock-repository-migration.sh joshjohanning-org test-repo-export -if [ -z "$2" ]; then - echo "Usage: $0 " +if [ -z "$3" ]; then + echo "Usage: $0 " echo "Example: ./unlock-repository-migration-by-id.sh joshjohanning-org test-repo-export 4451412" exit 1 fi @@ -16,4 +16,41 @@ org="$1" repo="$2" id="$3" -gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock +echo "Attempting to unlock repository $org/$repo with migration ID: $id" + +# Capture the API response and check for errors +response=$(gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock 2>&1) +exit_code=$? + +if [ $exit_code -ne 0 ]; then + # Check for specific error patterns + if echo "$response" | grep -q "Authorization failed\|HTTP 403"; then + echo "Error: Authorization failed (HTTP 403)" + echo "" + echo "This endpoint requires a Personal Access Token (PAT) instead of OAuth CLI token." + echo "Please export your PAT as GH_TOKEN:" + echo " export GH_TOKEN=your_personal_access_token" + echo "" + echo "For more information, visit:" + echo "https://docs.github.com/migrations/using-ghe-migrator/exporting-migration-data-from-githubcom" + exit 1 + elif echo "$response" | grep -q "Not Found\|HTTP 404"; then + echo "Error: Migration or repository lock not found (HTTP 404)" + echo "" + echo "This could mean:" + echo "- The migration ID ($id) is incorrect" + echo "- The repository is not locked" + echo "- You don't have access to this migration" + echo "" + echo "Note: This endpoint requires a Personal Access Token (PAT)." + echo "Make sure you have exported your PAT as GH_TOKEN:" + echo " export GH_TOKEN=your_personal_access_token" + exit 1 + else + echo "Error: Failed to unlock repository" + echo "$response" + exit 1 + fi +fi + +echo "Successfully unlocked repository $org/$repo" diff --git a/gh-cli/unlock-repository-migration.sh b/gh-cli/unlock-repository-migration.sh index d841354..08532d5 100755 --- a/gh-cli/unlock-repository-migration.sh +++ b/gh-cli/unlock-repository-migration.sh @@ -12,6 +12,19 @@ fi org="$1" repo="$2" +# Get the migration ID (error handling is done in the called script) id=$(./get-most-recent-migration-id-for-repository.sh $org $repo true) +exit_code=$? -gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock +# Check if the migration ID script failed +if [ $exit_code -ne 0 ]; then + # Error messages already printed by the called script + exit $exit_code +fi + +# If we reach here, the migration ID was found successfully +echo "Migration ID found: $id" +echo "Delegating to unlock-repository-migration-by-id.sh..." + +# Delegate to the other script with proper error handling +./unlock-repository-migration-by-id.sh "$org" "$repo" "$id" From fc489d55333e4ba48b5cb5ec658576f3b7acb97f Mon Sep 17 00:00:00 2001 From: Josh Johanning Date: Wed, 30 Jul 2025 12:11:55 -0500 Subject: [PATCH 2/2] fix: update error messages to specify classic PAT --- .../get-most-recent-migration-id-for-repository.sh | 12 ++++++------ gh-cli/unlock-repository-migration-by-id.sh | 12 ++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gh-cli/get-most-recent-migration-id-for-repository.sh b/gh-cli/get-most-recent-migration-id-for-repository.sh index f64503f..df82d04 100755 --- a/gh-cli/get-most-recent-migration-id-for-repository.sh +++ b/gh-cli/get-most-recent-migration-id-for-repository.sh @@ -21,9 +21,9 @@ if [ $exit_code -ne 0 ]; then if echo "$migrations" | grep -q "Authorization failed\|HTTP 403"; then echo "Error: Authorization failed (HTTP 403)" >&2 echo "" >&2 - echo "This endpoint requires a Personal Access Token (PAT) instead of OAuth CLI token." >&2 - echo "Please export your PAT as GH_TOKEN:" >&2 - echo " export GH_TOKEN=your_personal_access_token" >&2 + echo "This endpoint requires a classic Personal Access Token (PAT) instead of OAuth CLI token." >&2 + echo "Please export your classic PAT as GH_TOKEN:" >&2 + echo " export GH_TOKEN=your_classic_personal_access_token" >&2 echo "" >&2 echo "For more information, visit:" >&2 echo "https://docs.github.com/migrations/using-ghe-migrator/exporting-migration-data-from-githubcom" >&2 @@ -35,9 +35,9 @@ if [ $exit_code -ne 0 ]; then echo "- The organization '$org' doesn't exist" >&2 echo "- You don't have access to view migrations for this organization" >&2 echo "" >&2 - echo "Note: This endpoint requires a Personal Access Token (PAT)." >&2 - echo "Make sure you have exported your PAT as GH_TOKEN:" >&2 - echo " export GH_TOKEN=your_personal_access_token" >&2 + echo "Note: This endpoint requires a classic Personal Access Token (PAT)." >&2 + echo "Make sure you have exported your classic PAT as GH_TOKEN:" >&2 + echo " export GH_TOKEN=your_classic_personal_access_token" >&2 exit 1 else echo "Error: Failed to retrieve migrations" >&2 diff --git a/gh-cli/unlock-repository-migration-by-id.sh b/gh-cli/unlock-repository-migration-by-id.sh index 8333a5e..cdc8dfa 100755 --- a/gh-cli/unlock-repository-migration-by-id.sh +++ b/gh-cli/unlock-repository-migration-by-id.sh @@ -27,9 +27,9 @@ if [ $exit_code -ne 0 ]; then if echo "$response" | grep -q "Authorization failed\|HTTP 403"; then echo "Error: Authorization failed (HTTP 403)" echo "" - echo "This endpoint requires a Personal Access Token (PAT) instead of OAuth CLI token." - echo "Please export your PAT as GH_TOKEN:" - echo " export GH_TOKEN=your_personal_access_token" + echo "This endpoint requires a classic Personal Access Token (PAT) instead of OAuth CLI token." + echo "Please export your classic PAT as GH_TOKEN:" + echo " export GH_TOKEN=your_classic_personal_access_token" echo "" echo "For more information, visit:" echo "https://docs.github.com/migrations/using-ghe-migrator/exporting-migration-data-from-githubcom" @@ -42,9 +42,9 @@ if [ $exit_code -ne 0 ]; then echo "- The repository is not locked" echo "- You don't have access to this migration" echo "" - echo "Note: This endpoint requires a Personal Access Token (PAT)." - echo "Make sure you have exported your PAT as GH_TOKEN:" - echo " export GH_TOKEN=your_personal_access_token" + echo "Note: This endpoint requires a classic Personal Access Token (PAT)." + echo "Make sure you have exported your classic PAT as GH_TOKEN:" + echo " export GH_TOKEN=your_classic_personal_access_token" exit 1 else echo "Error: Failed to unlock repository"