| 1 | import { schema } from '@ghost/db'; |
| 2 | import { eq, type |
| 3 | import { alias } |
| 4 | |
| 5 | import { isoTimestamp } |
| 6 | import type { Executor |
| 7 | |
| 8 | type DiffSide |
| 9 | |
| 10 | export interface ReviewReply |
| 11 | id: string; |
| 12 | body: string; |
| 13 | authorUsername: string; |
| 14 | authorImage: string |
| 15 | createdAt: string; |
| 16 | updatedAt: string; |
| 17 | } |
| 18 | |
| 19 | export interface ReviewThread |
| 20 | path: string; |
| 21 | side: DiffSide |
| 22 | line: number; |
| 23 | startSide: DiffSide |
| 24 | startLine: number |
| 25 | commitSha: string; |
| 26 | diffHunk: string |
| 27 | replies: ReviewReply |
| 28 | } |
| 29 | |
| 30 | const thread = alias |
| 31 | const threadAuthor = alias |
| 32 | const reply = alias |
| 33 | const replyAuthor = alias |
| 34 | const reviewAuthor = alias |
| 35 | const dismisser = alias |
| 36 | |
| 37 | const replies = sql |
| 38 | |
| 39 | const threads = sql |
| 40 | ReviewThread[] |
| 41 | >`(select coalesce(json_agg(json_build_object('id', ${thread.id |
| 42 | |
| 43 | // A pending review has not been submitted, so it is dated by when it was started. |
| 44 | const submittedAt = sql |
| 45 | |
| 46 | /** Reviews exactly as the conversation renders them, oldest first: each line comment is a thread carrying its replies. */ |
| 47 | export function selectReviews(db |
| 48 | return db |
| 49 | .select({ |
| 50 | kind: sql<'review |
| 51 | id: schema.pullRequestReview |
| 52 | state: schema.pullRequestReview |
| 53 | body: schema.pullRequestReview |
| 54 | authorUsername: sql<string |
| 55 | authorImage: reviewAuthor.image |
| 56 | commitSha: schema.pullRequestReview |
| 57 | dismissedByUsername: dismisser.username |
| 58 | dismissalMessage: schema.pullRequestReview |
| 59 | comments: threads, |
| 60 | createdAt: isoTimestamp(submittedAt |
| 61 | }) |
| 62 | .from(schema.pullRequestReview |
| 63 | .innerJoin( |
| 64 | schema.pullRequest, |
| 65 | eq(schema.pullRequest |
| 66 | ) |
| 67 | .innerJoin( |
| 68 | reviewAuthor, |
| 69 | eq(reviewAuthor.id |
| 70 | ) |
| 71 | .leftJoin( |
| 72 | dismisser, |
| 73 | eq(dismisser.id |
| 74 | ) |
| 75 | .where(where) |
| 76 | .orderBy(submittedAt, |
| 77 | } |