1- import type { WorkspaceEngine } from "@ctrlplane/workspace-engine-sdk" ;
21import type { WorkflowRunEvent } from "@octokit/webhooks-types" ;
32
3+ import { eq , takeFirstOrNull } from "@ctrlplane/db" ;
44import { db } from "@ctrlplane/db/client" ;
55import * as schema from "@ctrlplane/db/schema" ;
6- import { Event , sendGoEvent } from "@ctrlplane/events " ;
6+ import { enqueueDesiredRelease } from "@ctrlplane/db/reconcilers " ;
77import { ReservedMetadataKey } from "@ctrlplane/validators/conditions" ;
88import { exitedStatus , JobStatus } from "@ctrlplane/validators/jobs" ;
99
@@ -29,36 +29,7 @@ const convertStatus = (
2929) : JobStatus =>
3030 status === "completed" ? JobStatus . Successful : JobStatus . InProgress ;
3131
32- const convertStatusToOapiStatus = (
33- status : JobStatus ,
34- ) : WorkspaceEngine [ "schemas" ] [ "JobStatus" ] => {
35- switch ( status ) {
36- case JobStatus . Successful :
37- return "successful" ;
38- case JobStatus . Cancelled :
39- return "cancelled" ;
40- case JobStatus . Skipped :
41- return "skipped" ;
42- case JobStatus . Pending :
43- return "pending" ;
44- case JobStatus . InProgress :
45- return "inProgress" ;
46- case JobStatus . ActionRequired :
47- return "actionRequired" ;
48- case JobStatus . InvalidJobAgent :
49- return "invalidJobAgent" ;
50- case JobStatus . InvalidIntegration :
51- return "invalidIntegration" ;
52- case JobStatus . ExternalRunNotFound :
53- return "externalRunNotFound" ;
54- case JobStatus . Failure :
55- return "failure" ;
56- }
57- } ;
58-
59- const generateOapiEvent = (
60- event : WorkflowRunEvent ,
61- ) : WorkspaceEngine [ "schemas" ] [ "JobUpdateEvent" ] | null => {
32+ export const handleWorkflowRunEvent = async ( event : WorkflowRunEvent ) => {
6233 const {
6334 id,
6435 status : externalStatus ,
@@ -70,77 +41,76 @@ const generateOapiEvent = (
7041 } = event . workflow_run ;
7142
7243 const jobId = extractUuid ( name ) ;
73- if ( jobId == null ) return null ;
44+ if ( jobId == null ) return ;
7445
75- const updatedAt = new Date ( updated_at ) ;
7646 const status =
7747 conclusion != null
7848 ? convertConclusion ( conclusion )
7949 : convertStatus ( externalStatus ) ;
8050
8151 const startedAt = new Date ( run_started_at ) ;
82- const isJobCompleted = exitedStatus . includes ( status ) ;
83- const completedAt = isJobCompleted ? updatedAt : null ;
84-
52+ const updatedAt = new Date ( updated_at ) ;
53+ const isCompleted = exitedStatus . includes ( status ) ;
54+ const completedAt = isCompleted ? updatedAt : null ;
8555 const externalId = id . toString ( ) ;
56+
57+ const [ updated ] = await db
58+ . update ( schema . job )
59+ . set ( {
60+ externalId,
61+ status,
62+ startedAt,
63+ completedAt,
64+ updatedAt,
65+ } )
66+ . where ( eq ( schema . job . id , jobId ) )
67+ . returning ( ) ;
68+
69+ if ( updated == null ) return ;
70+
8671 const Run = `https://github.com/${ repository . owner . login } /${ repository . name } /actions/runs/${ id } ` ;
8772 const Workflow = `${ Run } /workflow` ;
88- const links = { Run, Workflow } ;
89- const linksStr = JSON . stringify ( links ) ;
90- const metadata = {
91- [ String ( ReservedMetadataKey . Links ) ] : linksStr ,
92- run_url : Run ,
93- } as Record < string , string > ;
73+ const links = JSON . stringify ( { Run, Workflow } ) ;
9474
95- return {
96- id : jobId ,
97- job : {
98- id : jobId ,
99- externalId,
100- createdAt : startedAt . toISOString ( ) ,
101- updatedAt : updatedAt . toISOString ( ) ,
102- completedAt : completedAt ?. toISOString ( ) ?? undefined ,
103- startedAt : startedAt . toISOString ( ) ,
104- workflowJobId : "" ,
105- status : convertStatusToOapiStatus ( status ) ,
106- releaseId : "" ,
107- jobAgentConfig : {
108- type : "github-app" ,
109- installationId : event . installation ?. id ?? 0 ,
110- owner : repository . owner . login ,
111- repo : repository . name ,
112- workflowId : event . workflow_run . workflow_id ,
113- } ,
114- jobAgentId : "" ,
115- metadata,
116- } ,
117- fieldsToUpdate : [
118- "externalId" ,
119- "updatedAt" ,
120- "completedAt" ,
121- "startedAt" ,
122- "status" ,
123- "metadata" ,
124- ] ,
125- } ;
126- } ;
75+ const metadataEntries = [
76+ { jobId, key : String ( ReservedMetadataKey . Links ) , value : links } ,
77+ { jobId, key : "run_url" , value : Run } ,
78+ ] ;
12779
128- const getAllWorkspaceIds = ( ) =>
129- db
130- . select ( { id : schema . workspace . id } )
131- . from ( schema . workspace )
132- . then ( ( rows ) => rows . map ( ( row ) => row . id ) ) ;
80+ for ( const entry of metadataEntries ) {
81+ await db
82+ . insert ( schema . jobMetadata )
83+ . values ( entry )
84+ . onConflictDoUpdate ( {
85+ target : [ schema . jobMetadata . key , schema . jobMetadata . jobId ] ,
86+ set : { value : entry . value } ,
87+ } ) ;
88+ }
13389
134- export const handleWorkflowRunEvent = async ( event : WorkflowRunEvent ) => {
135- const oapiEvent = generateOapiEvent ( event ) ;
136- if ( oapiEvent == null ) return ;
137- const workspaceIds = await getAllWorkspaceIds ( ) ;
90+ const releaseTarget = await db
91+ . select ( {
92+ deploymentId : schema . release . deploymentId ,
93+ environmentId : schema . release . environmentId ,
94+ resourceId : schema . release . resourceId ,
95+ workspaceId : schema . deployment . workspaceId ,
96+ } )
97+ . from ( schema . releaseJob )
98+ . innerJoin (
99+ schema . release ,
100+ eq ( schema . releaseJob . releaseId , schema . release . id ) ,
101+ )
102+ . innerJoin (
103+ schema . deployment ,
104+ eq ( schema . release . deploymentId , schema . deployment . id ) ,
105+ )
106+ . where ( eq ( schema . releaseJob . jobId , jobId ) )
107+ . then ( takeFirstOrNull ) ;
138108
139- for ( const workspaceId of workspaceIds )
140- await sendGoEvent ( {
141- workspaceId,
142- eventType : Event . JobUpdated ,
143- data : oapiEvent ,
144- timestamp : Date . now ( ) ,
109+ if ( releaseTarget ?. workspaceId != null )
110+ await enqueueDesiredRelease ( db , {
111+ workspaceId : releaseTarget . workspaceId ,
112+ deploymentId : releaseTarget . deploymentId ,
113+ environmentId : releaseTarget . environmentId ,
114+ resourceId : releaseTarget . resourceId ,
145115 } ) ;
146116} ;
0 commit comments