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