| 1 | import { mkdir, mkdtemp, rm } from "node:fs/promises"; |
| 2 | import path from "node:path |
| 3 | import { z } |
| 4 | |
| 5 | import { chunkBySize } |
| 6 | import type { Config |
| 7 | import { PermanentImportError, StaleAttemptError |
| 8 | import { |
| 9 | fetchFromGitHub, |
| 10 | listRefs, |
| 11 | presentCommits, |
| 12 | pushToGhost, |
| 13 | } from "./git.ts"; |
| 14 | import { |
| 15 | GhostCallbacks, |
| 16 | type ImportedIssue, |
| 17 | type ImportedPullRequest, |
| 18 | } from "./ghost.ts"; |
| 19 | import { |
| 20 | GitHubClient, |
| 21 | type GitHubComment, |
| 22 | type GitHubIssue, |
| 23 | type GitHubPull, |
| 24 | type GitHubRelease, |
| 25 | type GitHubRepository, |
| 26 | issueNumberOf, |
| 27 | loginOf, |
| 28 | } from "./github.ts"; |
| 29 | |
| 30 | const HEARTBEAT_MS = 30_000 |
| 31 | |
| 32 | export const jobSchema = |
| 33 | importId: z.string |
| 34 | attempt: z.uuid |
| 35 | source: z.string |
| 36 | destination: z.string |
| 37 | githubToken: z.string |
| 38 | ghostToken: z.string |
| 39 | }); |
| 40 | |
| 41 | export type Job |
| 42 | |
| 43 | /** Runs one attempt to the end and reports how it went. Never throws: whatever goes wrong becomes the outcome the API decides to retry or not. */ |
| 44 | export async function runImport |
| 45 | const ghost = |
| 46 | config.GHOST_API_URL, |
| 47 | config.IMPORTER_SECRET, |
| 48 | job.importId, |
| 49 | job.attempt, |
| 50 | ); |
| 51 | const attempt = |
| 52 | const stop = |
| 53 | |
| 54 | const heartbeat = |
| 55 | ghost.heartbeat().catch |
| 56 | if (error instanceof |
| 57 | }); |
| 58 | }, HEARTBEAT_MS); |
| 59 | |
| 60 | let dir: |
| 61 | try { |
| 62 | await mkdir(config |
| 63 | dir = await |
| 64 | const defaultBranch = |
| 65 | await ghost.finish |
| 66 | console.log(`import |
| 67 | } catch (error) |
| 68 | const reason = |
| 69 | if (reason instanceof |
| 70 | console.log(`import |
| 71 | return; |
| 72 | } |
| 73 | const message = |
| 74 | console.error(`import |
| 75 | await ghost |
| 76 | .finish({ |
| 77 | succeeded: false, |
| 78 | error: message, |
| 79 | retryable: !(reason |
| 80 | }) |
| 81 | .catch((finishError: |
| 82 | console.error( |
| 83 | `import ${job.importId |
| 84 | ), |
| 85 | ); |
| 86 | } finally { |
| 87 | clearInterval(heartbeat); |
| 88 | if (dir) await |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | async function importAll( |
| 93 | job: Job |
| 94 | config: Config |
| 95 | ghost: GhostCallbacks |
| 96 | dir: string, |
| 97 | signal: AbortSignal |
| 98 | ) { |
| 99 | const github = |
| 100 | const repository = |
| 101 | |
| 102 | await fetchFromGitHub({ |
| 103 | dir, |
| 104 | source: job.source |
| 105 | token: job.githubToken |
| 106 | signal, |
| 107 | }); |
| 108 | const refs = |
| 109 | await pushToGhost({ |
| 110 | dir, |
| 111 | apiUrl: config.GHOST_API_URL |
| 112 | destination: job.destination |
| 113 | token: job.ghostToken |
| 114 | refs, |
| 115 | signal, |
| 116 | }); |
| 117 | |
| 118 | for await (const |
| 119 | `/repos/${job.source |
| 120 | )) { |
| 121 | for (const chunk |
| 122 | await ghost.releases |
| 123 | } |
| 124 | |
| 125 | const pulls = |
| 126 | for await (const |
| 127 | `/repos/${job.source |
| 128 | )) { |
| 129 | for (const pull |
| 130 | } |
| 131 | const mergeCommits = |
| 132 | pull.merge_commit_sha ? |
| 133 | ); |
| 134 | const present = |
| 135 | |
| 136 | for await (const |
| 137 | `/repos/${job.source |
| 138 | )) { |
| 139 | const issues = |
| 140 | issueFrom(issue, pulls |
| 141 | source: job.source |
| 142 | refs, |
| 143 | present, |
| 144 | }), |
| 145 | ); |
| 146 | for (const chunk |
| 147 | } |
| 148 | |
| 149 | // Sent after every issue, since writing an issue clears the comments a previous attempt left on it. |
| 150 | for await (const |
| 151 | `/repos/${job.source |
| 152 | )) { |
| 153 | const comments = |
| 154 | githubId: comment.id |
| 155 | issueNumber: issueNumberOf(comment |
| 156 | authorLogin: loginOf(comment |
| 157 | body: comment.body |
| 158 | createdAt: comment.created_at |
| 159 | updatedAt: comment.updated_at |
| 160 | })); |
| 161 | for (const chunk |
| 162 | } |
| 163 | |
| 164 | return repository.default_branch |
| 165 | } |
| 166 | |
| 167 | export function releaseFrom(release |
| 168 | return { |
| 169 | tagName: release.tag_name |
| 170 | name: release.name |
| 171 | body: release.body |
| 172 | isDraft: release.draft |
| 173 | isPrerelease: release.prerelease |
| 174 | createdAt: release.created_at |
| 175 | publishedAt: release.published_at |
| 176 | }; |
| 177 | } |
| 178 | |
| 179 | export function issueFrom( |
| 180 | issue: GitHubIssue |
| 181 | pull: GitHubPull |
| 182 | context: { |
| 183 | ): ImportedIssue { |
| 184 | return { |
| 185 | number: issue.number |
| 186 | title: issue.title |
| 187 | body: issue.body |
| 188 | authorLogin: loginOf(issue |
| 189 | state: issue.state |
| 190 | createdAt: issue.created_at |
| 191 | updatedAt: issue.updated_at |
| 192 | closedAt: issue.closed_at |
| 193 | labels: issue.labels |
| 194 | name: label.name |
| 195 | color: label.color |
| 196 | description: label.description |
| 197 | })), |
| 198 | pullRequest: pull && |
| 199 | }; |
| 200 | } |
| 201 | |
| 202 | function pullRequestFrom( |
| 203 | pull: GitHubPull |
| 204 | { |
| 205 | source, |
| 206 | refs, |
| 207 | present, |
| 208 | }: { source |
| 209 | ): ImportedPullRequest { |
| 210 | const sameRepository = |
| 211 | pull.head.repo |
| 212 | const mergeCommitSha = |
| 213 | pull.merged_at && |
| 214 | pull.merge_commit_sha && |
| 215 | present.has(pull |
| 216 | ? pull.merge_commit_sha |
| 217 | : null; |
| 218 | return { |
| 219 | state: pull.merged_at |
| 220 | draft: pull.draft |
| 221 | baseRef: pull.base |
| 222 | headRef: pull.head |
| 223 | headSha: pull.head |
| 224 | headInRepository: sameRepository && |
| 225 | mergeCommitSha, |
| 226 | mergedAt: pull.merged_at |
| 227 | }; |
| 228 | } |