-
Notifications
You must be signed in to change notification settings - Fork 1
161 lines (132 loc) · 5.51 KB
/
Copy pathdiscord-notify.yml
File metadata and controls
161 lines (132 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# License: MIT (template code)
name: Discord Notify (safe) - AI_7-team
on:
push:
branches:
- main
- dev
pull_request:
branches:
- main
- dev
types: [opened, reopened, synchronize, closed, edited, ready_for_review]
jobs:
notify:
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Build Discord payload (safe embeds)
env:
GITHUB_EVENT_NAME: ${{ github.event_name }}
GITHUB_EVENT_PATH: ${{ github.event_path }}
REPO: ${{ github.repository }}
SERVER_URL: ${{ github.server_url }}
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
run: |
python - << 'PY' > payload.json
# License: MIT (template code)
import os, json, datetime
event_name = os.environ["GITHUB_EVENT_NAME"]
event_path = os.environ["GITHUB_EVENT_PATH"]
with open(event_path, "r", encoding="utf-8") as f:
ev = json.load(f)
repo = os.environ.get("REPO", "")
server_url = os.environ.get("SERVER_URL", "https://github.com")
run_url = os.environ.get("RUN_URL", "")
now_iso = datetime.datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
# Discord hard limits:
# title 256, description 4096, field.value 1024
def clip(s: str, n: int) -> str:
s = (s or "").strip()
if len(s) <= n:
return s
return s[: max(0, n - 1)] + "…"
def build(embed: dict) -> dict:
return {"embeds": [embed]}
def skip():
print(json.dumps({"__skip__": True}, ensure_ascii=False))
raise SystemExit(0)
# -------------------------
# PUSH main/dev
# -------------------------
if event_name == "push":
branch = (ev.get("ref","") or "").replace("refs/heads/","")
sender = (ev.get("sender") or {}).get("login") or "unknown"
compare = ev.get("compare") or f"{server_url}/{repo}"
commits = ev.get("commits") or []
# 너무 길어지면 400 터지니까 커밋 메시지 최소화
lines = []
for c in commits[:3]:
msg = (c.get("message") or "").splitlines()[0]
sha = (c.get("id") or "")[:7]
lines.append(f"• {clip(msg, 70)} `{sha}`")
more = len(commits) - 3
if more > 0:
lines.append(f"• … (+{more} more)")
desc = clip("\n".join(lines), 3500)
embed = {
"title": clip(f"{repo} · push → {branch}", 256),
"url": compare,
"description": desc,
"fields": [
{"name": "By", "value": clip(f"`{sender}`", 1024), "inline": True},
{"name": "Commits", "value": f"`{len(commits)}`", "inline": True},
],
"footer": {"text": clip(f"Actions · {run_url}", 2048)},
"timestamp": now_iso,
}
print(json.dumps(build(embed), ensure_ascii=False))
raise SystemExit(0)
# -------------------------
# PR main/dev
# -------------------------
if event_name == "pull_request":
action = ev.get("action","")
pr = ev.get("pull_request") or {}
number = pr.get("number") or ev.get("number")
title = pr.get("title") or ""
author = (pr.get("user") or {}).get("login") or "unknown"
base = ((pr.get("base") or {}).get("ref") or "")
head = ((pr.get("head") or {}).get("ref") or "")
url = pr.get("html_url") or f"{server_url}/{repo}"
merged = bool(pr.get("merged", False))
kind = "MERGED" if (action == "closed" and merged) else f"PR {action}"
body = clip(pr.get("body") or "", 300)
desc = f"**#{number}** {clip(title, 160)}"
if body:
desc += f"\n{body}"
desc = clip(desc, 3500)
embed = {
"title": clip(f"{repo} · {kind}", 256),
"url": url,
"description": desc,
"fields": [
{"name": "From", "value": clip(f"`{head}`", 1024), "inline": True},
{"name": "To", "value": clip(f"`{base}`", 1024), "inline": True},
{"name": "Author", "value": clip(f"`{author}`", 1024), "inline": True},
],
"footer": {"text": clip(f"Actions · {run_url}", 2048)},
"timestamp": now_iso,
}
print(json.dumps(build(embed), ensure_ascii=False))
raise SystemExit(0)
skip()
PY
- name: Send Discord (non-blocking)
continue-on-error: true
env:
DISCORD_WEBHOOK_URL: ${{ secrets.DISCORD_WEBHOOK_URL }}
run: |
python - << 'PY'
import json
with open("payload.json","r",encoding="utf-8") as f:
data = json.load(f)
if data.get("__skip__"):
raise SystemExit(0)
PY
# 디버그용: payload 구조 확인(원하면 주석해제)
# cat payload.json
curl -sS -H "Content-Type: application/json" \
-d @payload.json \
"$DISCORD_WEBHOOK_URL"