Skip to content

Commit ee1b774

Browse files
chore: github entity events
1 parent 480c48a commit ee1b774

File tree

9 files changed

+172
-0
lines changed

9 files changed

+172
-0
lines changed

apps/event-queue/src/events/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ const workspaceHandlers: Record<Event, Handler<any>> = {
7979
[Event.JobAgentCreated]: () => Promise.resolve(),
8080
[Event.JobAgentUpdated]: () => Promise.resolve(),
8181
[Event.JobAgentDeleted]: () => Promise.resolve(),
82+
[Event.GithubEntityCreated]: () => Promise.resolve(),
83+
[Event.GithubEntityUpdated]: () => Promise.resolve(),
84+
[Event.GithubEntityDeleted]: () => Promise.resolve(),
8285
};
8386

8487
export type Handler<T extends keyof EventPayload> = (

apps/workspace-engine/pkg/events/events.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"workspace-engine/pkg/events/handler/deploymentvariables"
77
"workspace-engine/pkg/events/handler/deploymentversion"
88
"workspace-engine/pkg/events/handler/environment"
9+
"workspace-engine/pkg/events/handler/githubentities"
910
"workspace-engine/pkg/events/handler/jobagents"
1011
"workspace-engine/pkg/events/handler/jobs"
1112
"workspace-engine/pkg/events/handler/policies"
@@ -66,6 +67,10 @@ var handlers = handler.HandlerRegistry{
6667
handler.UserApprovalRecordCreate: userapprovalrecords.HandleUserApprovalRecordCreated,
6768
handler.UserApprovalRecordUpdate: userapprovalrecords.HandleUserApprovalRecordUpdated,
6869
handler.UserApprovalRecordDelete: userapprovalrecords.HandleUserApprovalRecordDeleted,
70+
71+
handler.GithubEntityCreate: githubentities.HandleGithubEntityCreated,
72+
handler.GithubEntityUpdate: githubentities.HandleGithubEntityUpdated,
73+
handler.GithubEntityDelete: githubentities.HandleGithubEntityDeleted,
6974
}
7075

7176
func NewEventHandler() *handler.EventListener {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package githubentities
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"workspace-engine/pkg/events/handler"
7+
"workspace-engine/pkg/oapi"
8+
"workspace-engine/pkg/workspace"
9+
)
10+
11+
func HandleGithubEntityCreated(ctx context.Context, ws *workspace.Workspace, event handler.RawEvent) error {
12+
githubEntity := &oapi.GithubEntity{}
13+
if err := json.Unmarshal(event.Data, githubEntity); err != nil {
14+
return err
15+
}
16+
17+
ws.GithubEntities().Upsert(ctx, githubEntity)
18+
19+
return nil
20+
}
21+
22+
func HandleGithubEntityUpdated(ctx context.Context, ws *workspace.Workspace, event handler.RawEvent) error {
23+
githubEntity := &oapi.GithubEntity{}
24+
if err := json.Unmarshal(event.Data, githubEntity); err != nil {
25+
return err
26+
}
27+
28+
ws.GithubEntities().Upsert(ctx, githubEntity)
29+
30+
return nil
31+
}
32+
33+
func HandleGithubEntityDeleted(ctx context.Context, ws *workspace.Workspace, event handler.RawEvent) error {
34+
githubEntity := &oapi.GithubEntity{}
35+
if err := json.Unmarshal(event.Data, githubEntity); err != nil {
36+
return err
37+
}
38+
39+
ws.GithubEntities().Remove(ctx, githubEntity.Slug, githubEntity.InstallationId)
40+
41+
return nil
42+
}

apps/workspace-engine/pkg/events/handler/handler.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ const (
7070
UserApprovalRecordCreate EventType = "user-approval-record.created"
7171
UserApprovalRecordUpdate EventType = "user-approval-record.updated"
7272
UserApprovalRecordDelete EventType = "user-approval-record.deleted"
73+
74+
GithubEntityCreate EventType = "github-entity.created"
75+
GithubEntityUpdate EventType = "github-entity.updated"
76+
GithubEntityDelete EventType = "github-entity.deleted"
7377
)
7478

7579
// RawEvent represents the raw event data received from Kafka messages

packages/events/src/dispatch-jobs.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,4 +399,16 @@ export class BullMQEventDispatcher implements EventDispatcher {
399399
): Promise<void> {
400400
return Promise.resolve();
401401
}
402+
403+
async dispatchGithubEntityCreated(_: schema.GithubEntity): Promise<void> {
404+
return Promise.resolve();
405+
}
406+
407+
async dispatchGithubEntityUpdated(_: schema.GithubEntity): Promise<void> {
408+
return Promise.resolve();
409+
}
410+
411+
async dispatchGithubEntityDeleted(_: schema.GithubEntity): Promise<void> {
412+
return Promise.resolve();
413+
}
402414
}

