GitHub Workflows: check HTTP status codes

This commit is contained in:
Martin Pulec
2020-04-03 11:47:02 +02:00
parent aa8e7c5f7d
commit ee974c7764
3 changed files with 69 additions and 38 deletions

View File

@@ -1,39 +1,23 @@
#!/bin/sh -eux
check_errors() {
TYPE=$(echo "$1" | jq -r type)
if [ "$TYPE" != object ]; then
return
fi
ERRORS=$(echo "$1" | jq -r '.errors')
if [ "$ERRORS" != null ]; then
echo $ERRORS >&2
exit 1
fi
}
check_type() {
TYPE=$(echo "$1" | jq -r type)
if [ "$TYPE" != "$2" ]; then
return
fi
}
. $(dirname $0)/json-common.sh
TAG_NAME=${1?}
PATTERN=${2?}
JSON=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X GET https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME)
check_errors "$JSON"
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME $GITHUB_TOKEN)
RELEASE_ID=$(echo "$JSON" | jq -r '.id')
JSON=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X GET https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets)
check_errors "$JSON"
check_type "$JSON" array
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets $GITHUB_TOKEN array)
LEN=$(echo "$JSON" | jq length)
for n in `seq 0 $(($LEN-1))`; do
NAME=$(echo "$JSON" | jq '.['$n'].name')
if expr "$NAME" : "\"$PATTERN\"$"; then
ID=$(echo "$JSON" | jq '.['$n'].id')
JSON=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X DELETE "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/assets/$ID")
TMPNAME=$(mktemp)
STATUS=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X DELETE "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/assets/$ID" -w %{http_code} -o $TMPNAME)
JSON=$(cat $TMPNAME)
rm $TMPNAME
check_errors "$JSON"
check_status $STATUS
fi
done

54
.github/scripts/json-common.sh vendored Normal file
View File

@@ -0,0 +1,54 @@
check_errors() {
TYPE=$(echo "$1" | jq -r type)
if [ "$TYPE" != object ]; then
return
fi
ERRORS=$(echo "$1" | jq -r '.errors')
if [ "$ERRORS" != null ]; then
echo $ERRORS >&2
exit 1
fi
}
check_type() {
TYPE=$(echo "$1" | jq -r type)
if [ "$TYPE" != "$2" ]; then
echo "Wrong JSON type - expected $2, got $TYPE" >&2
echo "JSON: $JSON" >&2
exit 1
fi
}
## @brief Returns json for given URL and authorization token while checking errors
## @param $1 URL
## @param $2 GITHUB_TOKEN
## @param $3 requested type (optional)
fetch_json() {
TMPNAM=$(mktemp)
STATUS=$(curl -S -H "Authorization: token ${2?GitHub token is required}" -X GET ${1?URL is required} -w "%{http_code}" -o $TMPNAM)
JSON=$(cat $TMPNAM)
rm $TMPNAM
if [ $STATUS -ne 200 ]; then
echo "HTTP error code $STATUS" >&2
echo "JSON: $JSON" >&2
fi
check_errors "$JSON"
if [ -n ${3-""} ]; then
check_type "$JSON" $3
fi
echo "$JSON"
}
## @brief Checks HTTP error code for success
## @param $1 returned HTTP status code
## @param $2 (optional) returned JSON (to be printed in case of error)
check_status() {
if [ $1 -lt 200 -o $1 -ge 300 ]; then
echo "Wrong response status $STATUS!" >&2
if [ -n ${2-""} ]; then
echo "JSON: $JSON" >&2
fi
exit 1
fi
}

View File

@@ -1,26 +1,19 @@
#!/bin/sh -eu
check_errors() {
TYPE=$(echo "$1" | jq -r type)
if [ "$TYPE" != object ]; then
return
fi
ERRORS=$(echo "$1" | jq -r '.errors')
if [ "$ERRORS" != null ]; then
echo $ERRORS >&2
exit 1
fi
}
. $(dirname $0)/json-common.sh
TAG_NAME=${1?}
FILE=${2?}
CONTENT_TYPE=${3?}
LABEL=${4?}
JSON=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X GET https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME)
check_errors "$JSON"
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME $GITHUB_TOKEN)
UPLOAD_URL=$(echo "$JSON" | jq -r .upload_url | sed "s/{.*}//")
JSON=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: $CONTENT_TYPE" -X POST "$UPLOAD_URL?name=$FILE&label=$LABEL" -T $FILE)
TMPNAME=$(mktemp)
STATUS=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: $CONTENT_TYPE" -X POST "$UPLOAD_URL?name=$FILE&label=$LABEL" -T $FILE -w %{http_code} -o $TMPNAME)
JSON=$(cat $TMPNAME)
rm $TMPNAME
check_errors "$JSON"
check_status $STATUS