Skip to content

Commit cd65c45

Browse files
committed
fix: simplify discord release post workflow
1 parent 93f366d commit cd65c45

1 file changed

Lines changed: 19 additions & 48 deletions

File tree

.github/workflows/discord-test.yml

Lines changed: 19 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ jobs:
6868
code="${out[0]}"
6969
body="$(printf '%s\n' "${out[@]:1}")"
7070
echo "probe $label: $code" >&2
71-
{ printf '%s\n' "$body" | head -c 300; echo; } >&2
71+
{ printf '%s\n' "$body" | head -c 200; echo; } >&2
7272
if [ "$code" = "200" ]; then
7373
echo "selected auth: $label" >&2
7474
printf '%s' "$auth"
@@ -84,53 +84,24 @@ jobs:
8484
code="${out[0]}"
8585
body="$(printf '%s\n' "${out[@]:1}")"
8686
echo "channel lookup: $code"
87-
printf '%s\n' "$body" | head -c 500; echo
87+
printf '%s\n' "$body" | head -c 300; echo
8888
[ "$code" = "200" ] || { echo "could not access target channel"; exit 1; }
8989
90-
export RELEASE_JSON="$(gh release view "$RELEASE_TAG" --repo Latitudes-Dev/shuvcode --json tagName,body,url)"
91-
python - <<'PY' > /tmp/discord-msgs.txt
92-
import json
93-
import os
90+
rel=$(gh release view "$RELEASE_TAG" --repo Latitudes-Dev/shuvcode --json tagName,body,url)
91+
tag=$(jq -r .tagName <<< "$rel")
92+
url=$(jq -r .url <<< "$rel")
93+
notes=$(jq -r .body <<< "$rel")
94+
npm="https://www.npmjs.com/package/shuvcode/v/${tag#v}"
95+
head="**shuvcode ${tag}** release notes"
96+
foot="\n\n[GitHub Release](<${url}>) | [npm](<${npm}>)"
97+
room=$((1900 - ${#head} - ${#foot} - 4))
98+
body=$(printf '%s' "$notes" | head -c "$room")
99+
msg=$(printf '%b' "$head\n\n$body$foot")
100+
payload=$(jq -Rn --arg content "$msg" '{content:$content}')
94101
95-
data = json.loads(os.environ["RELEASE_JSON"])
96-
tag = data["tagName"]
97-
body = data.get("body") or ""
98-
url = data["url"]
99-
npm = f"https://www.npmjs.com/package/shuvcode/v/{tag.removeprefix('v')}"
100-
head = f"**shuvcode {tag}** release notes\n\n[GitHub Release](<{url}>) | [npm](<{npm}>)"
101-
parts = [head]
102-
buf = ""
103-
limit = 1900
104-
105-
for line in body.splitlines():
106-
chunk = line if line else ""
107-
add = chunk + "\n"
108-
if len(buf) + len(add) > limit:
109-
if buf.strip():
110-
parts.append(buf.rstrip())
111-
buf = add
112-
continue
113-
buf += add
114-
115-
if buf.strip():
116-
parts.append(buf.rstrip())
117-
118-
with open('/tmp/discord-msgs.txt', 'w', encoding='utf-8') as f:
119-
for part in parts:
120-
f.write(part)
121-
f.write('\0')
122-
PY
123-
124-
while IFS= read -r -d '' msg; do
125-
payload=$(python - <<'PY' "$msg"
126-
import json, sys
127-
print(json.dumps({"content": sys.argv[1]}))
128-
PY
129-
)
130-
mapfile -t out < <(call "$auth" "/channels/$DISCORD_CHANNEL_ID/messages" POST "$payload")
131-
code="${out[0]}"
132-
body="$(printf '%s\n' "${out[@]:1}")"
133-
echo "post: $code"
134-
printf '%s\n' "$body" | head -c 500; echo
135-
[[ "$code" = "200" || "$code" = "201" ]] || { echo "post failed"; exit 1; }
136-
done < /tmp/discord-msgs.txt
102+
mapfile -t out < <(call "$auth" "/channels/$DISCORD_CHANNEL_ID/messages" POST "$payload")
103+
code="${out[0]}"
104+
body="$(printf '%s\n' "${out[@]:1}")"
105+
echo "post: $code"
106+
printf '%s\n' "$body" | head -c 300; echo
107+
[[ "$code" = "200" || "$code" = "201" ]] || { echo "post failed"; exit 1; }

0 commit comments

Comments
 (0)