-
Notifications
You must be signed in to change notification settings - Fork 0
367 lines (330 loc) · 14.5 KB
/
release-notify.yml
File metadata and controls
367 lines (330 loc) · 14.5 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
name: Release Notification
on:
pull_request:
types: [closed]
branches:
- main
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to use for context (for testing)'
required: false
default: ''
jobs:
release-notify:
if: github.event_name == 'workflow_dispatch' || github.event.pull_request.merged == true
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
id-token: write
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: Resolve PR context
id: pr
env:
GH_TOKEN: ${{ github.token }}
run: |
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
PR_NUM="${{ inputs.pr_number }}"
if [ -z "$PR_NUM" ]; then
PR_NUM=$(gh pr list --state merged --base main --limit 1 --json number --jq '.[0].number')
fi
PR_JSON=$(gh pr view "$PR_NUM" --json number,title,author,url)
echo "number=$(echo "$PR_JSON" | jq -r '.number')" >> "$GITHUB_OUTPUT"
echo "title=$(echo "$PR_JSON" | jq -r '.title')" >> "$GITHUB_OUTPUT"
echo "author=$(echo "$PR_JSON" | jq -r '.author.login')" >> "$GITHUB_OUTPUT"
echo "url=$(echo "$PR_JSON" | jq -r '.url')" >> "$GITHUB_OUTPUT"
else
echo "number=${{ github.event.pull_request.number }}" >> "$GITHUB_OUTPUT"
echo "title=${{ github.event.pull_request.title }}" >> "$GITHUB_OUTPUT"
echo "author=${{ github.event.pull_request.user.login }}" >> "$GITHUB_OUTPUT"
echo "url=${{ github.event.pull_request.html_url }}" >> "$GITHUB_OUTPUT"
fi
- name: Get commits since last notification
id: commits
run: |
LAST_TAG=$(git tag -l 'release-notify/*' --sort=-creatordate | head -n1)
if [ -z "$LAST_TAG" ]; then
echo "No previous release-notify tag found, using merge commit only"
MERGE_SHA="${{ github.event.pull_request.merge_commit_sha }}"
if [ -n "$MERGE_SHA" ]; then
COMMIT_LOG=$(git log --oneline --no-merges "${MERGE_SHA}^..${MERGE_SHA}")
DIFF_STATS=$(git diff --stat "${MERGE_SHA}^" "${MERGE_SHA}" 2>/dev/null || echo "Unable to get diff stats")
else
COMMIT_LOG=$(git log --oneline --no-merges -1)
DIFF_STATS=$(git diff --stat HEAD~1 HEAD 2>/dev/null || echo "Unable to get diff stats")
fi
else
echo "Last release-notify tag: $LAST_TAG"
COMMIT_LOG=$(git log --oneline --no-merges "${LAST_TAG}..HEAD")
DIFF_STATS=$(git diff --stat "${LAST_TAG}..HEAD")
fi
COMMIT_COUNT=$(echo "$COMMIT_LOG" | grep -c . || true)
echo "commit_count=${COMMIT_COUNT}" >> "$GITHUB_OUTPUT"
{
echo "commit_log<<COMMITS_EOF"
echo "$COMMIT_LOG"
echo "COMMITS_EOF"
} >> "$GITHUB_OUTPUT"
{
echo "diff_stats<<DIFF_EOF"
echo "$DIFF_STATS"
echo "DIFF_EOF"
} >> "$GITHUB_OUTPUT"
echo "Found $COMMIT_COUNT commits since last notification"
- name: Claude Analysis
if: steps.commits.outputs.commit_count != '0'
id: claude
uses: anthropics/claude-code-action@v1
with:
claude_code_oauth_token: ${{ secrets.CLAUDE_CODE_OAUTH_TOKEN }}
prompt: |
You are analyzing a production release for the cli repository (TypeScript CLI tool for InstaWP — command-line interface for managing WordPress sites, servers, and deployments on the InstaWP platform).
A PR was just merged to production:
- PR: #${{ steps.pr.outputs.number }} - ${{ steps.pr.outputs.title }}
- Author: ${{ steps.pr.outputs.author }}
Commits since last notification:
${{ steps.commits.outputs.commit_log }}
File change statistics:
${{ steps.commits.outputs.diff_stats }}
Analyze these changes and output ONLY a valid JSON object (no markdown fences, no explanation, no other text) with exactly these fields:
{
"should_notify": true/false - set false ONLY if changes are exclusively docs, CI config, code style, or comments with zero user impact,
"summary": "1-2 sentence executive summary of what changed and why it matters",
"risk_level": "low|medium|high" - low for minor fixes, medium for feature work, high for breaking changes or security fixes,
"support_notes": "- bullet points for support team\n- user-facing changes, new behaviors, potential questions",
"marketing_notes": "- bullet points for marketing about features worth highlighting (empty string if purely technical)",
"changelog": "### Added\n- items\n### Improved\n- items\n### Fixed\n- items\n### Security\n- items (omit empty categories)"
}
claude_args: '--max-turns 3'
- name: Post to Slack
id: slack
if: steps.commits.outputs.commit_count != '0'
env:
SLACK_WEBHOOK: ${{ secrets.RELEASE_SLACK_WEBHOOK }}
CLAUDE_ACTION_OUTPUT: ${{ steps.claude.outputs.result }}
PR_URL: ${{ steps.pr.outputs.url }}
PR_TITLE: ${{ steps.pr.outputs.title }}
PR_AUTHOR: ${{ steps.pr.outputs.author }}
COMMIT_COUNT: ${{ steps.commits.outputs.commit_count }}
run: |
# Extract Claude's JSON response using python3 for robust parsing
EXEC_FILE="/home/runner/work/_temp/claude-execution-output.json"
python3 -c '
import json, os, sys
def extract_json_with_should_notify(text):
if not text or not isinstance(text, str):
return None
text = text.strip()
try:
obj = json.loads(text)
if isinstance(obj, dict) and "should_notify" in obj:
return json.dumps(obj)
except Exception:
pass
# Brace-matching to find JSON blocks in surrounding text
brace_depth = 0
start = None
for i, ch in enumerate(text):
if ch == "{":
if brace_depth == 0:
start = i
brace_depth += 1
elif ch == "}":
brace_depth -= 1
if brace_depth == 0 and start is not None:
try:
obj = json.loads(text[start:i+1])
if isinstance(obj, dict) and "should_notify" in obj:
return json.dumps(obj)
except Exception:
pass
start = None
return None
def search_events(events):
"""Search a list of event dicts for result containing should_notify."""
for ev in events:
if not isinstance(ev, dict):
continue
if ev.get("type") == "result":
raw = ev.get("result", "")
if isinstance(raw, dict) and "should_notify" in raw:
return json.dumps(raw)
if isinstance(raw, str):
r = extract_json_with_should_notify(raw)
if r:
return r
# Also check subResult or nested content
for key in ("result", "content", "text", "output"):
val = ev.get(key, "")
if isinstance(val, str) and "should_notify" in val:
r = extract_json_with_should_notify(val)
if r:
return r
return None
# 1. Try action output
action_output = os.environ.get("CLAUDE_ACTION_OUTPUT", "")
r = extract_json_with_should_notify(action_output)
if r:
print(r)
sys.exit(0)
# 2. Try execution output file
exec_file = os.environ.get("EXEC_FILE", "/home/runner/work/_temp/claude-execution-output.json")
if not os.path.exists(exec_file):
sys.exit(1)
with open(exec_file) as f:
content = f.read()
print(f"Exec file: {len(content)} bytes", file=sys.stderr)
# Try as JSON array first
try:
data = json.loads(content)
if isinstance(data, list):
r = search_events(data)
if r:
print(r)
sys.exit(0)
except Exception:
pass
# Try as JSONL (one JSON object per line)
for line in content.splitlines():
line = line.strip()
if not line:
continue
try:
obj = json.loads(line)
except Exception:
continue
if isinstance(obj, dict):
r = search_events([obj])
if r:
print(r)
sys.exit(0)
# Last resort: search raw text
r = extract_json_with_should_notify(content)
if r:
print(r)
sys.exit(0)
sys.exit(1)
' > /tmp/claude_extracted.json 2>/tmp/claude_extract.log || true
CLAUDE_OUTPUT=""
if [ -f /tmp/claude_extracted.json ] && [ -s /tmp/claude_extracted.json ]; then
CLAUDE_OUTPUT=$(cat /tmp/claude_extracted.json)
fi
if [ -z "$CLAUDE_OUTPUT" ] || ! echo "$CLAUDE_OUTPUT" | jq -e '.should_notify' > /dev/null 2>&1; then
echo "::warning::Could not extract Claude JSON output."
cat /tmp/claude_extract.log 2>/dev/null || true
if [ -f "$EXEC_FILE" ]; then
echo "Execution file last 5 lines:"
tail -5 "$EXEC_FILE" 2>/dev/null || true
echo "---"
echo "Searching for 'should_notify' in execution file:"
grep -c "should_notify" "$EXEC_FILE" 2>/dev/null || echo "Not found"
echo "Searching for 'result' type entries:"
grep -c '"type"' "$EXEC_FILE" 2>/dev/null || echo "Not found"
fi
echo "notified=false" >> "$GITHUB_OUTPUT"
exit 0
fi
echo "Extracted Claude output: ${#CLAUDE_OUTPUT} chars"
if [ -z "$SLACK_WEBHOOK" ]; then
echo "::warning::RELEASE_SLACK_WEBHOOK secret is not set. Skipping."
echo "notified=false" >> "$GITHUB_OUTPUT"
exit 0
fi
SHOULD_NOTIFY=$(echo "$CLAUDE_OUTPUT" | jq -r '.should_notify')
if [ "$SHOULD_NOTIFY" != "true" ]; then
echo "Claude determined notification is not warranted. Skipping."
echo "notified=false" >> "$GITHUB_OUTPUT"
exit 0
fi
SUMMARY=$(echo "$CLAUDE_OUTPUT" | jq -r '.summary')
RISK_LEVEL=$(echo "$CLAUDE_OUTPUT" | jq -r '.risk_level')
SUPPORT_NOTES=$(echo "$CLAUDE_OUTPUT" | jq -r '.support_notes')
MARKETING_NOTES=$(echo "$CLAUDE_OUTPUT" | jq -r '.marketing_notes')
CHANGELOG=$(echo "$CLAUDE_OUTPUT" | jq -r '.changelog')
case "$RISK_LEVEL" in
low) RISK_EMOJI="🟢" ;;
medium) RISK_EMOJI="🟡" ;;
high) RISK_EMOJI="🔴" ;;
*) RISK_EMOJI="⚪" ;;
esac
PAYLOAD=$(jq -n \
--arg repo "cli" \
--arg summary "$SUMMARY" \
--arg risk "$RISK_EMOJI $RISK_LEVEL" \
--arg pr_url "$PR_URL" \
--arg pr_title "$PR_TITLE" \
--arg pr_author "$PR_AUTHOR" \
--arg commits "$COMMIT_COUNT" \
--arg support "$SUPPORT_NOTES" \
--arg changelog "$CHANGELOG" \
--arg marketing "$MARKETING_NOTES" \
--arg timestamp "$(date -u '+%Y-%m-%d %H:%M UTC')" \
'{
blocks: ([
{
type: "header",
text: { type: "plain_text", text: "\($repo) — Release Deployed", emoji: true }
},
{
type: "section",
fields: [
{ type: "mrkdwn", text: "*PR:* <\($pr_url)|\($pr_title)>" },
{ type: "mrkdwn", text: "*Author:* \($pr_author)" },
{ type: "mrkdwn", text: "*Commits:* \($commits)" },
{ type: "mrkdwn", text: "*Risk:* \($risk)" }
]
},
{ type: "divider" },
{
type: "section",
text: { type: "mrkdwn", text: "*Summary*\n\($summary)" }
},
{
type: "section",
text: { type: "mrkdwn", text: "*🎧 Support Notes*\n\($support)" }
},
{
type: "section",
text: { type: "mrkdwn", text: "*📜 Changelog*\n\($changelog)" }
}
] + (if $marketing != "" then [{
type: "section",
text: { type: "mrkdwn", text: "*📣 Marketing Notes*\n\($marketing)" }
}] else [] end) + [
{
type: "context",
elements: [
{ type: "mrkdwn", text: "\($timestamp) • Generated by Claude AI" }
]
}
])
}')
RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" \
-X POST "$SLACK_WEBHOOK" \
-H "Content-Type: application/json" \
-d "$PAYLOAD")
if [ "$RESPONSE" = "200" ]; then
echo "Successfully posted to Slack"
echo "notified=true" >> "$GITHUB_OUTPUT"
else
echo "::error::Slack webhook returned HTTP $RESPONSE"
echo "notified=false" >> "$GITHUB_OUTPUT"
exit 1
fi
- name: Create release-notify tag
if: steps.slack.outputs.notified == 'true'
env:
GH_TOKEN: ${{ github.token }}
run: |
TAG="release-notify/$(date -u '+%Y-%m-%d-%H%M%S')"
SHA=$(git rev-parse HEAD)
gh api repos/${{ github.repository }}/git/refs \
-f ref="refs/tags/$TAG" \
-f sha="$SHA"
echo "Created tag: $TAG"