forked from router-for-me/CLIProxyAPI
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcoderabbit-rate-limit-retry.yml
More file actions
223 lines (200 loc) · 8.03 KB
/
coderabbit-rate-limit-retry.yml
File metadata and controls
223 lines (200 loc) · 8.03 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
name: coderabbit-rate-limit-retry
on:
workflow_dispatch:
permissions:
checks: write
contents: read
pull-requests: write
issues: write
jobs:
retrigger:
name: retrigger-coderabbit-on-rate-limit
runs-on: ubuntu-latest
steps:
- name: Re-request CodeRabbit when backlog is high and check is stale
uses: actions/github-script@v7
with:
script: |
const owner = context.repo.owner;
const repo = context.repo.repo;
const STALE_MINUTES = 20;
const BYPASS_LABEL = "ci:coderabbit-bypass";
const GATE_CHECK_NAME = "CodeRabbit Gate";
const MARKER = "<!-- codex:coderabbit-rate-limit-retry -->";
const nowMs = Date.now();
async function listOpenPRs() {
const all = await github.paginate(github.rest.pulls.list, {
owner,
repo,
state: "open",
per_page: 100,
});
return all;
}
async function getCodeRabbitState(prNumber) {
const checks = await github.graphql(
`query($owner:String!,$repo:String!,$number:Int!){
repository(owner:$owner,name:$repo){
pullRequest(number:$number){
commits(last:1){
nodes{
commit{
statusCheckRollup{
contexts(first:50){
nodes{
__typename
... on CheckRun {
name
conclusion
status
completedAt
}
... on StatusContext {
context
state
createdAt
}
}
}
}
}
}
}
}
}
}`,
{ owner, repo, number: prNumber },
);
const nodes = checks.repository.pullRequest.commits.nodes[0]?.commit?.statusCheckRollup?.contexts?.nodes || [];
for (const n of nodes) {
if (n.__typename === "CheckRun" && n.name === "CodeRabbit") {
return {
state: (n.conclusion || n.status || "UNKNOWN").toUpperCase(),
at: n.completedAt ? new Date(n.completedAt).getTime() : nowMs,
};
}
if (n.__typename === "StatusContext" && n.context === "CodeRabbit") {
return {
state: (n.state || "UNKNOWN").toUpperCase(),
at: n.createdAt ? new Date(n.createdAt).getTime() : nowMs,
};
}
}
return { state: "MISSING", at: nowMs };
}
async function hasRecentRetryComment(prNumber) {
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: prNumber,
per_page: 100,
});
const latest = comments
.filter((c) => c.user?.login === "github-actions[bot]" && c.body?.includes(MARKER))
.sort((a, b) => new Date(b.created_at) - new Date(a.created_at))[0];
if (!latest) return false;
const ageMin = (nowMs - new Date(latest.created_at).getTime()) / 60000;
return ageMin < STALE_MINUTES;
}
async function ensureBypassLabelExists() {
try {
await github.rest.issues.getLabel({
owner,
repo,
name: BYPASS_LABEL,
});
} catch (error) {
if (error.status !== 404) throw error;
await github.rest.issues.createLabel({
owner,
repo,
name: BYPASS_LABEL,
color: "B60205",
description: "Temporary bypass for CodeRabbit rate-limit under high PR backlog.",
});
}
}
async function hasLabel(prNumber, name) {
const labels = await github.paginate(github.rest.issues.listLabelsOnIssue, {
owner,
repo,
issue_number: prNumber,
per_page: 100,
});
return labels.some((l) => l.name === name);
}
async function setBypassLabel(prNumber, enable) {
const present = await hasLabel(prNumber, BYPASS_LABEL);
if (enable && !present) {
await github.rest.issues.addLabels({
owner,
repo,
issue_number: prNumber,
labels: [BYPASS_LABEL],
});
core.notice(`PR #${prNumber}: applied label '${BYPASS_LABEL}'.`);
}
if (!enable && present) {
await github.rest.issues.removeLabel({
owner,
repo,
issue_number: prNumber,
name: BYPASS_LABEL,
});
core.notice(`PR #${prNumber}: removed label '${BYPASS_LABEL}'.`);
}
}
async function publishGate(pr, pass, summary) {
await github.rest.checks.create({
owner,
repo,
name: GATE_CHECK_NAME,
head_sha: pr.head.sha,
status: "completed",
conclusion: pass ? "success" : "failure",
output: {
title: pass ? "CodeRabbit gate passed" : "CodeRabbit gate blocked",
summary,
},
});
}
async function processPR(pr) {
const state = await getCodeRabbitState(pr.number);
const ageMin = (nowMs - state.at) / 60000;
const stateOk = state.state === "SUCCESS" || state.state === "NEUTRAL";
const stale = ageMin >= STALE_MINUTES;
const bypassEligible = stale && !stateOk;
await setBypassLabel(pr.number, bypassEligible);
if (bypassEligible && !(await hasRecentRetryComment(pr.number))) {
const body = [
MARKER,
"@coderabbitai full review",
"",
`Automated retrigger: CodeRabbit state=${state.state}, age=${ageMin.toFixed(1)}m (stale after ${STALE_MINUTES}m).`,
].join("\n");
await github.rest.issues.createComment({
owner,
repo,
issue_number: pr.number,
body,
});
core.notice(`PR #${pr.number}: posted CodeRabbit retrigger comment.`);
}
const gatePass = stateOk || bypassEligible;
const summary = [
`CodeRabbit state: ${state.state}`,
`Age minutes: ${ageMin.toFixed(1)}`,
`Stale threshold: ${STALE_MINUTES}m`,
`Bypass eligible: ${bypassEligible}`,
].join("\n");
await publishGate(pr, gatePass, summary);
}
const openPRs = await listOpenPRs();
core.info(`Open PR count: ${openPRs.length}`);
await ensureBypassLabelExists();
const targetPRs = context.eventName === "pull_request_target"
? openPRs.filter((p) => p.number === context.payload.pull_request.number)
: openPRs;
for (const pr of targetPRs) {
await processPR(pr);
}