Skip to content

Commit 9f9d2db

Browse files
committed
workflows: Add support for chat message when issue is closed
1 parent cc7be1f commit 9f9d2db

File tree

1 file changed

+40
-27
lines changed

1 file changed

+40
-27
lines changed
Lines changed: 40 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
name: Notify Chat on new issue
1+
name: Notify Chat on issue open/close
22
on:
33
issues:
4-
types: [opened]
4+
types: [opened, closed]
55

66
permissions:
77
issues: read
@@ -10,48 +10,61 @@ jobs:
1010
notify-telegram:
1111
runs-on: ubuntu-latest
1212
steps:
13+
- name: Read event JSON
14+
id: event
15+
run: |
16+
echo "event_path=${GITHUB_EVENT_PATH}" >> "$GITHUB_OUTPUT"
17+
1318
- name: Build Telegram message
1419
id: build
20+
env:
21+
EVENT_PATH: ${{ steps.event.outputs.event_path }}
1522
run: |
1623
set -euo pipefail
17-
# Inputs from event JSON
18-
issue_number="${{ github.event.issue.number }}"
19-
issue_title="${{ github.event.issue.title }}"
20-
issue_body="${{ github.event.issue.body || '' }}"
21-
issue_user="${{ github.event.issue.user.login }}"
22-
issue_url="${{ github.event.issue.html_url }}"
23-
labels_array="${{ toJson(github.event.issue.labels) }}"
24-
25-
# Extract label names (jq not available by default). Use GitHub-provided string if simple:
26-
# Build comma-separated labels
27-
labels=""
28-
if [ "${labels_array}" != "[]" ]; then
29-
# labels_array is JSON like [{"id":..., "name":"bug",...},...]
30-
# Use simple parsing to extract "name" fields
31-
labels=$(printf '%s' "${labels_array}" | sed -n 's/.*"name":"\([^"]*\)".*/\1/p' | paste -sd ", " -)
32-
fi
33-
if [ -z "$labels" ]; then
24+
if command -v jq >/dev/null 2>&1; then
25+
number=$(jq -r .issue.number "$EVENT_PATH")
26+
title=$(jq -r .issue.title "$EVENT_PATH")
27+
body_text=$(jq -r '.issue.body // ""' "$EVENT_PATH")
28+
user=$(jq -r .issue.user.login "$EVENT_PATH")
29+
url=$(jq -r .issue.html_url "$EVENT_PATH")
30+
labels=$(jq -r '[.issue.labels[]?.name] | join(", ")' "$EVENT_PATH")
31+
action=$(jq -r .action "$EVENT_PATH")
32+
actor=$(jq -r '.sender.login // ""' "$EVENT_PATH")
33+
else
34+
number="${{ github.event.issue.number }}"
35+
title="${{ github.event.issue.title }}"
36+
body_text="${{ github.event.issue.body || '' }}"
37+
user="${{ github.event.issue.user.login }}"
38+
url="${{ github.event.issue.html_url }}"
3439
labels="(none)"
40+
action="${{ github.event.action }}"
41+
actor="${{ github.actor }}"
3542
fi
3643
37-
# Truncate body
3844
maxlen=800
39-
if [ "${#issue_body}" -gt "$maxlen" ]; then
40-
issue_body="${issue_body:0:$maxlen}...(truncated)"
45+
if [ "${#body_text}" -gt "$maxlen" ]; then
46+
body_text="${body_text:0:$maxlen}...(truncated)"
4147
fi
4248
43-
# Escape HTML for Telegram parse_mode=HTML
4449
esc() {
4550
printf '%s' "$1" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'
4651
}
4752
4853
repo="${GITHUB_REPOSITORY}"
4954
repo_url="https://github.com/${repo}"
5055
repo_link="<a href=\"${repo_url}\">${repo}</a>"
51-
issue_link="<a href=\"${issue_url}\">#${issue_number} ${esc "${issue_title}"}</a>"
56+
issue_link="<a href=\"${url}\">#${number} ${esc "$title"}</a>"
57+
58+
if [ "$action" = "opened" ]; then
59+
msg_title="New issue opened"
60+
body="Repo: ${repo_link}\nIssue: ${issue_link}\nAuthor: ${esc "$user"}\nLabels: ${esc "${labels:-(none)}"}\n\n${esc "$body_text"}"
61+
else
62+
msg_title="Issue closed"
63+
body="Repo: ${repo_link}\nIssue: ${issue_link}\nClosed by: ${esc "$actor"}\nLabels: ${esc "${labels:-(none)}"}\n\n${esc "$body_text"}"
64+
fi
5265
53-
body="Repo: ${repo_link}%0AIssue: ${issue_link}%0AAuthor: ${esc "${issue_user}"}%0ALabels: ${esc "${labels}"}%0A%0A${esc "${issue_body}"}"
54-
echo "body=$body" >> "$GITHUB_OUTPUT"
66+
final="<b>$(esc "$msg_title")</b>\n${body}"
67+
echo "body=${final}" >> "$GITHUB_OUTPUT"
5568
5669
- name: Send Telegram notification
5770
env:
@@ -65,5 +78,5 @@ jobs:
6578
body="${{ steps.build.outputs.body }}"
6679
curl -sS --fail -X POST "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \
6780
-d "chat_id=${TELEGRAM_CHAT_ID}" \
68-
-d "text=${body}" \
81+
--data-urlencode "text=${body}" \
6982
-d "parse_mode=HTML"

0 commit comments

Comments
 (0)