| 1 | import { createId } from "@paralleldrive/cuid2"; |
| 2 | import { |
| 3 | index, |
| 4 | integer, |
| 5 | pgEnum, |
| 6 | pgTable, |
| 7 | primaryKey, |
| 8 | text, |
| 9 | timestamp, |
| 10 | uniqueIndex, |
| 11 | } from "drizzle-orm/pg-core"; |
| 12 | import { user } |
| 13 | import { repository } |
| 14 | |
| 15 | export const issueState = |
| 16 | |
| 17 | export const issueEventType = |
| 18 | "opened", |
| 19 | "closed", |
| 20 | "reopened", |
| 21 | "renamed", |
| 22 | "edited", |
| 23 | "labeled", |
| 24 | "unlabeled", |
| 25 | "assigned", |
| 26 | "unassigned", |
| 27 | ]); |
| 28 | |
| 29 | /** |
| 30 | * GitHub-style issue. `number` is per repository, 1-based, and what the URL |
| 31 | * carries — same convention as `pull_request.number`. |
| 32 | * |
| 33 | * `commentCount` is denormalized so `sort=comments` is one indexed query |
| 34 | * instead of a join + group-by on every list call. Writers must bump it on |
| 35 | * comment create/delete. |
| 36 | */ |
| 37 | export const issue = |
| 38 | "issue", |
| 39 | { |
| 40 | id: text() |
| 41 | .primaryKey() |
| 42 | .unique() |
| 43 | .notNull() |
| 44 | .$defaultFn(() => |
| 45 | repositoryId: text() |
| 46 | .references(() => |
| 47 | .notNull(), |
| 48 | // per repository, 1-based, and what the URL carries |
| 49 | number: integer().notNull |
| 50 | title: text().notNull |
| 51 | body: text(), |
| 52 | state: issueState().notNull |
| 53 | |
| 54 | authorId: text() |
| 55 | .references(() => |
| 56 | .notNull(), |
| 57 | closedById: text().references |
| 58 | |
| 59 | commentCount: integer().notNull |
| 60 | |
| 61 | closedAt: timestamp({ |
| 62 | createdAt: timestamp({ |
| 63 | updatedAt: timestamp({ |
| 64 | .notNull() |
| 65 | .defaultNow() |
| 66 | .$onUpdateFn(() => |
| 67 | }, |
| 68 | (t) => |
| 69 | uniqueIndex("issue_repo_number_idx"). |
| 70 | index("issue_repo_state_idx"). |
| 71 | index("issue_repo_updated_idx"). |
| 72 | ], |
| 73 | ); |
| 74 | |
| 75 | /** Timeline comments on an issue, oldest first. */ |
| 76 | export const issueComment = |
| 77 | "issue_comment", |
| 78 | { |
| 79 | id: text() |
| 80 | .primaryKey() |
| 81 | .unique() |
| 82 | .notNull() |
| 83 | .$defaultFn(() => |
| 84 | issueId: text() |
| 85 | .references(() => |
| 86 | .notNull(), |
| 87 | authorId: text() |
| 88 | .references(() => |
| 89 | .notNull(), |
| 90 | body: text().notNull |
| 91 | |
| 92 | createdAt: timestamp({ |
| 93 | updatedAt: timestamp({ |
| 94 | .notNull() |
| 95 | .defaultNow() |
| 96 | .$onUpdateFn(() => |
| 97 | }, |
| 98 | (t) => |
| 99 | ); |
| 100 | |
| 101 | /** |
| 102 | * Repository-scoped labels, exactly like GitHub: a name, an optional |
| 103 | * description, and a 6-char hex color (stored without `#`). |
| 104 | */ |
| 105 | export const label = |
| 106 | "label", |
| 107 | { |
| 108 | id: text() |
| 109 | .primaryKey() |
| 110 | .unique() |
| 111 | .notNull() |
| 112 | .$defaultFn(() => |
| 113 | repositoryId: text() |
| 114 | .references(() => |
| 115 | .notNull(), |
| 116 | name: text().notNull |
| 117 | description: text(), |
| 118 | // 6 hex chars, lowercase, no `#` — the client adds `#` when rendering |
| 119 | color: text().notNull |
| 120 | |
| 121 | createdAt: timestamp({ |
| 122 | updatedAt: timestamp({ |
| 123 | .notNull() |
| 124 | .defaultNow() |
| 125 | .$onUpdateFn(() => |
| 126 | }, |
| 127 | (t) => |
| 128 | uniqueIndex("label_repo_name_idx"). |
| 129 | index("label_repo_idx"). |
| 130 | ], |
| 131 | ); |
| 132 | |
| 133 | export const issueLabel = |
| 134 | "issue_label", |
| 135 | { |
| 136 | issueId: text() |
| 137 | .references(() => |
| 138 | .notNull(), |
| 139 | labelId: text() |
| 140 | .references(() => |
| 141 | .notNull(), |
| 142 | }, |
| 143 | (t) => |
| 144 | ); |
| 145 | |
| 146 | export const issueAssignee = |
| 147 | "issue_assignee", |
| 148 | { |
| 149 | issueId: text() |
| 150 | .references(() => |
| 151 | .notNull(), |
| 152 | userId: text() |
| 153 | .references(() => |
| 154 | .notNull(), |
| 155 | }, |
| 156 | (t) => |
| 157 | ); |
| 158 | |
| 159 | /** |
| 160 | * Audit timeline for an issue — what GitHub renders between comments: |
| 161 | * opened/closed/reopened/renamed/edited/labeled/unlabeled/assigned/unassigned. |
| 162 | * Only the columns relevant to `type` are set. |
| 163 | */ |
| 164 | export const issueEvent = |
| 165 | "issue_event", |
| 166 | { |
| 167 | id: text() |
| 168 | .primaryKey() |
| 169 | .unique() |
| 170 | .notNull() |
| 171 | .$defaultFn(() => |
| 172 | issueId: text() |
| 173 | .references(() => |
| 174 | .notNull(), |
| 175 | actorId: text().references |
| 176 | type: issueEventType().notNull |
| 177 | |
| 178 | labelName: text(), |
| 179 | assigneeUsername: text(), |
| 180 | oldTitle: text(), |
| 181 | newTitle: text(), |
| 182 | |
| 183 | createdAt: timestamp({ |
| 184 | }, |
| 185 | (t) => |
| 186 | ); |