Skip to content

Commit d2c71b6

Browse files
committed
fix: retrigger CodeRabbit on labeled event for unlocked+approved issues
- Switch bot-coderabbit-plan-trigger.yml trigger from 'assigned' back to 'labeled' so CodeRabbit fires when a skill label is added to an issue that is already approved and unlocked (fallback path) - Add 'approved' label guard to the job condition to prevent triggering on locked/unapproved issues - Move getLatestIssue() helper before logSummary() for clarity; no logic change — already called in main() to refresh locked status after unlock Primary path: approved-issues.yml unlocks the issue then calls the script (handles skill label present at approval time). Fallback path: this workflow fires on 'labeled' when skill label arrives after approval (issue already unlocked). Signed-off-by: Anshul Kushwaha <137977998+sudo-anshul@users.noreply.github.com>
1 parent 5638ba8 commit d2c71b6

2 files changed

Lines changed: 36 additions & 5 deletions

File tree

.github/scripts/coderabbit_plan_trigger.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ async function hasExistingCodeRabbitPlan(github, owner, repo, issueNumber) {
8080
}
8181
}
8282

83+
async function getLatestIssue(github, owner, repo, issue) {
84+
if (!issue?.number) return issue;
85+
86+
try {
87+
const { data } = await github.rest.issues.get({
88+
owner,
89+
repo,
90+
issue_number: issue.number,
91+
});
92+
93+
return data || issue;
94+
} catch (error) {
95+
console.log('Failed to refresh issue state; falling back to event payload:', {
96+
message: error?.message,
97+
status: error?.status,
98+
owner,
99+
repo,
100+
issueNumber: issue?.number,
101+
});
102+
103+
return issue;
104+
}
105+
}
106+
83107
function logSummary(owner, repo, issue) {
84108
console.log('=== Summary ===');
85109
console.log(`Repository: ${owner}/${repo}`);
@@ -92,10 +116,14 @@ function logSummary(owner, repo, issue) {
92116
async function main({ github, context }) {
93117
try {
94118
const { owner, repo } = context.repo;
95-
const { issue, label } = context.payload;
119+
const { issue: eventIssue, label } = context.payload;
96120

97121
// Validations
98-
if (!issue?.number) return console.log('No issue in payload');
122+
if (!eventIssue?.number) return console.log('No issue in payload');
123+
124+
// Refresh issue state to avoid stale payload fields (e.g., locked status)
125+
// when this script is invoked after earlier workflow steps mutate the issue.
126+
const issue = await getLatestIssue(github, owner, repo, eventIssue);
99127

100128
if (issue.locked) {
101129
return console.log(`Issue #${issue.number} is locked. CodeRabbit plan trigger will be deferred until the issue is approved and unlocked.`);

.github/workflows/bot-coderabbit-plan-trigger.yml

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Triggers CodeRabbit's plan feature for difficulty-labeled issues.
2-
# It runs when a difficulty label is added to an unlocked issue.
1+
# Triggers CodeRabbit's plan feature when a skill label is added to an already-approved (unlocked) issue.
2+
# Primary trigger path: approved-issues.yml fires CodeRabbit immediately after unlocking the issue.
3+
# This workflow is the fallback: fires when a skill label arrives after the issue was already approved and unlocked.
34
name: CodeRabbit Plan Trigger
45
on:
56
issues:
@@ -15,10 +16,12 @@ jobs:
1516
concurrency:
1617
group: coderabbit-plan-${{ github.event.issue.number }}
1718
cancel-in-progress: false
18-
# Run only for open, unlocked issues when a difficulty label is added.
19+
# Only run when a skill label is added to an open, unlocked, approved issue.
20+
# This guards against triggering while the issue is still locked (newly created issues).
1921
if: >
2022
github.event.issue.state == 'open' &&
2123
github.event.issue.locked == false &&
24+
contains(github.event.issue.labels.*.name, 'approved') &&
2225
contains(fromJson('["skill: beginner","skill: intermediate","skill: advanced"]'), github.event.label.name)
2326
2427
steps:

0 commit comments

Comments
 (0)