Skip to content

Instantly share code, notes, and snippets.

@granoeste
Last active Jun 15, 2020
Embed
What would you like to do?
GitHub Releases Migration Script
GITHUB_MIGRATOR_SOURCE_TOKEN={{your_access_tokens}}
GITHUB_MIGRATOR_SOURCE_HOSTNAME=github.example.com/api/v3
GITHUB_MIGRATOR_SOURCE_REPO={{source_repository}}
GITHUB_MIGRATOR_DESTINATION_TOKEN={{your_access_tokens}}
GITHUB_MIGRATOR_DESTINATION_HOSTNAME=
GITHUB_MIGRATOR_DESTINATION_REPO={{destination_repository}}
#!/bin/bash
if [ ! -e .env ]; then
echo ".env not found!"
exit 1
fi
eval "$(cat .env <(echo) <(declare -x))"
if [[ -z $GITHUB_MIGRATOR_SOURCE_TOKEN ]]; then
echo "GITHUB_MIGRATOR_SOURCE_TOKEN not found in environment variables"
exit 1
fi
if [[ -z $GITHUB_MIGRATOR_SOURCE_HOSTNAME ]]; then
GITHUB_MIGRATOR_SOURCE_HOSTNAME='api.github.com'
fi
if [[ -z $GITHUB_MIGRATOR_SOURCE_REPO ]]; then
echo "GITHUB_MIGRATOR_SOURCE_REPO not found in environment variables"
exit 1
fi
if [[ -z $GITHUB_MIGRATOR_DESTINATION_TOKEN ]]; then
echo "GITHUB_MIGRATOR_DESTINATION_TOKEN not found in environment variables"
exit 1
fi
if [[ -z $GITHUB_MIGRATOR_DESTINATION_HOSTNAME ]]; then
GITHUB_MIGRATOR_DESTINATION_HOSTNAME='api.github.com'
fi
if [[ -z $GITHUB_MIGRATOR_DESTINATION_REPO ]]; then
echo "GITHUB_MIGRATOR_DESTINATION_REPO not found in environment variables"
exit 1
fi
if [ ! -d ./tmp ]; then
mkdir ./tmp
fi
if [ ! -d ./tmp/src ]; then
mkdir ./tmp/src
fi
if [ ! -d ./tmp/src/releases ]; then
mkdir ./tmp/src/releases
fi
if [ ! -d ./tmp/dst ]; then
mkdir ./tmp/dst
fi
if [ ! -d ./tmp/dst/releases ]; then
mkdir ./tmp/dst/releases
fi
function getReleases () {
echo "- getReleases"
releases="./tmp/src/releases/releases.json"
curl -s -H "Authorization: Bearer ${GITHUB_MIGRATOR_SOURCE_TOKEN}" https://${GITHUB_MIGRATOR_SOURCE_HOSTNAME}/repos/${GITHUB_MIGRATOR_SOURCE_REPO}/releases | jq '. | sort_by(.created_at)' > $releases
}
function createRelease () {
json=`cat $1 | sed s/github.dena.jp/github.com/ | sed -e "s|${GITHUB_MIGRATOR_SOURCE_REPO}|${GITHUB_MIGRATOR_DESTINATION_REPO}|g"`
echo "- createRelease ${json}"
name=`echo $json | jq -r ".name"`
tag_name=`echo $json | jq -r ".tag_name"`
output="./tmp/dst/releases/${name}/release.json"
httpCode=`curl -s -w '%{http_code}' -o "${output}" -H "Authorization: Bearer ${GITHUB_MIGRATOR_DESTINATION_TOKEN}" https://${GITHUB_MIGRATOR_DESTINATION_HOSTNAME}/repos/${GITHUB_MIGRATOR_DESTINATION_REPO}/releases/tags/${tag_name}`
if [ $httpCode -eq "404" ]; then
echo " *Create a release"
httpCode=`curl -s -w '%{http_code}' -o "${output}" -H "Authorization: Bearer ${GITHUB_MIGRATOR_DESTINATION_TOKEN}" -H "Content-Type: application/json" -X POST -d "${json}" https://${GITHUB_MIGRATOR_DESTINATION_HOSTNAME}/repos/${GITHUB_MIGRATOR_DESTINATION_REPO}/releases`
else
release_id=$(cat ${output} | jq -r ".id")
echo " *Update a release at ${release_id}"
httpCode=`curl -s -w '%{http_code}' -o "${output}" -H "Authorization: Bearer ${GITHUB_MIGRATOR_DESTINATION_TOKEN}" -H "Content-Type: application/json" -X PATCH -d "${json}" https://${GITHUB_MIGRATOR_DESTINATION_HOSTNAME}/repos/${GITHUB_MIGRATOR_DESTINATION_REPO}/releases/${release_id}`
fi
echo " > reult: ${httpCode}"
}
function downloadAsset () {
name=$1
filename=$2
url=$3
echo "- downloadAsset ${filename} ${url}"
output="./tmp/src/releases/${name}/${filename}"
httpCode=`curl -L -w '%{http_code}' -o "${output}" -H "Authorization: Bearer ${GITHUB_MIGRATOR_SOURCE_TOKEN}" -H "Accept: application/octet-stream" ${url}`
echo " > reult: ${httpCode}"
}
function getReleaseAssets () {
release_id=$1
name=$2
echo "- getReleaseAssets ${release_id} ${name}"
output="./tmp/dst/releases/${name}/assets.json"
curl -o "${output}" -s -H "Authorization: Bearer ${GITHUB_MIGRATOR_DESTINATION_TOKEN}" https://${GITHUB_MIGRATOR_DESTINATION_HOSTNAME}/repos/${GITHUB_MIGRATOR_DESTINATION_REPO}/releases/${release_id}/assets
}
function uploadReleaseAsset () {
name=$1
filename=$2
upload_url=$3
echo "- uploadReleaseAsset ${filename} ${upload_url}"
input="./tmp/src/releases/${name}/${filename}"
output="./tmp/dst/releases/${name}/${filename}.json"
url=${upload_url//\{?name,label\}/?name=${filename}}
echo " > URL: ${url}"
httpCode=`curl -L -w '%{http_code}' -o "${output}" -H "Authorization: Bearer ${GITHUB_MIGRATOR_DESTINATION_TOKEN}" -H "Content-Type: application/zip" -X POST --data-binary @${input} ${url}`
echo " > reult: ${httpCode}"
}
function deleteReleaseAsset() {
url=$1
echo "- deleteReleaseAsset"
echo " > URL: ${url}"
httpCode=`curl -s -L -w '%{http_code}' -H "Authorization: Bearer ${GITHUB_MIGRATOR_DESTINATION_TOKEN}" -X DELETE ${url}`
echo " > reult: ${httpCode}"
}
function run () {
### getReleases
releases="./tmp/src/releases/releases.json"
getReleases
len=$(cat $releases | jq length)
for i in $( seq 0 $(($len - 1)) ); do
echo "=============================="
release=$(cat $releases | jq .[$i])
name=$(jq -r ".name" <<<"$release")
echo "# ${name}"
src_release=./tmp/src/releases/${name}/release.json
src_assets=./tmp/src/releases/${name}/assets.json
if [ ! -d ./tmp/src/releases/${name} ]; then
mkdir ./tmp/src/releases/${name}
fi
dst_release=./tmp/dst/releases/${name}/release.json
dst_assets=./tmp/dst/releases/${name}/assets.json
if [ ! -d ./tmp/dst/releases/${name} ]; then
mkdir ./tmp/dst/releases/${name}
fi
### createRelease
echo ${release} | jq -r '{name: .name, tag_name: .tag_name, draft: .draft, prerelease: .prerelease, body: .body}' > $src_release
createRelease $src_release
created_release=`cat ./tmp/dst/releases/${name}/release.json`
release_id=$(jq -r ".id" <<<"$created_release")
upload_url=$(jq -r ".upload_url" <<<"$created_release")
name=$(jq -r ".name" <<<"$created_release")
echo ${release} | jq -r '{assets: .assets}' | jq -r '.assets | map({name: .name, url: .url})' > $src_assets
len_assets=$(cat $src_assets | jq length)
if [ ${len_assets} -gt 0 ];then
getReleaseAssets ${release_id} ${name}
for i in $( seq 0 $(($len_assets - 1)) ); do
asset=$(cat $src_assets | jq .[$i])
# echo $asset | jq .
filename=$(jq -r ".name" <<<"$asset")
url=$(jq -r ".url" <<<"$asset")
### downloadAsset
downloadAsset $name $filename $url
asset_url=`cat $dst_assets | jq -r --arg FILENAME "$filename" '.[] | select(.name == $FILENAME).url'`
if [[ $asset_url ]]; then
deleteReleaseAsset $asset_url
fi
uploadReleaseAsset $name $filename $upload_url
done
fi
done
}
echo '_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/'
echo '_/ GitHub Releases Migrate _/'
echo '_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/'
echo ''
run
echo '_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment