2023-05-09 05:34:39 +02:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
|
|
|
# Migrate (mirror) a remote repository
|
|
|
|
#
|
|
|
|
# migrate-repo.sh <reponame> <organisation> [<target organisation>]
|
|
|
|
#
|
2023-06-04 01:08:49 +02:00
|
|
|
if [[ -z $1 ]] ; then
|
|
|
|
echo "git_migrate-repo.sh <reponame> <organisation> [<target organization> <copy|mirror> <local> <debug>]"
|
|
|
|
echo "will migrate repo to local user unless <target organisation> specified"
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
# get config file and params
|
2023-05-09 05:34:39 +02:00
|
|
|
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-repo.sh <reponame> <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
|
|
|
|
|
|
|
|
DEBUG=
|
|
|
|
if [ ${smegit_DEBUG} == true ] ; then DEBUG=true ; fi
|
|
|
|
if [ $DEBUG ] ; then echo "found ini file: $inifilename" ; fi
|
|
|
|
|
2023-06-04 01:08:49 +02:00
|
|
|
SILENT="-s"
|
|
|
|
if [ $DEBUG ] ; then SILENT="-v" ; fi
|
2023-05-09 05:34:39 +02:00
|
|
|
|
2023-06-04 01:08:49 +02:00
|
|
|
SOURCEHOST=${remote_GITEAHOST}
|
|
|
|
SOURCEACCESSTOKEN=${remote_GITEAACCESSTOKEN}
|
|
|
|
TARGETGITEAHOST=${local_GITEAHOST}
|
|
|
|
TARGETACCESSTOKEN=${local_GITEAACCESSTOKEN}
|
2023-05-10 02:17:35 +02:00
|
|
|
TARGETORG=${local_USER}
|
2023-06-04 01:08:49 +02:00
|
|
|
MIRROR=
|
|
|
|
for param in $2 $3 $4 $5 $6; do
|
|
|
|
if [ $param ] ; then
|
|
|
|
case $param in
|
|
|
|
local )
|
|
|
|
SOURCEHOST=${local_GITEAHOST}
|
|
|
|
SOURCEACCESSTOKEN=${local_GITEAACCESSTOKEN}
|
|
|
|
CLONEADDRESS=$LOCALGITEAHOST/$2/$1.git
|
|
|
|
;;
|
|
|
|
copy )
|
|
|
|
MIRROR=
|
|
|
|
;;
|
|
|
|
mirror )
|
|
|
|
MIRROR=true ;;
|
|
|
|
debug )
|
|
|
|
DEBUG=true ;;
|
|
|
|
* )
|
|
|
|
TARGETORG=$param ;;
|
|
|
|
esac
|
|
|
|
else
|
|
|
|
break
|
|
|
|
fi
|
2023-05-11 02:48:31 +02:00
|
|
|
done
|
2023-06-04 01:08:49 +02:00
|
|
|
CLONEADDRESS=$SOURCEGITEAHOST/$2/$1.git
|
2023-05-11 02:48:31 +02:00
|
|
|
if [ $DEBUG ] ; then
|
|
|
|
echo "MIRROR=$MIRROR"
|
|
|
|
echo "TARGETORG=$TARGETORG"
|
|
|
|
fi
|
2023-05-09 05:34:39 +02:00
|
|
|
|
2023-05-10 02:17:35 +02:00
|
|
|
# check that the TARGETORG exists
|
2023-06-04 01:08:49 +02:00
|
|
|
if [ $DEBUG ] ; then echo "checking if $TARGETORG exists on $TARGETHOST" ; fi
|
|
|
|
RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" $TARGETHOST/$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
|
2023-06-04 01:08:49 +02:00
|
|
|
echo "$TARGETORG does not exist on $TARGETHOST"
|
2023-05-09 05:34:39 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
# And check if this repo already exists on the target
|
2023-06-04 01:08:49 +02:00
|
|
|
if [ $DEBUG ] ; then echo "checking if $TARGETORG/$1 already exists on $TARGETHOST" ; fi
|
|
|
|
RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" $TARGETHOST/$TARGETORG/$1)
|
2023-05-10 04:34:09 +02:00
|
|
|
if [ $DEBUG ] ; then echo "RESPONSE=$RESPONSE" ; fi
|
2023-05-10 05:04:48 +02:00
|
|
|
if [ "$RESPONSE" == "200" ] ; then
|
2023-06-04 01:08:49 +02:00
|
|
|
echo "Repository $TARGETORG/$1 already exists on $TARGETHOST - cannot migrate"
|
2023-05-09 05:34:39 +02:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-05-11 02:48:31 +02:00
|
|
|
DATA=$(cat << EOT
|
2023-06-04 01:08:49 +02:00
|
|
|
"auth_token": "$TARGETACCESSTOKEN",
|
2023-05-11 02:48:31 +02:00
|
|
|
"clone_addr": "$CLONEADDRESS",
|
2023-05-09 05:34:39 +02:00
|
|
|
"issues": true,
|
|
|
|
"labels": true,
|
|
|
|
"lfs": true,
|
|
|
|
"milestones": true,
|
2023-05-11 02:48:31 +02:00
|
|
|
"mirror": $MIRROR,
|
2023-05-09 05:34:39 +02:00
|
|
|
"private": false,
|
|
|
|
"pull_requests": true,
|
|
|
|
"releases": true,
|
2023-05-11 02:48:31 +02:00
|
|
|
"repo_name": "$1",
|
|
|
|
"repo_owner": "$TARGETORG",
|
2023-05-09 05:34:39 +02:00
|
|
|
"service": "git",
|
|
|
|
"uid": 0,
|
|
|
|
"wiki": true
|
2023-05-11 02:48:31 +02:00
|
|
|
EOT
|
|
|
|
)
|
|
|
|
if [ $DEBUG ] ; then echo "JSON DATA=$DATA" ; fi
|
2023-05-09 05:34:39 +02:00
|
|
|
|
2023-06-04 01:08:49 +02:00
|
|
|
if [ $DEBUG ] ; then echo "Migrating $2/$1 as $TARGETORG/$1 on $TARGETHOST MIRROR=$MIRROR" ; fi
|
|
|
|
RESPONSE=$(curl $SILENT -k -X 'POST' \
|
|
|
|
"$TARGETHOST/api/v1/repos/migrate" \
|
2023-05-11 02:48:31 +02:00
|
|
|
-H 'accept: application/json' \
|
2023-06-04 01:08:49 +02:00
|
|
|
-H "Authorization: token $TARGETACCESSTOKEN" \
|
2023-05-11 02:48:31 +02:00
|
|
|
-H 'Content-Type: application/json' \
|
|
|
|
-d "{ $DATA }"
|
|
|
|
)
|
2023-05-09 05:34:39 +02:00
|
|
|
if [ $DEBUG ] ; then echo $? $RESPONSE ; fi
|
|
|
|
|
|
|
|
# And check if this repo is there
|
2023-05-10 02:17:35 +02:00
|
|
|
if [ $DEBUG ] ; then echo "checking that $TARGETORG/$1 exists" ; fi
|
2023-06-04 01:08:49 +02:00
|
|
|
RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" $TARGETHOST/$TARGETORG/$1)
|
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
|
2023-05-11 02:48:31 +02:00
|
|
|
msg="Repository $TARGETORG/$1 has been created"
|
|
|
|
if [ $MIRROR == "true" ] ; then
|
|
|
|
msg=$msg" and mirrors $2/$1"
|
|
|
|
else
|
|
|
|
msg=$msg" and is a copy of $2/$1"
|
|
|
|
fi
|
|
|
|
echo $msg
|
2023-05-09 05:34:39 +02:00
|
|
|
else
|
2023-05-11 02:48:31 +02:00
|
|
|
echo "migrate-repo was unable to migrate $2/$1 sucessfully"
|
|
|
|
exit 1
|
2023-05-09 05:34:39 +02:00
|
|
|
fi
|
|
|
|
|