| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { sql } |
| 3 | import { |
| 4 | type AnyPgColumn, |
| 5 | bigint, |
| 6 | boolean, |
| 7 | index, |
| 8 | integer, |
| 9 | pgEnum, |
| 10 | pgTable, |
| 11 | text, |
| 12 | timestamp, |
| 13 | uniqueIndex, |
| 14 | } from "drizzle-orm/pg-core"; |
| 15 | import { user } |
| 16 | import { issue } |
| 17 | import { repository } |
| 18 | |
| 19 | export const pullRequestState = |
| 20 | "open", |
| 21 | "closed", |
| 22 | "merged", |
| 23 | ]); |
| 24 | |
| 25 | /** Base and head repository ids differ for a fork request, which spans two logs; `headSha` is the tip at the last refresh, so the reviewed diff stays addressable. |
| 26 | * Number, title, body, author and comments live on `issue`; `state` adds `merged` and is kept in step with `issue.state`. */ |
| 27 | export const pullRequest = |
| 28 | "pull_request", |
| 29 | { |
| 30 | id: text() |
| 31 | .primaryKey() |
| 32 | .unique() |
| 33 | .notNull() |
| 34 | .$defaultFn(() => |
| 35 | issueId: text() |
| 36 | .references(() => |
| 37 | .notNull() |
| 38 | .unique(), |
| 39 | state: pullRequestState().notNull |
| 40 | draft: boolean().notNull |
| 41 | |
| 42 | baseRepositoryId: text() |
| 43 | .references(() => |
| 44 | .notNull(), |
| 45 | baseRef: text().notNull |
| 46 | // 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. |
| 47 | headRepositoryId: text().references |
| 48 | onDelete: "set null", |
| 49 | }), |
| 50 | headRef: text().notNull |
| 51 | headSha: text().notNull |
| 52 | |
| 53 | mergeCommitSha: text(), |
| 54 | // Why `refs/pull/<n>/*` stopped following the branches: the size limit that refused the last update. Cleared by the next update that lands. |
| 55 | pullRefsBlocked: text(), |
| 56 | |
| 57 | mergedAt: timestamp({ |
| 58 | }, |
| 59 | (t) => |
| 60 | // 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. |
| 61 | uniqueIndex("pull_request_open_branch_idx") |
| 62 | .on(t.baseRepositoryId |
| 63 | .where(sql`${ |
| 64 | |
| 65 | index("pull_request_head_idx"). |
| 66 | index("pull_request_base_state_idx"). |
| 67 | ], |
| 68 | ); |
| 69 | |
| 70 | export const pullRequestReviewState = |
| 71 | "commented", |
| 72 | "approved", |
| 73 | "changes_requested", |
| 74 | ]); |
| 75 | |
| 76 | export const diffSide = |
| 77 | |
| 78 | /** No `submittedAt` means pending: the reviewer's unsent line comments, visible to nobody else, one per reviewer and request. A dismissed review keeps its verdict but stops counting; `dismissalMessage` marks it, since `dismissedById` goes null with the account. */ |
| 79 | export const pullRequestReview = |
| 80 | "pull_request_review", |
| 81 | { |
| 82 | id: text() |
| 83 | .primaryKey() |
| 84 | .unique() |
| 85 | .notNull() |
| 86 | .$defaultFn(() => |
| 87 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 88 | numericId: bigint({ |
| 89 | .generatedAlwaysAsIdentity() |
| 90 | .notNull() |
| 91 | .unique(), |
| 92 | pullRequestId: text() |
| 93 | .references(() => |
| 94 | .notNull(), |
| 95 | authorId: text() |
| 96 | .references(() => |
| 97 | .notNull(), |
| 98 | state: pullRequestReviewState(), |
| 99 | body: text(), |
| 100 | commitSha: text().notNull |
| 101 | |
| 102 | dismissedById: text().references |
| 103 | dismissalMessage: text(), |
| 104 | |
| 105 | createdAt: timestamp({ |
| 106 | submittedAt: timestamp({ |
| 107 | }, |
| 108 | (t) => |
| 109 | index("pull_request_review_pull_idx"). |
| 110 | uniqueIndex("pull_request_review_pending_idx") |
| 111 | .on(t.pullRequestId |
| 112 | .where(sql`${ |
| 113 | ], |
| 114 | ); |
| 115 | |
| 116 | /** A comment on a line, or on the range from `startLine` on `startSide` to `line` on `side`, of the diff at `commitSha`; outdated, not wrong, once the head moves on. A thread's first comment belongs to a review; replies carry `inReplyToId` and copy the thread's path, line and commit but not its `diffHunk`. */ |
| 117 | export const pullRequestReviewComment = |
| 118 | "pull_request_review_comment", |
| 119 | { |
| 120 | id: text() |
| 121 | .primaryKey() |
| 122 | .unique() |
| 123 | .notNull() |
| 124 | .$defaultFn(() => |
| 125 | pullRequestId: text() |
| 126 | .references(() => |
| 127 | .notNull(), |
| 128 | reviewId: text().references |
| 129 | onDelete: "cascade", |
| 130 | }), |
| 131 | inReplyToId: text().references |
| 132 | (): AnyPgColumn => |
| 133 | { onDelete: "cascade" |
| 134 | ), |
| 135 | authorId: text() |
| 136 | .references(() => |
| 137 | .notNull(), |
| 138 | path: text().notNull |
| 139 | side: diffSide().notNull |
| 140 | line: integer().notNull |
| 141 | startSide: diffSide(), |
| 142 | startLine: integer(), |
| 143 | commitSha: text().notNull |
| 144 | // The rows of the diff the comment points at, kept as written: the conversation shows them long after the head has moved on. |
| 145 | diffHunk: text(), |
| 146 | body: text().notNull |
| 147 | |
| 148 | createdAt: timestamp({ |
| 149 | updatedAt: timestamp({ |
| 150 | .notNull() |
| 151 | .defaultNow() |
| 152 | .$onUpdateFn(() => |
| 153 | }, |
| 154 | (t) => |
| 155 | index("pull_request_review_comment_review_idx"). |
| 156 | index("pull_request_review_comment_reply_idx"). |
| 157 | index("pull_request_review_comment_pull_idx"). |
| 158 | ], |
| 159 | ); |
| 160 | |
| 161 | /** One entry Ghost wrote into a base repository's log to keep `refs/pull/<n>/*` current, and the bytes it added. Nobody pays for them while the request is unmerged; a merge bills them to the base repository's account. */ |
| 162 | export const pullRequestRefWrite = |
| 163 | "pull_request_ref_write", |
| 164 | { |
| 165 | id: text() |
| 166 | .primaryKey() |
| 167 | .$defaultFn(() => |
| 168 | pullRequestId: text() |
| 169 | .references(() => |
| 170 | .notNull(), |
| 171 | size: bigint({ |
| 172 | createdAt: timestamp({ |
| 173 | }, |
| 174 | (t) => |
| 175 | ); |
| 176 | |
| 177 | /** Durable intent written before a pull-ref log commit, outside its quota transaction. The WAL index supplies the committed pack size during recovery. */ |
| 178 | export const pullRequestRefWritePending = |
| 179 | "pull_request_ref_write_pending", |
| 180 | { |
| 181 | // The preallocated WAL entry ULID; also forms the final accounting row id. |
| 182 | id: text().primaryKey |
| 183 | pullRequestId: text() |
| 184 | .references(() => |
| 185 | .notNull(), |
| 186 | }, |
| 187 | (t) => |
| 188 | ); |