From 75aa1774301dfdab9bf3ba53a941686971e2473e Mon Sep 17 00:00:00 2001 From: Trevor Batley Date: Mon, 5 Jun 2023 10:41:39 +1000 Subject: [PATCH] added in delete of pkg on transfer --- rename-e-smith-pkg.sh | 67 +++++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 15 deletions(-) diff --git a/rename-e-smith-pkg.sh b/rename-e-smith-pkg.sh index d69f75a..328b8a8 100755 --- a/rename-e-smith-pkg.sh +++ b/rename-e-smith-pkg.sh @@ -282,25 +282,62 @@ if [ -e archivefilename ] ; then git rm archivefilename $QUIET ; fi git commit -m "rename-e-smith-pkg script (#12359)" $QUIET git push origin &> /dev/null -# Transfer back into the source organisation +# Do we want to transfer the new package into the source organisation if [ $TRANSFER ] ; then - RESPONSE=$(curl "$SILENT" "$checkSSL" -o /dev/null -w "%{http_code}" -X 'POST' \ - "$GITEAHOST/api/v1/repos/$TARGETORG/$TARGETPKG/transfer" \ - -H 'accept: application/json' \ - -H "Authorization: token $GITEAACCESSTOKEN" \ - -H 'Content-Type: application/json' \ - -d '{ - "new_owner": "'"$SOURCEORG"'" - }' - ) - if [[ $RESPONSE != "202" ]] ; then - echo "Unable to transfer $TARGETORG/$TARGETPKG back to $SOURCEORG - Aborting!" + #Check that target package does not exist + if [ $DEBUG ] ; then echo "Check if $SORCEORG/$TARGETPKG is there!" ; fi + RESPONSE=$(curl $SILENT -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$SOURCEORG/$TARGETPKG") + EXISTS= + if [ "$RESPONSE" == "200" ]; then + EXISTS=true + if [ $DEBUG ] ; then echo "Repository for $SOURCEORG/$TARGETPKG exists!" ; fi + if [ -z ${DELETEIT} ] ; then + while true; do + read -p "$SOURCEORG/$TARGETPKG 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/$TARGETPKG" ; fi + RESPONSE=$(curl "$checkSSL" "$SILENT" -X 'DELETE' \ + "$GITEAHOST/api/v1/repos/$TARGETORG/$TARGETPKG" \ + -H 'accept: application/json' \ + -H "Authorization: token $GITEAACCESSTOKEN" ) + EXISTS= + fi + else + if [ $DEBUG ] ; then echo "Repository for $SOURCEORG/$TARGETPKG does not exist on $GITEAHOST" ; fi + fi + + # Transfer the package back to source organisation + if [[ -z $EXISTS ]] ; then + RESPONSE=$(curl "$SILENT" "$checkSSL" -o /dev/null -w "%{http_code}" -X 'POST' \ + "$GITEAHOST/api/v1/repos/$TARGETORG/$TARGETPKG/transfer" \ + -H 'accept: application/json' \ + -H "Authorization: token $GITEAACCESSTOKEN" \ + -H 'Content-Type: application/json' \ + -d '{ + "new_owner": "'"$SOURCEORG"'" + }' + ) + fi + if [[ $EXISTS || $RESPONSE != "202" ]] ; then + echo "Unable to transfer $TARGETORG/$TARGETPKG to $SOURCEORG/$TARGETPKG ($RESPONSE) - Aborting!" echo "But everything else has been done and $TARGETORG/$TARGETPKG should be complete." exit 1 + else + if [[ -z $NOOUTPUT ]] ; then echo "$SOURCEORG/$SOURCEPKG has been renamed as $SOURCEORG/$TARGETPKG" ; fi fi +else + if [[ -z $NOOUTPUT ]] ; then echo "$SOURCEORG/$SOURCEPKG has been renamed as $TARGETORG/$TARGETPKG" ; fi fi -if [[ -z $NOOUTPUT ]] ; then - echo "$SOURCEORG/$SOURCEPKG has been renamed as $TARGETORG/$TARGETPKG" -fi exit 0