packages/events/src/event-dispatcher.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,8 @@ export interface EventDispatcher {
112112
dispatchUserApprovalRecordDeleted(
113113
userApprovalRecord: schema.PolicyRuleAnyApprovalRecord,
114114
): Promise<void>;
115+
116+
dispatchGithubEntityCreated(githubEntity: schema.GithubEntity): Promise<void>;
117+
dispatchGithubEntityUpdated(githubEntity: schema.GithubEntity): Promise<void>;
118+
dispatchGithubEntityDeleted(githubEntity: schema.GithubEntity): Promise<void>;
115119
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import type * as schema from "@ctrlplane/db/schema";
2+
import type { Span } from "@ctrlplane/logger";
3+
import type { WorkspaceEngine } from "@ctrlplane/workspace-engine-sdk";
4+
5+
import type { GoEventPayload, GoMessage } from "../events.js";
6+
import { createSpanWrapper } from "../../span.js";
7+
import { sendGoEvent } from "../client.js";
8+
import { Event } from "../events.js";
9+
10+
const getOapiGithubEntity = (
11+
githubEntity: schema.GithubEntity,
12+
): WorkspaceEngine["schemas"]["GithubEntity"] => ({
13+
installationId: githubEntity.installationId,
14+
slug: githubEntity.slug,
15+
});
16+
17+
const convertGithubEntityToGoEvent = (
18+
githubEntity: schema.GithubEntity,
19+
eventType: keyof GoEventPayload,
20+
): GoMessage<keyof GoEventPayload> => ({
21+
workspaceId: githubEntity.workspaceId,
22+
eventType,
23+
data: getOapiGithubEntity(githubEntity),
24+
timestamp: Date.now(),
25+
});
26+
27+
export const dispatchGithubEntityCreated = createSpanWrapper(
28+
"dispatchGithubEntityCreated",
29+
async (span: Span, githubEntity: schema.GithubEntity) => {
30+
span.setAttribute(
31+
"github-entity.installationId",
32+
githubEntity.installationId,
33+
);
34+
span.setAttribute("github-entity.slug", githubEntity.slug);
35+
span.setAttribute("github-entity.workspaceId", githubEntity.workspaceId);
36+
37+
await sendGoEvent(
38+
convertGithubEntityToGoEvent(githubEntity, Event.GithubEntityCreated),
39+
);
40+
},
41+
);
42+
43+
export const dispatchGithubEntityUpdated = createSpanWrapper(
44+
"dispatchGithubEntityUpdated",
45+
async (span: Span, githubEntity: schema.GithubEntity) => {
46+
span.setAttribute(
47+
"github-entity.installationId",
48+
githubEntity.installationId,
49+
);
50+
span.setAttribute("github-entity.slug", githubEntity.slug);
51+
span.setAttribute("github-entity.workspaceId", githubEntity.workspaceId);
52+
53+
await sendGoEvent(
54+
convertGithubEntityToGoEvent(githubEntity, Event.GithubEntityUpdated),
55+
);
56+
},
57+
);
58+
59+
export const dispatchGithubEntityDeleted = createSpanWrapper(
60+
"dispatchGithubEntityDeleted",
61+
async (span: Span, githubEntity: schema.GithubEntity) => {
62+
span.setAttribute(
63+
"github-entity.installationId",
64+
githubEntity.installationId,
65+
);
66+
span.setAttribute("github-entity.slug", githubEntity.slug);
67+
span.setAttribute("github-entity.workspaceId", githubEntity.workspaceId);
68+
69+
await sendGoEvent(
70+
convertGithubEntityToGoEvent(githubEntity, Event.GithubEntityDeleted),
71+
);
72+
},
73+
);

packages/events/src/kafka/event-dispatch/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as deploymentVariableDispatch from "./deployment-variable.js";
55
import * as deploymentVersionDispatch from "./deployment-version.js";
66
import * as deploymentDispatch from "./deployment.js";
77
import * as environmentDispatch from "./environment.js";
8+
import * as githubEntityDispatch from "./github-entities.js";
89
import * as jobAgentDispatch from "./job-agent.js";
910
import * as jobDispatch from "./job.js";
1011
import * as policyDispatch from "./policy.js";
@@ -243,4 +244,22 @@ export class KafkaEventDispatcher implements EventDispatcher {
243244
userApprovalRecord,
244245
);
245246
}
247+
248+
async dispatchGithubEntityCreated(
249+
githubEntity: schema.GithubEntity,
250+
): Promise<void> {
251+
await githubEntityDispatch.dispatchGithubEntityCreated(githubEntity);
252+
}
253+
254+
async dispatchGithubEntityUpdated(
255+
githubEntity: schema.GithubEntity,
256+
): Promise<void> {
257+
await githubEntityDispatch.dispatchGithubEntityUpdated(githubEntity);
258+
}
259+
260+
async dispatchGithubEntityDeleted(
261+
githubEntity: schema.GithubEntity,
262+
): Promise<void> {
263+
await githubEntityDispatch.dispatchGithubEntityDeleted(githubEntity);
264+
}
246265
}

