You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
7.8 KiB
Bash

#!/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 "git-make-release <modulename> <organization> <release> [local] [debug] [noisy] [draft] [prerelease]"
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-make-release <modulename> <organization> <release> [local] [debug] [noisy] [draft] [prerelease]"
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=
MAKEPRE=false
MAKEDRAFT=false
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 ;;
draft )
MAKEDRAFT=true ;;
prerelease )
MAKEPRE=true ;;
draftpre )
MAKEPRE=true;MAKEDRAFT=true ;;
* )
EXTRAPARAMS=$EXTRAPARAMS" "$param ;;
esac
else
break
fi
done
if [ $DEBUG ] ; then echo "draft:$MAKEDRAFT*pre:$MAKEPRE"; fi
#check for release tag
if [ -z $3 ]; then
echo "Release tag not set";
exit 1;
fi
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 -s -X 'GET' \
"$GITEAHOST/api/v1/repos/$2/$1/releases/tags/SME$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
# 3 branches 'cos I can't get the ascaping correct for using $MAKEPRE and $MAKEDRAFT in the -d json parameter to curl!
if [ $MAKEDRAFT = "true" ]; then
if [ $DEBUG ] ; then echo "$MAKEDRAFT:Creating draft release"; fi
RESPONSE=$(curl -s -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 Draft Release of rpm built from git - `date` "'",
"draft": true,
"name": "'"$1"'",
"prerelease": false,
"tag_name": "'SME"$3"'",
"target_commitish": ""}'
);
elif [ $MAKEPRE = "true" ]; then
if [ $DEBUG ] ; then echo "Creating pre-release"; fi
RESPONSE=$(curl -s -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 Pre-Release of rpm built from git - `date` "'",
"draft": false,
"name": "'"$1"'",
"prerelease": true,
"tag_name": "'SME"$3"'",
"target_commitish": ""}'
);
else
if [ $DEBUG ] ; then echo "Creating full release"; fi
RESPONSE=$(curl -s -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 Stable Release of rpm built from git - `date` "'",
"draft": false,
"name": "'"$1"'",
"prerelease": false,
"tag_name": "'SME"$3"'",
"target_commitish": ""}'
);
fi
# Extract id from release
if [ $DEBUG ]; then echo "Create Release response="$RESPONSE; fi
exit 0
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 -s -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 -s -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
if [ $DEBUG ]; then echo "Did not find $BASEFile in \"$NAMES\""; fi
fi
# Then send current version
FULLFILENAME=$FULLPATH/$File
if [ $DEBUG ] ; then echo $FULLFILENAME; fi
RESPONSE=$(curl -s -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