smeserver-gitutils/git-retag.sh

159 lines
4.7 KiB
Bash
Executable File

#!/bin/bash
#
# $1 = Module name e.g. smeserver-ddclient
# $2 = Organisation (smeserver or smecontrib)e
# optional (can be in any order)
# <local> will use parameters set for local repository else it will use remote
# <debug> turn on debug regardless of ~/.smegit/config
# <noisy> print a line showing how it was called
# Module name must exist
#
if [[ -z $1 ]] ; then
echo "************git-retag.sh <modulename> <organization> [<local> | <debug>]"
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-retag.sh <modulename> <organization> [<local> | <debug>]"
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
REPO_NAME=$1
ORGGITEA=$2
GITEAUser=${remote_USER}
GITEAACCESSTOKEN=${remote_GITEAACCESSTOKEN}
GITEAHOST=${remote_GITEAHOST}
NOISY=
EXTRAPARAMS=
for param in $3 $4 $5 $6 ; do
if [ $param ] ; then
case $param in
local )
GITEAUser=${local_USER}
GITEAACCESSTOKEN=${local_GITEAACCESSTOKEN}
GITEAHOST=${local_GITEAHOST}
;;
debug )
DEBUG=true ;;
noisy )
NOISY=true ;;
* )
echo "Unkown parameter: $param"
exit 1
;;
esac
else
break
fi
done
if [ $NOISY ] ; then echo "git-retag.sh $1 $2 $3 $4 $5 $6" ; fi
if [ $DEBUG ] ; then echo "************found ini file: $inifilename" ; fi
# Make this null if you want lots of output. Still quite a bit anyway
QUIET="-q"
SILENT4CURL="-s"
SILENT4MAKE="-s"
if [ $DEBUG ] ; then
QUIET=
SILENT4CURL="-v"
SILENT4MAKE=
fi
# Make this null to get curl to check ssl cert
checkSSL="-k"
LOCALUser=${local_USER}
if [[ $smegit_WORKDIR == ~* ]] ; then
# relative to users home dir
WORKDIR=$(echo ~)${smegit_WORKDIR:1}
else
# absolute path
WORKDIR=${smegit_WORKDIR}
fi
if [ $DEBUG ] ; then echo "WORKDIR=$WORKDIR" ; fi
RemoteRepoURL="$GITEAHOST/$ORGGITEA/$REPO_NAME"
# Make sure credentials taken from store
# The first time you pull or push it will ask for credentials and then save them in a file.
git config --global credential.helper store
# Create the local Git repository
GITFiles=$WORKDIR/$ORGGITEA
mkdir -p $GITFiles
cd $GITFiles
# Delete the local first
if [ -d "$REPO_NAME" ] ; then
if [ $DEBUG ] ; then echo "************Deleting local GIT files" ; fi
rm -Rf $GITFiles/$REPO_NAME
fi
# See if it exists on the Gitea host
if [ $DEBUG ] ; then echo "check that $GITEAHOST/$ORGGITEA/$REPO_NAME exists" ; fi
RESPONSE=$(curl $checkSSL $SILENT4CURL -o /dev/null -w "%{http_code}" "$GITEAHOST/api/v1/repos/$ORGGITEA/$REPO_NAME")
if [ "$RESPONSE" == "200" ]; then
if [ $DEBUG ] ; then echo "************Repository for $REPO_NAME exists, cloning!" ; fi
# If so, clone it (just want the README.md)
if [ $DEBUG ] ; then echo "git clone $GITEAHOST/$ORGGITEA/$REPO_NAME.git" ; fi
cd $GITFiles
git clone "$GITEAHOST/$ORGGITEA/$REPO_NAME.git"
else
echo "************Repository for $ORGGITEA/$REPO_NAME does not exist. Aborting $RESPONSE"
exit 1
fi
cd $GITFiles/$REPO_NAME
# read the existing tags
OLDTAGS=$(git tag --list)
# delete old tag/s
for tag in $OLDTAGS ; do
# git tag --delete $tag
# git push origin --delete $tag
# for some reason the remote delete is rejected, so were using the gitea API call instead
if [[ $DEBUG ]] ; then echo "curl $checkSSL $SILENT4CURL -X 'DELETE' '$GITEAHOST/api/v1/orgs/$ORGGITEA/$REPO_NAME/tags/$tag' -H 'accept: application/json' -H 'Authorization: token $GITEAACCESSTOKEN'" ; fi
RESPONSE=$(curl "$checkSSL" "$SILENT4CURL" -X 'DELETE' \
"$GITEAHOST/api/v1/orgs/$ORGGITEA/$REPO_NAME/tags/$tag" \
-H 'accept: application/json' \
-H "Authorization: token $GITEAACCESSTOKEN" )
done
# Now create the version and release tag from the specfile
VERSION_RELEASE=`rpm --queryformat '%{version} %{release}\n' --specfile $REPO_NAME.spec`
VERSION=${VERSION_RELEASE% *}
RELEASE=${VERSION_RELEASE#* }
# Release is not reliable - if you run on Rocky it comes back EL8, centos 7 - EL7
# So, we need to just take the build part (first word)
TAG=$VERSION"-"${RELEASE%.*}
git pull
git tag -a $TAG -m "Setting tag to current Version-Release in spec file: $TAG"
git push origin --tag $TAG $QUIET
echo "Current $ORGGITEA/$REPO_NAME tagged as $TAG - old tags removed"
exit 0