Skip to content

Commit e00e05f

Browse files
committed
.github/scripts/*.sh: fix handling \r\n in JSON
In dash echo (ubuntu-22 runner) \n and \r are interpreted which causes fail of subsequent jq with error: ``` parse error: Invalid string: control characters from U+0000 through U+001F must be escaped at line 258, column 394 ``` POSIX actually requires that for XSI-conformant systems.
1 parent ac80c85 commit e00e05f

File tree

3 files changed

+11
-12
lines changed

3 files changed

+11
-12
lines changed

.github/scripts/delete-asset.sh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
TAG_NAME=${1?}
66
PATTERN=${2?}
77
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/tags/$TAG_NAME $GITHUB_TOKEN)
8-
RELEASE_ID=$(echo "$JSON" | jq -r '.id')
8+
RELEASE_ID=$(printf '%s' "$JSON" | jq -r '.id')
99
JSON=$(fetch_json https://api.github.com/repos/$GITHUB_REPOSITORY/releases/$RELEASE_ID/assets $GITHUB_TOKEN array)
10-
LEN=$(echo "$JSON" | jq length)
10+
LEN=$(printf '%s' "$JSON" | jq length)
1111
for n in `seq 0 $(($LEN-1))`; do
12-
NAME=$(echo "$JSON" | jq -r '.['$n'].name')
12+
NAME=$(printf '%s' "$JSON" | jq -r '.['$n'].name')
1313
if expr "$NAME" : "$PATTERN$"; then
14-
ID=$(echo "$JSON" | jq '.['$n'].id')
14+
ID=$(printf '%s' "$JSON" | jq '.['$n'].id')
1515
TMPNAME=$(mktemp)
1616
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)
1717
JSON=$(cat $TMPNAME)

.github/scripts/json-common.sh

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
11
check_errors() {
2-
printf '%s' "$1" | busybox nc x0.at 9999
32
TYPE=$(printf '%s' "$1" | jq -r type)
43
if [ "$TYPE" != object ]; then
54
return
65
fi
7-
ERRORS=$(echo "$1" | jq -r '.errors')
6+
ERRORS=$(printf '%s' "$1" | jq -r '.errors')
87
if [ "$ERRORS" != null ]; then
98
echo $ERRORS >&2
109
exit 1
1110
fi
1211
}
1312

1413
check_type() {
15-
TYPE=$(echo "$1" | jq -r type)
14+
TYPE=$(printf '%s' "$1" | jq -r type)
1615
if [ "$TYPE" != "$2" ]; then
1716
echo "Wrong JSON type - expected $2, got $TYPE" >&2
18-
echo "JSON: $JSON" >&2
17+
printf '%s' "JSON: $JSON" >&2
1918
exit 1
2019
fi
2120
}
@@ -31,13 +30,13 @@ fetch_json() {
3130
rm $TMPNAM
3231
if [ $STATUS -ne 200 ]; then
3332
echo "HTTP error code $STATUS" >&2
34-
echo "JSON: $JSON" >&2
33+
printf '%s' "JSON: $JSON" >&2
3534
fi
3635
check_errors "$JSON"
3736
if [ -n ${3-""} ]; then
3837
check_type "$JSON" $3
3938
fi
40-
echo "$JSON"
39+
printf '%s' "$JSON"
4140
}
4241

4342
## @brief Checks HTTP error code for success
@@ -47,7 +46,7 @@ check_status() {
4746
if [ $1 -lt 200 -o $1 -ge 300 ]; then
4847
echo "Wrong response status $STATUS!" >&2
4948
if [ -n ${2-""} ]; then
50-
echo "JSON: $JSON" >&2
49+
printf '%s' "JSON: $JSON" >&2
5150
fi
5251
exit 1
5352
fi

.github/scripts/upload-asset.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CONTENT_TYPE=${3?}
88
LABEL=$(echo "${4?}" | sed 's/ /%20/g')
99

1010
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/{.*}//")
11+
UPLOAD_URL=$(printf '%s' "$JSON" | jq -r .upload_url | sed "s/{.*}//")
1212

1313
TMPNAME=$(mktemp)
1414
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)

0 commit comments

Comments
 (0)