Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132// Test the real adapter at its gh CLI seam, without signing in or writing to GitHub.import assert from "node:assert/strict";import { readFileSync } from "node:fs";import { mkdtemp, rm, writeFile } from "node:fs/promises";import { tmpdir } from "node:os";import { delimiter, join } from "node:path";import { test } from "node:test";import { ghGitHub } from "../src/github.ts";
test("the GitHub adapter sends the draft flag and body through gh and reads back the Task's pull request", async (t) => { const bin = await mkdtemp(join(tmpdir(), "pilot-gh-test-")); const path = process.env.PATH; t.after(async () => { if (path === undefined) delete process.env.PATH; else process.env.PATH = path; await rm(bin, { recursive: true, force: true }); }); const branch = "pilot/task-1-draft"; const body = "## Summary\nSum all rows."; const response = { number: 4, title: "Sum all rows", head: { ref: branch, repo: { full_name: "acme/api" } }, state: "open", draft: true, merged_at: null }; const asset = "https://github.com/user-attachments/assets/00000000-0000-4000-8000-000000000001"; const attached = { ...response, body: body + "\n\n" }; const fork = { ...response, number: 5, head: { ...response.head, repo: { full_name: "example/api" } } }; await writeFile( join(bin, "gh"), [ "#!/usr/bin/env node", 'const assert = require("node:assert/strict");', 'const fs = require("node:fs");', "const args = process.argv.slice(2);", 'if (args[0] === "pr" && args[1] === "create") {', ' assert.ok(args.includes("--draft") && args.includes("github.com/acme/api"));', ' assert.equal(args[args.indexOf("--head") + 1], ' + JSON.stringify(branch) + ");", ' assert.equal(fs.readFileSync(args[args.indexOf("--body-file") + 1], "utf8"), ' + JSON.stringify(body) + ");", ' const images = args.flatMap((flag, i) => flag === "--attach" ? [args[i + 1].split("#")] : []);', ' assert.deepEqual(images.map(([file, name]) => [fs.readFileSync(file, "utf8"), name]), [["before", "before.png"], ["after", "after.png"]]);', ' console.log("https://github.com/acme/api/pull/4");', ' console.error("Could not upload after.png.");', " process.exit(1);", "}", 'assert.deepEqual(args.slice(0, 3), ["api", "--hostname", "github.com"]);', 'if (args.includes("--paginate")) {', ' assert.ok(args.includes("--slurp") && args.includes("GET") && args.includes("state=all"));', " assert.ok(args.includes(" + JSON.stringify("head=acme:" + branch) + "));", " console.log(" + JSON.stringify(JSON.stringify([[response, fork]])) + ");", '} else if (args.includes("POST")) {', ' assert.ok(args.includes("repos/acme/api/pulls") && args.includes("base=trunk"));', ' assert.equal(args[args.indexOf("draft=true") - 1], "-F");', " assert.ok(args.includes(" + JSON.stringify("body=" + body) + "));", " console.log(" + JSON.stringify(JSON.stringify(response)) + ");", '} else if (args[3] === "repos/acme/api/pulls/4") {', " console.log(" + JSON.stringify(JSON.stringify(attached)) + ");", "} else {", ' assert.equal(args[3], "repos/acme/api");', ' console.log(JSON.stringify({ default_branch: "trunk" }));', "}", ].join("\n"), { mode: 0o700 }, ); process.env.PATH = [bin, path].filter(Boolean).join(delimiter); const github = ghGitHub(); const { url, base } = await github.publicationTarget("acme/api"); assert.equal(url, "https://github.com/acme/api.git"); const pull = await github.createDraft({ repo: "acme/api", branch, base, title: response.title, body }); assert.deepEqual(pull, { repo: "acme/api", number: 4, branch, title: "Sum all rows", state: "draft" }); assert.deepEqual(await github.pullRequests("acme/api", branch), [pull]); const withImages = await github.createDraft({ repo: "acme/api", branch, base, title: response.title, body, images: [ { name: "before.png", bytes: Buffer.from("before") }, { name: "after.png", bytes: Buffer.from("after") }, ], }); assert.deepEqual(withImages, { ...pull, attachments: [{ name: "before.png", url: asset }] });});
test("the GitHub adapter reads the state of many pull requests in one GraphQL call, and gives null for one GitHub has not", async (t) => { const bin = await mkdtemp(join(tmpdir(), "pilot-gh-test-")); const path = process.env.PATH; t.after(async () => { if (path === undefined) delete process.env.PATH; else process.env.PATH = path; await rm(bin, { recursive: true, force: true }); }); const calls = join(bin, "calls"); // gh prints GraphQL's answer, with its partial data, and exits with 1 when GitHub has no pull request by that number. const answer = { data: { p0: { pullRequest: { state: "OPEN", isDraft: true } }, p1: { pullRequest: { state: "OPEN", isDraft: false } }, p2: { pullRequest: { state: "MERGED", isDraft: false } }, p3: { pullRequest: { state: "CLOSED", isDraft: true } }, p4: { pullRequest: null }, }, errors: [{ type: "NOT_FOUND", message: "Could not resolve to a PullRequest with the number of 99." }], }; await writeFile( join(bin, "gh"), [ "#!/usr/bin/env node", 'const assert = require("node:assert/strict");', 'const fs = require("node:fs");', "const args = process.argv.slice(2);", "fs.appendFileSync(" + JSON.stringify(calls) + ', "call\\n");', 'assert.deepEqual(args.slice(0, 4), ["api", "--hostname", "github.com", "graphql"]);', 'const fields = Object.fromEntries(args.flatMap((flag, i) => (flag === "-f" || flag === "-F") ? [[args[i + 1].split("=")[0], flag + " " + args[i + 1]]] : []));', "assert.match(fields.query, /p4: repository\\(owner: \\$o4, name: \\$n4\\) \\{ pullRequest\\(number: \\$p4\\)/);", 'assert.equal(fields.o2, "-f o2=acme");', 'assert.equal(fields.n2, "-f n2=web");', 'assert.equal(fields.p2, "-F p2=7");', "console.log(" + JSON.stringify(JSON.stringify(answer)) + ");", 'console.error("gh: Could not resolve to a PullRequest with the number of 99.");', "process.exit(1);", ].join("\n"), { mode: 0o700 }, ); process.env.PATH = [bin, path].filter(Boolean).join(delimiter); const states = await ghGitHub().pullRequestStates([ { repo: "acme/api", number: 4 }, { repo: "acme/api", number: 5 }, { repo: "acme/web", number: 7 }, { repo: "acme/web", number: 8 }, { repo: "acme/api", number: 99 }, ]); assert.deepEqual(states, ["draft", "ready", "merged", "closed", null]); assert.equal(readFileSync(calls, "utf8"), "call\n");});