-
-
Notifications
You must be signed in to change notification settings - Fork 227
99 lines (87 loc) · 3.79 KB
/
preview-watchdog.yml
File metadata and controls
99 lines (87 loc) · 3.79 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
name: Preview watchdog
# Catches PR previews whose "Backend target: preview requested" comment is
# stuck because a `backend_preview_ready` or `backend_preview_failed`
# dispatch was lost (token expired, GitHub API blip, job crashed, etc.).
# Today this leaves the author staring at a stale "preview requested"
# message with no recourse. Watchdog runs every 15 minutes and nudges
# anything older than 20 minutes with a follow-up comment and link to
# re-run the upsert-preview workflow.
#
# Strictly additive — doesn't modify existing preview comments, only adds
# a sibling `mcpjam-preview-watchdog` comment. Posts once per stuck PR and
# keeps the comment updated, so it won't spam the PR thread.
on:
schedule:
- cron: "*/15 * * * *"
workflow_dispatch: {}
permissions:
contents: read
pull-requests: write
jobs:
scan-stuck-previews:
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
const previewMarker = "<!-- mcpjam-preview -->";
const watchdogMarker = "<!-- mcpjam-preview-watchdog -->";
const stuckAfterMs = 20 * 60 * 1000;
const prs = await github.paginate(github.rest.pulls.list, {
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
per_page: 100,
});
for (const pr of prs) {
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
per_page: 100,
});
const previewComment = comments.find((c) =>
c.body?.includes(previewMarker),
);
if (!previewComment) continue;
// Only nudge when the current state is "preview requested"
// — that's the specific stuck-waiting state we can resolve.
if (!previewComment.body?.includes("Backend target: preview requested")) {
continue;
}
const updatedAt = new Date(previewComment.updated_at).getTime();
if (Date.now() - updatedAt < stuckAfterMs) continue;
const ageMinutes = Math.round(
(Date.now() - updatedAt) / 60000,
);
const runsUrl = `https://github.com/${context.repo.owner}/${context.repo.repo}/actions/workflows/pr-preview.yml?query=branch%3A${encodeURIComponent(pr.head.ref)}`;
const body = [
watchdogMarker,
"### Preview watchdog",
`The "preview requested" state has been stuck for ~${ageMinutes} minutes.`,
"The backend callback probably dropped (token expired, dispatch failed, or the backend job crashed).",
"",
`Recover: re-run the [\`upsert-preview\` workflow](${runsUrl}) for this PR, or push an empty commit.`,
"",
"_Watchdog runs every 15 minutes; this comment updates in place when conditions change._",
].join("\n");
const existing = comments.find((c) =>
c.body?.includes(watchdogMarker),
);
if (existing) {
if (existing.body === body) continue;
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body,
});
continue;
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body,
});
}