fix: Get externalid from webhook#177
Conversation
|
Warning Rate limit exceeded@adityachoudhari26 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 46 seconds before requesting another review. ⌛ How to resolve this issue?After the wait time has elapsed, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout. Please see our FAQ for further information. WalkthroughThe pull request introduces several changes across multiple files in the Changes
Possibly related PRs
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
Documentation and Community
|
| // the externalRunId yet, as it depends on the job's instantiation. Therefore, | ||
| // the first event lacks the run ID, so we skip it and wait for the next event. | ||
| if (job == null) return; | ||
| .set({ status, externalId }) |
There was a problem hiding this comment.
you can also generate the url at this point? I think
There was a problem hiding this comment.
we are generating the URL below
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Outside diff range and nitpick comments (3)
apps/event-worker/src/job-dispatch/github.ts (2)
Line range hint
89-106: Add error handling for workflow dispatch.The workflow dispatch call is not wrapped in a try-catch block. If the dispatch fails, the error won't be properly handled and the job status won't be updated.
Consider adding error handling:
- await octokit.actions.createWorkflowDispatch({ - owner: parsed.data.owner, - repo: parsed.data.repo, - workflow_id: parsed.data.workflowId, - ref: ghOrg.branch, - inputs: { - job_id: je.id, - }, - headers: { - "X-GitHub-Api-Version": "2022-11-28", - authorization: `Bearer ${installationToken.token}`, - }, - }); + try { + await octokit.actions.createWorkflowDispatch({ + owner: parsed.data.owner, + repo: parsed.data.repo, + workflow_id: parsed.data.workflowId, + ref: ghOrg.branch, + inputs: { + job_id: je.id, + }, + headers: { + "X-GitHub-Api-Version": "2022-11-28", + authorization: `Bearer ${installationToken.token}`, + }, + }); + await db.update(job).set({ + status: JobStatus.Dispatched, + message: "Workflow dispatched successfully", + }); + } catch (error) { + logger.error(`Failed to dispatch workflow for job ${je.id}:`, error); + await db.update(job).set({ + status: JobStatus.DispatchFailed, + message: `Failed to dispatch workflow: ${error.message}`, + }); + }
Line range hint
19-106: Consider updating job status after successful dispatch.The function currently doesn't update the job status after successfully dispatching the workflow. This could lead to the job appearing stuck in its previous state.
Add a status update after successful dispatch as shown in the error handling diff above. This will help track the job's progress and indicate that it was successfully dispatched while waiting for the webhook to update its status with the external ID.
apps/webservice/src/app/api/github/webhook/workflow/handler.ts (1)
24-29: LGTM with a minor suggestion for input validation.The UUID extraction logic is well-implemented. Consider adding input validation to handle undefined/null input gracefully.
const extractUuid = (str: string) => { + if (!str) return null; const uuidRegex = /\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/; const match = uuidRegex.exec(str); return match ? match[0] : null; };
📜 Review details
Configuration used: .coderabbit.yaml
Review profile: CHILL
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (4)
- apps/event-worker/package.json (0 hunks)
- apps/event-worker/src/github-utils.ts (0 hunks)
- apps/event-worker/src/job-dispatch/github.ts (1 hunks)
- apps/webservice/src/app/api/github/webhook/workflow/handler.ts (1 hunks)
💤 Files with no reviewable changes (2)
- apps/event-worker/package.json
- apps/event-worker/src/github-utils.ts
🔇 Additional comments (3)
apps/event-worker/src/job-dispatch/github.ts (1)
17-17: Verify webhook implementation for external ID updates.Since we're removing the polling mechanism for getting the run ID, we need to ensure the webhook handler properly updates the job's external ID when the workflow run is created.
Let's verify the webhook handler implementation:
apps/webservice/src/app/api/github/webhook/workflow/handler.ts (2)
59-61: LGTM: Improved job retrieval with early return.The changes properly handle external ID from webhooks and implement a clean early return pattern when no job is found.
59-71: Verify webhook event field availability.Let's ensure that all workflow webhook events contain the required fields.
✅ Verification successful
Required webhook fields are properly handled
The code safely handles the workflow run webhook event fields:
- The
id,status,conclusion,repository, andnamefields are destructured fromevent.workflow_run- Null checks are in place for
conclusion- The
statusis derived using proper fallback logic- Type definitions from
@octokit/webhooks-typesensure type safety🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Search for other webhook handlers to verify consistent field usage rg -l "WorkflowRunEvent" | xargs rg "workflow_run\." -A 2Length of output: 57
Script:
#!/bin/bash # Let's first find the workflow event type definition and its usage rg -A 5 "type WorkflowRunEvent" # Then search for workflow run related handlers rg -A 10 "workflow.*run.*event"Length of output: 67
Script:
#!/bin/bash # Let's look at the imports and type definitions in the handler file cat "apps/webservice/src/app/api/github/webhook/workflow/handler.ts" # Also search for any GitHub webhook type definitions fd -e ts -e js | xargs rg "interface.*WorkflowRun"Length of output: 3042
| const externalId = id.toString(); | ||
| await db | ||
| .update(schema.job) | ||
| .set({ status }) | ||
| .where(eq(schema.job.externalId, id.toString())) | ||
| .returning() | ||
| .then(takeFirstOrNull); | ||
|
|
||
| // Addressing a race condition: When the job is created externally on GitHub, | ||
| // it triggers a webhook event. However, our system hasn't updated the job with | ||
| // the externalRunId yet, as it depends on the job's instantiation. Therefore, | ||
| // the first event lacks the run ID, so we skip it and wait for the next event. | ||
| if (job == null) return; | ||
| .set({ status, externalId }) | ||
| .where(eq(schema.job.id, job.id)); |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Consider using a transaction for atomic updates.
The status update and subsequent metadata update should be atomic to prevent potential race conditions.
- await db
- .update(schema.job)
- .set({ status, externalId })
- .where(eq(schema.job.id, job.id));
+ await db.transaction(async (tx) => {
+ await tx
+ .update(schema.job)
+ .set({ status, externalId })
+ .where(eq(schema.job.id, job.id));
+
+ const existingUrlMetadata = await tx
+ .select()
+ .from(schema.jobMetadata)
+ // ... rest of the metadata query ...
+
+ // ... metadata update logic ...
+ await tx
+ .insert(schema.jobMetadata)
+ // ... rest of the metadata insert ...
+ });Committable suggestion was skipped due to low confidence.
| const getJob = async (externalId: number, name: string) => { | ||
| const jobFromExternalId = await db | ||
| .select() | ||
| .from(schema.job) | ||
| .where(eq(schema.job.externalId, externalId.toString())) | ||
| .then(takeFirstOrNull); | ||
|
|
||
| if (jobFromExternalId != null) return jobFromExternalId; | ||
|
|
||
| const uuid = extractUuid(name); | ||
| if (uuid == null) return null; | ||
|
|
||
| return db | ||
| .select() | ||
| .from(schema.job) | ||
| .where(eq(schema.job.id, uuid)) | ||
| .then(takeFirstOrNull); | ||
| }; |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Add error handling for database operations.
The job retrieval logic is well-structured with a good fallback strategy. However, database operations could throw errors that should be handled gracefully.
const getJob = async (externalId: number, name: string) => {
+ try {
const jobFromExternalId = await db
.select()
.from(schema.job)
.where(eq(schema.job.externalId, externalId.toString()))
.then(takeFirstOrNull);
if (jobFromExternalId != null) return jobFromExternalId;
const uuid = extractUuid(name);
if (uuid == null) return null;
return db
.select()
.from(schema.job)
.where(eq(schema.job.id, uuid))
.then(takeFirstOrNull);
+ } catch (error) {
+ console.error('Failed to retrieve job:', error);
+ return null;
+ }
};📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| const getJob = async (externalId: number, name: string) => { | |
| const jobFromExternalId = await db | |
| .select() | |
| .from(schema.job) | |
| .where(eq(schema.job.externalId, externalId.toString())) | |
| .then(takeFirstOrNull); | |
| if (jobFromExternalId != null) return jobFromExternalId; | |
| const uuid = extractUuid(name); | |
| if (uuid == null) return null; | |
| return db | |
| .select() | |
| .from(schema.job) | |
| .where(eq(schema.job.id, uuid)) | |
| .then(takeFirstOrNull); | |
| }; | |
| const getJob = async (externalId: number, name: string) => { | |
| try { | |
| const jobFromExternalId = await db | |
| .select() | |
| .from(schema.job) | |
| .where(eq(schema.job.externalId, externalId.toString())) | |
| .then(takeFirstOrNull); | |
| if (jobFromExternalId != null) return jobFromExternalId; | |
| const uuid = extractUuid(name); | |
| if (uuid == null) return null; | |
| return db | |
| .select() | |
| .from(schema.job) | |
| .where(eq(schema.job.id, uuid)) | |
| .then(takeFirstOrNull); | |
| } catch (error) { | |
| console.error('Failed to retrieve job:', error); | |
| return null; | |
| } | |
| }; |
Summary by CodeRabbit
extractUuidfor UUID extraction andgetJobfor retrieving job information.p-retrypackage and associated retry logic for dispatching GitHub jobs.