git-sync/sync.sh

88 lines
3.2 KiB
Bash
Raw Normal View History

2020-12-09 12:36:11 +00:00
#!/bin/bash
EXIT_CODE=0
2020-12-09 13:07:27 +00:00
FAILED_MESSAGE=""
2020-12-09 12:36:11 +00:00
GITEA_TOKEN=${GITEA_TOKEN:?is not set}
GITHUB_TOKEN=${GITHUB_TOKEN:?is not set}
2020-12-09 13:43:31 +00:00
BITBUCKET_TOKEN=${BITBUCKET_TOKEN:?is not set}
2020-12-09 13:08:16 +00:00
GITLAB_TOKEN=${GITLAB_TOKEN:?is not set}
2020-12-09 12:36:11 +00:00
GITEA_BASE="https://averagemarcus:${GITEA_TOKEN}@git.cluster.fun/AverageMarcus/"
GITHUB_BASE="https://averagemarcus:${GITHUB_TOKEN}@github.com/AverageMarcus/"
2020-12-09 13:08:16 +00:00
BITBUCKET_BASE="https://bitbucket.org/AverageMarcus/"
2020-12-09 12:36:11 +00:00
GITLAB_BASE="https://gitlab.com/AverageMarcus/"
REPOS=$(curl -X GET "https://git.cluster.fun/api/v1/user/repos?page=1&limit=50&access_token=${GITEA_TOKEN}" -H "accept: application/json" --silent | jq -r '.[] | select(.private!=true) | .name')
getDefaultBranch() {
curl -X GET "https://git.cluster.fun/api/v1/repos/AverageMarcus/${1}?access_token=${GITEA_TOKEN}" -H "accept: application/json" --silent | jq -r '.default_branch'
}
githubGetRepo() {
curl -f -u averagemarcus:${GITHUB_TOKEN} "https://api.github.com/repos/averagemarcus/${1}" -H "accept: application/vnd.github.v3+json" --silent
}
githubMakeRepo() {
curl -X POST -f -u averagemarcus:${GITHUB_TOKEN} "https://api.github.com/user/repos" -H "accept: application/vnd.github.v3+json" -d '{"name": "'${1}'", "private": false, "auto_init": false, "delete_branch_on_merge": true}' --silent
}
2020-12-09 13:43:31 +00:00
bitbucketGetRepo() {
curl -f "https://api.bitbucket.org/2.0/repositories/averagemarcus/${1}"
}
bitbucketMakeRepo() {
curl -X POST -H "Content-Type: application/json" -d '{"scm": "git", "is_private": false,"project": {"key": "PROJ"}}' "https://api.bitbucket.org/2.0/repositories/averagemarcus/${0}"
}
2020-12-09 12:36:11 +00:00
2020-12-09 13:08:16 +00:00
gitlabGetRepo() {
2020-12-09 13:43:31 +00:00
curl -f --silent -u averagemarcus:${BITBUCKET_TOKEN} "https://gitlab.com/api/v4/projects/averagemarcus/${1}?access_token=${GITLAB_TOKEN}"
2020-12-09 13:08:16 +00:00
}
gitlabMakeRepo() {
2020-12-09 13:43:31 +00:00
curl -X POST -u averagemarcus:${BITBUCKET_TOKEN} "https://gitlab.com/api/v4/projects?access_token=${GITLAB_TOKEN}" -d '{"name": "'${1}'", "visibility": "public"}' --silent
2020-12-09 13:08:16 +00:00
}
2020-12-09 12:36:11 +00:00
for REPO in ${REPOS}; do
echo "Syncing ${REPO}"
rm -rf ${REPO}
mkdir -p ${REPO}
cd ${REPO}
git init
BRANCH=$(getDefaultBranch ${REPO})
git remote add gitea "${GITEA_BASE}${REPO}"
git remote add github "${GITHUB_BASE}${REPO}"
2020-12-09 13:43:31 +00:00
git remote add bitbucket "${BITBUCKET_BASE}${REPO}"
2020-12-09 13:08:16 +00:00
git remote add gitlab "${GITLAB_BASE}${REPO}"
2020-12-09 12:36:11 +00:00
failed() {
EXIT_CODE=1
printf "\n⚠ Failed to sync ${REPO}\n\n"
cd ..
2020-12-09 13:07:27 +00:00
FAILED_MESSAGE="${FAILED_MESSAGE}\n⚠ Failed to sync ${REPO}\n\n"
2020-12-09 12:36:11 +00:00
continue
}
githubGetRepo ${REPO} || githubMakeRepo ${REPO}
2020-12-09 13:08:16 +00:00
gitlabGetRepo ${REPO} || gitlabMakeRepo ${REPO}
2020-12-09 13:43:31 +00:00
bitbucketGetRepo ${REPO} || bitbucketMakeRepo ${REPO}
2020-12-09 12:36:11 +00:00
git pull --ff-only gitea ${BRANCH} || failed
git pull --ff-only github ${BRANCH} || printf "\n Unable to pull from GitHub\n\n"
2020-12-09 13:43:31 +00:00
git pull --ff-only bitbucket ${BRANCH} || printf "\n Unable to pull from BitBucket\n\n"
2020-12-09 13:08:16 +00:00
git pull --ff-only gitlab ${BRANCH} || printf "\n Unable to pull from Gitlab\n\n"
2020-12-09 12:36:11 +00:00
git push gitea ${BRANCH} || failed
git push github ${BRANCH} || failed
2020-12-09 13:43:31 +00:00
git push bitbucket ${BRANCH} || failed
2020-12-09 13:08:16 +00:00
git push gitlab ${BRANCH} || failed
2020-12-09 12:36:11 +00:00
cd ..
rm -rf ${REPO}
printf "\n✅ Successfully synced ${REPO}\n\n"
done
2020-12-09 13:07:27 +00:00
echo ${FAILED_MESSAGE}
2020-12-09 12:36:11 +00:00
exit ${EXIT_CODE}