| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { and, eq |
| 4 | |
| 5 | import { DATABASE } |
| 6 | import { excluded } |
| 7 | import { attributed } |
| 8 | import type { Executor |
| 9 | import type { |
| 10 | ImportedCommentDTO, |
| 11 | ImportedIssueDTO, |
| 12 | ImportedReleaseDTO, |
| 13 | } from '../../resources/imports/dto/importer-callback.dto.js'; |
| 14 | |
| 15 | const IMPORTER = schema |
| 16 | |
| 17 | /** |
| 18 | * Writes what the importer read from GitHub. Every write is an upsert keyed the way GitHub keys it (tag name, issue number), so a retried attempt overwrites the previous one instead of duplicating it. |
| 19 | * |
| 20 | * Nothing here publishes events: an import is history, not activity, and thousands of `issue.opened` webhooks for it would be noise. |
| 21 | */ |
| 22 | @Injectable() |
| 23 | export class ImportWriterService |
| 24 | constructor(@Inject( |
| 25 | |
| 26 | async writeReleases(repositoryId |
| 27 | if (!releases.length |
| 28 | await this.db |
| 29 | .insert(schema.release |
| 30 | .values( |
| 31 | releases.map((release |
| 32 | repositoryId, |
| 33 | tagName: release.tagName |
| 34 | name: release.name |
| 35 | body: release.body |
| 36 | isDraft: release.isDraft |
| 37 | isPrerelease: release.isPrerelease |
| 38 | authorId: IMPORTER, |
| 39 | createdAt: new Date |
| 40 | publishedAt: release.publishedAt |
| 41 | ? new Date( |
| 42 | : null, |
| 43 | })), |
| 44 | ) |
| 45 | .onConflictDoUpdate({ |
| 46 | target: [schema.release |
| 47 | set: { |
| 48 | name: excluded(schema |
| 49 | body: excluded(schema |
| 50 | isDraft: excluded(schema |
| 51 | isPrerelease: excluded(schema |
| 52 | createdAt: excluded(schema |
| 53 | publishedAt: excluded(schema |
| 54 | }, |
| 55 | }); |
| 56 | } |
| 57 | |
| 58 | /** Issues and pull requests share GitHub's number sequence, as they do here, so numbers are kept and `#123` links in imported text still point at the right thing. A re-sent issue loses its comments, which the attempt sends again after it. */ |
| 59 | async writeIssues(repositoryId |
| 60 | if (!issues.length |
| 61 | await this.db |
| 62 | const labelIds = |
| 63 | |
| 64 | for (const issue |
| 65 | const forkPullRequest = |
| 66 | issue.pullRequest?.state |
| 67 | !issue.pullRequest.headInRepository |
| 68 | const closedAt = |
| 69 | ? new Date( |
| 70 | : issue.closedAt |
| 71 | ? new Date( |
| 72 | : null; |
| 73 | const body = |
| 74 | issue.authorLogin, |
| 75 | 'Opened', |
| 76 | forkPullRequest |
| 77 | ? `${issue.body |
| 78 | : issue.body, |
| 79 | ); |
| 80 | |
| 81 | const [row] |
| 82 | .insert(schema.issue |
| 83 | .values({ |
| 84 | repositoryId, |
| 85 | number: issue.number |
| 86 | title: issue.title |
| 87 | body, |
| 88 | state: forkPullRequest ? |
| 89 | isPullRequest: Boolean(issue |
| 90 | authorId: IMPORTER, |
| 91 | commentCount: 0, |
| 92 | closedAt, |
| 93 | createdAt: new Date |
| 94 | updatedAt: new Date |
| 95 | }) |
| 96 | .onConflictDoUpdate({ |
| 97 | target: [schema.issue |
| 98 | set: { |
| 99 | title: excluded(schema |
| 100 | body: excluded(schema |
| 101 | state: excluded(schema |
| 102 | isPullRequest: excluded(schema |
| 103 | commentCount: 0, |
| 104 | closedAt: excluded(schema |
| 105 | createdAt: excluded(schema |
| 106 | updatedAt: excluded(schema |
| 107 | }, |
| 108 | }) |
| 109 | .returning({ id: |
| 110 | |
| 111 | await tx |
| 112 | .delete(schema.issueComment |
| 113 | .where(eq(schema |
| 114 | await tx |
| 115 | .delete(schema.issueLabel |
| 116 | .where(eq(schema |
| 117 | if (issue.labels |
| 118 | await tx.insert |
| 119 | issue.labels.map |
| 120 | issueId: row.id |
| 121 | labelId: labelIds.get |
| 122 | })), |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | const pull = |
| 127 | if (!pull) |
| 128 | const values = |
| 129 | state: forkPullRequest ? |
| 130 | draft: pull.draft |
| 131 | baseRepositoryId: repositoryId, |
| 132 | baseRef: pull.baseRef |
| 133 | headRepositoryId: pull.headInRepository |
| 134 | headRef: pull.headRef |
| 135 | headSha: pull.headSha |
| 136 | mergeCommitSha: pull.mergeCommitSha |
| 137 | mergedAt: pull.mergedAt |
| 138 | }; |
| 139 | await tx |
| 140 | .insert(schema.pullRequest |
| 141 | .values({ issueId: |
| 142 | .onConflictDoUpdate({ |
| 143 | target: schema.pullRequest |
| 144 | set: values, |
| 145 | }); |
| 146 | } |
| 147 | }); |
| 148 | } |
| 149 | |
| 150 | async writeComments(repositoryId |
| 151 | if (!comments.length |
| 152 | const numbers = |
| 153 | ...new Set |
| 154 | ]; |
| 155 | await this.db |
| 156 | const issues = |
| 157 | .select({ id: |
| 158 | .from(schema.issue |
| 159 | .where( |
| 160 | and( |
| 161 | eq(schema.issue |
| 162 | inArray(schema.issue |
| 163 | ), |
| 164 | ); |
| 165 | const idOf = |
| 166 | |
| 167 | // GitHub lists comments on issues the token could not list itself, such as ones transferred away; those are dropped. |
| 168 | const rows = |
| 169 | const issueId = |
| 170 | if (!issueId) |
| 171 | return [ |
| 172 | { |
| 173 | issueId, |
| 174 | githubId: comment.githubId |
| 175 | authorId: IMPORTER, |
| 176 | body: attributed(comment |
| 177 | createdAt: new Date |
| 178 | updatedAt: new Date |
| 179 | }, |
| 180 | ]; |
| 181 | }); |
| 182 | if (!rows.length |
| 183 | await tx |
| 184 | .insert(schema.issueComment |
| 185 | .values(rows) |
| 186 | .onConflictDoNothing({ |
| 187 | target: [schema.issueComment |
| 188 | }); |
| 189 | |
| 190 | await tx |
| 191 | .update(schema.issue |
| 192 | .set({ |
| 193 | commentCount: sql`(select count(*) from ${ |
| 194 | // Keeps GitHub's timestamp rather than letting `$onUpdateFn` stamp the import time. |
| 195 | updatedAt: sql`${schema |
| 196 | }) |
| 197 | .where(inArray(schema |
| 198 | }); |
| 199 | } |
| 200 | |
| 201 | private async upsertLabels |
| 202 | tx: Executor |
| 203 | repositoryId: string, |
| 204 | issues: ImportedIssueDTO |
| 205 | ) { |
| 206 | const labels = |
| 207 | issues |
| 208 | .flatMap((issue) |
| 209 | .map((label) |
| 210 | ); |
| 211 | if (!labels.size |
| 212 | |
| 213 | await tx |
| 214 | .insert(schema.label |
| 215 | .values( |
| 216 | [...labels.values |
| 217 | repositoryId, |
| 218 | name: label.name |
| 219 | color: label.color |
| 220 | description: label.description |
| 221 | })), |
| 222 | ) |
| 223 | .onConflictDoNothing({ |
| 224 | target: [schema.label |
| 225 | }); |
| 226 | |
| 227 | const rows = |
| 228 | .select({ id: |
| 229 | .from(schema.label |
| 230 | .where( |
| 231 | and( |
| 232 | eq(schema.label |
| 233 | inArray(schema.label |
| 234 | ), |
| 235 | ); |
| 236 | return new Map |
| 237 | } |
| 238 | } |