diff --git a/src/server/webhooks/events/event.ts b/src/server/webhooks/events/event.ts index e015af3..fe37d8e 100644 --- a/src/server/webhooks/events/event.ts +++ b/src/server/webhooks/events/event.ts @@ -1,6 +1,6 @@ -import type { PrEvent } from "./pr/event.js" -import type { ProjectEvent } from "./project/event.js" -import type { RepoEvent } from "./repo/event.js" +import { prEventKeys, type PrEvent } from "./pr/event.js" +import { projectEventKeys, type ProjectEvent } from "./project/event.js" +import { repoEventKeys, type RepoEvent } from "./repo/event.js" /** * When you have a webhook with an event, Bitbucket Data Center sends the event @@ -17,8 +17,16 @@ import type { RepoEvent } from "./repo/event.js" * event's user. */ export type Event = PrEvent | ProjectEvent | RepoEvent +export type EventKey = Event["eventKey"] -export type EventKey = - | PrEvent["eventKey"] - | ProjectEvent["eventKey"] - | RepoEvent["eventKey"] +export function isEventKey(key: unknown): key is EventKey { + return Object.values(eventKeys).includes(key) +} + +const eventKeys = { + ...prEventKeys, + ...projectEventKeys, + ...repoEventKeys, +} as const + +eventKeys satisfies Record diff --git a/src/server/webhooks/events/pr/event.test.ts b/src/server/webhooks/events/pr/event.test.ts new file mode 100644 index 0000000..e2c544b --- /dev/null +++ b/src/server/webhooks/events/pr/event.test.ts @@ -0,0 +1,7 @@ +import { test } from "vitest" +import { isPrEventKey } from "./event.js" + +test("isPrEventKey", ({ expect }) => { + const result = isPrEventKey("pr:comment:added") + expect(result).toBe(true) +}) diff --git a/src/server/webhooks/events/pr/event.ts b/src/server/webhooks/events/pr/event.ts index caee61f..f5fbe51 100644 --- a/src/server/webhooks/events/pr/event.ts +++ b/src/server/webhooks/events/pr/event.ts @@ -27,18 +27,26 @@ export type PrEvent = | PRReviewerChangesRequested | PRReviewerUnapproved | PRReviewerUpdated +export type PrEventKey = PrEvent["eventKey"] -export type PrEventKey = - | PRCommentAdded["eventKey"] - | PRCommentDeleted["eventKey"] - | PRCommentEdited["eventKey"] - | PRDeclined["eventKey"] - | PRDeleted["eventKey"] - | PRFromRefUpdated["eventKey"] - | PRMerged["eventKey"] - | PRModified["eventKey"] - | PROpened["eventKey"] - | PRReviewerApproved["eventKey"] - | PRReviewerChangesRequested["eventKey"] - | PRReviewerUnapproved["eventKey"] - | PRReviewerUpdated["eventKey"] +export function isPrEventKey(key: unknown): key is PrEventKey { + return Object.values(prEventKeys).includes(key) +} + +export const prEventKeys = { + "pr:comment:added": "pr:comment:added", + "pr:comment:deleted": "pr:comment:deleted", + "pr:comment:edited": "pr:comment:edited", + "pr:declined": "pr:declined", + "pr:deleted": "pr:deleted", + "pr:from_ref_updated": "pr:from_ref_updated", + "pr:merged": "pr:merged", + "pr:modified": "pr:modified", + "pr:opened": "pr:opened", + "pr:reviewer:approved": "pr:reviewer:approved", + "pr:reviewer:changes_requested": "pr:reviewer:changes_requested", + "pr:reviewer:unapproved": "pr:reviewer:unapproved", + "pr:reviewer:updated": "pr:reviewer:updated", +} as const + +prEventKeys satisfies Record diff --git a/src/server/webhooks/events/project/event.test.ts b/src/server/webhooks/events/project/event.test.ts new file mode 100644 index 0000000..866cf2e --- /dev/null +++ b/src/server/webhooks/events/project/event.test.ts @@ -0,0 +1,7 @@ +import { test } from "vitest" +import { isProjectEventKey } from "./event.js" + +test("isProjectEventKey", ({ expect }) => { + const result = isProjectEventKey("project:modified") + expect(result).toBe(true) +}) diff --git a/src/server/webhooks/events/project/event.ts b/src/server/webhooks/events/project/event.ts index 797c216..3c1ab9f 100644 --- a/src/server/webhooks/events/project/event.ts +++ b/src/server/webhooks/events/project/event.ts @@ -2,4 +2,14 @@ import type { ProjectModified } from "./modified.js" /** You can create webhooks for events that occur in a project. */ export type ProjectEvent = ProjectModified -export type ProjectEventKey = ProjectModified["eventKey"] +export type ProjectEventKey = ProjectEvent["eventKey"] + +export function isProjectEventKey(key: unknown): key is ProjectEventKey { + return Object.values(projectEventKeys).includes(key) +} + +export const projectEventKeys = { + "project:modified": "project:modified", +} as const + +projectEventKeys satisfies Record diff --git a/src/server/webhooks/events/repo/event.test.ts b/src/server/webhooks/events/repo/event.test.ts new file mode 100644 index 0000000..f5663bc --- /dev/null +++ b/src/server/webhooks/events/repo/event.test.ts @@ -0,0 +1,7 @@ +import { test } from "vitest" +import { isRepoEventKey } from "./event.js" + +test("isRepoEventKey", ({ expect }) => { + const result = isRepoEventKey("mirror:repo_synchronized") + expect(result).toBe(true) +}) diff --git a/src/server/webhooks/events/repo/event.ts b/src/server/webhooks/events/repo/event.ts index 7880585..a102c8a 100644 --- a/src/server/webhooks/events/repo/event.ts +++ b/src/server/webhooks/events/repo/event.ts @@ -17,12 +17,21 @@ export type RepoEvent = | RepoModified | RepoRefsChanged | RepoSecretDetected -export type RepoEventKey = - | MirrorRepoSynchronized["eventKey"] - | RepoCommentAdded["eventKey"] - | RepoCommentDeleted["eventKey"] - | RepoCommentEdited["eventKey"] - | RepoForked["eventKey"] - | RepoModified["eventKey"] - | RepoRefsChanged["eventKey"] - | RepoSecretDetected["eventKey"] +export type RepoEventKey = RepoEvent["eventKey"] + +export function isRepoEventKey(key: unknown): key is RepoEventKey { + return Object.values(repoEventKeys).includes(key) +} + +export const repoEventKeys = { + "mirror:repo_synchronized": "mirror:repo_synchronized", + "repo:comment:added": "repo:comment:added", + "repo:comment:deleted": "repo:comment:deleted", + "repo:comment:edited": "repo:comment:edited", + "repo:forked": "repo:forked", + "repo:modified": "repo:modified", + "repo:refs_changed": "repo:refs_changed", + "repo:secret_detected": "repo:secret_detected", +} as const + +repoEventKeys satisfies Record