pull-request.ts219 lines, 5.5 KB
| 1 | import { z } from "zod"; |
| 2 | import { TRPCError } from "@trpc/server"; |
| 3 | import { createTRPCRouter, protectedProcedure } from "../trpc"; |
| 4 | import { |
| 5 | fetchPullRequests, |
| 6 | fetchPullRequest, |
| 7 | getGitHubAccessToken, |
| 8 | fetchPullRequestFiles, |
| 9 | } from "@/server/services/github"; |
| 10 | |
| 11 | export const pullRequestRouter = createTRPCRouter({ |
| 12 | list: protectedProcedure |
| 13 | .input( |
| 14 | z.object({ |
| 15 | repositoryId: z.string(), |
| 16 | state: z.enum(["open", "closed", "all"]).default("open"), |
| 17 | }), |
| 18 | ) |
| 19 | .query(async ({ ctx, input }) => { |
| 20 | const repository = await ctx.db.repository.findUnique({ |
| 21 | where: { id: input.repositoryId, userId: ctx.user.id }, |
| 22 | }); |
| 23 | |
| 24 | if (!repository) { |
| 25 | throw new TRPCError({ |
| 26 | code: "NOT_FOUND", |
| 27 | message: "Repository not found", |
| 28 | }); |
| 29 | } |
| 30 | |
| 31 | const accessToken = await getGitHubAccessToken(ctx.user.id); |
| 32 | if (!accessToken) { |
| 33 | throw new TRPCError({ |
| 34 | code: "PRECONDITION_FAILED", |
| 35 | message: "Github account not connected", |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | const [owner, repo] = repository.fullName.split("/"); |
| 40 | if (!owner || !repo) { |
| 41 | throw new TRPCError({ |
| 42 | code: "BAD_REQUEST", |
| 43 | message: "Invalid repository name", |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | const prs = await fetchPullRequests( |
| 48 | accessToken, |
| 49 | owner, |
| 50 | repo, |
| 51 | input.state, |
| 52 | ); |
| 53 | |
| 54 | const existingReviews = await ctx.db.review.findMany({ |
| 55 | where: { |
| 56 | repositoryId: repository.id, |
| 57 | prNumber: { in: prs.map((pr) => pr.number) }, |
| 58 | }, |
| 59 | select: { |
| 60 | prNumber: true, |
| 61 | status: true, |
| 62 | createdAt: true, |
| 63 | }, |
| 64 | orderBy: { createdAt: "desc" }, |
| 65 | }); |
| 66 | |
| 67 | const reviewMap = new Map(existingReviews.map((r) => [r.prNumber, r])); |
| 68 | |
| 69 | return prs.map((pr) => ({ |
| 70 | id: pr.id, |
| 71 | number: pr.number, |
| 72 | title: pr.title, |
| 73 | state: pr.state, |
| 74 | draft: pr.draft, |
| 75 | htmlUrl: pr.html_url, |
| 76 | author: { |
| 77 | login: pr.user.login, |
| 78 | avatarUrl: pr.user.avatar_url, |
| 79 | }, |
| 80 | headRef: pr.head.ref, |
| 81 | baseRef: pr.base.ref, |
| 82 | additions: pr.additions, |
| 83 | deletions: pr.deletions, |
| 84 | changedFiles: pr.changed_files, |
| 85 | createdAt: pr.created_at, |
| 86 | updatedAt: pr.updated_at, |
| 87 | mergedAt: pr.merged_at, |
| 88 | review: reviewMap.get(pr.number) ?? null, |
| 89 | })); |
| 90 | }), |
| 91 | |
| 92 | get: protectedProcedure |
| 93 | .input( |
| 94 | z.object({ |
| 95 | repositoryId: z.string(), |
| 96 | prNumber: z.number(), |
| 97 | }), |
| 98 | ) |
| 99 | .query(async ({ ctx, input }) => { |
| 100 | const repository = await ctx.db.repository.findUnique({ |
| 101 | where: { id: input.repositoryId, userId: ctx.user.id }, |
| 102 | }); |
| 103 | |
| 104 | if (!repository) { |
| 105 | throw new TRPCError({ |
| 106 | code: "NOT_FOUND", |
| 107 | message: "Repository not found", |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | const accessToken = await getGitHubAccessToken(ctx.user.id); |
| 112 | if (!accessToken) { |
| 113 | throw new TRPCError({ |
| 114 | code: "PRECONDITION_FAILED", |
| 115 | message: "GitHub account not connected", |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | const [owner, repo] = repository.fullName.split("/"); |
| 120 | if (!owner || !repo) { |
| 121 | throw new TRPCError({ |
| 122 | code: "BAD_REQUEST", |
| 123 | message: "Invalid repository name", |
| 124 | }); |
| 125 | } |
| 126 | |
| 127 | const pr = await fetchPullRequest( |
| 128 | accessToken, |
| 129 | owner, |
| 130 | repo, |
| 131 | input.prNumber, |
| 132 | ); |
| 133 | |
| 134 | const existingReview = await ctx.db.review.findFirst({ |
| 135 | where: { |
| 136 | repositoryId: repository.id, |
| 137 | prNumber: pr.number, |
| 138 | }, |
| 139 | orderBy: { createdAt: "desc" }, |
| 140 | }); |
| 141 | |
| 142 | return { |
| 143 | id: pr.id, |
| 144 | number: pr.number, |
| 145 | title: pr.title, |
| 146 | state: pr.state, |
| 147 | draft: pr.draft, |
| 148 | htmlUrl: pr.html_url, |
| 149 | author: { |
| 150 | login: pr.user.login, |
| 151 | avatarUrl: pr.user.avatar_url, |
| 152 | }, |
| 153 | headRef: pr.head.ref, |
| 154 | headSha: pr.head.sha, |
| 155 | baseRef: pr.base.ref, |
| 156 | additions: pr.additions, |
| 157 | deletions: pr.deletions, |
| 158 | changedFiles: pr.changed_files, |
| 159 | createdAt: pr.created_at, |
| 160 | updatedAt: pr.updated_at, |
| 161 | mergedAt: pr.merged_at, |
| 162 | review: existingReview, |
| 163 | }; |
| 164 | }), |
| 165 | |
| 166 | files: protectedProcedure |
| 167 | .input( |
| 168 | z.object({ |
| 169 | repositoryId: z.string(), |
| 170 | prNumber: z.number(), |
| 171 | }), |
| 172 | ) |
| 173 | .query(async ({ ctx, input }) => { |
| 174 | const repository = await ctx.db.repository.findUnique({ |
| 175 | where: { id: input.repositoryId, userId: ctx.user.id }, |
| 176 | }); |
| 177 | |
| 178 | if (!repository) { |
| 179 | throw new TRPCError({ |
| 180 | code: "NOT_FOUND", |
| 181 | message: "Repository not found", |
| 182 | }); |
| 183 | } |
| 184 | |
| 185 | const accessToken = await getGitHubAccessToken(ctx.user.id); |
| 186 | if (!accessToken) { |
| 187 | throw new TRPCError({ |
| 188 | code: "PRECONDITION_FAILED", |
| 189 | message: "GitHub account not connected", |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | const [owner, repo] = repository.fullName.split("/"); |
| 194 | if (!owner || !repo) { |
| 195 | throw new TRPCError({ |
| 196 | code: "BAD_REQUEST", |
| 197 | message: "Invalid repository name", |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | const files = await fetchPullRequestFiles( |
| 202 | accessToken, |
| 203 | owner, |
| 204 | repo, |
| 205 | input.prNumber, |
| 206 | ); |
| 207 | |
| 208 | return files.map((file) => ({ |
| 209 | sha: file.sha, |
| 210 | filename: file.filename, |
| 211 | status: file.status, |
| 212 | additions: file.additions, |
| 213 | deletions: file.deletions, |
| 214 | changes: file.changes, |
| 215 | patch: file.patch, |
| 216 | previousFilename: file.previous_filename, |
| 217 | })); |
| 218 | }), |
| 219 | }); |