| 1 | import { describe, expect, spyOn, test } from "bun:test"; |
| 2 | |
| 3 | import { existsSync } |
| 4 | import { mkdir, mkdtemp |
| 5 | import os from "node:os |
| 6 | import path from "node:path |
| 7 | import { loadConfig } |
| 8 | import { PermanentImportError } |
| 9 | import { GhostCallbacks } |
| 10 | import { |
| 11 | type GitHubComment, |
| 12 | GitHubClient, |
| 13 | type GitHubIssue, |
| 14 | type GitHubPull, |
| 15 | } from "./github.ts"; |
| 16 | import { |
| 17 | commentPages, |
| 18 | issueFrom, |
| 19 | type Job, |
| 20 | releaseFrom, |
| 21 | runImport, |
| 22 | } from "./import-job.ts"; |
| 23 | |
| 24 | const issue: GitHubIssue |
| 25 | number: 7, |
| 26 | title: "Crash", |
| 27 | body: "It crashes", |
| 28 | user: { login: " |
| 29 | state: "closed", |
| 30 | labels: [{ name: |
| 31 | created_at: "2024-01-01T00:00:00Z", |
| 32 | updated_at: "2024-01-02T00:00:00Z", |
| 33 | closed_at: "2024-01-02T00:00:00Z", |
| 34 | }; |
| 35 | |
| 36 | const pull: GitHubPull |
| 37 | number: 7, |
| 38 | state: "closed", |
| 39 | draft: false, |
| 40 | merged_at: "2024-01-02T00:00:00Z", |
| 41 | merge_commit_sha: "a".repeat |
| 42 | base: { ref: " |
| 43 | head: { ref: " |
| 44 | }; |
| 45 | |
| 46 | const context = { |
| 47 | source: "octo/repo", |
| 48 | refs: new Set |
| 49 | present: new Set |
| 50 | }; |
| 51 | |
| 52 | describe("issueFrom", () |
| 53 | test("maps an issue, lowercasing label colours", |
| 54 | expect(issueFrom(issue |
| 55 | number: 7, |
| 56 | title: "Crash", |
| 57 | body: "It crashes", |
| 58 | authorLogin: "octocat", |
| 59 | state: "closed", |
| 60 | createdAt: "2024-01-01T00:00:00Z", |
| 61 | updatedAt: "2024-01-02T00:00:00Z", |
| 62 | closedAt: "2024-01-02T00:00:00Z", |
| 63 | labels: [{ name: |
| 64 | pullRequest: undefined, |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | test("a merged pull request keeps its merge commit when the history has it", |
| 69 | expect(issueFrom(issue |
| 70 | state: "merged", |
| 71 | draft: false, |
| 72 | baseRef: "main", |
| 73 | headRef: "fix", |
| 74 | headSha: "b".repeat |
| 75 | headInRepository: true, |
| 76 | mergeCommitSha: "a".repeat |
| 77 | mergedAt: "2024-01-02T00:00:00Z", |
| 78 | }); |
| 79 | }); |
| 80 | |
| 81 | test("drops a merge commit the history lacks", |
| 82 | const result = |
| 83 | expect(result.pullRequest |
| 84 | }); |
| 85 | |
| 86 | test("a fork's branch, or a deleted one, is not in the repository", |
| 87 | const fork = |
| 88 | ...pull, |
| 89 | merged_at: null, |
| 90 | state: "open" as |
| 91 | head: { ...pull |
| 92 | }; |
| 93 | expect(issueFrom(issue |
| 94 | state: "open", |
| 95 | headInRepository: false, |
| 96 | mergeCommitSha: null, |
| 97 | }); |
| 98 | const gone = |
| 99 | expect(issueFrom(issue |
| 100 | false, |
| 101 | ); |
| 102 | expect( |
| 103 | issueFrom(issue, pull |
| 104 | ?.headInRepository, |
| 105 | ).toBe(false); |
| 106 | }); |
| 107 | }); |
| 108 | |
| 109 | test("commentPages reads past GitHub's page cap in windows", async |
| 110 | // Six comments, two sharing an update time across a window's edge, behind a listing that stops after two pages of two. |
| 111 | const times = |
| 112 | const all = |
| 113 | (minute, index) |
| 114 | ({ |
| 115 | id: index + |
| 116 | updated_at: `2024-01-01T00:${minute |
| 117 | }) as GitHubComment |
| 118 | ); |
| 119 | const paths: |
| 120 | const github = |
| 121 | async *pages<T |
| 122 | paths.push(path |
| 123 | const since = |
| 124 | const rest = |
| 125 | for (let page |
| 126 | yield rest.slice |
| 127 | }, |
| 128 | }; |
| 129 | |
| 130 | const seen = |
| 131 | for await (const |
| 132 | for (const comment |
| 133 | |
| 134 | expect([...seen] |
| 135 | expect(paths[0]) |
| 136 | "/repos/o/r/issues/comments?sort=updated&direction=asc", |
| 137 | ); |
| 138 | expect(paths.at |
| 139 | }); |
| 140 | |
| 141 | test("releaseFrom turns empty names and bodies into null", () |
| 142 | expect( |
| 143 | releaseFrom({ |
| 144 | tag_name: "v1", |
| 145 | name: "", |
| 146 | body: "", |
| 147 | draft: false, |
| 148 | prerelease: true, |
| 149 | created_at: "2024-01-01T00:00:00Z", |
| 150 | published_at: null, |
| 151 | }), |
| 152 | ).toEqual({ |
| 153 | tagName: "v1", |
| 154 | name: null, |
| 155 | body: null, |
| 156 | isDraft: false, |
| 157 | isPrerelease: true, |
| 158 | createdAt: "2024-01-01T00:00:00Z", |
| 159 | publishedAt: null, |
| 160 | }); |
| 161 | }); |
| 162 | |
| 163 | const attemptJob = ( |
| 164 | importId: "import_1", |
| 165 | attempt: "attempt-1", |
| 166 | source: "octo/repo", |
| 167 | destination: "owner/repo", |
| 168 | githubToken: "synthetic-github-token", |
| 169 | ghostToken: "synthetic-ghost-token", |
| 170 | lastAttempt: false, |
| 171 | ...overrides, |
| 172 | }); |
| 173 | |
| 174 | const workConfig = ( |
| 175 | loadConfig({ |
| 176 | GHOST_API_URL: "http://localhost:3001", |
| 177 | IMPORTER_SECRET: "synthetic-importer-secret", |
| 178 | IMPORTER_WORKDIR: workdir, |
| 179 | }); |
| 180 | |
| 181 | test("runImport reports a workdir it cannot create and clears its heartbeat", async |
| 182 | const root = |
| 183 | const workdir = |
| 184 | await writeFile(workdir |
| 185 | const finish = |
| 186 | const clear = |
| 187 | const log = |
| 188 | try { |
| 189 | await runImport(attemptJob |
| 190 | expect(finish).toHaveBeenCalledTimes |
| 191 | expect(finish).toHaveBeenCalledWith |
| 192 | succeeded: false, |
| 193 | error: expect.any |
| 194 | retryable: true, |
| 195 | }); |
| 196 | expect(clear).toHaveBeenCalledTimes |
| 197 | } finally { |
| 198 | finish.mockRestore(); |
| 199 | clear.mockRestore(); |
| 200 | log.mockRestore(); |
| 201 | await rm(root |
| 202 | } |
| 203 | }); |
| 204 | |
| 205 | describe("the import's working directory", () |
| 206 | const cases = |
| 207 | { name: "survives a failure a retry will follow", |
| 208 | { |
| 209 | name: "goes after the last attempt", |
| 210 | keep: false, |
| 211 | job: { lastAttempt: true |
| 212 | }, |
| 213 | { |
| 214 | name: "goes after a failure no retry can fix", |
| 215 | keep: false, |
| 216 | error: new PermanentImportError |
| 217 | }, |
| 218 | ]; |
| 219 | for (const { |
| 220 | test(name, async |
| 221 | const workdir = |
| 222 | // what an earlier attempt fetched |
| 223 | const clone = |
| 224 | await mkdir(clone |
| 225 | await writeFile(path |
| 226 | const get = |
| 227 | error ?? new |
| 228 | ); |
| 229 | const finish = |
| 230 | GhostCallbacks.prototype, |
| 231 | "finish", |
| 232 | ).mockResolvedValue(); |
| 233 | const log = |
| 234 | try { |
| 235 | await runImport(attemptJob |
| 236 | expect(existsSync(path |
| 237 | } finally { |
| 238 | get.mockRestore(); |
| 239 | finish.mockRestore(); |
| 240 | log.mockRestore(); |
| 241 | await rm(workdir |
| 242 | } |
| 243 | }); |
| 244 | } |
| 245 | }); |