Skip to content

Commit 3d70eb5

Browse files
committed
sync GH scripts with UltraGrid
but keep our change with replace ' ' with %20 in LABEL (not in UG)
1 parent 1462cd0 commit 3d70eb5

File tree

4 files changed

+76
-47
lines changed

4 files changed

+76
-47
lines changed

.github/scripts/delete-asset.sh

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
#!/bin/sh -eux
22

3-
. $(dirname $0)/json-common.sh
3+
DIR=$(dirname "$0")
4+
# shellcheck source=/dev/null
5+
. "$DIR/json-common.sh"
46

57
TAG_NAME=${1?}
6-
PATTERN=${2?}
7-
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME $GITHUB_TOKEN)
8-
RELEASE_ID=$(echo "$JSON" | jq -r '.id')
9-
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets $GITHUB_TOKEN array)
10-
LEN=$(echo "$JSON" | jq length)
11-
for n in `seq 0 $(($LEN-1))`; do
12-
NAME=$(echo "$JSON" | jq -r '.['$n'].name')
8+
PATTERN=$(basename "${2?}")
9+
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME" "$GITHUB_TOKEN")
10+
RELEASE_ID=$(jq -r '.id' "$JSON")
11+
rm "$JSON"
12+
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets" "$GITHUB_TOKEN" array)
13+
LEN=$(jq length "$JSON")
14+
for n in $(seq 0 $((LEN-1))); do
15+
NAME=$(jq -r ".[$n].name" "$JSON")
1316
if expr "$NAME" : "$PATTERN$"; then
14-
ID=$(echo "$JSON" | jq '.['$n'].id')
15-
TMPNAME=$(mktemp)
16-
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)
17-
JSON=$(cat $TMPNAME)
18-
rm $TMPNAME
19-
check_errors "$JSON"
20-
check_status $STATUS
17+
ID=$(jq ".[$n].id" "$JSON")
18+
JSON2=$(mktemp)
19+
STATUS=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X DELETE "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/assets/$ID" -w '%{http_code}' -o "$JSON2")
20+
check_errors "$JSON2"
21+
check_status "$STATUS"
22+
rm "$JSON2"
2123
fi
2224
done
25+
rm "$JSON"
2326

.github/scripts/json-common.sh

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,64 @@
1+
# shellcheck shell=sh
2+
is_int() { test "$@" -eq "$@"; }
3+
14
check_errors() {
2-
TYPE=$(echo "$1" | jq -r type)
5+
6+
TYPE=$(jq -r type "$1")
37
if [ "$TYPE" != object ]; then
48
return
59
fi
6-
ERRORS=$(echo "$1" | jq -r '.errors')
10+
ERRORS=$(jq -r '.errors' "$1")
711
if [ "$ERRORS" != null ]; then
8-
echo $ERRORS >&2
12+
echo "$ERRORS" >&2
913
exit 1
1014
fi
1115
}
1216

1317
check_type() {
14-
TYPE=$(echo "$1" | jq -r type)
18+
TYPE=$(jq -r type "$1")
1519
if [ "$TYPE" != "$2" ]; then
1620
echo "Wrong JSON type - expected $2, got $TYPE" >&2
17-
echo "JSON: $JSON" >&2
21+
json=$(cat "$1")
22+
echo "JSON: $json" >&2
1823
exit 1
1924
fi
2025
}
2126

