2018-06-27 09:01:06 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
|
|
|
|
json_escape () {
|
|
|
|
printf '%s' "$1" | python -c 'import json,sys; print(json.dumps(sys.stdin.read()))'
|
|
|
|
#printf '%s' "$1" | php -r 'echo json_encode(file_get_contents("php://stdin"));'
|
|
|
|
}
|
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
set -e
|
|
|
|
|
|
|
|
#Cmdline options
|
|
|
|
# -t: tag (*_RC* determines prerelease version, can be overriden be -p)
|
|
|
|
# -a: GitHub API access token
|
|
|
|
# -s: GitHub repository slug (user/repo)
|
|
|
|
# -p: prerelease true/false
|
|
|
|
# -f: files to upload (ie assets. delim = ';', must come quoted)
|
|
|
|
# -d: directory to upload (by adding dir contents to assets)
|
|
|
|
while getopts ":t:,:a:,:s:,:p:,:f:,:d:" opt; do
|
|
|
|
case $opt in
|
|
|
|
t)
|
|
|
|
varTagName=$OPTARG
|
|
|
|
echo "TAG: $varTagName" >&2
|
|
|
|
;;
|
|
|
|
a)
|
|
|
|
varAccessToken=$OPTARG
|
|
|
|
echo "ACCESS TOKEN: $varAccessToken" >&2
|
|
|
|
;;
|
|
|
|
s)
|
|
|
|
varRepoSlug=$OPTARG
|
|
|
|
echo "REPO SLUG: $varRepoSlug" >&2
|
|
|
|
;;
|
|
|
|
p)
|
|
|
|
varPrerelease=$OPTARG
|
|
|
|
echo "PRERELEASE: $varPrerelease" >&2
|
|
|
|
;;
|
|
|
|
f)
|
|
|
|
varAssets=$OPTARG
|
|
|
|
echo "ASSETS: $varAssets" >&2
|
|
|
|
;;
|
|
|
|
d)
|
|
|
|
varAssetsDir=$OPTARG
|
|
|
|
echo "ASSETS DIR: $varAssetsDir" >&2
|
|
|
|
;;
|
|
|
|
\?)
|
|
|
|
echo "Invalid option: -$OPTARG" >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
:)
|
|
|
|
echo "Option -$OPTARG requires an argument." >&2
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
# use TravisCI env as default, if available
|
|
|
|
if [ -z $varTagName ] && [ ! -z $TRAVIS_TAG ]; then
|
|
|
|
varTagName=$TRAVIS_TAG
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ -z $varTagName ]; then
|
|
|
|
echo "No tag name available => aborting"
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
#Check tag name for release/prerelease (prerelease tag contains '_RC' as for release-candidate. case-insensitive)
|
|
|
|
shopt -s nocasematch
|
|
|
|
if [ -z $varPrerelease ]; then
|
|
|
|
if [[ $varTagName == *-RC* ]]; then
|
|
|
|
varPrerelease=true
|
|
|
|
else
|
|
|
|
varPrerelease=false
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
shopt -u nocasematch
|
|
|
|
|
|
|
|
#
|
|
|
|
# Prepare Markdown release notes:
|
|
|
|
#################################
|
|
|
|
#
|
|
|
|
# - tag's description:
|
|
|
|
# ignore first 3 lines - commiter, tagname, blank
|
|
|
|
# first line of message: heading
|
|
|
|
# other lines: converted to bullets
|
|
|
|
# empty lines ignored
|
|
|
|
# if '* ' found as a first char pair, it's converted to '- ' to keep bulleting unified
|
2018-07-16 20:47:30 +02:00
|
|
|
echo
|
2018-07-04 15:46:46 +02:00
|
|
|
echo Preparing release notes
|
|
|
|
echo -----------------------
|
|
|
|
echo "Tag's message:"
|
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
relNotesRaw=`git show -s --format=%b $varTagName`
|
|
|
|
readarray -t msgArray <<<"$relNotesRaw"
|
|
|
|
|
|
|
|
arrLen=${#msgArray[@]}
|
|
|
|
if [ $arrLen > 3 ]; then
|
|
|
|
ind=3
|
|
|
|
while [ $ind -lt $arrLen ]; do
|
|
|
|
if [ $ind -eq 3 ]; then
|
2018-07-16 20:47:30 +02:00
|
|
|
releaseNotes="#### ${msgArray[ind]}"
|
|
|
|
releaseNotes+=$'\r\n'
|
2018-06-27 09:01:06 +02:00
|
|
|
else
|
|
|
|
oneLine="$(echo -e "${msgArray[ind]}" | sed -e 's/^[[:space:]]*//')"
|
|
|
|
|
|
|
|
if [ ${#oneLine} -gt 0 ]; then
|
|
|
|
if [ "${oneLine:0:2}" == "* " ]; then oneLine=$(echo ${oneLine/\*/-}); fi
|
|
|
|
if [ "${oneLine:0:2}" != "- " ]; then releaseNotes+="- "; fi
|
2018-07-16 20:47:30 +02:00
|
|
|
releaseNotes+="$oneLine"
|
|
|
|
releaseNotes+=$'\r\n'
|
2018-07-04 15:46:46 +02:00
|
|
|
|
|
|
|
#debug output
|
|
|
|
echo " ${oneLine}"
|
2018-06-27 09:01:06 +02:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
let ind=$ind+1
|
|
|
|
done
|
|
|
|
else
|
2018-07-16 20:47:30 +02:00
|
|
|
releaseNotes="#### Release of $varTagName"
|
|
|
|
releaseNotes+=$'\r\n'
|
2018-07-04 15:46:46 +02:00
|
|
|
|
|
|
|
#debug output
|
|
|
|
echo " Release of $varTagName"
|
2018-06-27 09:01:06 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
# - list of commits (commits.txt must exit in the output dir)
|
|
|
|
commitFile=$varAssetsDir/commits.txt
|
2018-07-16 20:47:30 +02:00
|
|
|
if [ -s "$commitFile" ]; then
|
2018-06-27 09:01:06 +02:00
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
releaseNotes+=$'\r\n##### Commits\r\n'
|
2018-06-27 09:01:06 +02:00
|
|
|
|
2018-07-04 15:46:46 +02:00
|
|
|
echo
|
|
|
|
echo "Commits:"
|
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
IFS=$'\n'
|
|
|
|
for next in `cat $commitFile`
|
|
|
|
do
|
|
|
|
IFS=' ' read -r commitId commitMsg <<< "$next"
|
2018-07-16 20:47:30 +02:00
|
|
|
commitLine="- [$commitId](https://github.com/$varRepoSlug/commit/$commitId) $commitMsg"
|
|
|
|
echo " $commitLine"
|
2018-07-04 15:46:46 +02:00
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
releaseNotes+="$commitLine"
|
|
|
|
releaseNotes+=$'\r\n'
|
2018-06-27 09:01:06 +02:00
|
|
|
done
|
|
|
|
rm -f $commitFile
|
|
|
|
fi
|
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
# Check possibly existing release for current tag
|
2018-07-16 20:47:30 +02:00
|
|
|
echo
|
|
|
|
echo "Processing GitHub release record for $varTagName:"
|
|
|
|
echo "-------------------------------------------------"
|
|
|
|
|
|
|
|
echo " - check $varTagName possible existence..."
|
|
|
|
|
|
|
|
# (eg build invoked by Create New Release GHUI button -> GH default release pack created immediately including default assests)
|
2018-06-29 00:54:26 +02:00
|
|
|
HTTP_RESPONSE=$(curl -L --silent --write-out "HTTPSTATUS:%{http_code}" https://api.github.com/repos/$varRepoSlug/releases/tags/$varTagName?access_token=$varAccessToken)
|
2018-07-16 20:47:30 +02:00
|
|
|
if [ $? -ne 0 ]; then echo "FAILED: $? => aborting"; exit 1; fi
|
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
HTTP_BODY=$(echo $HTTP_RESPONSE | sed -e 's/HTTPSTATUS\:.*//g')
|
|
|
|
HTTP_STATUS=$(echo $HTTP_RESPONSE | tr -d '\n' | sed -e 's/.*HTTPSTATUS://')
|
2018-07-16 20:47:30 +02:00
|
|
|
echo " ---> GitHub server HTTP response: $HTTP_STATUS"
|
2018-06-29 00:54:26 +02:00
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
# if the release exists, append/update recent files to its assets vector
|
|
|
|
if [ $HTTP_STATUS -eq 200 ]; then
|
|
|
|
releaseId=$(echo $HTTP_BODY | jq -r '.id')
|
2018-06-29 00:54:26 +02:00
|
|
|
echo " - $varTagName release found (id $releaseId)"
|
2018-07-04 15:46:46 +02:00
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
#Merge release notes and overwrite pre-release flag. all other attributes remain unchanged:
|
|
|
|
|
|
|
|
# 1. take existing notes from server (added by release creator)
|
2018-07-04 15:46:46 +02:00
|
|
|
releaseNotesGH=$(echo $HTTP_BODY | jq -r '.body')
|
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
# - strip possibly trailing CR
|
|
|
|
if [ "${releaseNotesGH: -1}" == $'\r' ]; then
|
|
|
|
releaseNotesTemp="${releaseNotesGH:0:-1}"
|
|
|
|
else
|
|
|
|
releaseNotesTemp="$releaseNotesGH"
|
|
|
|
fi
|
|
|
|
# - add CRLF to make relnotes consistent for JSON encoding
|
|
|
|
releaseNotesTemp+=$'\r\n'
|
|
|
|
|
|
|
|
# 2. #append generated relnotes (usually commit oneliners)
|
|
|
|
releaseNotes="$releaseNotesTemp$releaseNotes"
|
|
|
|
|
|
|
|
# 3. JSON-encode whole string for GH API transfer
|
|
|
|
releaseNotes=$(json_escape "$releaseNotes")
|
|
|
|
|
|
|
|
# 4. remove extra quotes returned by python (dummy but whatever)
|
|
|
|
releaseNotes=${releaseNotes:1:-1}
|
|
|
|
|
|
|
|
#Update current GH release record
|
|
|
|
echo " - updating release notes and pre-release flag:"
|
2018-07-04 15:46:46 +02:00
|
|
|
|
|
|
|
curlData="{\"body\": \"$releaseNotes\",\"prerelease\": $varPrerelease}"
|
2018-07-16 20:47:30 +02:00
|
|
|
echo " <data.begin>$curlData<data.end>"
|
|
|
|
echo
|
|
|
|
#echo "DEBUG: curl --data \"$curlData\" https://api.github.com/repos/$varRepoSlug/releases/$releaseId?access_token=$varAccessToken"
|
2018-07-04 15:46:46 +02:00
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
curl --data "$curlData" https://api.github.com/repos/$varRepoSlug/releases/$releaseId?access_token=$varAccessToken
|
2018-07-04 15:46:46 +02:00
|
|
|
if [ $? -ne 0 ]; then echo "FAILED: $? => aborting"; exit 1; fi
|
|
|
|
|
2018-07-16 20:47:30 +02:00
|
|
|
echo " - $varTagName release record successfully updated"
|
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
#... or create a new release record
|
|
|
|
else
|
2018-07-16 20:47:30 +02:00
|
|
|
releaseNotes=$(json_escape "$releaseNotes")
|
|
|
|
releaseNotes=${releaseNotes:1:-1}
|
|
|
|
|
|
|
|
echo " - release $varTagName not found, creating a new record:"
|
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
curlData="{\"tag_name\": \"$varTagName\",\"target_commitish\": \"master\",\"name\": \"v$varTagName\",\"body\": \"$releaseNotes\",\"draft\": false,\"prerelease\": $varPrerelease}"
|
2018-07-16 20:47:30 +02:00
|
|
|
echo " <data.begin>$curlData<data.end>"
|
|
|
|
|
2018-06-28 13:58:01 +02:00
|
|
|
#echo "DEBUG: curl --data \"${curlData}\" https://api.github.com/repos/${varRepoSlug}/releases?access_token=$varAccessToken | jq -r '.id'"
|
|
|
|
releaseId=$(curl --data "$curlData" https://api.github.com/repos/$varRepoSlug/releases?access_token=$varAccessToken | jq -r '.id')
|
2018-07-04 15:46:46 +02:00
|
|
|
if [ $? -ne 0 ]; then echo "FAILED: $? => aborting"; exit 1; fi
|
2018-07-16 20:47:30 +02:00
|
|
|
|
|
|
|
echo " - $varTagName release record successfully created (id $releaseId)"
|
2018-06-28 13:58:01 +02:00
|
|
|
fi
|
2018-06-27 09:01:06 +02:00
|
|
|
|
|
|
|
# Assets defined by dir contents
|
|
|
|
if [ ! -z $varAssetsDir ]; then
|
|
|
|
varAssetsTemp=$(ls -p $varAssetsDir | grep -v / | tr '\n' ';')
|
|
|
|
for item in $(echo $varAssetsTemp | tr ";" "\n")
|
|
|
|
do
|
|
|
|
varAssets+=$varAssetsDir/$item;
|
|
|
|
varAssets+=';'
|
|
|
|
done
|
|
|
|
fi
|
|
|
|
|
|
|
|
#Upload additional assets
|
|
|
|
if [ ! -z $varAssets ]; then
|
2018-07-04 15:46:46 +02:00
|
|
|
echo
|
|
|
|
echo "Uploading assets:"
|
|
|
|
echo "-----------------"
|
2018-07-16 20:47:30 +02:00
|
|
|
echo " Files to upload:"
|
|
|
|
echo " $varAssets"
|
|
|
|
echo
|
2018-07-04 15:46:46 +02:00
|
|
|
|
2018-06-27 09:01:06 +02:00
|
|
|
curlAuth="Authorization: token $varAccessToken"
|
|
|
|
for filename in $(echo $varAssets | tr ";" "\n")
|
|
|
|
do
|
2018-07-16 20:47:30 +02:00
|
|
|
echo " - ${filename}:"
|
2018-07-04 15:46:46 +02:00
|
|
|
curl -X POST -sH "$curlAuth" -H "Content-Type: application/octet-stream" --data-binary @"$filename" https://uploads.github.com/repos/$varRepoSlug/releases/$releaseId/assets?name=$(basename $filename)
|
|
|
|
|
|
|
|
if [ $? -ne 0 ]; then echo "FAILED: $? => aborting"; exit 1; fi
|
|
|
|
|
|
|
|
echo
|
|
|
|
echo "OK"
|
2018-06-27 09:01:06 +02:00
|
|
|
echo
|
|
|
|
|
|
|
|
done
|
|
|
|
fi
|