| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | bigint, |
| 4 | boolean, |
| 5 | index, |
| 6 | integer, |
| 7 | pgEnum, |
| 8 | pgTable, |
| 9 | primaryKey, |
| 10 | text, |
| 11 | timestamp, |
| 12 | uniqueIndex, |
| 13 | } from "drizzle-orm/pg-core"; |
| 14 | import { user } |
| 15 | import { repository } |
| 16 | |
| 17 | export const issueState = |
| 18 | |
| 19 | export const issueEventType = |
| 20 | "opened", |
| 21 | "closed", |
| 22 | "reopened", |
| 23 | "renamed", |
| 24 | "edited", |
| 25 | "labeled", |
| 26 | "unlabeled", |
| 27 | "assigned", |
| 28 | "unassigned", |
| 29 | "merged", |
| 30 | "ready_for_review", |
| 31 | "converted_to_draft", |
| 32 | // a commit a push added to a pull request's head, and a push that rewrote it |
| 33 | "committed", |
| 34 | "head_force_pushed", |
| 35 | ]); |
| 36 | |
| 37 | export const issueReferenceSource = |
| 38 | "issue", |
| 39 | "comment", |
| 40 | "commit", |
| 41 | ]); |
| 42 | |
| 43 | /** `number` is per repository, 1-based, and what the URL carries; a pull request is an issue with a `pull_request` row, sharing its number sequence, comment thread and timeline. `commentCount` is denormalized for `sort=comments`; writers must bump it on comment create/delete. */ |
| 44 | export const issue = |
| 45 | "issue", |
| 46 | { |
| 47 | id: text() |
| 48 | .primaryKey() |
| 49 | .unique() |
| 50 | .notNull() |
| 51 | .$defaultFn(() => |
| 52 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 53 | numericId: bigint({ |
| 54 | .generatedAlwaysAsIdentity() |
| 55 | .notNull() |
| 56 | .unique(), |
| 57 | repositoryId: text() |
| 58 | .references(() => |
| 59 | .notNull(), |
| 60 | number: integer().notNull |
| 61 | title: text().notNull |
| 62 | body: text(), |
| 63 | state: issueState().notNull |
| 64 | isPullRequest: boolean().notNull |
| 65 | |
| 66 | authorId: text() |
| 67 | .references(() => |
| 68 | .notNull(), |
| 69 | closedById: text().references |
| 70 | |
| 71 | commentCount: integer().notNull |
| 72 | |
| 73 | closedAt: timestamp({ |
| 74 | createdAt: timestamp({ |
| 75 | updatedAt: timestamp({ |
| 76 | .notNull() |
| 77 | .defaultNow() |
| 78 | .$onUpdateFn(() => |
| 79 | }, |
| 80 | (t) => |
| 81 | uniqueIndex("issue_repo_number_idx"). |
| 82 | index("issue_repo_kind_state_idx"). |
| 83 | t.repositoryId, |
| 84 | t.isPullRequest, |
| 85 | t.state, |
| 86 | ), |
| 87 | index("issue_repo_updated_idx"). |
| 88 | ], |
| 89 | ); |
| 90 | |
| 91 | export const issueComment = |
| 92 | "issue_comment", |
| 93 | { |
| 94 | id: text() |
| 95 | .primaryKey() |
| 96 | .unique() |
| 97 | .notNull() |
| 98 | .$defaultFn(() => |
| 99 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 100 | numericId: bigint({ |
| 101 | .generatedAlwaysAsIdentity() |
| 102 | .notNull() |
| 103 | .unique(), |
| 104 | issueId: text() |
| 105 | .references(() => |
| 106 | .notNull(), |
| 107 | authorId: text() |
| 108 | .references(() => |
| 109 | .notNull(), |
| 110 | body: text().notNull |
| 111 | githubId: bigint({ |
| 112 | |
| 113 | createdAt: timestamp({ |
| 114 | updatedAt: timestamp({ |
| 115 | .notNull() |
| 116 | .defaultNow() |
| 117 | .$onUpdateFn(() => |
| 118 | }, |
| 119 | (t) => |
| 120 | index("issue_comment_issue_idx"). |
| 121 | uniqueIndex("issue_comment_issue_github_idx"). |
| 122 | ], |
| 123 | ); |
| 124 | |
| 125 | export const label = |
| 126 | "label", |
| 127 | { |
| 128 | id: text() |
| 129 | .primaryKey() |
| 130 | .unique() |
| 131 | .notNull() |
| 132 | .$defaultFn(() => |
| 133 | // Integer id for the GitHub compatibility layer only (REST `id`, GraphQL `databaseId`); nothing references it and Ghost's own API does not expose it. |
| 134 | numericId: bigint({ |
| 135 | .generatedAlwaysAsIdentity() |
| 136 | .notNull() |
| 137 | .unique(), |
| 138 | repositoryId: text() |
| 139 | .references(() => |
| 140 | .notNull(), |
| 141 | name: text().notNull |
| 142 | description: text(), |
| 143 | // 6 hex chars, lowercase, no `#` — the client adds `#` when rendering |
| 144 | color: text().notNull |
| 145 | |
| 146 | createdAt: timestamp({ |
| 147 | updatedAt: timestamp({ |
| 148 | .notNull() |
| 149 | .defaultNow() |
| 150 | .$onUpdateFn(() => |
| 151 | }, |
| 152 | (t) => |
| 153 | uniqueIndex("label_repo_name_idx"). |
| 154 | index("label_repo_idx"). |
| 155 | ], |
| 156 | ); |
| 157 | |
| 158 | export const issueLabel = |
| 159 | "issue_label", |
| 160 | { |
| 161 | issueId: text() |
| 162 | .references(() => |
| 163 | .notNull(), |
| 164 | labelId: text() |
| 165 | .references(() => |
| 166 | .notNull(), |
| 167 | }, |
| 168 | (t) => |
| 169 | ); |
| 170 | |
| 171 | export const issueAssignee = |
| 172 | "issue_assignee", |
| 173 | { |
| 174 | issueId: text() |
| 175 | .references(() => |
| 176 | .notNull(), |
| 177 | userId: text() |
| 178 | .references(() => |
| 179 | .notNull(), |
| 180 | }, |
| 181 | (t) => |
| 182 | ); |
| 183 | |
| 184 | /** Audit timeline for an issue — what GitHub renders between comments. Only the columns relevant to `type` are set; a `closed` event carries `sourceIssueId` or `commitSha` when a pull request or commit closed it. A `committed` event keeps its commit's subject and author, since a later force push can leave the commit unreachable; `head_force_pushed` goes from `beforeSha` to `commitSha`. */ |
| 185 | export const issueEvent = |
| 186 | "issue_event", |
| 187 | { |
| 188 | id: text() |
| 189 | .primaryKey() |
| 190 | .unique() |
| 191 | .notNull() |
| 192 | .$defaultFn(() => |
| 193 | issueId: text() |
| 194 | .references(() => |
| 195 | .notNull(), |
| 196 | actorId: text().references |
| 197 | type: issueEventType().notNull |
| 198 | |
| 199 | labelName: text(), |
| 200 | assigneeUsername: text(), |
| 201 | oldTitle: text(), |
| 202 | newTitle: text(), |
| 203 | sourceIssueId: text().references |
| 204 | commitSha: text(), |
| 205 | beforeSha: text(), |
| 206 | commitMessage: text(), |
| 207 | commitAuthorName: text(), |
| 208 | |
| 209 | createdAt: timestamp({ |
| 210 | }, |
| 211 | (t) => |
| 212 | ); |
| 213 | |
| 214 | /** One row per `#N` written somewhere, resolved on save so a timeline read is one indexed lookup on `targetIssueId`. `sourceId` is the issue id, comment id or commit sha holding the text, and an edit replaces all its rows; `sourceIssueId` is the issue or pull request it belongs to, null for a commit. */ |
| 215 | export const issueReference = |
| 216 | "issue_reference", |
| 217 | { |
| 218 | id: text() |
| 219 | .primaryKey() |
| 220 | .unique() |
| 221 | .notNull() |
| 222 | .$defaultFn(() => |
| 223 | sourceType: issueReferenceSource().notNull |
| 224 | sourceId: text().notNull |
| 225 | sourceRepositoryId: text() |
| 226 | .references(() => |
| 227 | .notNull(), |
| 228 | sourceIssueId: text().references |
| 229 | targetIssueId: text() |
| 230 | .references(() => |
| 231 | .notNull(), |
| 232 | closing: boolean().notNull |
| 233 | actorId: text().references |
| 234 | |
| 235 | createdAt: timestamp({ |
| 236 | }, |
| 237 | (t) => |
| 238 | uniqueIndex("issue_reference_source_target_idx"). |
| 239 | t.sourceType, |
| 240 | t.sourceId, |
| 241 | t.targetIssueId, |
| 242 | ), |
| 243 | index("issue_reference_target_idx"). |
| 244 | index("issue_reference_source_issue_idx"). |
| 245 | ], |
| 246 | ); |