| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { sql } |
| 3 | import { |
| 4 | type AnyPgColumn, |
| 5 | boolean, |
| 6 | index, |
| 7 | integer, |
| 8 | pgEnum, |
| 9 | pgTable, |
| 10 | text, |
| 11 | timestamp, |
| 12 | uniqueIndex, |
| 13 | } from "drizzle-orm/pg-core"; |
| 14 | import { user } |
| 15 | import { issue } |
| 16 | import { repository } |
| 17 | |
| 18 | export const pullRequestState = |
| 19 | "open", |
| 20 | "closed", |
| 21 | "merged", |
| 22 | ]); |
| 23 | |
| 24 | /** |
| 25 | * Base and head are stored as separate repository ids because they are only the same row for a branch-to-branch request; a fork request spans two write-ahead logs that never learn about each other. |
| 26 | * |
| 27 | * `headSha` is the tip as of the last time the request was refreshed, so the diff a reviewer read stays addressable after the branch moves on. |
| 28 | * |
| 29 | * Number, title, body, author and comments live on the `issue` row; this row holds only what a git comparison needs. `state` keeps `merged`, which an issue has no word for, and is kept in step with `issue.state`. |
| 30 | */ |
| 31 | export const pullRequest = |
| 32 | "pull_request", |
| 33 | { |
| 34 | id: text() |
| 35 | .primaryKey() |
| 36 | .unique() |
| 37 | .notNull() |
| 38 | .$defaultFn(() => |
| 39 | issueId: text() |
| 40 | .references(() => |
| 41 | .notNull() |
| 42 | .unique(), |
| 43 | state: pullRequestState().notNull |
| 44 | draft: boolean().notNull |
| 45 | |
| 46 | baseRepositoryId: text() |
| 47 | .references(() => |
| 48 | .notNull(), |
| 49 | baseRef: text().notNull |
| 50 | // Null once the head repository is deleted. It cannot be deleted while it heads an open request, so only closed and merged requests lose it. |
| 51 | headRepositoryId: text().references |
| 52 | onDelete: "set null", |
| 53 | }), |
| 54 | headRef: text().notNull |
| 55 | headSha: text().notNull |
| 56 | |
| 57 | mergeCommitSha: text(), |
| 58 | |
| 59 | mergedAt: timestamp({ |
| 60 | }, |
| 61 | (t) => |
| 62 | // One open request per branch pair. Closed and merged rows are history, so the constraint has to be partial or reopening the same branch is blocked. |
| 63 | uniqueIndex("pull_request_open_branch_idx") |
| 64 | .on(t.baseRepositoryId |
| 65 | .where(sql`${ |
| 66 | |
| 67 | index("pull_request_head_idx"). |
| 68 | index("pull_request_base_state_idx"). |
| 69 | ], |
| 70 | ); |
| 71 | |
| 72 | export const pullRequestReviewState = |
| 73 | "commented", |
| 74 | "approved", |
| 75 | "changes_requested", |
| 76 | ]); |
| 77 | |
| 78 | export const diffSide = |
| 79 | |
| 80 | /** |
| 81 | * A review with no `submittedAt` is pending: the reviewer's unsent batch of line comments, visible to nobody else and with no verdict yet. One per reviewer and request. |
| 82 | * |
| 83 | * A dismissed review keeps its verdict for the record but stops counting; `dismissalMessage` is what marks it, since `dismissedById` goes null with the account. |
| 84 | */ |
| 85 | export const pullRequestReview = |
| 86 | "pull_request_review", |
| 87 | { |
| 88 | id: text() |
| 89 | .primaryKey() |
| 90 | .unique() |
| 91 | .notNull() |
| 92 | .$defaultFn(() => |
| 93 | pullRequestId: text() |
| 94 | .references(() => |
| 95 | .notNull(), |
| 96 | authorId: text() |
| 97 | .references(() => |
| 98 | .notNull(), |
| 99 | state: pullRequestReviewState(), |
| 100 | body: text(), |
| 101 | commitSha: text().notNull |
| 102 | |
| 103 | dismissedById: text().references |
| 104 | dismissalMessage: text(), |
| 105 | |
| 106 | createdAt: timestamp({ |
| 107 | submittedAt: timestamp({ |
| 108 | }, |
| 109 | (t) => |
| 110 | index("pull_request_review_pull_idx"). |
| 111 | uniqueIndex("pull_request_review_pending_idx") |
| 112 | .on(t.pullRequestId |
| 113 | .where(sql`${ |
| 114 | ], |
| 115 | ); |
| 116 | |
| 117 | /** |
| 118 | * A comment on one line of one side of the diff at `commitSha`, or on the lines from `startLine` on `startSide` down to `line` on `side`; once the head moves on it is outdated rather than wrong. |
| 119 | * |
| 120 | * A thread's first comment belongs to a review. Replies belong to no review: they carry `inReplyToId` and copy the thread's path, line and commit, but not its `diffHunk`. |
| 121 | */ |
| 122 | export const pullRequestReviewComment = |
| 123 | "pull_request_review_comment", |
| 124 | { |
| 125 | id: text() |
| 126 | .primaryKey() |
| 127 | .unique() |
| 128 | .notNull() |
| 129 | .$defaultFn(() => |
| 130 | pullRequestId: text() |
| 131 | .references(() => |
| 132 | .notNull(), |
| 133 | reviewId: text().references |
| 134 | onDelete: "cascade", |
| 135 | }), |
| 136 | inReplyToId: text().references |
| 137 | (): AnyPgColumn => |
| 138 | { onDelete: "cascade" |
| 139 | ), |
| 140 | authorId: text() |
| 141 | .references(() => |
| 142 | .notNull(), |
| 143 | path: text().notNull |
| 144 | side: diffSide().notNull |
| 145 | line: integer().notNull |
| 146 | startSide: diffSide(), |
| 147 | startLine: integer(), |
| 148 | commitSha: text().notNull |
| 149 | // The rows of the diff the comment points at, kept as written: the conversation shows them long after the head has moved on. |
| 150 | diffHunk: text(), |
| 151 | body: text().notNull |
| 152 | |
| 153 | createdAt: timestamp({ |
| 154 | updatedAt: timestamp({ |
| 155 | .notNull() |
| 156 | .defaultNow() |
| 157 | .$onUpdateFn(() => |
| 158 | }, |
| 159 | (t) => |
| 160 | index("pull_request_review_comment_review_idx"). |
| 161 | index("pull_request_review_comment_reply_idx"). |
| 162 | index("pull_request_review_comment_pull_idx"). |
| 163 | ], |
| 164 | ); |