smeserver-gitutils/git-make-release.sh

205 lines
6.2 KiB
Bash
Raw Normal View History

2023-07-09 12:52:15 +02:00
#!/bin/bash
#
# git-make-release.sh <organisation> <repo> <tagname> [usual options]
#
# Create the release (deleting one that is the same tag),
# then uploads the .src.rpm and .rpm as an attachment (deleting any one of the same name)
#
#
if [[ -z $1 ]] ; then
echo "************make-release <modulename> <organization> <release> [local] [debug] [noisy]"
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 "make-release <modulename> <organization> <release> [local] [debug] [noisy]"
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
GITEAUser=${remote_USER}
GITEAACCESSTOKEN=${remote_GITEAACCESSTOKEN}
GITEAHOST=${remote_GITEAHOST}
NOISY=
EXTRAPARAMS=
for param in $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 ;;
* )
EXTRAPARAMS=$EXTRAPARAMS" "$param ;;
esac
else
break
fi
done
if [ $NOISY ] ; then echo "make-release $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/$2/$1"
if [ $DEBUG ] ; then echo "URL="$RemoteRepoURL; fi
# First see if release exists:
RESPONSE=$(curl -X 'GET' \
"$GITEAHOST/api/v1/repos/$2/$1/releases/tags/$3" \
-H 'accept: application/json')
if [ $DEBUG ] ; then echo "See if Release exists response="$RESPONSE; fi
#parse result
ID=""
if echo "$RESPONSE" | jq empty >/dev/null 2>&1; then
if echo "$RESPONSE" | jq 'has("errors")' | grep -q 'true'; then
if [ $DEBUG ] ; then echo "Error detected:"; fi
ERROR=$(echo "$RESPONSE" | jq '.message')
if [ $DEBUG ] ; then echo "Error="$ERROR; fi
elif echo "$RESPONSE" | jq 'has("id")' | grep -q 'true'; then
if [ $DEBUG ] ; then echo "Id detected:"; fi
ID=$(echo "$RESPONSE" | jq -r '.id')
else
echo "No error or id detected ($RESPONSE)."
exit 1
fi
else
echo "Invalid JSON string ($RESPONSE)"
exit 1
fi
if [[ -z "$ID" ]]; then
# Create Release
if [ $NOISY ]; then echo "Creating Release $3 for $2/$1"; fi
RESPONSE=$(curl -X 'POST' \
"$GITEAHOST/api/v1/repos/$2/$1/releases" \
-H 'accept: application/json' \
-H "Authorization: token $GITEAACCESSTOKEN" \
-H 'Content-Type: application/json' \
-d '{
"body": "'"1st Release of rpm built from git - `date` "'",
"draft": true,
"name": "'"$1"'",
"prerelease": true,
"tag_name": "'SME"$3"'",
"target_commitish": ""
}')
# Extract id from release
if [ $DEBUG ]; then echo "Create Release response="$RESPONSE; fi
ID=$(echo $RESPONSE | jq -r '.id')
if [[ -z $ID ]] ; then
echo "Unable to create release for $2/$1 ($3)"
exit 1
fi
else
if [ $NOISY ]; then echo "Found Release $ID for $2/$1 ($3)"; fi
fi
if [ $DEBUG ] ; then echo "ID:"$ID; fi
# And use it to upload the rpm and src rpm
cd $WORKDIR/$2/$1
# Got to cd to rpm directory here
RPMDIR=$(ls $(basename `pwd`)*.rpm | sort | head -n 1 | sed 's/.src.rpm//' | sed 's/\./_/g')
echo $RPMDIR
# List the existing attachments
ASSETSRESPONSE=$(curl -X 'GET' \
"$GITEAHOST/api/v1/repos/$2/$1/releases/$ID/assets" \
-H 'accept: application/json')
if [ $DEBUG ]; then echo "Asset response:"$ASSETSRESPONSE; fi
NAMES=$(echo $ASSETSRESPONSE | jq -r '.[].name' | paste -sd "," -)
if [ $DEBUG ]; then echo "Names:"$NAMES; fi
if [ $DEBUG ]; then echo $(ls $RPMDIR/*.rpm); fi
FULLPATH=$(pwd);
for File in $(ls $RPMDIR/*.rpm) ; do
if [ $NOISY ]; then echo "Uploading $File"; fi
BASEFile=$(basename $File)
if [ $DEBUG ]; then echo $BASEFile; fi
# Need to check if file is there already
if [[ "$NAMES" == *"$BASEFile"* ]] ; then
#Delete it - need the ID for it
if [ $NOISY ]; then echo "Deleting $BASEFile from $1/$2 $3 ($ID) release"; fi
ATTID=$(echo $ASSETSRESPONSE | jq -r '[ .[] | select(.name == "'"$BASEFile"'") | .id]')
#echo "ATTID:"$ATTID
ATTID=$(echo $ATTID | sed 's/\[//' | sed 's/\]//' | sed 's/ //g')
if [ $DEBUG ]; then echo "ATTID:"$ATTID; fi
RESPONSE=$(curl -X 'DELETE' \
"$GITEAHOST/api/v1/repos/$2/$1/releases/$ID/assets/$ATTID" \
-H "Authorization: token $GITEAACCESSTOKEN" \
-H 'accept: application/json')
if [ $DEBUG ]; then echo "DELETED Response:"$RESPONSE; fi
if [[ -z $RESPONSE ]] ; then
echo "Deleted $BASEFile from $1/$2 $3 ($ID) release"
else
echo "Delete failed $BASEFile from $2/$1 $3 ($ID) release ($RESPONSE)"
fi
else
echo "Did not find $BASEFile in \"$NAMES\""
fi
# Then send current version
FULLFILENAME=$FULLPATH/$File
if [ $DEBUG ] ; then echo $FULLFILENAME; fi
RESPONSE=$(curl -X 'POST' \
"$GITEAHOST/api/v1/repos/$2/$1/releases/$ID/assets?name=$BASEFile" \
-H 'accept: application/json' \
-H "Authorization: token $GITEAACCESSTOKEN" \
-H 'Content-Type: multipart/form-data' \
-F "attachment=@$FULLFILENAME;type=application/x-rpm")
if [ $DEBUG ]; then echo "Send current version($File) response="$RESPONSE; fi
if echo "$RESPONSE" | jq 'has("errors")' | grep -q 'true'; then
ERROR=$(echo "$RESPONSE" | jq '.message')
echo "$BASEFile not sucessfully uploaded ($ERROR)"
else
if [ $NOISY ]; then echo "$BASEFile sucessfully uploaded"; fi
fi
done