| 1 | import { describe, expect, spyOn, test } from "bun:test"; |
| 2 | |
| 3 | import { mkdtemp, rm |
| 4 | import os from "node:os |
| 5 | import path from "node:path |
| 6 | import { loadConfig } |
| 7 | import { GhostCallbacks } |
| 8 | import type { GitHubIssue, |
| 9 | import { issueFrom, releaseFrom |
| 10 | |
| 11 | const issue: GitHubIssue |
| 12 | number: 7, |
| 13 | title: "Crash", |
| 14 | body: "It crashes", |
| 15 | user: { login: " |
| 16 | state: "closed", |
| 17 | labels: [{ name: |
| 18 | created_at: "2024-01-01T00:00:00Z", |
| 19 | updated_at: "2024-01-02T00:00:00Z", |
| 20 | closed_at: "2024-01-02T00:00:00Z", |
| 21 | }; |
| 22 | |
| 23 | const pull: GitHubPull |
| 24 | number: 7, |
| 25 | state: "closed", |
| 26 | draft: false, |
| 27 | merged_at: "2024-01-02T00:00:00Z", |
| 28 | merge_commit_sha: "a".repeat |
| 29 | base: { ref: " |
| 30 | head: { ref: " |
| 31 | }; |
| 32 | |
| 33 | const context = { |
| 34 | source: "octo/repo", |
| 35 | refs: new Set |
| 36 | present: new Set |
| 37 | }; |
| 38 | |
| 39 | describe("issueFrom", () |
| 40 | test("maps an issue, lowercasing label colours", |
| 41 | expect(issueFrom(issue |
| 42 | number: 7, |
| 43 | title: "Crash", |
| 44 | body: "It crashes", |
| 45 | authorLogin: "octocat", |
| 46 | state: "closed", |
| 47 | createdAt: "2024-01-01T00:00:00Z", |
| 48 | updatedAt: "2024-01-02T00:00:00Z", |
| 49 | closedAt: "2024-01-02T00:00:00Z", |
| 50 | labels: [{ name: |
| 51 | pullRequest: undefined, |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | test("a merged pull request keeps its merge commit when the history has it", |
| 56 | expect(issueFrom(issue |
| 57 | state: "merged", |
| 58 | draft: false, |
| 59 | baseRef: "main", |
| 60 | headRef: "fix", |
| 61 | headSha: "b".repeat |
| 62 | headInRepository: true, |
| 63 | mergeCommitSha: "a".repeat |
| 64 | mergedAt: "2024-01-02T00:00:00Z", |
| 65 | }); |
| 66 | }); |
| 67 | |
| 68 | test("drops a merge commit the history lacks", |
| 69 | const result = |
| 70 | expect(result.pullRequest |
| 71 | }); |
| 72 | |
| 73 | test("a fork's branch, or a deleted one, is not in the repository", |
| 74 | const fork = |
| 75 | ...pull, |
| 76 | merged_at: null, |
| 77 | state: "open" as |
| 78 | head: { ...pull |
| 79 | }; |
| 80 | expect(issueFrom(issue |
| 81 | state: "open", |
| 82 | headInRepository: false, |
| 83 | mergeCommitSha: null, |
| 84 | }); |
| 85 | const gone = |
| 86 | expect(issueFrom(issue |
| 87 | false, |
| 88 | ); |
| 89 | expect( |
| 90 | issueFrom(issue, pull |
| 91 | ?.headInRepository, |
| 92 | ).toBe(false); |
| 93 | }); |
| 94 | }); |
| 95 | |
| 96 | test("releaseFrom turns empty names and bodies into null", () |
| 97 | expect( |
| 98 | releaseFrom({ |
| 99 | tag_name: "v1", |
| 100 | name: "", |
| 101 | body: "", |
| 102 | draft: false, |
| 103 | prerelease: true, |
| 104 | created_at: "2024-01-01T00:00:00Z", |
| 105 | published_at: null, |
| 106 | }), |
| 107 | ).toEqual({ |
| 108 | tagName: "v1", |
| 109 | name: null, |
| 110 | body: null, |
| 111 | isDraft: false, |
| 112 | isPrerelease: true, |
| 113 | createdAt: "2024-01-01T00:00:00Z", |
| 114 | publishedAt: null, |
| 115 | }); |
| 116 | }); |
| 117 | |
| 118 | for (const failure |
| 119 | test(`runImport reports ${failure |
| 120 | const root = |
| 121 | const workdir = |
| 122 | if (failure === |
| 123 | const finish = |
| 124 | GhostCallbacks.prototype, |
| 125 | "finish", |
| 126 | ).mockResolvedValue(); |
| 127 | const clear = |
| 128 | const log = |
| 129 | try { |
| 130 | await runImport( |
| 131 | { |
| 132 | // A missing parent inside the prefix makes mkdtemp fail after mkdir succeeds. |
| 133 | importId: failure === |
| 134 | attempt: "attempt-1", |
| 135 | source: "octo/repo", |
| 136 | destination: "owner/repo", |
| 137 | githubToken: "synthetic-github-token", |
| 138 | ghostToken: "synthetic-ghost-token", |
| 139 | }, |
| 140 | loadConfig({ |
| 141 | GHOST_API_URL: "http://localhost:3001", |
| 142 | IMPORTER_SECRET: "synthetic-importer-secret", |
| 143 | IMPORTER_WORKDIR: workdir, |
| 144 | }), |
| 145 | ); |
| 146 | expect(finish).toHaveBeenCalledTimes |
| 147 | expect(finish).toHaveBeenCalledWith |
| 148 | succeeded: false, |
| 149 | error: expect.any |
| 150 | retryable: true, |
| 151 | }); |
| 152 | expect(clear).toHaveBeenCalledTimes |
| 153 | } finally { |
| 154 | finish.mockRestore(); |
| 155 | clear.mockRestore(); |
| 156 | log.mockRestore(); |
| 157 | await rm(root |
| 158 | } |
| 159 | }); |
| 160 | } |