packages/events/src/kafka/events.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ export enum Event {
5353
UserApprovalRecordUpdated = "user-approval-record.updated",
5454
UserApprovalRecordDeleted = "user-approval-record.deleted",
5555

56+
GithubEntityCreated = "github-entity.created",
57+
GithubEntityUpdated = "github-entity.updated",
58+
GithubEntityDeleted = "github-entity.deleted",
59+
5660
// ReleaseCreated = "release.created",
5761
// ReleaseUpdated = "release.updated",
5862
// ReleaseDeleted = "release.deleted",
@@ -141,6 +145,9 @@ export type EventPayload = {
141145
[Event.UserApprovalRecordCreated]: schema.PolicyRuleAnyApprovalRecord;
142146
[Event.UserApprovalRecordUpdated]: schema.PolicyRuleAnyApprovalRecord;
143147
[Event.UserApprovalRecordDeleted]: schema.PolicyRuleAnyApprovalRecord;
148+
[Event.GithubEntityCreated]: schema.GithubEntity;
149+
[Event.GithubEntityUpdated]: schema.GithubEntity;
150+
[Event.GithubEntityDeleted]: schema.GithubEntity;
144151
[Event.SystemCreated]: schema.System;
145152
[Event.SystemUpdated]: schema.System;
146153
[Event.SystemDeleted]: schema.System;
@@ -183,6 +190,9 @@ export type GoEventPayload = {
183190
[Event.UserApprovalRecordCreated]: WorkspaceEngine["schemas"]["UserApprovalRecord"];
184191
[Event.UserApprovalRecordUpdated]: WorkspaceEngine["schemas"]["UserApprovalRecord"];
185192
[Event.UserApprovalRecordDeleted]: WorkspaceEngine["schemas"]["UserApprovalRecord"];
193+
[Event.GithubEntityCreated]: WorkspaceEngine["schemas"]["GithubEntity"];
194+
[Event.GithubEntityUpdated]: WorkspaceEngine["schemas"]["GithubEntity"];
195+
[Event.GithubEntityDeleted]: WorkspaceEngine["schemas"]["GithubEntity"];
186196
};
187197

188198
export type Message<T extends keyof EventPayload> = {

0 commit comments

Comments
 (0)