Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions src/events/workflow_run.completed/checklint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
"",
"Youre good to merge 🚀",
"You're good to merge 🚀",
].join("\n");

await context.octokit.rest.issues.createComment({
Expand All @@ -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 = [
Expand Down Expand Up @@ -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}`,
});
Comment on lines +93 to +98

Copy link
Copy Markdown
Contributor

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
Suggested change
const { data: prs } = await context.octokit.rest.pulls.list({
owner,
repo,
state: "open",
head: `${owner}:${headBranch}`,
});
try {
const { data: prs } = await context.octokit.rest.pulls.list({
owner,
repo,
state: "open",
head: `${owner}:${headBranch}`,
});
} catch (error) {
context.log.error("Failed to fetch pull requests", error);
return null;
}

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 null to skip processing when the API is unavailable.

Prompt for AI

Copy this prompt to your AI IDE to fix this issue locally:

In src/events/workflow_run.completed/checklint.ts around line 93, the context.octokit.rest.pulls.list() API call lacks error handling and can cause the entire workflow handler to crash if the GitHub API fails; wrap the API call (lines 93-98) in a try-catch block that logs the error and returns null on failure, ensuring graceful degradation when the API is unavailable.

📍 This suggestion applies to lines 93-98


const match = prs.find((pr) => pr.head.sha === headSha);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback logic ?? prs[0] is dangerous. If no PR matches the head_sha, it falls back to the first open PR with the same branch name, which could be a completely different PR. This could result in posting workflow comments to the wrong PR, confusing developers and potentially exposing information about one PR in another.

Confidence: 5/5

Suggested Fix
Suggested change
const match = prs.find((pr) => pr.head.sha === headSha);
const match = prs.find((pr) => pr.head.sha === headSha);
return match?.number ?? null;

Remove the ?? prs[0] fallback. If no PR matches the exact SHA, return null to skip processing rather than risk commenting on the wrong PR. This ensures comments are only posted to the correct PR.

Prompt for AI

Copy this prompt to your AI IDE to fix this issue locally:

In src/events/workflow_run.completed/checklint.ts at line 100, the fallback logic `?? prs[0]` can cause comments to be posted to the wrong PR if the SHA doesn't match; remove the `?? prs[0]` fallback and return null when no matching PR is found, ensuring comments are only posted to the correct PR with the matching SHA.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ehh

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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;
}
3 changes: 0 additions & 3 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
--text: #e5e7ff;
--muted: #9194b8;
--accent: #8b8dff;
--accent-glow: rgba(139, 141, 255, 0.35);
}

* {
Expand Down Expand Up @@ -91,8 +90,6 @@
line-height: 1.05;

margin-bottom: 24px;

text-shadow: 0 0 30px var(--accent-glow);
}

.accent {
Expand Down
Loading