Skip to content

Commit 628a7fb

Browse files
feat: add script to retrieve migration status using GraphQL (#122)
1 parent 447aeb0 commit 628a7fb

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

β€Žgh-cli/README.mdβ€Ž

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -843,6 +843,36 @@ Gets the issue type of an issue. See: [Community Discussions Post](https://githu
843843

844844
Gets the usage of a label in a repository. Returns data in table format.
845845

846+
### get-migration-status.sh
847+
848+
Gets migration information using GraphQL API for a given migration ID.
849+
850+
Usage:
851+
852+
```shell
853+
./get-migration-info.sh RM_kgDaACQzNWUwMWIxNS0yZmRjLTRjYWQtOTUwNy00YTgwNGNhZThiMTk
854+
```
855+
856+
Output:
857+
858+
```text
859+
πŸ” Fetching migration information for ID: RM_kgDaACQzNWUwMWIxNS0yZmRjLTRjYWQtOTUwNy00YTgwNGNhZThiMTk
860+
861+
πŸ“Š Migration Information
862+
=======================
863+
864+
πŸ†” Migration ID: RM_kgDaACQzNWUwMWIxNS0yZmRjLTRjYWQtOTUwNy00YTgwNGNhZThiMTk
865+
🌐 Source URL: https://github.com/joshjohanning-org/export-actions-usage-report
866+
πŸ“ Migration Source: GHEC Source
867+
πŸ“Š State: SUCCEEDED
868+
❌ Failure Reason:
869+
870+
βœ… Migration information retrieved successfully
871+
```
872+
873+
> [!NOTE]
874+
> Migration ID is the GraphQL node ID (not the REST API migration ID). Requires using a classic Personal Access Token (ghp_*) with appropriate scopes set as the `GITHUB_TOKEN` environment variable.
875+
846876
### get-most-recent-migration-id-for-organization.sh
847877

848878
Returns the most recent migration ID for a given organization.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
Β (0)