-
Notifications
You must be signed in to change notification settings - Fork 4
fix: resolve pr number via branch fallback when pull_requests is empty #24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,19 +12,22 @@ export default async (context: Context<"workflow_run.completed">) => { | |||||||||
| const { owner, repo } = context.repo(); | ||||||||||
| const workflowRun = context.payload.workflow_run; | ||||||||||
|
|
||||||||||
| if (workflowRun.conclusion === "success") { | ||||||||||
| const pullRequests = workflowRun.pull_requests as Array<{ number: number }>; | ||||||||||
| if (!pullRequests || pullRequests.length === 0) return; | ||||||||||
|
|
||||||||||
| const pull_number = pullRequests[0].number; | ||||||||||
| const pull_number = await resolvePullNumber( | ||||||||||
| context, | ||||||||||
| workflowRun, | ||||||||||
| owner, | ||||||||||
| repo, | ||||||||||
| ); | ||||||||||
| if (!pull_number) return; | ||||||||||
|
|
||||||||||
| if (workflowRun.conclusion === "success") { | ||||||||||
| const body = [ | ||||||||||
| "> [!NOTE]", | ||||||||||
| "> Linting checks passed successfully 🎉", | ||||||||||
| "", | ||||||||||
| "All formatting and code quality checks are clean.", | ||||||||||
| "", | ||||||||||
| "You’re good to merge 🚀", | ||||||||||
| "You're good to merge 🚀", | ||||||||||
| ].join("\n"); | ||||||||||
|
|
||||||||||
| await context.octokit.rest.issues.createComment({ | ||||||||||
|
|
@@ -37,13 +40,6 @@ export default async (context: Context<"workflow_run.completed">) => { | |||||||||
| return; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| // Find the PR associated with this workflow run | ||||||||||
| const pullRequests = workflowRun.pull_requests as Array<{ number: number }>; | ||||||||||
|
|
||||||||||
| if (!pullRequests || pullRequests.length === 0) return; | ||||||||||
|
|
||||||||||
| const pull_number = pullRequests[0].number; | ||||||||||
|
|
||||||||||
| const logsUrl = `https://github.com/${owner}/${repo}/actions/runs/${workflowRun.id}`; | ||||||||||
|
|
||||||||||
| const body = [ | ||||||||||
|
|
@@ -77,3 +73,30 @@ export default async (context: Context<"workflow_run.completed">) => { | |||||||||
| body, | ||||||||||
| }); | ||||||||||
| }; | ||||||||||
|
|
||||||||||
| async function resolvePullNumber( | ||||||||||
| context: Context<"workflow_run.completed">, | ||||||||||
| workflowRun: Context<"workflow_run.completed">["payload"]["workflow_run"], | ||||||||||
| owner: string, | ||||||||||
| repo: string, | ||||||||||
| ): Promise<number | null> { | ||||||||||
| const directPRs = workflowRun.pull_requests as Array<{ number: number }>; | ||||||||||
| if (directPRs?.length > 0) { | ||||||||||
| return directPRs[0].number; | ||||||||||
| } | ||||||||||
|
|
||||||||||
| const headBranch = workflowRun.head_branch; | ||||||||||
| const headSha = workflowRun.head_sha; | ||||||||||
|
|
||||||||||
| if (!headBranch) return null; | ||||||||||
|
|
||||||||||
| const { data: prs } = await context.octokit.rest.pulls.list({ | ||||||||||
| owner, | ||||||||||
| repo, | ||||||||||
| state: "open", | ||||||||||
| head: `${owner}:${headBranch}`, | ||||||||||
| }); | ||||||||||
|
|
||||||||||
| const match = prs.find((pr) => pr.head.sha === headSha); | ||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The fallback logic Confidence: 5/5 Suggested Fix
Suggested change
Remove the Prompt for AICopy this prompt to your AI IDE to fix this issue locally:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ehh
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @calebephrem I encountered an error while generating a response. Please try again. |
||||||||||
| return match?.number ?? null; | ||||||||||
| } | ||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
context.octokit.rest.pulls.list()API call lacks error handling. If this call fails (network issues, rate limits, permission errors), the entire workflow handler will crash and no comment will be posted to the PR. This is a critical path that should handle failures gracefully.Confidence: 5/5
Suggested Fix
Wrap the API call in a try-catch block to handle potential failures gracefully. This ensures the workflow handler doesn't crash and can return
nullto skip processing when the API is unavailable.Prompt for AI
Copy this prompt to your AI IDE to fix this issue locally:
📍 This suggestion applies to lines 93-98