| 1 | import { PermanentImportError } from "./errors.ts"; |
| 2 | |
| 3 | const GITHUB = " |
| 4 | |
| 5 | // What an import carries over. `refs/pull/*/head` keeps every pull request's commits, including ones whose branch is gone or lived in a fork. |
| 6 | const REFSPECS = [ |
| 7 | |
| 8 | /** Credentials go in through the environment rather than argv, where any local user could read them from `ps`, and are scoped to one origin so a redirect cannot carry them elsewhere. */ |
| 9 | export function credentialEnv( |
| 10 | origin: string, |
| 11 | username: string, |
| 12 | password: string, |
| 13 | ) { |
| 14 | return { |
| 15 | GIT_TERMINAL_PROMPT: "0", |
| 16 | GIT_CONFIG_COUNT: "1", |
| 17 | GIT_CONFIG_KEY_0: `http.${origin |
| 18 | GIT_CONFIG_VALUE_0: `Authorization: Basic ${Buffer |
| 19 | }; |
| 20 | } |
| 21 | |
| 22 | async function git( |
| 23 | args: string[] |
| 24 | { |
| 25 | env = {}, |
| 26 | signal, |
| 27 | stdin, |
| 28 | }: { env |
| 29 | ) { |
| 30 | const child = |
| 31 | env: { ...process |
| 32 | stdin: stdin === |
| 33 | stdout: "pipe", |
| 34 | stderr: "pipe", |
| 35 | signal, |
| 36 | }); |
| 37 | const [stdout, |
| 38 | new Response(child |
| 39 | new Response(child |
| 40 | child.exited, |
| 41 | ]); |
| 42 | if (code !== |
| 43 | throw new Error( |
| 44 | `git ${args.join |
| 45 | ); |
| 46 | return stdout; |
| 47 | } |
| 48 | |
| 49 | export async function fetchFromGitHub |
| 50 | dir, |
| 51 | source, |
| 52 | token, |
| 53 | tokenWorked, |
| 54 | signal, |
| 55 | }: { |
| 56 | dir: string |
| 57 | source: string |
| 58 | token: string |
| 59 | /** GitHub has already accepted `token` in this attempt, so a refusal now means it expired rather than that it never worked. */ |
| 60 | tokenWorked: boolean |
| 61 | signal: AbortSignal |
| 62 | }) { |
| 63 | await git(["init |
| 64 | await git( |
| 65 | [ |
| 66 | "-C", |
| 67 | dir, |
| 68 | "fetch", |
| 69 | "--quiet", |
| 70 | "--no-tags", |
| 71 | `${GITHUB}/${source |
| 72 | ...REFSPECS.map( |
| 73 | ], |
| 74 | { env: credentialEnv( |
| 75 | ).catch((error |
| 76 | throw fetchFailure(error, |
| 77 | }); |
| 78 | } |
| 79 | |
| 80 | /** Whether a failed fetch is worth another attempt. No access stays no access; a token that expired mid-attempt is replaced by the next one. */ |
| 81 | export function fetchFailure(error |
| 82 | if (tokenWorked && |
| 83 | return new Error |
| 84 | `${error.message} |
| 85 | ); |
| 86 | if (/Repository not found|Authentication failed|403 |
| 87 | return new PermanentImportError |
| 88 | return error; |
| 89 | } |
| 90 | |
| 91 | export async function listRefs |
| 92 | dir: string, |
| 93 | signal: AbortSignal |
| 94 | ): Promise<Set |
| 95 | const output = |
| 96 | signal, |
| 97 | }); |
| 98 | return new Set |
| 99 | } |
| 100 | |
| 101 | /** Which of `shas` the fetched history holds. GitHub reports merge commits that a later force-push left unreachable. */ |
| 102 | export async function presentCommits |
| 103 | dir: string, |
| 104 | shas: string[] |
| 105 | signal: AbortSignal |
| 106 | ): Promise<Set |
| 107 | if (!shas.length |
| 108 | const output = |
| 109 | ["-C", dir |
| 110 | { signal, stdin: |
| 111 | ); |
| 112 | return new Set |
| 113 | output |
| 114 | .split("\n") |
| 115 | .map((line) |
| 116 | .filter(([, type |
| 117 | .map(([sha |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** One push of every imported ref, named by pattern so argv stays small however many pull requests there are. Not forced: refs a previous attempt already pushed are up to date, and anything else on the other side is a conflict worth failing on. */ |
| 122 | export async function pushToGhost |
| 123 | dir, |
| 124 | apiUrl, |
| 125 | destination, |
| 126 | token, |
| 127 | signal, |
| 128 | }: { |
| 129 | dir: string |
| 130 | apiUrl: string |
| 131 | destination: string |
| 132 | token: string |
| 133 | signal: AbortSignal |
| 134 | }) { |
| 135 | const origin = |
| 136 | await git( |
| 137 | [ |
| 138 | "-C", |
| 139 | dir, |
| 140 | "push", |
| 141 | "--quiet", |
| 142 | `${origin}/${destination |
| 143 | ...REFSPECS.map( |
| 144 | ], |
| 145 | { env: credentialEnv( |
| 146 | ); |
| 147 | } |