83 lines
2.4 KiB
Bash
Executable File
83 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
|
#
|
|
# Migrate (mirror) a remote repository
|
|
#
|
|
# migrate-repo.sh <reponame> <organisation> [<traget organisation>]
|
|
#
|
|
inifilename=$(echo ~)"/.smegit/config"
|
|
if [ ! -e $inifilename ] ; then
|
|
# Not here, look at system default
|
|
if [ ! -e /etc/smegit.ini ] ; then
|
|
echo "No ini file found $inifiename or /etc/smegit.ini"
|
|
echo "git-mirror-org.sh <source organisation> [<target organisation>]"
|
|
exit 1
|
|
else
|
|
initfilename="/etc/smegit.ini"
|
|
fi
|
|
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 "migrate-org.sh <source organisation> [<target organisation>]"
|
|
echo "<target organisation> is optional and will default to the gitea user"
|
|
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
|
|
|
|
REMOTEACCESSTOKEN=${remote_GITEAACCESSTOKEN}
|
|
LOCALACCESSTOKEN=${local_GITEAACCESSTOKEN}
|
|
REMOTEGITEAHOST=${remote_GITEAHOST}
|
|
LOCALGITEAHOST=${local_GITEAHOST}
|
|
TARGETORG=
|
|
if [ $2 ] ; then TARGETORG=$2 ; fi
|
|
if [ $DEBUG ] ; then echo "TARGETORG=$TARGETORG" ; fi
|
|
|
|
# check that the target organisation exists locally
|
|
if [ $DEBUG ] ; then echo "checking that $TARGETORG exists on $LOCALGITEAHOST" ; fi
|
|
RESPONSE=$(curl $curlsilent -o /dev/null -w "%{http_code}" $LOCALGITEAHOST/$TARGETORG)
|
|
if [ $DEBUG ] ; then echo "RESPONSE=$RESPONSE" ; fi
|
|
if [[ "$RESPONSE" != "200" ]] ; then
|
|
echo "$TARGETORG does not exist on $LOCALGITEAHOST"
|
|
exit 1
|
|
fi
|
|
|
|
# get a list of repositories in the source organisation
|
|
for page in {1..10} ; do
|
|
if [ $DEBUG ] ; then echo "getting page $page of repos from $REMOTEGITHOST $1" ; fi
|
|
RESPONSE=$(curl $curlsilent -X 'GET' \
|
|
"$REMOTEGITEAHOST/api/v1/orgs/$1/repos?page=$page" \
|
|
-H 'accept: application/json' \
|
|
-H "Authorization: token $REMOTEACCESSTOKEN"
|
|
)
|
|
if [ $DEBUG ] ; then echo "RESPONSE=$RESPONSE" ; fi
|
|
|
|
if [ $RESPONSE == "[]" ] ; then
|
|
# we have them all
|
|
break
|
|
else
|
|
echo $RESPONSE | grep -oP '(?<="name":").+?(?=")' | while read repo; do
|
|
if [ $DEBUG ] echo "git-mirror-repo.sh ${repo} $1 $TARGETORG" ; fi
|
|
git-mirror-repo.sh ${repo} $1 $TARGETORG
|
|
done
|
|
fi
|
|
done
|