diff --git a/README.md b/README.md index dec4d83..910c1ac 100644 --- a/README.md +++ b/README.md @@ -19,12 +19,14 @@ bring down current git repository and run mockbuild\ git-get-and-build.sh \ \ \[\\]\ \ will build from local GITEA (in config) otherwise from remote GITEA -* git-mirror-repo.sh\ -create mirror of src.koozali.org repository in a local GITEA instance\ -git-mirror-repo.sh \ \ \[\\]\ +* git-migrate-repo.sh\ +create mirror or copy of src.koozali.org repository in a local GITEA instance\ +git-mirror-repo.sh \ \ \[\ \\]\ +\ will create local repo as either a copy or a mirror of the remote repo (default = copy) \ within your local GITEA, otherwise local user -* git-mirror-org.sh\ +* git-migrate-org.sh\ create mirrors of all repos for an organisation in a local GITEA instance\ -git-mirror-org.sh \ \[\\]\ +git-mirror-org.sh \ \[\ \\]\ +\ will create local repos as either a copy or a mirror of the remote repos (default = copy) \ within your local GITEA, otherwise local user \ No newline at end of file diff --git a/git-mirror-org.sh b/git-migrate-org.sh similarity index 82% rename from git-mirror-org.sh rename to git-migrate-org.sh index 8ad05b5..5a21712 100755 --- a/git-mirror-org.sh +++ b/git-migrate-org.sh @@ -37,18 +37,32 @@ if [[ -z $1 ]] ; then 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 +if [ ${smegit_DEBUG} == true ] ; then DEBUG=true ; fi +if [ $DEBUG ] ; then + echo "found ini file: $inifilename" + curlsilent= +fi +MIRROR="copy" +TARGETORG= +#if [ $2 ] ; then TARGETORG=$2 ; fi +for $param in {$2 $3} ; do + if [ $PARAM ] ; then + if [ $param == "copy" || $param == "mirror" ] ; then + $MIRROR=$param + else + $TARGETORG=$param + fi + else + break + fi +done + 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 @@ -75,8 +89,8 @@ for page in {1..10} ; do break else echo $RESPONSE | grep -oP '(?<="name":").+?(?=")' | while read repo; do - if [ $DEBUG ] ; then echo "git-mirror-repo.sh ${repo} $1 $TARGETORG" ; fi - git-mirror-repo.sh ${repo} $1 $TARGETORG + if [ $DEBUG ] ; then echo "git-migrate-repo.sh ${repo} $1 $TARGETORG $MIRROR" ; fi + git-migrate-repo.sh ${repo} $1 $TARGETORG $MIRROR done fi done diff --git a/git-mirror-repo.sh b/git-migrate-repo.sh similarity index 74% rename from git-mirror-repo.sh rename to git-migrate-repo.sh index dcffa15..ba30149 100755 --- a/git-mirror-repo.sh +++ b/git-migrate-repo.sh @@ -41,7 +41,7 @@ if [ ${smegit_DEBUG} == true ] ; then DEBUG=true ; fi if [ $DEBUG ] ; then echo "found ini file: $inifilename" ; fi curlsilent="-s" -if [ $DEBUG ] ; then curlsilent= ; fi +if [ $DEBUG ] ; then curlsilent="-v" ; fi REMOTEACCESSTOKEN=${remote_GITEAACCESSTOKEN} LOCALACCESSTOKEN=${local_GITEAACCESSTOKEN} @@ -49,7 +49,23 @@ REMOTEGITEAHOST=${remote_GITEAHOST} CLONEADDRESS=$REMOTEGITEAHOST/$2/$1.git LOCALGITEAHOST=${local_GITEAHOST} TARGETORG=${local_USER} -if [ $3 ] ; then TARGETORG=$3 ; fi +#if [ $3 ] ; then TARGETORG=$3 ; fi +MIRROR="false" +for param in $3 $4 ; do + if [ $param ] ; then + if [[ $param == "copy" || $param == "mirror" ]] ; then + if [ $param == "mirror" ] ; then MIRROR="true" ; fi + else + TARGETORG=$param + fi + else + break + fi +done +if [ $DEBUG ] ; then + echo "MIRROR=$MIRROR" + echo "TARGETORG=$TARGETORG" +fi # check that the TARGETORG exists if [ $DEBUG ] ; then echo "checking if $TARGETORG exists on $LOCALGITEAHOST" ; fi @@ -69,30 +85,34 @@ if [ "$RESPONSE" == "200" ] ; then exit 1 fi -if [ $DEBUG ] ; then echo "Mirroring $2/$1 as $TARGETORG/$1 on $LOCALGITEAHOST" ; fi +DATA=$(cat << EOT + "auth_token": "$REMOTEACCESSTOKEN", + "clone_addr": "$CLONEADDRESS", + "issues": true, + "labels": true, + "lfs": true, + "milestones": true, + "mirror": $MIRROR, + "private": false, + "pull_requests": true, + "releases": true, + "repo_name": "$1", + "repo_owner": "$TARGETORG", + "service": "git", + "uid": 0, + "wiki": true +EOT +) +if [ $DEBUG ] ; then echo "JSON DATA=$DATA" ; fi + +if [ $DEBUG ] ; then echo "Migrating $2/$1 as $TARGETORG/$1 on $LOCALGITEAHOST MIRROR=$MIRROR" ; 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": "'"$TARGETORG"'", - "service": "git", - "uid": 0, - "wiki": true -}') - + -d "{ $DATA }" + ) if [ $DEBUG ] ; then echo $? $RESPONSE ; fi # And check if this repo is there @@ -100,9 +120,15 @@ if [ $DEBUG ] ; then echo "checking that $TARGETORG/$1 exists" ; fi RESPONSE=$(curl $curlsilent -o /dev/null -w "%{http_code}" $LOCALGITEAHOST/$TARGETORG/$1) if [ $DEBUG ] ; then echo "RESPONSE=$RESPONSE" ; fi if [ "$RESPONSE" == "200" ] ; then - echo "Repository $TARGETORG/$1 has been created and mirrors $2/$1" + 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 else - echo "migrate-repo was unable to migrate $2/$1 sucessfully" - exit 1 + echo "migrate-repo was unable to migrate $2/$1 sucessfully" + exit 1 fi