Skip to content

Commit

Permalink
fix: Add started_at and completed_at on jobs (#283)
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 authored Jan 20, 2025
1 parent 3307270 commit 912f903
Show file tree
Hide file tree
Showing 7 changed files with 4,609 additions and 7 deletions.
21 changes: 14 additions & 7 deletions apps/webservice/src/app/api/github/webhook/workflow/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { db } from "@ctrlplane/db/client";
import * as schema from "@ctrlplane/db/schema";
import { updateJob } from "@ctrlplane/job-dispatch";
import { ReservedMetadataKey } from "@ctrlplane/validators/conditions";
import { JobStatus } from "@ctrlplane/validators/jobs";
import { exitedStatus, JobStatus } from "@ctrlplane/validators/jobs";

type Conclusion = Exclude<WorkflowRunEvent["workflow_run"]["conclusion"], null>;
const convertConclusion = (conclusion: Conclusion): schema.JobStatus => {
Expand Down Expand Up @@ -55,6 +55,8 @@ export const handleWorkflowWebhookEvent = async (event: WorkflowRunEvent) => {
conclusion,
repository,
name,
run_started_at,
updated_at,
} = event.workflow_run;

const job = await getJob(id, name);
Expand All @@ -65,14 +67,19 @@ export const handleWorkflowWebhookEvent = async (event: WorkflowRunEvent) => {
? convertConclusion(conclusion)
: convertStatus(externalStatus);

const startedAt = new Date(run_started_at);
const isJobCompleted = exitedStatus.includes(status as JobStatus);

// the workflow run object doesn't have an explicit completedAt field
// but if the job is in an exited state, the updated_at field works as a proxy
// since thats the last time the job was updated
const completedAt = isJobCompleted ? new Date(updated_at) : null;

const externalId = id.toString();
const GitHub = `https://github.com/${repository.owner.login}/${repository.name}/actions/runs/${id}`;
await updateJob(
job.id,
{ status, externalId },
{
[String(ReservedMetadataKey.Links)]: {
GitHub: `https://github.com/${repository.owner.login}/${repository.name}/actions/runs/${id}`,
},
},
{ status, externalId, startedAt, completedAt },
{ [String(ReservedMetadataKey.Links)]: { GitHub } },
);
};
10 changes: 10 additions & 0 deletions openapi.v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2655,6 +2655,16 @@
"type": "string",
"format": "date-time"
},
"startedAt": {
"type": "string",
"format": "date-time",
"nullable": true
},
"completedAt": {
"type": "string",
"format": "date-time",
"nullable": true
},
"updatedAt": {
"type": "string",
"format": "date-time"
Expand Down
2 changes: 2 additions & 0 deletions packages/db/drizzle/0057_keen_trish_tilby.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "job" ADD COLUMN "started_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "job" ADD COLUMN "completed_at" timestamp with time zone;
Loading

0 comments on commit 912f903

Please sign in to comment.