From 15ffd74a3eb72e9c68c53db1e48bb772c6cbf0dd Mon Sep 17 00:00:00 2001 From: Trevor Batley Date: Mon, 5 Jun 2023 15:31:16 +1000 Subject: [PATCH] add git-transfer-repo.sh script --- git-transfer-repo.sh | 178 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 178 insertions(+) create mode 100755 git-transfer-repo.sh diff --git a/git-transfer-repo.sh b/git-transfer-repo.sh new file mode 100755 index 0000000..5776e5b --- /dev/null +++ b/git-transfer-repo.sh @@ -0,0 +1,178 @@ +#!/bin/bash +# +# Transfer a repo to a different user (or organisation) +# $1 = Module name e.g. e-smith-dnscache +# $2 = Source Organisation (any valid organisation or user) +# $3 = Target Organisation (any valid organisation or user - you need owner privileges) +# $4-$7 can appear in any order and are optional +# = "local" will use parameters set for local repository else it will use remote +# = "force" will automagically delete an existing repo, otherwise it will prompt +# = "debug" run in debug mode +# = "silent" suppress success message +# + +if [[ -z $1 ]] ; then + echo "Transfer a repo to a different user (or organisation)" + echo "git-transfer-repo.sh ,target organisation> [ ]" + echo " = repository name (e.g. smeserver-clamav)" + echo " (any valid organisation or user)" + echo " (any valid organisation or user - you need owner privileges)" + echo " can appear in any order and are optional" + echo " - will use parameters set for local repository, else it will use remote" + echo " - will automagically delete an existing target repo, otherwise it will prompt" + echo " - run in debug mode" + echo " - suppress success message" + exit 0 +fi + +# +# Pull in parameters from a config file ~/.smegit/config +# +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-cvs2git.sh []" + 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" +DEBUG= +if [ ${smegit_DEBUG} == "true" ] ; then DEBUG=true ; fi +if [ $DEBUG ] ; then echo "************found ini file: $inifilename" ; fi + +if [[ $smegit_WORKDIR == ~* ]] ; then + # relative to users home dir + WORKDIR=$(echo ~)${smegit_WORKDIR:1} +else + # absolute path + WORKDIR=${smegit_WORKDIR} +fi + +# Command line parameters +REPO=$1 +SOURCEORG=$2 +TARGETORG=$3 + +GITEAHOST=${remote_GITEAHOST} +GITEAACCESSTOKEN=${remote_GITEAACCESSTOKEN} +DELETEIT= +NOOUTPUT= +for param in $4 $5 $6 $7; do + if [ $param ] ; then + case $param in + local ) + GITEAHOST=${local_GITEAHOST} + GITEAACCESSTOKEN=${local_GITEAACCESSTOKEN} + ;; + force ) + DELETEIT=true ;; + debug ) + DEBUG=true ;; + silent ) + NOOUTPUT=true ;; + * ) + echo "Ignoring $param" ;; + esac + else + break + fi +done + +# Debug settings +# Make this null if you want lots of output. Still quite a bit anyway +QUIET="--quiet" +SILENT="-s" +if [ $DEBUG ] ; then + QUIET= + SILENT="-v" +fi +# Make this null to get curl to check ssl cert +checkSSL="-k" + +#Check that source package exists +if [ $DEBUG ] ; then echo "Check if $SOURCEORG/$REPO is there!" ; fi +RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$SOURCEORG/$REPO") +if [ "$RESPONSE" == "200" ]; then + if [ $DEBUG ] ; then echo "Repository for $SOURCEORG/$REPO exists!" ; fi +else + echo "************Repository for $SOURCEORG/$REPO does not exist on $GITEAHOST ($RESPONSE)" + exit 1 +fi + +if [ $DEBUG ] ; then echo "Transferring $TARGETORG/$REPO to $SOURCEORG/$REPO" ; fi +#Check that target repo does not exist +if [ $DEBUG ] ; then echo "Check if $TARGETORG/$REPO is there!" ; fi +RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$TARGETORG/$REPO") +EXISTS= +if [ "$RESPONSE" == "200" ]; then + EXISTS=true + if [ $DEBUG ] ; then echo "Repository for $TARGETORG/$REPO exists!" ; fi + if [ -z ${DELETEIT} ] ; then + while true; do + read -p "$TARGETORG/$REPO exists! Do you wish to delete it and continue?(y/n) " yn + case $yn in + [Yy]* ) + DELETEIT=true + break ;; + [Nn]* ) + DELETEIT= + break ;; + * ) echo "Please answer yes or no.";; + esac + done + fi + if [ $DELETEIT ] ; then + if [ $DEBUG ] ; then echo "Deleting $TARGETORG/$REPO" ; fi + RESPONSE=$(curl "$checkSSL" "$SILENT" -o /dev/null -w "%{http_code}" -X 'DELETE' \ + "$GITEAHOST/api/v1/repos/$TARGETORG/$REPO" \ + -H 'accept: application/json' \ + -H "Authorization: token $GITEAACCESSTOKEN" ) + if [[ $RESPONSE != "204" ]] ; then + echo "Unable to delete $TARGETORG/$REPO ($RESPONSE)" + else + EXISTS= + fi + fi +else + if [ $DEBUG ] ; then echo "Repository for $SOURCEORG/$REPO does not exist on $GITEAHOST" ; fi +fi + +# Transfer the repo +if [[ -z $EXISTS ]] ; then + if [ $DEBUG ] ; then echo "Actual Transfer" ; fi + RESPONSE=$(curl "$SILENT" "$checkSSL" -o /dev/null -w "%{http_code}" -X 'POST' \ + "$GITEAHOST/api/v1/repos/$SOURCEORG/$REPO/transfer" \ + -H 'accept: application/json' \ + -H "Authorization: token $GITEAACCESSTOKEN" \ + -H 'Content-Type: application/json' \ + -d '{ + "new_owner": "'"$TARGETORG"'" + }' + ) +fi +if [[ $EXISTS || $RESPONSE != "202" ]] ; then + echo "Unable to transfer $SOURCEORG/$REPO to $TARGETORG/$REPO ($RESPONSE) - Aborting!" + exit 1 +else + if [[ -z $NOOUTPUT ]] ; then echo "$SOURCEORG/$REPO has been transferred to $TARGETORG/$REPO" ; fi +fi + +exit 0