Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add type guards for eventKey #5

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
NatoBoram marked this conversation as resolved.
Show resolved Hide resolved

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
NatoBoram marked this conversation as resolved.
Show resolved Hide resolved

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>
NatoBoram marked this conversation as resolved.
Show resolved Hide resolved
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)
}
NatoBoram marked this conversation as resolved.
Show resolved Hide resolved

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>