2023-05-09 05:34:39 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Migrate (mirror) a remote repository
|
|
|
|
#
|
|
|
|
# migrate-repo.sh <reponame> <organisation> [<traget organisation>]
|
|
|
|
#
|
|
|
|
inifilename=$(echo ~)"/.smegit/config"
|
2023-05-10 02:17:35 +02:00
|
|
|
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
|
2023-05-09 05:34:39 +02:00
|
|
|
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
|
2023-05-12 00:39:20 +02:00
|
|
|
echo "migrate-org.sh <source organisation> [<target organisation> <review>]"
|
2023-05-09 05:34:39 +02:00
|
|
|
echo "<target organisation> is optional and will default to the gitea user"
|
2023-05-12 00:39:20 +02:00
|
|
|
echo "<review> is optional and will echo the changes, but not execute"
|
2023-05-09 05:34:39 +02:00
|
|
|
exit 0
|
|
|
|
fi
|
2023-05-12 00:39:20 +02:00
|
|
|
|
2023-05-09 05:34:39 +02:00
|
|
|
DEBUG=
|
2023-05-11 02:48:31 +02:00
|
|
|
curlsilent="-s"
|
2023-05-09 05:34:39 +02:00
|
|
|
if [ ${smegit_DEBUG} == true ] ; then DEBUG=true ; fi
|
2023-05-11 02:48:31 +02:00
|
|
|
if [ $DEBUG ] ; then
|
|
|
|
echo "found ini file: $inifilename"
|
2023-05-11 08:49:37 +02:00
|
|
|
curlsilent="-v"
|
2023-05-11 02:48:31 +02:00
|
|
|
fi
|
2023-05-12 00:39:20 +02:00
|
|
|
|
2023-05-11 02:48:31 +02:00
|
|
|
MIRROR="copy"
|
|
|
|
TARGETORG=
|
2023-05-12 00:39:20 +02:00
|
|
|
for $param in $2 $3 $4; do
|
2023-05-11 08:19:24 +02:00
|
|
|
if [ $param ] ; then
|
2023-05-11 08:49:37 +02:00
|
|
|
if [[ $param == "copy" || $param == "mirror" ]] ; then
|
|
|
|
MIRROR=$param
|
2023-05-12 00:39:20 +02:00
|
|
|
elif [[ $param == "review"]] ; then
|
|
|
|
REVIEW=true
|
2023-05-11 08:19:24 +02:00
|
|
|
else
|
2023-05-11 08:49:37 +02:00
|
|
|
TARGETORG=$param
|
2023-05-11 08:19:24 +02:00
|
|
|
fi
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
2023-05-11 02:48:31 +02:00
|
|
|
done
|
2023-05-09 05:34:39 +02:00
|
|
|
|
|
|
|
REMOTEACCESSTOKEN=${remote_GITEAACCESSTOKEN}
|
|
|
|
LOCALACCESSTOKEN=${local_GITEAACCESSTOKEN}
|
|
|
|
REMOTEGITEAHOST=${remote_GITEAHOST}
|
|
|
|
LOCALGITEAHOST=${local_GITEAHOST}
|
2023-05-10 04:34:09 +02:00
|
|
|
if [ $DEBUG ] ; then echo "TARGETORG=$TARGETORG" ; fi
|
2023-05-09 05:34:39 +02:00
|
|
|
|
|
|
|
# check that the target organisation exists locally
|
2023-05-10 04:34:09 +02:00
|
|
|
if [ $DEBUG ] ; then echo "checking that $TARGETORG exists on $LOCALGITEAHOST" ; fi
|
2023-05-09 05:34:39 +02:00
|
|
|
RESPONSE=$(curl $curlsilent -o /dev/null -w "%{http_code}" $LOCALGITEAHOST/$TARGETORG)
|
2023-05-10 04:34:09 +02:00
|
|
|
if [ $DEBUG ] ; then echo "RESPONSE=$RESPONSE" ; fi
|
2023-05-09 05:34:39 +02:00
|
|
|
if [[ "$RESPONSE" != "200" ]] ; then
|
|
|
|
echo "$TARGETORG does not exist on $LOCALGITEAHOST"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-05-12 00:39:20 +02:00
|
|
|
if [[ ! $REVIEW ]] ; then
|
|
|
|
while true; do
|
|
|
|
read -p "Are you sure?(y/n) " yn
|
|
|
|
case $yn in
|
|
|
|
[Yy]* ) break;;
|
|
|
|
[Nn]* ) exit;;
|
|
|
|
* ) echo "Please answer yes or no.";;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
2023-05-09 05:34:39 +02:00
|
|
|
# get a list of repositories in the source organisation
|
2023-05-10 04:34:09 +02:00
|
|
|
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
|
2023-05-09 05:34:39 +02:00
|
|
|
|
2023-05-11 08:49:37 +02:00
|
|
|
if [[ $RESPONSE == '[]' ]] ; then
|
2023-05-10 04:34:09 +02:00
|
|
|
# we have them all
|
|
|
|
break
|
|
|
|
else
|
|
|
|
echo $RESPONSE | grep -oP '(?<="name":").+?(?=")' | while read repo; do
|
2023-05-11 02:48:31 +02:00
|
|
|
if [ $DEBUG ] ; then echo "git-migrate-repo.sh ${repo} $1 $TARGETORG $MIRROR" ; fi
|
2023-05-12 00:39:20 +02:00
|
|
|
if [[ $REVIEW ]] ; then
|
|
|
|
echo "git-migrate-repo.sh ${repo} $1 $TARGETORG $MIRROR"
|
|
|
|
else
|
|
|
|
git-migrate-repo.sh ${repo} $1 $TARGETORG $MIRROR
|
|
|
|
fi
|
2023-05-10 04:34:09 +02:00
|
|
|
done
|
|
|
|
fi
|
2023-05-09 05:34:39 +02:00
|
|
|
done
|