Skip to content

Commit

Permalink
✨ Add type guards for eventKey (#5)
Browse files Browse the repository at this point in the history
* ✨ Add type guards for eventKey

* ♻️ Refactor import statements in event.ts

* ♻️ Refactor event test files to use describe and test functions
  • Loading branch information
NatoBoram authored Sep 23, 2024
1 parent 877b14e commit 9f91213
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 28 deletions.
19 changes: 15 additions & 4 deletions src/server/webhooks/events/event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { PrEvent } from "./pr/event.js"
import { prEventKeys } from "./pr/event.js"
import type { ProjectEvent } from "./project/event.js"
import { projectEventKeys } from "./project/event.js"
import type { RepoEvent } from "./repo/event.js"
import { repoEventKeys } from "./repo/event.js"

/**
* When you have a webhook with an event, Bitbucket Data Center sends the event
Expand All @@ -17,8 +20,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>
14 changes: 14 additions & 0 deletions src/server/webhooks/events/pr/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, test } from "vitest"
import { isPrEventKey } from "./event.js"

describe("isPrEventKey", () => {
test("pr:comment:added", ({ expect }) => {
const result = isPrEventKey("pr:comment:added")
expect(result).toBe(true)
})

test("project:modified", ({ expect }) => {
const result = isPrEventKey("project:modified")
expect(result).toBe(false)
})
})
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>
14 changes: 14 additions & 0 deletions src/server/webhooks/events/project/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, test } from "vitest"
import { isProjectEventKey } from "./event.js"

describe("isProjectEventKey", () => {
test("project:modified", ({ expect }) => {
const result = isProjectEventKey("project:modified")
expect(result).toBe(true)
})

test("mirror:repo_synchronized", ({ expect }) => {
const result = isProjectEventKey("mirror:repo_synchronized")
expect(result).toBe(false)
})
})
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>
14 changes: 14 additions & 0 deletions src/server/webhooks/events/repo/event.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { describe, test } from "vitest"
import { isRepoEventKey } from "./event.js"

describe("isRepoEventKey", () => {
test("mirror:repo_synchronized", ({ expect }) => {
const result = isRepoEventKey("mirror:repo_synchronized")
expect(result).toBe(true)
})

test("project:modified", ({ expect }) => {
const result = isRepoEventKey("project:modified")
expect(result).toBe(false)
})
})
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 9f91213

Please sign in to comment.