|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Gets migration information for a GitHub Enterprise Importer (GEI) migration using GraphQL API |
| 4 | + |
| 5 | +# Usage function |
| 6 | +usage() { |
| 7 | + echo "Usage: $0 <migration-id>" |
| 8 | + echo "" |
| 9 | + echo "Get migration information for a given migration ID" |
| 10 | + echo "" |
| 11 | + echo "Examples:" |
| 12 | + echo " ./get-migration-info.sh RM_kgDaACQzNWUwMWIxNS0yZmRjLTRjYWQtOTUwNy00YTgwNGNhZThiMTk" |
| 13 | + echo "" |
| 14 | + echo "Notes:" |
| 15 | + echo " - Migration ID is the GraphQL node ID (not the REST API migration ID)" |
| 16 | + echo " - Requires using a classic Personal Access Token (ghp_*) with appropriate scopes" |
| 17 | + exit 1 |
| 18 | +} |
| 19 | + |
| 20 | +# Check if migration ID is provided |
| 21 | +if [ -z "$1" ]; then |
| 22 | + usage |
| 23 | +fi |
| 24 | + |
| 25 | +migration_id="$1" |
| 26 | + |
| 27 | +# Define color codes |
| 28 | +RED='\033[0;31m' |
| 29 | +GREEN='\033[0;32m' |
| 30 | +YELLOW='\033[0;33m' |
| 31 | +BLUE='\033[0;34m' |
| 32 | +NC='\033[0m' # No Color |
| 33 | + |
| 34 | +echo "π Fetching migration information for ID: $migration_id" |
| 35 | +echo "" |
| 36 | + |
| 37 | +# Execute the GraphQL query |
| 38 | +migration_info=$(gh api graphql -f id="$migration_id" -f query=' |
| 39 | +query ($id: ID!) { |
| 40 | + node(id: $id) { |
| 41 | + ... on Migration { |
| 42 | + id |
| 43 | + sourceUrl |
| 44 | + migrationSource { |
| 45 | + name |
| 46 | + } |
| 47 | + state |
| 48 | + failureReason |
| 49 | + } |
| 50 | + } |
| 51 | +}') |
| 52 | + |
| 53 | +# Check if the query was successful |
| 54 | +if [ $? -ne 0 ]; then |
| 55 | + echo -e "${RED}β Failed to fetch migration information${NC}" |
| 56 | + echo "This could be due to:" |
| 57 | + echo " - Invalid migration ID" |
| 58 | + echo " - Insufficient permissions (set a class token with \`export GH_TOKEN=ghp_your_token\`)" |
| 59 | + exit 1 |
| 60 | +fi |
| 61 | + |
| 62 | +# Check if migration data is null |
| 63 | +if echo "$migration_info" | jq -e '.data.node == null' >/dev/null 2>&1; then |
| 64 | + echo -e "${RED}β Migration not found${NC}" |
| 65 | + echo "Migration ID '$migration_id' does not exist or you don't have access to it." |
| 66 | + exit 1 |
| 67 | +fi |
| 68 | + |
| 69 | +# Extract migration details |
| 70 | +id=$(echo "$migration_info" | jq -r '.data.node.id // "N/A"') |
| 71 | +source_url=$(echo "$migration_info" | jq -r '.data.node.sourceUrl // "N/A"') |
| 72 | +migration_source=$(echo "$migration_info" | jq -r '.data.node.migrationSource.name // "N/A"') |
| 73 | +state=$(echo "$migration_info" | jq -r '.data.node.state // "N/A"') |
| 74 | +failure_reason=$(echo "$migration_info" | jq -r '.data.node.failureReason // "N/A"') |
| 75 | + |
| 76 | +# Display the results with formatting |
| 77 | +echo "π Migration Information" |
| 78 | +echo "=======================" |
| 79 | +echo "" |
| 80 | +echo -e "${BLUE}π Migration ID:${NC} $id" |
| 81 | +echo -e "${BLUE}π Source URL:${NC} $source_url" |
| 82 | +echo -e "${BLUE}π Migration Source:${NC} $migration_source" |
| 83 | + |
| 84 | +# Color code the state |
| 85 | +case "$state" in |
| 86 | + "SUCCEEDED" | "SUCCESS") |
| 87 | + echo -e "${BLUE}π State:${NC} ${GREEN}$state${NC}" |
| 88 | + ;; |
| 89 | + "FAILED" | "FAILURE") |
| 90 | + echo -e "${BLUE}π State:${NC} ${RED}$state${NC}" |
| 91 | + ;; |
| 92 | + "IN_PROGRESS" | "PENDING") |
| 93 | + echo -e "${BLUE}π State:${NC} ${YELLOW}$state${NC}" |
| 94 | + ;; |
| 95 | + *) |
| 96 | + echo -e "${BLUE}π State:${NC} $state" |
| 97 | + ;; |
| 98 | +esac |
| 99 | + |
| 100 | +# Only show failure reason if it exists and is not "N/A" |
| 101 | +if [ "$failure_reason" != "N/A" ] && [ "$failure_reason" != "null" ]; then |
| 102 | + echo -e "${BLUE}β Failure Reason:${NC} ${RED}$failure_reason${NC}" |
| 103 | +fi |
| 104 | + |
| 105 | +echo "" |
| 106 | +echo "β
Migration information retrieved successfully" |
0 commit comments