|
| 1 | +// Script to trigger CodeRabbit plan for intermediate and advanced issues |
| 2 | + |
| 3 | +const marker = '<!-- CodeRabbit Plan Trigger -->'; |
| 4 | + |
| 5 | +async function triggerCodeRabbitPlan(github, owner, repo, issue, marker) { |
| 6 | + const comment = `${marker} @coderabbitai plan`; |
| 7 | + |
| 8 | + try { |
| 9 | + await github.rest.issues.createComment({ |
| 10 | + owner, |
| 11 | + repo, |
| 12 | + issue_number: issue.number, |
| 13 | + body: comment, |
| 14 | + }); |
| 15 | + console.log(`Triggered CodeRabbit plan for issue #${issue.number}`); |
| 16 | + return true; |
| 17 | + } catch (commentErr) { |
| 18 | + console.log(`Failed to trigger CodeRabbit plan for issue #${issue.number}:`, commentErr.message || commentErr); |
| 19 | + return false; |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +function hasIntermediateOrAdvancedLabel(issue, label) { |
| 24 | + // Check if issue has intermediate or advanced label (case-insensitive) |
| 25 | + const hasIntermediateLabel = issue.labels?.some(l => l?.name?.toLowerCase() === 'intermediate'); |
| 26 | + const hasAdvancedLabel = issue.labels?.some(l => l?.name?.toLowerCase() === 'advanced'); |
| 27 | + |
| 28 | + // Also check if newly added label is intermediate/advanced |
| 29 | + const isNewLabelIntermediate = label?.name?.toLowerCase() === 'intermediate'; |
| 30 | + const isNewLabelAdvanced = label?.name?.toLowerCase() === 'advanced'; |
| 31 | + |
| 32 | + return hasIntermediateLabel || hasAdvancedLabel || isNewLabelIntermediate || isNewLabelAdvanced; |
| 33 | +} |
| 34 | + |
| 35 | +async function hasExistingCodeRabbitPlan(github, owner, repo, issueNumber) { |
| 36 | + // Check for existing CodeRabbit plan comment (limited to first 500 comments) |
| 37 | + const comments = []; |
| 38 | + const iterator = github.paginate.iterator(github.rest.issues.listComments, { |
| 39 | + owner, repo, issue_number: issueNumber, per_page: 100 |
| 40 | + }); |
| 41 | + |
| 42 | + let count = 0; |
| 43 | + for await (const { data: page } of iterator) { |
| 44 | + comments.push(...page); |
| 45 | + count += page.length; |
| 46 | + if (count >= 500) break; // Hard upper bound to prevent unbounded pagination |
| 47 | + } |
| 48 | + |
| 49 | + return comments.some(c => c.body?.includes('@coderabbitai plan')); |
| 50 | +} |
| 51 | + |
| 52 | +function logSummary(owner, repo, issue) { |
| 53 | + console.log('=== Summary ==='); |
| 54 | + console.log(`Repository: ${owner}/${repo}`); |
| 55 | + console.log(`Issue Number: ${issue.number}`); |
| 56 | + console.log(`Issue Title: ${issue.title || '(no title)'}`); |
| 57 | + console.log(`Labels: ${issue.labels?.map(l => l.name).join(', ') || 'none'}`); |
| 58 | +} |
| 59 | + |
| 60 | +module.exports = async ({ github, context }) => { |
| 61 | + try { |
| 62 | + const { owner, repo } = context.repo; |
| 63 | + const { issue, label } = context.payload; |
| 64 | + |
| 65 | + // Validations |
| 66 | + if (!issue?.number) return console.log('No issue in payload'); |
| 67 | + |
| 68 | + if (!hasIntermediateOrAdvancedLabel(issue, label)) { |
| 69 | + return console.log('Issue does not have intermediate or advanced label'); |
| 70 | + } |
| 71 | + |
| 72 | + if (await hasExistingCodeRabbitPlan(github, owner, repo, issue.number)) { |
| 73 | + return console.log(`CodeRabbit plan already triggered for #${issue.number}`); |
| 74 | + } |
| 75 | + |
| 76 | + // Post CodeRabbit plan trigger |
| 77 | + await triggerCodeRabbitPlan(github, owner, repo, issue, marker); |
| 78 | + |
| 79 | + logSummary(owner, repo, issue); |
| 80 | + } catch (err) { |
| 81 | + console.log('❌ Error:', err.message); |
| 82 | + } |
| 83 | +}; |
0 commit comments