|
| 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 | +); |
0 commit comments