Skip to content

Commit 033b888

Browse files
feat: enhance migration scripts with detailed error handling and usage instructions (#100)
* feat: enhance migration scripts with detailed error handling and usage instructions * fix: update error messages to specify classic PAT
1 parent 828c905 commit 033b888

File tree

3 files changed

+98
-6
lines changed

3 files changed

+98
-6
lines changed

gh-cli/get-most-recent-migration-id-for-repository.sh

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,54 @@ org="$1"
1313
repo="$2"
1414
return_only_id=${3:-false}
1515

16-
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}')
16+
# Make the API call with error handling
17+
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)
18+
exit_code=$?
19+
20+
if [ $exit_code -ne 0 ]; then
21+
if echo "$migrations" | grep -q "Authorization failed\|HTTP 403"; then
22+
echo "Error: Authorization failed (HTTP 403)" >&2
23+
echo "" >&2
24+
echo "This endpoint requires a classic Personal Access Token (PAT) instead of OAuth CLI token." >&2
25+
echo "Please export your classic PAT as GH_TOKEN:" >&2
26+
echo " export GH_TOKEN=your_classic_personal_access_token" >&2
27+
echo "" >&2
28+
echo "For more information, visit:" >&2
29+
echo "https://docs.github.com/migrations/using-ghe-migrator/exporting-migration-data-from-githubcom" >&2
30+
exit 1
31+
elif echo "$migrations" | grep -q "Not Found\|HTTP 404"; then
32+
echo "Error: Organization not found or no access to migrations (HTTP 404)" >&2
33+
echo "" >&2
34+
echo "This could mean:" >&2
35+
echo "- The organization '$org' doesn't exist" >&2
36+
echo "- You don't have access to view migrations for this organization" >&2
37+
echo "" >&2
38+
echo "Note: This endpoint requires a classic Personal Access Token (PAT)." >&2
39+
echo "Make sure you have exported your classic PAT as GH_TOKEN:" >&2
40+
echo " export GH_TOKEN=your_classic_personal_access_token" >&2
41+
exit 1
42+
else
43+
echo "Error: Failed to retrieve migrations" >&2
44+
echo "$migrations" >&2
45+
exit 1
46+
fi
47+
fi
1748

1849
if [ "$return_only_id" = "false" ]; then
1950
most_recent_migration=$(echo "$migrations" | jq -s -r --arg repo "$org/$repo" 'map(select(.repositories == $repo)) | sort_by(.created_at) | last')
2051
else
2152
most_recent_migration=$(echo "$migrations" | jq -s -r --arg repo "$org/$repo" 'map(select(.repositories == $repo)) | sort_by(.created_at) | last | .id')
2253
fi
2354

55+
# Check if we found a migration for this repository
56+
if [ "$most_recent_migration" = "null" ] || [ -z "$most_recent_migration" ]; then
57+
echo "Error: No migrations found for repository $org/$repo" >&2
58+
echo "" >&2
59+
echo "This could mean:" >&2
60+
echo "- No migrations exist for this repository" >&2
61+
echo "- The repository name is incorrect" >&2
62+
echo "- You don't have access to migrations for this repository" >&2
63+
exit 1
64+
fi
65+
2466
echo "$most_recent_migration"

gh-cli/unlock-repository-migration-by-id.sh

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
# you need to get the migration id first; ./get-most-recent-migration-id-for-repository.sh joshjohanning-org test-repo-export
55

66
# Note: Use this other script to automatically look up the migration id for the repository:
7-
# ./unlock-migrated-repository.sh joshjohanning-org test-repo-export
7+
# ./unlock-repository-migration.sh joshjohanning-org test-repo-export
88

9-
if [ -z "$2" ]; then
10-
echo "Usage: $0 <org> <migration-id>"
9+
if [ -z "$3" ]; then
10+
echo "Usage: $0 <org> <repo> <migration-id>"
1111
echo "Example: ./unlock-repository-migration-by-id.sh joshjohanning-org test-repo-export 4451412"
1212
exit 1
1313
fi
@@ -16,4 +16,41 @@ org="$1"
1616
repo="$2"
1717
id="$3"
1818

19-
gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock
19+
echo "Attempting to unlock repository $org/$repo with migration ID: $id"
20+
21+
# Capture the API response and check for errors
22+
response=$(gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock 2>&1)
23+
exit_code=$?
24+
25+
if [ $exit_code -ne 0 ]; then
26+
# Check for specific error patterns
27+
if echo "$response" | grep -q "Authorization failed\|HTTP 403"; then
28+
echo "Error: Authorization failed (HTTP 403)"
29+
echo ""
30+
echo "This endpoint requires a classic Personal Access Token (PAT) instead of OAuth CLI token."
31+
echo "Please export your classic PAT as GH_TOKEN:"
32+
echo " export GH_TOKEN=your_classic_personal_access_token"
33+
echo ""
34+
echo "For more information, visit:"
35+
echo "https://docs.github.com/migrations/using-ghe-migrator/exporting-migration-data-from-githubcom"
36+
exit 1
37+
elif echo "$response" | grep -q "Not Found\|HTTP 404"; then
38+
echo "Error: Migration or repository lock not found (HTTP 404)"
39+
echo ""
40+
echo "This could mean:"
41+
echo "- The migration ID ($id) is incorrect"
42+
echo "- The repository is not locked"
43+
echo "- You don't have access to this migration"
44+
echo ""
45+
echo "Note: This endpoint requires a classic Personal Access Token (PAT)."
46+
echo "Make sure you have exported your classic PAT as GH_TOKEN:"
47+
echo " export GH_TOKEN=your_classic_personal_access_token"
48+
exit 1
49+
else
50+
echo "Error: Failed to unlock repository"
51+
echo "$response"
52+
exit 1
53+
fi
54+
fi
55+
56+
echo "Successfully unlocked repository $org/$repo"

gh-cli/unlock-repository-migration.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ fi
1212
org="$1"
1313
repo="$2"
1414

15+
# Get the migration ID (error handling is done in the called script)
1516
id=$(./get-most-recent-migration-id-for-repository.sh $org $repo true)
17+
exit_code=$?
1618

17-
gh api -X DELETE /orgs/$org/migrations/$id/repos/$repo/lock
19+
# Check if the migration ID script failed
20+
if [ $exit_code -ne 0 ]; then
21+
# Error messages already printed by the called script
22+
exit $exit_code
23+
fi
24+
25+
# If we reach here, the migration ID was found successfully
26+
echo "Migration ID found: $id"
27+
echo "Delegating to unlock-repository-migration-by-id.sh..."
28+
29+
# Delegate to the other script with proper error handling
30+
./unlock-repository-migration-by-id.sh "$org" "$repo" "$id"

0 commit comments

Comments
 (0)