Skip to content

Commit 2b9b642

Browse files
committed
GH scripts: import back from GPUJPEG
import improvements from GPUJPEG - summary: - LABEL - replace ' ' with %20 (will get to URL) - use while instead of for..seq - cat json file directly (avoid interpretting \n and \r that XSI-compilant echo, as in dash, does) - cat really the $json file, don't print its name - wrap long lines + early return in while-loop The rewrite of `for n in \`seq` may be perhaps a bit controversal (with while is less readable) but technically POSIX doesn't specify the seq comand.
1 parent 227c6d1 commit 2b9b642

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

.github/scripts/delete-asset.sh

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,26 @@ DIR=$(dirname "$0")
66

77
TAG_NAME=${1?}
88
PATTERN=$(basename "${2?}")
9-
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME" "$GITHUB_TOKEN")
9+
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/\
10+
tags/$TAG_NAME" "$GITHUB_TOKEN")
1011
RELEASE_ID=$(jq -r '.id' "$JSON")
1112
rm "$JSON"
12-
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets" "$GITHUB_TOKEN" array)
13+
JSON=$(fetch_json "https://api.github.com/repos/$GITHUB_REPOSITORY/releases/\
14+
$RELEASE_ID/assets" "$GITHUB_TOKEN" array)
1315
LEN=$(jq length "$JSON")
14-
for n in $(seq 0 $((LEN-1))); do
16+
n=-1; while n=$((n + 1)); [ "$n" -lt "$LEN" ]; do
1517
NAME=$(jq -r ".[$n].name" "$JSON")
16-
if expr "$NAME" : "$PATTERN$"; then
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"
18+
if ! expr "$NAME" : "$PATTERN$"; then
19+
continue
2320
fi
21+
ID=$(jq ".[$n].id" "$JSON")
22+
JSON2=$(mktemp)
23+
STATUS=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -X DELETE \
24+
"https://api.github.com/repos/$GITHUB_REPOSITORY/releases/assets/$ID" \
25+
-w '%{http_code}' -o "$JSON2")
26+
check_errors "$JSON2"
27+
check_status "$STATUS"
28+
rm "$JSON2"
2429
done
2530
rm "$JSON"
2631

.github/scripts/json-common.sh

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ check_type() {
1818
TYPE=$(jq -r type "$1")
1919
if [ "$TYPE" != "$2" ]; then
2020
echo "Wrong JSON type - expected $2, got $TYPE" >&2
21-
json=$(cat "$1")
22-
echo "JSON: $json" >&2
21+
echo 'JSON:' >&2
22+
cat "$1" >&2
2323
exit 1
2424
fi
2525
}
2626

27-
## @brief Returns json for given URL and authorization token while checking errors
27+
## @brief Returns json file for given URL and authorization token while checking errors
2828
## @param $1 URL
2929
## @param $2 GITHUB_TOKEN (optional)
3030
## @param $3 requested type (optional)
@@ -41,7 +41,8 @@ fetch_json() {
4141
status=$(curl -sS "$@" -X GET "$url" -w "%{http_code}" -o "$json")
4242
if ! is_int "$status" || [ "$status" -ne 200 ]; then
4343
echo "HTTP error code $status" >&2
44-
echo "JSON: $json" >&2
44+
echo "JSON:" >&2
45+
cat "$json" >&2
4546
fi
4647
check_errors "$json"
4748
if [ -n "$req_type" ]; then
@@ -57,8 +58,8 @@ check_status() {
5758
if ! is_int "$1" || [ "$1" -lt 200 ] || [ "$1" -ge 300 ]; then
5859
echo "Wrong response status $1!" >&2
5960
if [ -n "${2-}" ]; then
60-
json=$(cat "$2")
61-
echo "JSON: $json" >&2
61+
echo "JSON:" >&2
62+
cat "$2"
6263
fi
6364
exit 1
6465
fi

.github/scripts/upload-asset.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ TAG_NAME=${1?}
88
FILE=${2?}
99
FILENAME=$(basename "${2?}")
1010
CONTENT_TYPE=${3?}
11-
LABEL=${4?}
11+
LABEL=$(echo "${4?}" | sed 's/ /%20/g')
1212

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

1618
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")
19+
STATUS=$(curl -S -H "Authorization: token $GITHUB_TOKEN" -H\
20+
"Content-Type: $CONTENT_TYPE" -X POST "$UPLOAD_URL?name=$FILENAME&label=$LABEL"\
21+
-T "$FILE" -w '%{http_code}' -o "$JSON")
1822
check_errors "$JSON"
1923
check_status "$STATUS"
2024
rm "$JSON"

0 commit comments

Comments
 (0)