forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcoderabbit_plan_trigger.js
More file actions
129 lines (110 loc) · 4.21 KB
/
Copy pathcoderabbit_plan_trigger.js
File metadata and controls
129 lines (110 loc) · 4.21 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
// Script to trigger CodeRabbit plan for intermediate and advanced issues
const CODERABBIT_MARKER = '<!-- CodeRabbit Plan Trigger -->';
const { DIFFICULTY_LABELS, GOOD_FIRST_ISSUE_LABEL } = require('./shared/labels.js');
async function triggerCodeRabbitPlan(github, owner, repo, issue, marker = CODERABBIT_MARKER) {
const comment = `${marker} @coderabbitai plan`;
try {
await github.rest.issues.createComment({
owner,
repo,
issue_number: issue.number,
body: comment,
});
console.log(`Triggered CodeRabbit plan for issue #${issue.number}`);
return true;
} catch (commentErr) {
console.log('Failed to trigger CodeRabbit plan:', {
message: commentErr?.message,
status: commentErr?.status,
owner,
repo,
issueNumber: issue?.number,
});
return false;
}
}
function hasBeginnerOrHigherLabel(issue, label) {
// Only beginner+ labels qualify here; GFI gets its own CodeRabbit plan
// trigger via the assignment bot chain (bot-gfi-assign-on-comment.js).
const beginnerPlus = DIFFICULTY_LABELS
.filter(d => d !== GOOD_FIRST_ISSUE_LABEL)
.map(d => d.toLowerCase());
const allowed = new Set(beginnerPlus);
const hasAllowedLabel = issue.labels?.some(l => allowed.has(l?.name?.toLowerCase()));
// Also check if newly added label is a difficulty label
const isNewLabelAllowed = allowed.has(label?.name?.toLowerCase());
return hasAllowedLabel || isNewLabelAllowed;
}
async function hasExistingCodeRabbitPlan(github, owner, repo, issueNumber) {
// Check for existing CodeRabbit plan comment (limited to first 500 comments)
// Uses marker-based detection to avoid false positives from quoted text
try {
const comments = [];
const iterator = github.paginate.iterator(github.rest.issues.listComments, {
owner,
repo,
issue_number: issueNumber,
per_page: 100,
});
let count = 0;
for await (const { data: page } of iterator) {
comments.push(...page);
count += page.length;
if (count >= 500) break; // Hard upper bound to prevent unbounded pagination
}
// Check for marker-based comment OR @coderabbitai plan text
return comments.some(c =>
typeof c?.body === 'string' &&
(c.body.includes(CODERABBIT_MARKER) || c.body.includes('@coderabbitai plan'))
);
} catch (error) {
console.log('Failed to check existing CodeRabbit plan comments:', {
message: error?.message,
status: error?.status,
owner,
repo,
issueNumber,
});
// Return false to allow plan trigger attempt (fail-open for better UX)
return false;
}
}
function logSummary(owner, repo, issue) {
console.log('=== Summary ===');
console.log(`Repository: ${owner}/${repo}`);
console.log(`Issue Number: ${issue.number}`);
console.log(`Issue Title: ${issue.title || '(no title)'}`);
console.log(`Labels: ${issue.labels?.map(l => l.name).join(', ') || 'none'}`);
}
// Main workflow handler (default export for workflow usage)
async function main({ github, context }) {
try {
const { owner, repo } = context.repo;
const { issue, label } = context.payload;
// Validations
if (!issue?.number) return console.log('No issue in payload');
if (!hasBeginnerOrHigherLabel(issue, label)) {
return console.log('Issue does not have beginner/intermediate/advanced label');
}
if (await hasExistingCodeRabbitPlan(github, owner, repo, issue.number)) {
return console.log(`CodeRabbit plan already triggered for #${issue.number}`);
}
// Post CodeRabbit plan trigger
await triggerCodeRabbitPlan(github, owner, repo, issue, CODERABBIT_MARKER);
logSummary(owner, repo, issue);
} catch (err) {
console.log('❌ Error:', {
message: err?.message,
status: err?.status,
owner: context?.repo?.owner,
repo: context?.repo?.repo,
issueNumber: context?.payload?.issue?.number,
});
}
}
// Default export for workflow usage: await script({ github, context })
module.exports = main;
// Named exports for reuse by other scripts (e.g., GFI assignment bot)
module.exports.triggerCodeRabbitPlan = triggerCodeRabbitPlan;
module.exports.hasExistingCodeRabbitPlan = hasExistingCodeRabbitPlan;
module.exports.CODERABBIT_MARKER = CODERABBIT_MARKER;