Skip to content

Commit

Permalink
✨ Add type guards for eventKey
Browse files Browse the repository at this point in the history
  • Loading branch information
NatoBoram committed Sep 23, 2024
1 parent 877b14e commit 720fa4b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 31 deletions.
22 changes: 15 additions & 7 deletions src/server/webhooks/events/event.ts
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<unknown>(eventKeys).includes(key)
}

const eventKeys = {
...prEventKeys,
...projectEventKeys,
...repoEventKeys,
} as const

eventKeys satisfies Record<EventKey, EventKey>
7 changes: 7 additions & 0 deletions src/server/webhooks/events/pr/event.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
36 changes: 22 additions & 14 deletions src/server/webhooks/events/pr/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>(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<PrEventKey, PrEventKey>
7 changes: 7 additions & 0 deletions src/server/webhooks/events/project/event.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
12 changes: 11 additions & 1 deletion src/server/webhooks/events/project/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>(projectEventKeys).includes(key)
}

export const projectEventKeys = {
"project:modified": "project:modified",
} as const

projectEventKeys satisfies Record<ProjectEventKey, ProjectEventKey>
7 changes: 7 additions & 0 deletions src/server/webhooks/events/repo/event.test.ts
Original file line number Diff line number Diff line change
@@ -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)
})
27 changes: 18 additions & 9 deletions src/server/webhooks/events/repo/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<unknown>(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<RepoEventKey, RepoEventKey>

0 comments on commit 720fa4b

Please sign in to comment.