git-sync/sync.sh

117 lines
5.4 KiB
Bash
Raw Normal View History

2020-12-09 12:36:11 +00:00
#!/bin/bash
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}
CODEBERG_TOKEN=${CODEBERG_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 14:05:44 +00:00
BITBUCKET_BASE="https://averagemarcus:${BITBUCKET_TOKEN}@bitbucket.org/AverageMarcus/"
GITLAB_BASE="https://averagemarcus:${GITLAB_TOKEN}@gitlab.com/AverageMarcus/"
CODEBERG_BASE="https://averagemarcus:${CODEBERG_TOKEN}@codeberg.org/AverageMarcus/"
2020-12-09 12:36:11 +00:00
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() {
2020-12-13 16:39:37 +00:00
curl -f -u averagemarcus:${GITHUB_TOKEN} "https://api.github.com/repos/averagemarcus/${1}" -H "accept: application/vnd.github.v3+json" --silent 1> /dev/null
2020-12-09 12:36:11 +00:00
}
githubMakeRepo() {
2020-12-13 16:39:37 +00:00
echo "Creating github repo"
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 1> /dev/null
2020-12-09 12:36:11 +00:00
}
2020-12-09 13:43:31 +00:00
bitbucketGetRepo() {
2020-12-13 16:39:37 +00:00
curl -f -u averagemarcus:${BITBUCKET_TOKEN} "https://api.bitbucket.org/2.0/repositories/averagemarcus/${1}" --silent 1> /dev/null
2020-12-09 13:43:31 +00:00
}
bitbucketMakeRepo() {
2020-12-13 16:39:37 +00:00
echo "Creating bitbucket repo"
2021-02-05 06:01:50 +00:00
curl -X POST -u averagemarcus:${BITBUCKET_TOKEN} -H "Content-Type: application/json" -d '{"scm": "git", "is_private": false,"project": {"key": "PROJ"}}' "https://api.bitbucket.org/2.0/repositories/averagemarcus/${1}" --silent 1> /dev/null
2020-12-09 13:43:31 +00:00
}
2020-12-09 12:36:11 +00:00
2020-12-09 13:08:16 +00:00
gitlabGetRepo() {
2020-12-13 17:14:28 +00:00
curl -f "https://gitlab.com/api/v4/projects/averagemarcus%2F$(echo ${1} |tr "." "-")?private_token=${GITLAB_TOKEN}" --silent 1> /dev/null
2020-12-09 13:08:16 +00:00
}
gitlabMakeRepo() {
2020-12-13 16:39:37 +00:00
echo "Creating gitlab repo"
curl -X POST --header "Content-Type: application/json" "https://gitlab.com/api/v4/projects?private_token=${GITLAB_TOKEN}" -d '{"name": "'${1}'", "visibility": "public"}' --silent 1> /dev/null
PROJECT_ID=$(curl --header "PRIVATE-TOKEN: $GITLAB_TOKEN" "https://gitlab.com/api/v4/projects/averagemarcus%2F${1}" | jq .id -r)
curl --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/protected_branches?name=master&push_access_level=40&merge_access_level=40&allow_force_push=true"
curl --request POST --header "PRIVATE-TOKEN: ${GITLAB_TOKEN}" --header "Content-Type: application/json" "https://gitlab.com/api/v4/projects/${PROJECT_ID}/protected_branches?name=main&push_access_level=40&merge_access_level=40&allow_force_push=true"
2020-12-09 13:08:16 +00:00
}
2020-12-09 12:36:11 +00:00
codebergGetRepo() {
curl -f -X GET "https://codeberg.org/api/v1/repos/averagemarcus/${1}?access_token=${CODEBERG_TOKEN}" -H 'accept: application/json' --silent 1> /dev/null
}
codebergMakeRepo() {
echo "Creating codeberg repo"
curl -X POST "https://codeberg.org/api/v1/user/repos?access_token=${CODEBERG_TOKEN}" \
-H "Content-Type: application/json" -H 'accept: application/json' \
-d '{"auto_init": false, "private": false, "name": "'${1}'"}' --silent 1> /dev/null
}
2020-12-09 12:36:11 +00:00
for REPO in ${REPOS}; do
2021-02-05 20:59:04 +00:00
printf "\n🔄 Syncing ${REPO}\n\n"
2020-12-09 12:36:11 +00:00
rm -rf ${REPO}
2021-07-24 11:33:13 +00:00
git clone "${GITEA_BASE}${REPO}" ${REPO}
2020-12-09 12:36:11 +00:00
cd ${REPO}
BRANCH=$(getDefaultBranch ${REPO})
2020-12-13 16:54:23 +00:00
git remote add gitea "${GITEA_BASE}${REPO}" 1> /dev/null
git remote add github "${GITHUB_BASE}${REPO}" 1> /dev/null
git remote add bitbucket "${BITBUCKET_BASE}${REPO}" 1> /dev/null
2020-12-13 17:14:28 +00:00
git remote add gitlab "${GITLAB_BASE}$(echo ${REPO} |tr "." "-")" 1> /dev/null
# git remote add codeberg "${CODEBERG_BASE}${REPO}" 1> /dev/null
2020-12-09 12:36:11 +00:00
failed() {
2021-07-24 10:53:47 +00:00
printf "\n⚠ Failed to sync ${REPO} to ${1}\n\n"
2020-12-09 12:36:11 +00:00
cd ..
2020-12-09 13:07:27 +00:00
2021-07-24 10:53:47 +00:00
FAILED_MESSAGE="${FAILED_MESSAGE}\n⚠ Failed to sync ${REPO} to ${1}\n\n"
2020-12-09 12:36:11 +00:00
}
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}
# codebergGetRepo ${REPO} || codebergMakeRepo ${REPO}
2020-12-09 12:36:11 +00:00
2021-02-05 06:01:50 +00:00
git pull --ff-only gitea ${BRANCH} 1> /dev/null || { failed; continue; }
2020-12-13 16:32:29 +00:00
git pull --ff-only github ${BRANCH} 1> /dev/null || printf "\n Unable to pull from GitHub\n\n"
git pull --ff-only bitbucket ${BRANCH} 1> /dev/null || printf "\n Unable to pull from BitBucket\n\n"
git pull --ff-only gitlab ${BRANCH} 1> /dev/null || printf "\n Unable to pull from Gitlab\n\n"
# git pull --ff-only codeberg ${BRANCH} 1> /dev/null || printf "\n Unable to pull from Codeberg\n\n"
2020-12-09 12:36:11 +00:00
2021-07-24 10:53:47 +00:00
git push -f --set-upstream github ${BRANCH} 1> /dev/null || { failed "github"; }
git push -f --set-upstream bitbucket ${BRANCH} 1> /dev/null || { failed "bitbucket"; }
git push -f --set-upstream gitlab ${BRANCH} 1> /dev/null || { failed "gitlab"; }
# git push -f --set-upstream codeberg ${BRANCH} 1> /dev/null || { failed "codeberg"; }
2021-07-24 10:53:47 +00:00
git push --set-upstream gitea ${BRANCH} 1> /dev/null || { failed "gitea"; }
2020-12-09 12:36:11 +00:00
cd ..
rm -rf ${REPO}
printf "\n✅ Successfully synced ${REPO}\n\n"
done
2020-12-13 16:54:23 +00:00
if [ -n "${FAILED_MESSAGE}" ];
2020-12-13 16:21:23 +00:00
then
2020-12-13 17:14:28 +00:00
printf "\n\n--------\n\n"
2020-12-13 16:45:45 +00:00
echo "Failed!"
2020-12-13 16:59:24 +00:00
printf "${FAILED_MESSAGE}"
2020-12-13 16:21:23 +00:00
exit 1
2020-12-13 16:45:45 +00:00
else
echo "All completed successfully!"
exit 0
2020-12-13 16:21:23 +00:00
fi
2020-12-13 16:39:37 +00:00