smeserver-gitutils/git-mirror-repo.sh

101 lines
2.8 KiB
Bash
Raw Normal View History

2023-05-09 05:34:39 +02:00
#!/bin/bash
#
# Migrate (mirror) a remote repository
#
# migrate-repo.sh <reponame> <organisation> [<target organisation>]
#
inifilename=$(echo ~)"/.smegit/config"
if [ -z $inifilename ] ; then
# Not here
echo "No ini file found $inifilename"
echo "git_mirror-repo.sh <reponame> <organisation> [<target organisation>]"
exit 1
fi
while read -r line || [[ -n "$line" ]]; do
if [[ $line =~ ^\[.*\]$ ]]
then
section=${line#*[}
section=${section%]*}
else
if [[ $line =~ ^[^#]*= ]]
then
key=${line%=*}
value=${line#*=}
declare "${section}_${key}=$value"
fi
fi
done < $inifilename
if [[ -z $1 ]] ; then
echo "git_migrate-repo.sh <reponame> <organisation> [<target organization>]"
echo "will migrate repo to ${local_USER} unless <target organisation> specified"
exit 0
fi
DEBUG=
if [ ${smegit_DEBUG} == true ] ; then DEBUG=true ; fi
if [ $DEBUG ] ; then echo "found ini file: $inifilename" ; fi
curlsilent="-s"
if [ $DEBUG ] ; then curlsilent= ; fi
REMOTEUser=${remote_USER}
REMOTEACCESSTOKEN=${remote_GITEAACCESSTOKEN}
LOCALACCESSTOKEN=${local_GITEAACCESSTOKEN}
REMOTEGITEAHOST=${remote_GITEAHOST}
CLONEADDRESS=$REMOTEGITEAHOST/$2/$1.git
LOCALGITEAHOST=${local_GITEAHOST}
NEWORG=${local_USER}
if [ $3 ] ; then NEWORG=$3 ; fi
# check that the NEWORG exists
RESPONSE=$(curl $curlsilent -o /dev/null -w "%{http_code}" $LOCALGITEAHOST/$NEWORG)
if [[ "$RESPONSE" != "200" ]] ; then
echo "$NEWORG does not exist on $LOCALGITEAHOST"
exit 1
fi
# And check if this repo already exists on the target
RESPONSE=$(curl $curlsilent -o /dev/null -w "%{http_code}" $LOCALGITEAHOST/$NEWORG/$1)
if [ "$RESPONSE" == "200" ] ; then
echo "Repository $NEWORG/$1 already exists on $LOCALGITEAHOST - cannot migrate"
exit 1
fi
if [ $DEBUG ] ; then echo "Mirroring $2/$1 as $NEWORG/$1 on $LOCALGITEAHOST" ; fi
RESPONSE=$(curl $curlsilent -k -X 'POST' \
"$LOCALGITEAHOST/api/v1/repos/migrate" \
-H 'accept: application/json' \
-H "Authorization: token $LOCALACCESSTOKEN" \
-H 'Content-Type: application/json' \
-d '{
"auth_token": "'"$REMOTEACCESSTOKEN"'",
"clone_addr": "'"$CLONEADDRESS"'",
"issues": true,
"labels": true,
"lfs": true,
"milestones": true,
"mirror": true,
"private": false,
"pull_requests": true,
"releases": true,
"repo_name": "'"$1"'",
"repo_owner": "'"$NEWORG"'",
"service": "git",
"uid": 0,
"wiki": true
}')
if [ $DEBUG ] ; then echo $? $RESPONSE ; fi
# And check if this repo is there
if [ $DEBUG ] ; then echo "checking that $NEWORG/$1 exists" ; fi
RESPONSE=$(curl $curlsilent -o /dev/null -w "%{http_code}" $LOCALGITEAHOST/$NEWORG/$1)
if [ "$RESPONSE" == "200" ] ; then
echo "Repository $NEWORG/$1 has been created and mirrors $2/$1"
else
echo "migrate-repo was unable to migrate $2/$1 sucessfully"
exit 1
fi