Skip to content

✨ Add type guards for events #6

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

Merged
merged 1 commit into from
Sep 27, 2024
Merged
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
15 changes: 14 additions & 1 deletion src/server/webhooks/events/pr/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, test } from "vitest"
import { isPrEventKey } from "./event.js"
import type { Event } from "../event.js"
import { isPrEvent, isPrEventKey } from "./event.js"

describe("isPrEventKey", () => {
test("pr:comment:added", ({ expect }) => {
@@ -12,3 +13,15 @@ describe("isPrEventKey", () => {
expect(result).toBe(false)
})
})

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

test("project:modified", ({ expect }) => {
const result = isPrEvent({ eventKey: "project:modified" } as Event)
expect(result).toBe(false)
})
})
5 changes: 5 additions & 0 deletions src/server/webhooks/events/pr/event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Event } from "../event.js"
import type { PRCommentAdded } from "./comment_added.js"
import type { PRCommentDeleted } from "./comment_deleted.js"
import type { PRCommentEdited } from "./comment_edited.js"
@@ -29,6 +30,10 @@ export type PrEvent =
| PRReviewerUpdated
export type PrEventKey = PrEvent["eventKey"]

export function isPrEvent(event: Event): event is PrEvent {
return isPrEventKey(event.eventKey)
}

export function isPrEventKey(key: unknown): key is PrEventKey {
return Object.values<unknown>(prEventKeys).includes(key)
}
17 changes: 16 additions & 1 deletion src/server/webhooks/events/project/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, test } from "vitest"
import { isProjectEventKey } from "./event.js"
import type { Event } from "../event.js"
import { isProjectEvent, isProjectEventKey } from "./event.js"

describe("isProjectEventKey", () => {
test("project:modified", ({ expect }) => {
@@ -12,3 +13,17 @@ describe("isProjectEventKey", () => {
expect(result).toBe(false)
})
})

describe("isProjectEvent", () => {
test("project:modified", ({ expect }) => {
const result = isProjectEvent({ eventKey: "project:modified" } as Event)
expect(result).toBe(true)
})

test("mirror:repo_synchronized", ({ expect }) => {
const result = isProjectEvent({
eventKey: "mirror:repo_synchronized",
} as Event)
expect(result).toBe(false)
})
})
5 changes: 5 additions & 0 deletions src/server/webhooks/events/project/event.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import type { Event } from "../event.js"
import type { ProjectModified } from "./modified.js"

/** You can create webhooks for events that occur in a project. */
export type ProjectEvent = ProjectModified
export type ProjectEventKey = ProjectEvent["eventKey"]

export function isProjectEvent(event: Event): event is ProjectEvent {
return isProjectEventKey(event.eventKey)
}

export function isProjectEventKey(key: unknown): key is ProjectEventKey {
return Object.values<unknown>(projectEventKeys).includes(key)
}
16 changes: 15 additions & 1 deletion src/server/webhooks/events/repo/event.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, test } from "vitest"
import { isRepoEventKey } from "./event.js"
import type { Event } from "../event.js"
import { isRepoEvent, isRepoEventKey } from "./event.js"

describe("isRepoEventKey", () => {
test("mirror:repo_synchronized", ({ expect }) => {
@@ -12,3 +13,16 @@ describe("isRepoEventKey", () => {
expect(result).toBe(false)
})
})
describe("isRepoEvent", () => {
test("mirror:repo_synchronized", ({ expect }) => {
const result = isRepoEvent({
eventKey: "mirror:repo_synchronized",
} as Event)
expect(result).toBe(true)
})

test("project:modified", ({ expect }) => {
const result = isRepoEvent({ eventKey: "project:modified" } as Event)
expect(result).toBe(false)
})
})
5 changes: 5 additions & 0 deletions src/server/webhooks/events/repo/event.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { Event } from "../event.js"
import type { RepoCommentAdded } from "./comment_added.js"
import type { RepoCommentDeleted } from "./comment_deleted.js"
import type { RepoCommentEdited } from "./comment_edited.js"
@@ -19,6 +20,10 @@ export type RepoEvent =
| RepoSecretDetected
export type RepoEventKey = RepoEvent["eventKey"]

export function isRepoEvent(event: Event): event is RepoEvent {
return isRepoEventKey(event.eventKey)
}

export function isRepoEventKey(key: unknown): key is RepoEventKey {
return Object.values<unknown>(repoEventKeys).includes(key)
}