2227
## @brief Returns json for given URL and authorization token while checking errors
2328
## @param $1 URL
24-
## @param $2 GITHUB_TOKEN
29+
## @param $2 GITHUB_TOKEN (optional)
2530
## @param $3 requested type (optional)
2631
fetch_json() {
27-
TMPNAM=$(mktemp)
28-
STATUS=$(curl -S -H "Authorization: token ${2?GitHub token is required}" -X GET ${1?URL is required} -w "%{http_code}" -o $TMPNAM)
29-
JSON=$(cat $TMPNAM)
30-
rm $TMPNAM
31-
if [ $STATUS -ne 200 ]; then
32-
echo "HTTP error code $STATUS" >&2
33-
echo "JSON: $JSON" >&2
32+
json=$(mktemp)
33+
url=${1?URL is required}
34+
github_token=${2-}
35+
req_type=${3-}
36+
if [ -n "$github_token" ]; then
37+
set -- -H "Authorization: token $github_token"
38+
else
39+
set --
40+
fi
41+
status=$(curl -sS "$@" -X GET "$url" -w "%{http_code}" -o "$json")
42+
if ! is_int "$status" || [ "$status" -ne 200 ]; then
43+
echo "HTTP error code $status" >&2
44+
echo "JSON: $json" >&2
3445
fi
35-
check_errors "$JSON"
36-
if [ -n ${3-""} ]; then
37-
check_type "$JSON" $3
46+
check_errors "$json"
47+
if [ -n "$req_type" ]; then
48+
check_type "$json" "$req_type"
3849
fi
39-
echo "$JSON"
50+
echo "$json"
4051
}
4152

4253
## @brief Checks HTTP error code for success
4354
## @param $1 returned HTTP status code
4455
## @param $2 (optional) returned JSON (to be printed in case of error)
4556
check_status() {
46-
if [ $1 -lt 200 -o $1 -ge 300 ]; then
47-
echo "Wrong response status $STATUS!" >&2
48-
if [ -n ${2-""} ]; then
49-
echo "JSON: $JSON" >&2
57+
if ! is_int "$1" || [ "$1" -lt 200 ] || [ "$1" -ge 300 ]; then
58+
echo "Wrong response status $1!" >&2
59+
if [ -n "${2-}" ]; then
60+
json=$(cat "$2")
61+
echo "JSON: $json" >&2
5062
fi
5163
exit 1
5264
fi

.github/scripts/replace-asset.sh

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
11
#!/bin/sh -eux
22

3-
DIR=$(dirname $0)
3+
set +e # pattern matching may fail
4+
# If first parameter 2 parameters is GITHUB_REPOSOTIRY and GITHUB_TOKEN, those willbe used as an env var (used by the scripts below)
5+
if repository=$(expr "$1" : "GITHUB_REPOSITORY=\(.*\)"); then
6+
export GITHUB_REPOSITORY="$repository"
7+
shift
8+
fi
9+
if token=$(expr "$1" : "GITHUB_TOKEN=\(.*\)"); then
10+
export GITHUB_TOKEN="$token"
11+
shift
12+
fi
13+
set -e
414

5-
$DIR/delete-asset.sh "$@"
6-
$DIR/upload-asset.sh "$@"
15+
dir=$(dirname "$0")
16+
17+
"$dir/delete-asset.sh" "$@"
18+
"$dir/upload-asset.sh" "$@"
719

.github/scripts/upload-asset.sh

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
#!/bin/sh -eu
22

3-
. $(dirname $0)/json-common.sh
3+
dir=$(dirname "$0")
4+
# shellcheck source=/dev/null
5+
. "$dir/json-common.sh"
46

57
TAG_NAME=${1?}
68
FILE=${2?}
9+
FILENAME=$(basename "${2?}")
710
CONTENT_TYPE=${3?}
811
LABEL=$(echo "${4?}" | sed 's/ /%20/g')
912

10-
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME $GITHUB_TOKEN)
11-
UPLOAD_URL=$(echo "$JSON" | jq -r .upload_url | sed "s/{.*}//")
13+
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME" "$GITHUB_TOKEN")
14+
UPLOAD_URL=$(jq -r .upload_url "$JSON" | sed "s/{.*}//")
1215

13-
TMPNAME=$(mktemp)
14-
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)
15-
JSON=$(cat $TMPNAME)
16-
rm $TMPNAME
16+
JSON=$(mktemp)
17+
STATUS=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -H "Content-Type: $CONTENT_TYPE" -X POST "$UPLOAD_URL?name=$FILENAME&label=$LABEL" -T "$FILE" -w '%{http_code}' -o "$JSON")
1718
check_errors "$JSON"
18-
check_status $STATUS
19+
check_status "$STATUS"
20+
rm "$JSON"
1921

0 commit comments

Comments
 (0)