review-pr.ts118 lines, 3.1 KB
| 1 | import { inngest } from "../client"; |
| 2 | import { db } from "@/server/db"; |
| 3 | import { reviewCode } from "@/server/services/ai"; |
| 4 | import { |
| 5 | fetchPullRequest, |
| 6 | fetchPullRequestFiles, |
| 7 | getGitHubAccessToken, |
| 8 | } from "@/server/services/github"; |
| 9 | |
| 10 | export type ReviewPREvent = { |
| 11 | name: "review/pr.requested"; |
| 12 | data: { |
| 13 | reviewId: string; |
| 14 | repositoryId: string; |
| 15 | prNumber: number; |
| 16 | userId: string; |
| 17 | }; |
| 18 | }; |
| 19 | |
| 20 | export const reviewPR = inngest.createFunction( |
| 21 | { |
| 22 | id: "review-pr", |
| 23 | retries: 2, |
| 24 | }, |
| 25 | { event: "review/pr.requested" }, |
| 26 | async ({ event, step }) => { |
| 27 | const { reviewId, repositoryId, prNumber, userId } = event.data; |
| 28 | |
| 29 | await step.run("update-status-processing", async () => { |
| 30 | await db.review.update({ |
| 31 | where: { id: reviewId }, |
| 32 | data: { status: "PROCESSING" }, |
| 33 | }); |
| 34 | }); |
| 35 | |
| 36 | const repository = await step.run("get-repository", async () => { |
| 37 | return db.repository.findUnique({ |
| 38 | where: { id: repositoryId }, |
| 39 | }); |
| 40 | }); |
| 41 | |
| 42 | if (!repository) { |
| 43 | await step.run("mark-failed-no-repo", async () => { |
| 44 | await db.review.update({ |
| 45 | where: { id: reviewId }, |
| 46 | data: { status: "FAILED", error: "No repository found" }, |
| 47 | }); |
| 48 | }); |
| 49 | return { success: false, error: "No repository found" }; |
| 50 | } |
| 51 | |
| 52 | const accessToken = await step.run("get-access-token", async () => { |
| 53 | return getGitHubAccessToken(userId); |
| 54 | }); |
| 55 | |
| 56 | if (!accessToken) { |
| 57 | await step.run("mark-failed-no-token", async () => { |
| 58 | await db.review.update({ |
| 59 | where: { id: reviewId }, |
| 60 | data: { |
| 61 | status: "FAILED", |
| 62 | error: "GitHub access token not found", |
| 63 | }, |
| 64 | }); |
| 65 | }); |
| 66 | return { success: false, error: "GitHub access token not found" }; |
| 67 | } |
| 68 | |
| 69 | const [owner, repo] = repository.fullName.split("/"); |
| 70 | if (!owner || !repo) { |
| 71 | await step.run("mark-failed-invalid-repo", async () => { |
| 72 | await db.review.update({ |
| 73 | where: { id: reviewId }, |
| 74 | data: { |
| 75 | status: "FAILED", |
| 76 | error: "Invalid repository name", |
| 77 | }, |
| 78 | }); |
| 79 | }); |
| 80 | return { success: false, error: "Invalid repository name" }; |
| 81 | } |
| 82 | |
| 83 | const files = await step.run("fetch-pr-files", async () => { |
| 84 | return fetchPullRequestFiles(accessToken, owner, repo, prNumber); |
| 85 | }); |
| 86 | |
| 87 | const pr = await step.run("fetch-pr", async () => { |
| 88 | return fetchPullRequest(accessToken, owner, repo, prNumber); |
| 89 | }); |
| 90 | |
| 91 | const reviewResult = await step.run("generate-review", async () => { |
| 92 | return reviewCode( |
| 93 | pr.title, |
| 94 | files.map((f) => ({ |
| 95 | filename: f.filename, |
| 96 | status: f.status, |
| 97 | additions: f.additions, |
| 98 | deletions: f.deletions, |
| 99 | patch: f.patch, |
| 100 | })), |
| 101 | ); |
| 102 | }); |
| 103 | |
| 104 | await step.run("save-review-result", async () => { |
| 105 | await db.review.update({ |
| 106 | where: { id: reviewId }, |
| 107 | data: { |
| 108 | status: "COMPLETED", |
| 109 | summary: reviewResult.summary, |
| 110 | riskScore: reviewResult.riskScore, |
| 111 | comments: reviewResult.comments, |
| 112 | }, |
| 113 | }); |
| 114 | }); |
| 115 | |
| 116 | return { success: true, reviewId }; |
| 117 | }, |
| 118 | ); |