Fix GraphQL review event to state mapping
swadhinbiswas committed 8 months ago
4187d66
1 parent 12bea2e
commit 4187d66
2 files changed
+85 -1
Showing 2 changed files+85-1
▼src/lib/graphql/resolvers.ts
+11-1
| @@ -601,13 +601,23 @@ export const resolvers = { | ||
601 | 601 | } |
602 | 602 | } |
603 | 603 | |
604 | + const reviewStateByEvent: Record<string, "commented" | "approved" | "changes_requested"> = { | |
605 | + COMMENT: "commented", | |
606 | + APPROVE: "approved", | |
607 | + REQUEST_CHANGES: "changes_requested", | |
608 | + }; | |
609 | + const mappedState = reviewStateByEvent[input.event]; | |
610 | + if (!mappedState) { | |
611 | + throw new Error("Invalid review event"); | |
612 | + } | |
613 | + | |
604 | 614 | const reviewId = generateId(); |
605 | 615 | |
606 | 616 | await ctx.db.insert(schema.pullRequestReviews).values({ |
607 | 617 | id: reviewId, |
608 | 618 | pullRequestId: pullRequest.id, |
609 | 619 | reviewerId: ctx.userId, |
610 | - state: input.event.toLowerCase(), // APPROVED, REQUEST_CHANGES, COMMENT, etc. | |
620 | + state: mappedState, | |
611 | 621 | body: input.body, |
612 | 622 | submittedAt: new Date(), |
613 | 623 | createdAt: new Date(), |
614 | 624 | |
▼tests/unit/graphql-review-state-mapping.test.ts
+74-0
| @@ -0,0 +1,74 @@ | ||
1 | +import { beforeEach, describe, expect, it, vi } from "vitest"; | |
2 | + | |
3 | +const { canWriteRepoMock, checkPathPermissionsMock } = vi.hoisted(() => ({ | |
4 | + canWriteRepoMock: vi.fn(async () => true), | |
5 | + checkPathPermissionsMock: vi.fn(async () => ({ allowed: true, deniedPaths: [] })), | |
6 | +})); | |
7 | + | |
8 | +vi.mock("@/lib/permissions", async () => { | |
9 | + const actual = await vi.importActual<any>("@/lib/permissions"); | |
10 | + return { | |
11 | + ...actual, | |
12 | + canWriteRepo: canWriteRepoMock, | |
13 | + }; | |
14 | +}); | |
15 | + | |
16 | +vi.mock("@/lib/path-scoping", () => ({ | |
17 | + checkPathPermissions: checkPathPermissionsMock, | |
18 | +})); | |
19 | + | |
20 | +import { resolvers } from "@/lib/graphql/resolvers"; | |
21 | + | |
22 | +function makeContext() { | |
23 | + const insertCalls: any[] = []; | |
24 | + return { | |
25 | + db: { | |
26 | + query: { | |
27 | + pullRequests: { | |
28 | + findFirst: vi.fn(async () => ({ id: "pr-1", repositoryId: "repo-1" })), | |
29 | + }, | |
30 | + repositories: { | |
31 | + findFirst: vi.fn(async () => ({ id: "repo-1" })), | |
32 | + }, | |
33 | + pullRequestReviews: { | |
34 | + findFirst: vi.fn(async () => ({ id: "review-1" })), | |
35 | + }, | |
36 | + }, | |
37 | + insert: vi.fn(() => ({ | |
38 | + values: vi.fn(async (value: unknown) => { | |
39 | + insertCalls.push(value); | |
40 | + }), | |
41 | + })), | |
42 | + __state: { insertCalls }, | |
43 | + }, | |
44 | + userId: "user-1", | |
45 | + user: { isAdmin: false }, | |
46 | + } as any; | |
47 | +} | |
48 | + | |
49 | +describe("GraphQL review state mapping", () => { | |
50 | + beforeEach(() => { | |
51 | + canWriteRepoMock.mockResolvedValue(true); | |
52 | + checkPathPermissionsMock.mockResolvedValue({ allowed: true, deniedPaths: [] }); | |
53 | + }); | |
54 | + | |
55 | + it("maps REQUEST_CHANGES event to changes_requested review state", async () => { | |
56 | + const ctx = makeContext(); | |
57 | + | |
58 | + await resolvers.Mutation.addPullRequestReview( | |
59 | + {}, | |
60 | + { | |
61 | + input: { | |
62 | + pullRequestId: "pr-1", | |
63 | + event: "REQUEST_CHANGES", | |
64 | + body: "Needs updates", | |
65 | + comments: [], | |
66 | + }, | |
67 | + }, | |
68 | + ctx | |
69 | + ); | |
70 | + | |
71 | + const insertedReview = ctx.db.__state.insertCalls[0]; | |
72 | + expect(insertedReview.state).toBe("changes_requested"); | |
73 | + }); | |
74 | +}); | |
0 | 75 | |