Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Migrate GH repo via API
### DEPENDENCIES
# - curl
# - jq
### VARIABLES
export REPO_NAME=FIXME
export ORG_NAME=FIXME
export SLEEP_TIME=15
export TOKEN=FIXME # empty scopes are sufficient
### SCRIPT
# create migration
curl -X POST -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${TOKEN}" \
https://api.github.com/orgs/${ORG_NAME}/migrations \
-d "{\"repositories\":[\"${REPO_NAME}\"]}"
# get migration id
MIGRATION_ID=$(curl -s -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${TOKEN}" \
https://api.github.com/orgs/${ORG_NAME}/migrations | jq '.[0].id')
MIGRATION_STATE=$(curl -s -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${TOKEN}" \
https://api.github.com/orgs/${ORG_NAME}/migrations | jq -r '.[0].state')
while [[ $MIGRATION_STATE != "exported" ]]; do
COUNTER=1
echo -e "Migration pending...Checking again in ${SLEEP_TIME} seconds [${COUNTER}]"
sleep ${SLEEP_TIME}
MIGRATION_STATE=$(curl -s -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${TOKEN}" \
https://api.github.com/orgs/${ORG_NAME}/migrations | jq -r '.[0].state')
done
if [[ $MIGRATION_STATE == "exported" ]]; then
# download migration archive
DOWNLOAD_URL=$(curl -s -H "Accept: application/vnd.github.v3+json" \
-H "Authorization: token ${TOKEN}" \
https://api.github.com/orgs/${ORG_NAME}/migrations/${MIGRATION_ID}/archive)
fi
curl -s -o gh-migration-${REPO_NAME}.tar.gz $DOWNLOAD_URL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment