| 1 | import { type Database, schema } from '@ghost/db'; |
| 2 | import { Inject, Injectable |
| 3 | import { ConfigService } |
| 4 | import { AuthService } |
| 5 | import { and, eq |
| 6 | |
| 7 | import { DATABASE } |
| 8 | import type { Auth |
| 9 | import { |
| 10 | assertGitHubRepositoryReadable, |
| 11 | listGitHubRepositories, |
| 12 | } from '../../lib/imports/github.js'; |
| 13 | import { githubImportConfig } |
| 14 | import { |
| 15 | GitHubImportsDisabledError, |
| 16 | GitHubNotConnectedError, |
| 17 | ImportNotFailedError, |
| 18 | ImportNotFoundError, |
| 19 | ImportRetryWouldOverwriteError, |
| 20 | } from '../../lib/imports/imports.errors.js'; |
| 21 | import { |
| 22 | githubAccessToken, |
| 23 | githubAccountIdOf, |
| 24 | } from '../../lib/imports/github-account.js'; |
| 25 | import { RepositoryAccessService } |
| 26 | import { ImportDispatcherService } |
| 27 | import { isoTimestamp } |
| 28 | import { RepositoriesService } |
| 29 | import type { StartImportRequestDTO |
| 30 | |
| 31 | @Injectable() |
| 32 | export class ImportsService |
| 33 | constructor( |
| 34 | @Inject(DATABASE) |
| 35 | private readonly config |
| 36 | private readonly auth |
| 37 | private readonly repositories |
| 38 | private readonly access |
| 39 | private readonly dispatcher |
| 40 | ) {} |
| 41 | |
| 42 | async githubStatus(userId |
| 43 | return { |
| 44 | enabled: Boolean(githubImportConfig |
| 45 | connected: Boolean(await |
| 46 | }; |
| 47 | } |
| 48 | |
| 49 | async githubRepositories(userId |
| 50 | return { |
| 51 | repositories: await listGitHubRepositories |
| 52 | await this.githubToken |
| 53 | ), |
| 54 | }; |
| 55 | } |
| 56 | |
| 57 | async start( |
| 58 | { source, ... |
| 59 | userId: string, |
| 60 | ) { |
| 61 | await assertGitHubRepositoryReadable( |
| 62 | await this.githubToken |
| 63 | source, |
| 64 | ); |
| 65 | |
| 66 | const created = |
| 67 | repository, |
| 68 | userId, |
| 69 | ); |
| 70 | await this.db |
| 71 | repositoryId: created.id |
| 72 | requestedById: userId, |
| 73 | source, |
| 74 | }); |
| 75 | this.dispatcher.wake |
| 76 | return created; |
| 77 | } |
| 78 | |
| 79 | async status({ |
| 80 | username, |
| 81 | repo, |
| 82 | requesterId, |
| 83 | }: { |
| 84 | username: string |
| 85 | repo: string |
| 86 | requesterId?: string |
| 87 | }) { |
| 88 | const repository = |
| 89 | username, |
| 90 | repo, |
| 91 | requesterId, |
| 92 | operation: 'read', |
| 93 | }); |
| 94 | const [row] |
| 95 | .select({ |
| 96 | status: schema.repositoryImport |
| 97 | source: schema.repositoryImport |
| 98 | attempts: schema.repositoryImport |
| 99 | lastError: schema.repositoryImport |
| 100 | createdAt: isoTimestamp(schema |
| 101 | updatedAt: isoTimestamp(schema |
| 102 | }) |
| 103 | .from(schema.repositoryImport |
| 104 | .where(eq(schema |
| 105 | if (!row) |
| 106 | return row; |
| 107 | } |
| 108 | |
| 109 | async retry({ |
| 110 | username, |
| 111 | repo, |
| 112 | requesterId, |
| 113 | }: { |
| 114 | username: string |
| 115 | repo: string |
| 116 | requesterId: string |
| 117 | }) { |
| 118 | const repository = |
| 119 | username, |
| 120 | repo, |
| 121 | requesterId, |
| 122 | operation: 'admin', |
| 123 | }); |
| 124 | if (!(await |
| 125 | throw new GitHubNotConnectedError() |
| 126 | } |
| 127 | // Locked for update, so an issue being opened (which shares the lock) either commits first and is seen below, or waits and finds the import pending. |
| 128 | await this.db |
| 129 | const [row] |
| 130 | .select({ status: |
| 131 | .from(schema.repositoryImport |
| 132 | .where(eq(schema |
| 133 | .for('update') |
| 134 | if (!row) |
| 135 | if (row.status |
| 136 | |
| 137 | const [localIssue] |
| 138 | .select({ id: |
| 139 | .from(schema.issue |
| 140 | .where( |
| 141 | and( |
| 142 | eq(schema.issue |
| 143 | ne(schema.issue |
| 144 | ), |
| 145 | ) |
| 146 | .limit(1); |
| 147 | if (localIssue) throw |
| 148 | |
| 149 | await tx |
| 150 | .update(schema.repositoryImport |
| 151 | .set({ |
| 152 | status: 'pending', |
| 153 | attempts: 0, |
| 154 | nextAttemptAt: new Date |
| 155 | lastError: null, |
| 156 | // Whoever retries is whose GitHub token and push access the next attempt uses. |
| 157 | requestedById: requesterId, |
| 158 | }) |
| 159 | .where(eq(schema |
| 160 | }); |
| 161 | this.dispatcher.wake |
| 162 | } |
| 163 | |
| 164 | private async githubToken |
| 165 | if (!githubImportConfig(this |
| 166 | throw new GitHubImportsDisabledError() |
| 167 | const token = |
| 168 | if (!token) |
| 169 | return token; |
| 170 | } |
| 171 | } |