| 1 | // End-to-end: a real `wrangler dev` Worker, the real git CLI and the real runner agent. |
| 2 | // |
| 3 | // pnpm --filter @diffinery/runner build && pnpm --filter @diffinery/app e2e |
| 4 | // |
| 5 | // Covers: token auth, repo creation, git push/clone/fetch (protocol v2), push |
| 6 | // protection, workflows triggered by push and pull_request and executed by a |
| 7 | // self-hosted runner (the same agent that runs in Cloudflare Containers), |
| 8 | // check runs, branch protection with required checks and human review for |
| 9 | // agent-authored PRs, merging, the REST contents API, the MCP server and |
| 10 | // profile pictures (WASM image processing in the Worker). |
| 11 | import "./fresh-connections.mjs"; |
| 12 | import { spawn, spawnSync, execFileSync } from "node:child_process"; |
| 13 | import { mkdtempSync, writeFileSync, mkdirSync, readFileSync, existsSync, rmSync, openSync } from "node:fs"; |
| 14 | import { tmpdir } from "node:os"; |
| 15 | import { createHash, randomBytes } from "node:crypto"; |
| 16 | import { join, dirname, resolve } from "node:path"; |
| 17 | import { fileURLToPath } from "node:url"; |
| 18 | import { crc32, deflateSync } from "node:zlib"; |
| 19 | import { test, describe, before, after } from "node:test"; |
| 20 | import assert from "node:assert/strict"; |
| 21 | import { fakeGitHub, lfsObject } from "./fake-github.mjs"; |
| 22 | import { migratedState } from "./d1-state.mjs"; |
| 23 | import { freePort } from "./ports.mjs"; |
| 24 | |
| 25 | const APP = resolve(dirname(fileURLToPath(import.meta.url)), "../.."); |
| 26 | const AGENT = resolve(APP, "../../containers/runner/dist/agent.mjs"); |
| 27 | const work = mkdtempSync(join(tmpdir(), "diffinery-e2e-")); |
| 28 | let base; |
| 29 | /** The shared server's Worker itself, behind wrangler dev's proxy (see startServer). */ |
| 30 | let direct; |
| 31 | // The dev server writes to a file, not a pipe: a long synchronous git command keeps this process |
| 32 | // from reading a pipe, and a full one (retried webhooks log a stack each) stalls the server mid-push. |
| 33 | const devLogFile = join(work, "wrangler.log"); |
| 34 | const devLog = () => (existsSync(devLogFile) ? readFileSync(devLogFile, "utf8") : ""); |
| 35 | let token; |
| 36 | let agentToken; |
| 37 | |
| 38 | async function api(method, path, body, tok = token) { |
| 39 | const send = () => |
| 40 | fetch(`${base}/api/v3${path}`, { |
| 41 | method, |
| 42 | headers: { ...(tok === null ? {} : { authorization: `Bearer ${tok}` }), "content-type": "application/json" }, |
| 43 | body: body === undefined ? undefined : JSON.stringify(body), |
| 44 | }); |
| 45 | // After minutes of git traffic alone, the kept-alive connection from the last API call may have |
| 46 | // been closed by the dev server just as it is reused ("other side closed"): a GET is sent again. |
| 47 | let res; |
| 48 | for (let i = 0; ; i++) { |
| 49 | try { |
| 50 | res = await send(); |
| 51 | break; |
| 52 | } catch (e) { |
| 53 | if (method !== "GET" || i >= 2) throw new Error(`${method} ${path}: ${e.message} (${e.cause?.code ?? e.cause})\n--- wrangler log tail ---\n${devLog().slice(-3000)}`); |
| 54 | await sleep(1000); |
| 55 | } |
| 56 | } |
| 57 | const text = await res.text(); |
| 58 | let json = null; |
| 59 | try { |
| 60 | json = text ? JSON.parse(text) : null; |
| 61 | } catch { |
| 62 | json = text; |
| 63 | } |
| 64 | return { status: res.status, json }; |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Claim a waiting self-hosted job as a registered runner offering `labels`, the way `diffinery runner` |
| 69 | * does (personal tokens can't claim jobs): one runner per repository and set of labels. |
| 70 | */ |
| 71 | const e2eRunners = new Map(); |
| 72 | async function runnerClaim(repoPath, labels = ["linux"]) { |
| 73 | const key = `${repoPath} ${labels.join(",")}`; |
| 74 | if (!e2eRunners.has(key)) { |
| 75 | const reg = await api("POST", `${repoPath}/actions/runners/registration-token`); |
| 76 | assert.equal(reg.status, 201, JSON.stringify(reg.json)); |
| 77 | const runner = await api("POST", `${repoPath}/actions/runners/register`, { registration_token: reg.json.token, name: `e2e-runner-${e2eRunners.size + 1}`, labels }); |
| 78 | assert.equal(runner.status, 201, JSON.stringify(runner.json)); |
| 79 | e2eRunners.set(key, runner.json.token); |
| 80 | } |
| 81 | const r = await api("POST", `${repoPath}/actions/runners/claim`, {}, e2eRunners.get(key)); |
| 82 | // A repository deleted and created again under the same name needs a new runner. |
| 83 | if (r.status !== 401) return r; |
| 84 | e2eRunners.delete(key); |
| 85 | return runnerClaim(repoPath, labels); |
| 86 | } |
| 87 | async function ok(method, path, body, tok) { |
| 88 | const r = await api(method, path, body, tok); |
| 89 | assert.ok(r.status >= 200 && r.status < 300, `${method} ${path} -> ${r.status} ${JSON.stringify(r.json)}`); |
| 90 | return r.json; |
| 91 | } |
| 92 | |
| 93 | function git(cwd, args, tok = token, opts = {}) { |
| 94 | const auth = Buffer.from(`x-token:${tok}`).toString("base64"); |
| 95 | try { |
| 96 | return execFileSync("git", ["-c", `http.extraHeader=Authorization: Basic ${auth}`, "-c", "protocol.version=2", ...args], { |
| 97 | cwd, |
| 98 | encoding: "utf8", |
| 99 | stdio: ["ignore", "pipe", "pipe"], |
| 100 | env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_AUTHOR_NAME: "E2E", GIT_AUTHOR_EMAIL: "e2e@example.com", GIT_COMMITTER_NAME: "E2E", GIT_COMMITTER_EMAIL: "e2e@example.com" }, |
| 101 | ...opts, |
| 102 | }); |
| 103 | } catch (e) { |
| 104 | // What the server said, for a git command that wasn't meant to fail (gitFails reads stderr, not this). |
| 105 | if (/HTTP 5\d\d/.test(e.stderr ?? "")) e.message += `\n--- wrangler log tail ---\n${devLog().slice(-4000)}`; |
| 106 | throw e; |
| 107 | } |
| 108 | } |
| 109 | function gitFails(cwd, args, tok) { |
| 110 | try { |
| 111 | git(cwd, args, tok); |
| 112 | } catch (e) { |
| 113 | return `${e.stdout ?? ""}${e.stderr ?? ""}`; |
| 114 | } |
| 115 | assert.fail(`expected git ${args.join(" ")} to fail`); |
| 116 | } |
| 117 | |
| 118 | const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 119 | async function until(what, fn, timeoutMs = 60_000) { |
| 120 | const end = Date.now() + timeoutMs; |
| 121 | let last; |
| 122 | while (Date.now() < end) { |
| 123 | last = await fn(); |
| 124 | if (last) return last; |
| 125 | await sleep(500); |
| 126 | } |
| 127 | assert.fail(`timed out waiting for ${what}\n--- wrangler log tail ---\n${devLog().slice(-4000)}`); |
| 128 | } |
| 129 | |
| 130 | function spawnAsync(cmd, args, env, timeoutMs) { |
| 131 | return new Promise((done) => { |
| 132 | const child = spawn(cmd, args, { env: { ...process.env, ...env } }); |
| 133 | let stdout = ""; |
| 134 | let stderr = ""; |
| 135 | child.stdout.on("data", (d) => (stdout += d)); |
| 136 | child.stderr.on("data", (d) => (stderr += d)); |
| 137 | const t = setTimeout(() => child.kill("SIGKILL"), timeoutMs); |
| 138 | child.on("close", (status) => { |
| 139 | clearTimeout(t); |
| 140 | done({ status, stdout, stderr }); |
| 141 | }); |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * Claim one waiting self-hosted job and run it with the real agent. |
| 147 | * `envFor(claim)` adds environment for the agent (e.g. HOME, DIFFINERY_DISABLE_BUILTINS). |
| 148 | */ |
| 149 | async function runOneJob(owner, repo, { allowFailure = false, envFor = () => ({}), timeoutMs = 180_000, labels = ["linux"] } = {}) { |
| 150 | const claim = await until("a job to claim", async () => { |
| 151 | const r = await runnerClaim(`/repos/${owner}/${repo}`, labels); |
| 152 | return r.status === 200 ? r.json : null; |
| 153 | }); |
| 154 | const jobEnv = { ...envFor(claim), DIFFINERY_API_URL: claim.api_url, DIFFINERY_JOB_ID: claim.job_id, DIFFINERY_JOB_TOKEN: claim.token }; |
| 155 | // E2E_RUNNER_IMAGE runs the job inside the real runner image instead of the local agent bundle. |
| 156 | const image = process.env.E2E_RUNNER_IMAGE; |
| 157 | // Spawned asynchronously: a blocked event loop leaves undici holding keep-alive sockets the server already closed. |
| 158 | const r = image |
| 159 | ? await spawnAsync("docker", ["run", "--rm", "--network", "host", ...Object.entries(jobEnv).flatMap(([k, v]) => ["-e", `${k}=${v}`]), image], {}, timeoutMs + 120_000) |
| 160 | : await spawnAsync(process.execPath, [AGENT], { ...process.env, ...jobEnv, DIFFINERY_WORK_ROOT: join(work, `job-${claim.job_id}`) }, timeoutMs); |
| 161 | const out = `${r.stdout}${r.stderr}`; |
| 162 | if (r.status !== 0 && !allowFailure) { |
| 163 | const log = await fetch(`${base}/api/v3/repos/${owner}/${repo}/actions/jobs/${claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } }).then((x) => x.text(), (e) => String(e)); |
| 164 | const job = await api("GET", `/repos/${owner}/${repo}/actions/jobs/${claim.job_id}`).catch((e) => ({ json: String(e) })); |
| 165 | assert.fail(`runner agent exited ${r.status}:\n${out}\n--- job ---\n${JSON.stringify(job.json, null, 2)}\n--- job log ---\n${log}\n--- wrangler log tail ---\n${devLog().slice(-3000)}`); |
| 166 | } |
| 167 | return { claim, out }; |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * A `wrangler dev` server of its own with an admin token. Most tests share the one before() starts; |
| 172 | * the real histories each push to their own, so they run side by side on separate processes. |
| 173 | */ |
| 174 | const servers = []; |
| 175 | /** `restart`: start again on the state a stopped server of the same name left. */ |
| 176 | async function startServer(name = "", { restart = false } = {}) { |
| 177 | const port = await freePort(); |
| 178 | const url = `http://127.0.0.1:${port}`; |
| 179 | const persist = join(work, name ? `state-${name}` : "state"); |
| 180 | const logFile = name ? join(work, `wrangler-${name}.log`) : devLogFile; |
| 181 | const debugLogFile = join(work, name ? `wrangler-debug-${name}.log` : "wrangler-debug.log"); |
| 182 | const log = () => (existsSync(logFile) ? readFileSync(logFile, "utf8") : ""); |
| 183 | if (!restart) migratedState(APP, persist); |
| 184 | // Until the web UI exists, run the same app through the API-only entry. |
| 185 | const entry = existsSync(join(APP, "src/web/routes.tsx")) ? [] : ["test/e2e/entry.ts"]; |
| 186 | const out = openSync(logFile, "a"); |
| 187 | const child = spawn("pnpm", ["exec", "wrangler", "dev", ...entry, "-e", "dev", "--ip", "127.0.0.1", "--port", String(port), "--inspector-port", String(await freePort()), "--persist-to", persist, "--var", `PUBLIC_URL:${url}`, "--var", `MASTER_KEY:${randomBytes(32).toString("base64")}`, "--var", "SCHEDULE_TICK_MINUTES:1", "--show-interactive-dev-session=false"], { |
| 188 | cwd: APP, |
| 189 | env: { ...process.env, CI: "1", WRANGLER_SEND_METRICS: "false", WRANGLER_LOG_PATH: debugLogFile }, |
| 190 | stdio: ["ignore", out, out], |
| 191 | detached: true, |
| 192 | }); |
| 193 | servers.push(child); |
| 194 | const end = Date.now() + 120_000; |
| 195 | while (!(await fetch(`${url}/healthz`).then((r) => r.ok, () => false))) { |
| 196 | if (Date.now() > end) assert.fail(`timed out waiting for wrangler dev${name ? ` (${name})` : ""}\n--- wrangler log tail ---\n${log().slice(-4000)}`); |
| 197 | await sleep(500); |
| 198 | } |
| 199 | // DEV_AUTH signs requests without credentials in as dev@diffinery.local; mint a real token for everything else. |
| 200 | const res = await fetch(`${url}/api/v3/diffinery/tokens`, { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ name: "e2e", scopes: ["admin"] }) }); |
| 201 | assert.equal(res.status, 201, await res.clone().text()); |
| 202 | const stop = () => { |
| 203 | try { |
| 204 | process.kill(-child.pid, "SIGTERM"); |
| 205 | } catch {} |
| 206 | }; |
| 207 | return { base: url, direct: workerUrl(debugLogFile), workerUrl: () => workerUrl(debugLogFile), token: (await res.json()).token, log, stop }; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * wrangler dev answers on `base` through a proxy Worker of its own, which drops one of several large |
| 212 | * pushes streaming through it at once ("Error inside ProxyWorker ... Network connection lost": the |
| 213 | * push never reaches our Worker; 3 of 5 runs of three pushes, none of 5 sent to the Worker itself). |
| 214 | * Its debug log names the Worker's own address; null if it doesn't. |
| 215 | */ |
| 216 | function workerUrl(debugLogFile) { |
| 217 | const log = existsSync(debugLogFile) ? readFileSync(debugLogFile, "utf8") : ""; |
| 218 | const at = [...log.matchAll(/"userWorkerUrl":\{"protocol":"(https?:)","hostname":"([^"]+)","port":"(\d+)"/g)].pop(); |
| 219 | return at ? `${at[1]}//${at[2]}:${at[3]}` : null; |
| 220 | } |
| 221 | |
| 222 | before(async () => { |
| 223 | assert.ok(existsSync(AGENT), `build the runner first: pnpm --filter @diffinery/runner build (missing ${AGENT})`); |
| 224 | ({ base, direct, token } = await startServer()); |
| 225 | }); |
| 226 | |
| 227 | after(() => { |
| 228 | for (const dev of servers) { |
| 229 | try { |
| 230 | process.kill(-dev.pid, "SIGTERM"); |
| 231 | } catch {} |
| 232 | } |
| 233 | if (process.env.E2E_KEEP !== "1") rmSync(work, { recursive: true, force: true }); |
| 234 | }); |
| 235 | |
| 236 | // Scheduled workflows: the dev environment has no cron trigger, like a deploy whose cron trigger |
| 237 | // hasn't been applied yet, so schedules must fire from the scheduler's own alarm. The schedule is |
| 238 | // registered first and checked by the last test, so its ticks (1 minute here) come while the rest runs. |
| 239 | let tickRepo; |
| 240 | test("scheduled workflows: a push to the default branch registers the schedule", async () => { |
| 241 | const me = await ok("GET", "/user"); |
| 242 | tickRepo = `${me.login}/schedules`; |
| 243 | await ok("POST", "/user/repos", { name: "schedules" }); |
| 244 | const src = join(work, "schedules"); |
| 245 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 246 | writeFileSync(join(src, ".github/workflows/tick.yml"), `on:\n schedule:\n - cron: "* * * * *"\njobs:\n tick:\n runs-on: [self-hosted, linux]\n steps:\n - run: echo tick\n`); |
| 247 | git(src, ["init", "-q", "-b", "main"]); |
| 248 | git(src, ["add", "-A"]); |
| 249 | git(src, ["commit", "-q", "-m", "schedule"]); |
| 250 | git(src, ["push", "-q", `${base}/${tickRepo}.git`, "main"]); |
| 251 | }); |
| 252 | |
| 253 | test("git hosting, Actions, protected PR flow and MCP", async () => { |
| 254 | const me = await ok("GET", "/user"); |
| 255 | const owner = me.login; |
| 256 | assert.ok(me.site_admin, "dev user is a bootstrap admin"); |
| 257 | |
| 258 | // ------------------------------------------------------------ repo + first push |
| 259 | const repo = await ok("POST", "/user/repos", { name: "demo", description: "e2e" }); |
| 260 | assert.equal(repo.full_name, `${owner}/demo`); |
| 261 | const src = join(work, "src"); |
| 262 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 263 | writeFileSync(join(src, "README.md"), "# demo\n"); |
| 264 | writeFileSync(join(src, "app.txt"), "one\ntwo\nthree\n"); |
| 265 | writeFileSync( |
| 266 | join(src, ".github/workflows/ci.yml"), |
| 267 | `name: CI |
| 268 | on: |
| 269 | push: |
| 270 | branches: [main] |
| 271 | pull_request: |
| 272 | jobs: |
| 273 | build: |
| 274 | runs-on: [self-hosted, linux] |
| 275 | steps: |
| 276 | - uses: actions/checkout@v4 |
| 277 | - name: Check files |
| 278 | run: | |
| 279 | test -f README.md |
| 280 | echo "event=\${{ github.event_name }} sha=\${{ github.sha }}" |
| 281 | grep -q bad app.txt && { echo "found bad line"; exit 1; } || true |
| 282 | - id: out |
| 283 | run: echo "lines=$(wc -l < app.txt | tr -d ' ')" >> "$GITHUB_OUTPUT" |
| 284 | - run: echo "app.txt has \${{ steps.out.outputs.lines }} lines" |
| 285 | `, |
| 286 | ); |
| 287 | git(src, ["init", "-q", "-b", "main"]); |
| 288 | git(src, ["add", "-A"]); |
| 289 | git(src, ["commit", "-q", "-m", "initial"]); |
| 290 | git(src, ["remote", "add", "origin", `${base}/${owner}/demo.git`]); |
| 291 | git(src, ["push", "-q", "origin", "main"]); |
| 292 | const mainSha = git(src, ["rev-parse", "HEAD"]).trim(); |
| 293 | |
| 294 | const branch = await ok("GET", `/repos/${owner}/demo/branches/main`); |
| 295 | assert.equal(branch.commit.sha, mainSha); |
| 296 | |
| 297 | // ------------------------------------------------------------ the push ran CI on a self-hosted runner |
| 298 | const pushRun = await until("push run", async () => (await ok("GET", `/repos/${owner}/demo/actions/runs?event=push`)).workflow_runs[0]); |
| 299 | assert.equal(pushRun.head_sha, mainSha); |
| 300 | const job1 = await runOneJob(owner, "demo"); |
| 301 | const finished = await until("push run to complete", async () => { |
| 302 | const r = await ok("GET", `/repos/${owner}/demo/actions/runs/${pushRun.id}`); |
| 303 | return r.status === "completed" ? r : null; |
| 304 | }); |
| 305 | assert.equal(finished.conclusion, "success", job1.out); |
| 306 | const logs = await (await fetch(`${base}/api/v3/repos/${owner}/demo/actions/jobs/${job1.claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 307 | assert.match(logs, /event=push/); |
| 308 | assert.match(logs, /app\.txt has 3 lines/); |
| 309 | const checks = await ok("GET", `/repos/${owner}/demo/commits/${mainSha}/check-runs`); |
| 310 | assert.equal(checks.check_runs.find((c) => c.name === "CI / build")?.conclusion, "success"); |
| 311 | |
| 312 | // ------------------------------------------------------------ clone over protocol v2 |
| 313 | const clone = join(work, "clone"); |
| 314 | git(work, ["clone", "-q", `${base}/${owner}/demo.git`, clone]); |
| 315 | assert.equal(readFileSync(join(clone, "app.txt"), "utf8"), "one\ntwo\nthree\n"); |
| 316 | |
| 317 | // ------------------------------------------------------------ secrets are blocked on push |
| 318 | // The key below is a fake that push protection must block in the pushed file; this |
| 319 | // marker exempts only the test source itself: diffinery:allow-secret |
| 320 | writeFileSync(join(clone, "config.env"), "AWS_ACCESS_KEY_ID=AKIAIOSFODNN7EXAMPL3\n"); |
| 321 | git(clone, ["add", "-A"]); |
| 322 | git(clone, ["commit", "-q", "-m", "oops"]); |
| 323 | assert.match(gitFails(clone, ["push", "origin", "main"]), /secret|AWS/i); |
| 324 | // Objects from a blocked push are not kept, so retrying (even with push negotiation) is blocked too. |
| 325 | assert.match(gitFails(clone, ["-c", "push.negotiate=true", "push", "origin", "HEAD:refs/heads/fixture"]), /secret|AWS/i); |
| 326 | // A repository admin can push anyway by giving a reason; the bypass is audited. |
| 327 | assert.match(gitFails(clone, ["push", "-o", "allow-secrets=", "origin", "HEAD:refs/heads/fixture"]), /secret|AWS/i); |
| 328 | git(clone, ["push", "-q", "-o", "allow-secrets=fake key used by this test", "origin", "HEAD:refs/heads/fixture"]); |
| 329 | assert.ok((await ok("GET", `/repos/${owner}/demo/branches`)).some((b) => b.name === "fixture")); |
| 330 | git(clone, ["push", "-q", "origin", ":refs/heads/fixture"]); |
| 331 | git(clone, ["reset", "-q", "--hard", "origin/main"]); |
| 332 | |
| 333 | // ------------------------------------------------------------ protect main: PR + green CI + review |
| 334 | await ok("PUT", `/repos/${owner}/demo/branches/main/protection`, { |
| 335 | requirePullRequest: true, |
| 336 | requiredApprovals: 0, |
| 337 | dismissStaleReviews: true, |
| 338 | requiredChecks: ["CI / build"], |
| 339 | allowForcePush: false, |
| 340 | allowDeletion: false, |
| 341 | requireLinearHistory: false, |
| 342 | enforceAdmins: true, |
| 343 | agentsMayPush: false, |
| 344 | agentsMayMerge: false, |
| 345 | }); |
| 346 | writeFileSync(join(clone, "app.txt"), "one\ntwo\nthree\nfour\n"); |
| 347 | git(clone, ["commit", "-q", "-am", "direct push"]); |
| 348 | assert.match(gitFails(clone, ["push", "origin", "main"]), /pull request|protected/i); |
| 349 | git(clone, ["reset", "-q", "--hard", "origin/main"]); |
| 350 | |
| 351 | // ------------------------------------------------------------ an AI agent opens a PR |
| 352 | const created = await ok("POST", "/diffinery/agents", { login: "e2e-bot", name: "E2E Bot" }, null); |
| 353 | agentToken = created.token.token; |
| 354 | const agentClone = join(work, "agent"); |
| 355 | git(work, ["clone", "-q", `${base}/${owner}/demo.git`, agentClone], agentToken); |
| 356 | git(agentClone, ["checkout", "-q", "-b", "feature"], agentToken); |
| 357 | writeFileSync(join(agentClone, "app.txt"), "one\ntwo\nthree\nfour\n"); |
| 358 | git(agentClone, ["commit", "-q", "-am", "Add a fourth line"], agentToken); |
| 359 | assert.match(gitFails(agentClone, ["push", "origin", "HEAD:main"], agentToken), /agent|protected|pull request/i); |
| 360 | // push.negotiate=true makes git run a protocol v2 fetch with wait-for-done before pushing. |
| 361 | const negotiated = spawnSync("git", ["-c", `http.extraHeader=Authorization: Basic ${Buffer.from(`x-token:${agentToken}`).toString("base64")}`, "-c", "protocol.version=2", "-c", "push.negotiate=true", "push", "-q", "origin", "feature"], { cwd: agentClone, encoding: "utf8", env: { ...process.env, GIT_TERMINAL_PROMPT: "0" } }); |
| 362 | assert.equal(negotiated.status, 0, negotiated.stderr); |
| 363 | assert.doesNotMatch(negotiated.stderr, /negotiation failed|unexpected/i); |
| 364 | const pr = await ok("POST", `/repos/${owner}/demo/pulls`, { title: "Add a fourth line", head: "feature", base: "main", intent: "Extend app.txt for the e2e test" }, agentToken); |
| 365 | assert.equal(pr.number, 1); |
| 366 | |
| 367 | // An agent may start an empty repository's default branch (as auto_init would), and then only through pull requests. |
| 368 | await ok("POST", "/user/repos", { name: "agent-seeded", private: true }); |
| 369 | const seed = join(work, "agent-seeded"); |
| 370 | git(work, ["init", "-q", "-b", "main", seed]); |
| 371 | mkdirSync(join(seed, ".github/workflows"), { recursive: true }); |
| 372 | writeFileSync(join(seed, ".github/workflows/ci.yml"), "on: push\njobs:\n a:\n runs-on: ubuntu-latest\n steps:\n - run: true\n"); |
| 373 | writeFileSync(join(seed, "README.md"), "# seeded by an agent\n"); |
| 374 | git(seed, ["add", "-A"]); |
| 375 | git(seed, ["commit", "-q", "-m", "Initial commit"]); |
| 376 | // Not with workflow files: those reach the default branch only through a pull request a person merges. |
| 377 | assert.match(gitFails(seed, ["push", `${base}/${owner}/agent-seeded.git`, "main"], agentToken), /can't add workflow file `\.github\/workflows\/ci\.yml`/); |
| 378 | git(seed, ["rm", "-q", "-r", ".github"]); |
| 379 | git(seed, ["commit", "-q", "--amend", "-m", "Initial commit"]); |
| 380 | git(seed, ["push", "-q", `${base}/${owner}/agent-seeded.git`, "main"], agentToken); |
| 381 | writeFileSync(join(seed, "README.md"), "# changed\n"); |
| 382 | git(seed, ["commit", "-q", "-am", "Second commit"]); |
| 383 | assert.match(gitFails(seed, ["push", `${base}/${owner}/agent-seeded.git`, "main"], agentToken), /agents may not push to protected or default branches/); |
| 384 | |
| 385 | // The pull_request run executes; until then the PR is blocked on the required check. |
| 386 | let m = await ok("GET", `/repos/${owner}/demo/pulls/1/merge`); |
| 387 | assert.equal(m.mergeable, false); |
| 388 | await runOneJob(owner, "demo"); |
| 389 | await until("PR check to pass", async () => { |
| 390 | const r = await ok("GET", `/repos/${owner}/demo/pulls/1/merge`); |
| 391 | return r.checks?.length > 0 && r.checks.every((c) => c.state === "success") ? r : null; |
| 392 | }); |
| 393 | m = await ok("GET", `/repos/${owner}/demo/pulls/1/merge`); |
| 394 | assert.equal(m.mergeable, false, "agent-authored PRs need a human approval"); |
| 395 | assert.ok(JSON.stringify(m.blockers).match(/approv|human/i), JSON.stringify(m.blockers)); |
| 396 | |
| 397 | // The agent cannot approve or merge its own work. |
| 398 | const agentMerge = await api("PUT", `/repos/${owner}/demo/pulls/1/merge`, { merge_method: "squash" }, agentToken); |
| 399 | assert.ok([403, 405, 409].includes(agentMerge.status), JSON.stringify(agentMerge)); |
| 400 | |
| 401 | // Another agent's approval is advisory and does not unblock the merge. |
| 402 | const reviewer = (await ok("POST", "/diffinery/agents", { login: "e2e-reviewer" }, null)).token.token; |
| 403 | await ok("POST", `/repos/${owner}/demo/pulls/1/reviews`, { event: "APPROVE", body: "bot says ok" }, reviewer); |
| 404 | assert.equal((await ok("GET", `/repos/${owner}/demo/pulls/1/merge`)).mergeable, false); |
| 405 | |
| 406 | // A human reviews (line comment + approval) and merges. |
| 407 | await ok("POST", `/repos/${owner}/demo/pulls/1/reviews`, { event: "APPROVE", body: "LGTM", comments: [{ path: "app.txt", line: 4, body: "nice line" }] }); |
| 408 | m = await ok("GET", `/repos/${owner}/demo/pulls/1/merge`); |
| 409 | assert.equal(m.mergeable, true, JSON.stringify(m)); |
| 410 | const merged = await ok("PUT", `/repos/${owner}/demo/pulls/1/merge`, { merge_method: "squash", delete_branch: true }); |
| 411 | assert.ok(merged.sha); |
| 412 | const prAfter = await ok("GET", `/repos/${owner}/demo/pulls/1`); |
| 413 | assert.equal(prAfter.merged, true); |
| 414 | |
| 415 | // ------------------------------------------------------------ fetch the merge, read through REST |
| 416 | git(clone, ["pull", "-q", "--ff-only", "origin", "main"]); |
| 417 | assert.equal(readFileSync(join(clone, "app.txt"), "utf8"), "one\ntwo\nthree\nfour\n"); |
| 418 | const content = await ok("GET", `/repos/${owner}/demo/contents/app.txt`); |
| 419 | assert.equal(Buffer.from(content.content, "base64").toString(), "one\ntwo\nthree\nfour\n"); |
| 420 | const branches = await ok("GET", `/repos/${owner}/demo/branches`); |
| 421 | assert.ok(!branches.some((b) => b.name === "feature"), "head branch deleted after merge"); |
| 422 | |
| 423 | // ------------------------------------------------------------ shallow clone, then deepen |
| 424 | const shallow = join(work, "shallow"); |
| 425 | git(work, ["clone", "-q", "--depth", "1", `${base}/${owner}/demo.git`, shallow]); |
| 426 | assert.equal(git(shallow, ["rev-list", "--count", "HEAD"]).trim(), "1"); |
| 427 | assert.equal(readFileSync(join(shallow, "app.txt"), "utf8"), "one\ntwo\nthree\nfour\n"); |
| 428 | git(shallow, ["fetch", "-q", "--unshallow"]); |
| 429 | assert.equal(git(shallow, ["rev-list", "--count", "HEAD"]).trim(), "2"); |
| 430 | git(shallow, ["fsck", "--no-progress"]); |
| 431 | |
| 432 | // ------------------------------------------------------------ MCP: the agent works without git |
| 433 | const rpc = async (method, params, id = 1) => { |
| 434 | const r = await fetch(`${base}/mcp`, { method: "POST", headers: { authorization: `Bearer ${agentToken}`, "content-type": "application/json", accept: "application/json, text/event-stream" }, body: JSON.stringify({ jsonrpc: "2.0", id, method, params }) }); |
| 435 | assert.equal(r.status, 200); |
| 436 | return (await r.json()).result; |
| 437 | }; |
| 438 | const init = await rpc("initialize", { protocolVersion: "2025-06-18", capabilities: {}, clientInfo: { name: "e2e", version: "1" } }); |
| 439 | assert.equal(init.serverInfo.name, "diffinery"); |
| 440 | const tools = await rpc("tools/list", {}); |
| 441 | assert.ok(tools.tools.some((t) => t.name === "commit_files")); |
| 442 | const file = await rpc("tools/call", { name: "get_file", arguments: { owner, repo: "demo", path: "README.md" } }); |
| 443 | assert.equal(file.isError, false); |
| 444 | assert.equal(file.structuredContent.content, "# demo\n"); |
| 445 | const commit = await rpc("tools/call", { name: "commit_files", arguments: { owner, repo: "demo", branch: "mcp-change", create_branch_from: "main", message: "Edit via MCP", files: [{ path: "docs/notes.md", content: "hello\n" }] } }); |
| 446 | assert.equal(commit.isError, false, JSON.stringify(commit)); |
| 447 | assert.equal(commit.structuredContent.html_url, `${base}/${owner}/demo/commit/${commit.structuredContent.sha}`); |
| 448 | const blocked = await rpc("tools/call", { name: "commit_files", arguments: { owner, repo: "demo", branch: "main", message: "sneaky", files: [{ path: "x", content: "y" }] } }); |
| 449 | assert.equal(blocked.isError, true); |
| 450 | const opened = await rpc("tools/call", { name: "create_pull_request", arguments: { owner, repo: "demo", title: "Notes", head: "mcp-change", intent: "Add notes" } }); |
| 451 | assert.equal(opened.isError, false, JSON.stringify(opened)); |
| 452 | assert.equal(opened.structuredContent.number, 2); |
| 453 | // Results link to what they made or read, for the agent to hand people. |
| 454 | assert.equal(opened.structuredContent.html_url, `${base}/${owner}/demo/pull/2`); |
| 455 | const said = await rpc("tools/call", { name: "comment", arguments: { owner, repo: "demo", number: 2, body: "Ready for a look" } }); |
| 456 | assert.equal(said.structuredContent.html_url, `${base}/${owner}/demo/issues/2#c-${said.structuredContent.id}`); |
| 457 | const runs = await rpc("tools/call", { name: "list_workflow_runs", arguments: { owner, repo: "demo" } }); |
| 458 | const run = runs.structuredContent.runs[0]; |
| 459 | assert.equal(run.html_url, `${base}/${owner}/demo/actions/runs/${run.id}`); |
| 460 | const detail = await rpc("tools/call", { name: "get_workflow_run", arguments: { owner, repo: "demo", run_id: run.id } }); |
| 461 | assert.equal(detail.structuredContent.jobs[0].html_url, `${base}/${owner}/demo/actions/runs/${run.id}/job/${detail.structuredContent.jobs[0].id}`); |
| 462 | for (const path of [new URL(said.structuredContent.html_url).pathname, new URL(detail.structuredContent.jobs[0].html_url).pathname]) assert.equal((await fetch(`${base}${path}`)).status, 200, path); |
| 463 | |
| 464 | // ------------------------------------------------------------ web UI renders the same data (signed in via DEV_AUTH) |
| 465 | for (const path of ["/", `/${owner}`, `/${owner}/demo`, `/${owner}/demo/blob/main/app.txt`, `/${owner}/demo/commits/main`, `/${owner}/demo/pulls`, `/${owner}/demo/pull/1`, `/${owner}/demo/pull/1/files`, `/${owner}/demo/actions`, `/${owner}/demo/actions/runs/${pushRun.id}`, `/${owner}/demo/actions/runs/${pushRun.id}/job/${job1.claim.job_id}`, `/${owner}/demo/settings/branches`, "/settings/tokens"]) { |
| 466 | const res = await fetch(`${base}${path}`); |
| 467 | const html = await res.text(); |
| 468 | assert.equal(res.status, 200, `${path} -> ${res.status}\n${html.slice(0, 500)}`); |
| 469 | assert.match(res.headers.get("content-type") ?? "", /text\/html/, path); |
| 470 | } |
| 471 | assert.match(await (await fetch(`${base}/${owner}/demo/pull/1`)).text(), /Add a fourth line/); |
| 472 | const forged = await fetch(`${base}/${owner}/demo/issues/1/comment`, { method: "POST", headers: { "content-type": "application/x-www-form-urlencoded", origin: "https://evil.example", "sec-fetch-site": "cross-site" }, body: "body=hi" }); |
| 473 | assert.equal(forged.status, 403); |
| 474 | |
| 475 | // ------------------------------------------------------------ the CLI against the same server |
| 476 | const CLI = resolve(APP, "../cli/dist/diffinery.mjs"); |
| 477 | if (existsSync(CLI)) { |
| 478 | const cli = (...args) => execFileSync(process.execPath, [CLI, ...args], { encoding: "utf8", cwd: clone, env: { ...process.env, DIFFINERY_URL: base, DIFFINERY_TOKEN: token, NO_COLOR: "1", XDG_CONFIG_HOME: join(work, "cfg") } }); |
| 479 | assert.match(cli("whoami"), new RegExp(owner)); |
| 480 | const prs = JSON.parse(cli("pr", "list", "--state", "all", "--json")); |
| 481 | assert.ok(prs.some((p) => p.number === 1)); |
| 482 | assert.match(cli("workflow", "validate"), /ci\.yml/); |
| 483 | } |
| 484 | |
| 485 | // The commit made through MCP carries the agent's identity. |
| 486 | const agentCommit = await ok("GET", `/repos/${owner}/demo/commits/mcp-change`); |
| 487 | assert.match(JSON.stringify(agentCommit), /e2e-bot/); |
| 488 | }); |
| 489 | |
| 490 | // 2026-10-24: objects of 64 KB to 1 MB failed to inflate in workerd (not in Node, so unit tests passed): |
| 491 | // pushes were refused, and reading those files and diffs answered 500. Sizes around each limit, read |
| 492 | // back through REST, a compare and a push that deltas against them. |
| 493 | test("objects of every size: read back, compared and used as delta bases", async () => { |
| 494 | const owner = (await ok("GET", "/user")).login; |
| 495 | await ok("POST", "/user/repos", { name: "sizes" }); |
| 496 | const src = join(work, "sizes"); |
| 497 | mkdirSync(src, { recursive: true }); |
| 498 | const sizes = [100, 16_385, 65_535, 65_537, 101_330, 600_000]; |
| 499 | const text = (size, seed) => { |
| 500 | const lines = []; |
| 501 | for (let n = 0, i = 0; n < size; i++) { |
| 502 | const line = `${seed} line ${i}: ${(i * 7919) % 10007} ${"abcdefghij".repeat(4)}\n`; |
| 503 | lines.push(line); |
| 504 | n += line.length; |
| 505 | } |
| 506 | return lines.join("").slice(0, size); |
| 507 | }; |
| 508 | for (const size of sizes) writeFileSync(join(src, `f${size}.txt`), text(size, "v1")); |
| 509 | git(src, ["init", "-q", "-b", "main"]); |
| 510 | git(src, ["add", "-A"]); |
| 511 | git(src, ["commit", "-q", "-m", "sizes"]); |
| 512 | git(src, ["remote", "add", "origin", `${base}/${owner}/sizes.git`]); |
| 513 | git(src, ["push", "-q", "origin", "main"]); |
| 514 | const first = git(src, ["rev-parse", "HEAD"]).trim(); |
| 515 | for (const size of sizes) { |
| 516 | const c = await ok("GET", `/repos/${owner}/sizes/contents/f${size}.txt`); |
| 517 | assert.equal(Buffer.from(c.content, "base64").toString(), text(size, "v1"), `f${size}.txt reads back`); |
| 518 | } |
| 519 | // One changed line each: git sends deltas against the stored objects, which the server inflates. |
| 520 | for (const size of sizes) writeFileSync(join(src, `f${size}.txt`), "changed\n" + text(size, "v1").slice(8)); |
| 521 | git(src, ["commit", "-q", "-am", "change"]); |
| 522 | git(src, ["push", "-q", "origin", "main"]); |
| 523 | const cmp = await ok("GET", `/repos/${owner}/sizes/compare/${first}...main`); |
| 524 | assert.equal(cmp.files.length, sizes.length); |
| 525 | // Relative refs, as git takes them. |
| 526 | for (const spec of ["main~1...main", "main^...main", "main~...HEAD", `${first}...main~0`]) { |
| 527 | const rel = await ok("GET", `/repos/${owner}/sizes/compare/${spec}`); |
| 528 | assert.deepEqual([rel.base_commit.sha, rel.total_commits, rel.files.length], [first, 1, sizes.length], spec); |
| 529 | } |
| 530 | assert.equal((await ok("GET", `/repos/${owner}/sizes/commits/main~1`)).sha, first); |
| 531 | assert.equal((await api("GET", `/repos/${owner}/sizes/compare/main~2...main`)).status, 404, "past the first commit"); |
| 532 | assert.equal((await api("GET", `/repos/${owner}/sizes/compare/main^2...main`)).status, 404, "no second parent"); |
| 533 | await ok("GET", `/repos/${owner}/sizes/commits/main`); |
| 534 | const clone = join(work, "sizes-clone"); |
| 535 | git(work, ["clone", "-q", `${base}/${owner}/sizes.git`, clone]); |
| 536 | git(clone, ["fsck", "--no-progress"]); |
| 537 | for (const size of sizes) assert.equal(readFileSync(join(clone, `f${size}.txt`), "utf8"), readFileSync(join(src, `f${size}.txt`), "utf8")); |
| 538 | }); |
| 539 | |
| 540 | test("reusable workflows: inputs, secrets, outputs, nesting and skipped calls", async () => { |
| 541 | const owner = (await ok("GET", "/user")).login; |
| 542 | await ok("POST", "/user/repos", { name: "reuse" }); |
| 543 | await api("PUT", `/repos/${owner}/reuse/actions/secrets/TOPSECRET`, { value: "s3cret" }); |
| 544 | const src = join(work, "reuse"); |
| 545 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 546 | writeFileSync( |
| 547 | join(src, ".github/workflows/lib.yml"), |
| 548 | `on: |
| 549 | workflow_call: |
| 550 | inputs: |
| 551 | greeting: { type: string, required: true } |
| 552 | loud: { type: boolean, default: false } |
| 553 | secrets: |
| 554 | TOPSECRET: { required: true } |
| 555 | outputs: |
| 556 | word: |
| 557 | value: \${{ jobs.shout.outputs.word }} |
| 558 | jobs: |
| 559 | say: |
| 560 | runs-on: [self-hosted, linux] |
| 561 | outputs: |
| 562 | word: \${{ steps.s.outputs.word }} |
| 563 | steps: |
| 564 | - id: s |
| 565 | run: echo "word=\${{ inputs.greeting }}-there" >> "$GITHUB_OUTPUT" |
| 566 | - run: echo "secret ok=\${{ secrets.TOPSECRET == 's3cret' }} loud=\${{ inputs.loud }} job=\${{ github.job }}" |
| 567 | shout: |
| 568 | needs: say |
| 569 | runs-on: [self-hosted, linux] |
| 570 | outputs: |
| 571 | word: \${{ steps.s.outputs.word }} |
| 572 | steps: |
| 573 | - id: s |
| 574 | run: echo "word=\${{ needs.say.outputs.word }}!" >> "$GITHUB_OUTPUT" |
| 575 | `, |
| 576 | ); |
| 577 | writeFileSync( |
| 578 | join(src, ".github/workflows/main.yml"), |
| 579 | `name: Main |
| 580 | on: push |
| 581 | jobs: |
| 582 | call: |
| 583 | name: Greeter |
| 584 | uses: ./.github/workflows/lib.yml |
| 585 | with: |
| 586 | greeting: \${{ github.event_name == 'push' && 'hi' || 'no' }} |
| 587 | loud: true |
| 588 | secrets: inherit |
| 589 | nope: |
| 590 | if: \${{ false }} |
| 591 | uses: ./.github/workflows/lib.yml |
| 592 | with: |
| 593 | greeting: never |
| 594 | secrets: |
| 595 | TOPSECRET: \${{ secrets.TOPSECRET }} |
| 596 | after: |
| 597 | needs: [call, nope] |
| 598 | if: \${{ always() }} |
| 599 | runs-on: [self-hosted, linux] |
| 600 | steps: |
| 601 | - run: echo "got=\${{ needs.call.outputs.word }} call=\${{ needs.call.result }} nope=\${{ needs.nope.result }}" |
| 602 | `, |
| 603 | ); |
| 604 | git(src, ["init", "-q", "-b", "main"]); |
| 605 | git(src, ["add", "."]); |
| 606 | git(src, ["commit", "-q", "-m", "reusable"]); |
| 607 | git(src, ["remote", "add", "origin", `${base}/${owner}/reuse.git`]); |
| 608 | git(src, ["push", "-q", "origin", "main"]); |
| 609 | |
| 610 | const run = await until("reuse run", async () => (await ok("GET", `/repos/${owner}/reuse/actions/runs`)).workflow_runs[0]); |
| 611 | const logOf = async (id) => (await fetch(`${base}/api/v3/repos/${owner}/reuse/actions/jobs/${id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 612 | const say = await runOneJob(owner, "reuse"); |
| 613 | assert.equal(say.claim.name, "Greeter / say"); |
| 614 | const sayLog = await logOf(say.claim.job_id); |
| 615 | assert.match(sayLog, /secret ok=true loud=true job=say/); |
| 616 | assert.doesNotMatch(sayLog, /s3cret/); |
| 617 | const shout = await runOneJob(owner, "reuse"); |
| 618 | assert.equal(shout.claim.name, "Greeter / shout"); |
| 619 | const after = await runOneJob(owner, "reuse"); |
| 620 | assert.equal(after.claim.name, "after"); |
| 621 | assert.match(await logOf(after.claim.job_id), /got=hi-there! call=success nope=skipped/); |
| 622 | const done = await until("reuse run to complete", async () => { |
| 623 | const r = await ok("GET", `/repos/${owner}/reuse/actions/runs/${run.id}`); |
| 624 | return r.status === "completed" ? r : null; |
| 625 | }); |
| 626 | assert.equal(done.conclusion, "success"); |
| 627 | const jobs = (await ok("GET", `/repos/${owner}/reuse/actions/runs/${run.id}/jobs`)).jobs; |
| 628 | assert.deepEqual(jobs.filter((j) => j.conclusion === "skipped").map((j) => j.name).sort(), ["nope / say", "nope / shout"]); |
| 629 | }); |
| 630 | |
| 631 | test("job graph: needs-only scheduling, skip cascades, per-leg runs-on, continue-on-error, job concurrency and public payloads", async () => { |
| 632 | const owner = (await ok("GET", "/user")).login; |
| 633 | await ok("POST", "/user/repos", { name: "graph", private: false }); |
| 634 | const src = join(work, "graph"); |
| 635 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 636 | writeFileSync( |
| 637 | join(src, ".github/workflows/graph.yml"), |
| 638 | `name: Graph |
| 639 | on: push |
| 640 | jobs: |
| 641 | slow: |
| 642 | runs-on: [self-hosted, e2e-slow] |
| 643 | steps: |
| 644 | - run: echo slow |
| 645 | fast: |
| 646 | runs-on: [self-hosted, linux] |
| 647 | steps: |
| 648 | - run: echo "private=\${{ github.event.repository.private }} visibility=\${{ github.event.repository.visibility }}" |
| 649 | after-fast: |
| 650 | needs: fast |
| 651 | runs-on: [self-hosted, linux] |
| 652 | steps: |
| 653 | - run: echo after-fast |
| 654 | gate: |
| 655 | if: \${{ github.event_name == 'nope' }} |
| 656 | runs-on: [self-hosted, linux] |
| 657 | steps: |
| 658 | - run: echo never |
| 659 | middle: |
| 660 | needs: gate |
| 661 | if: \${{ always() }} |
| 662 | runs-on: [self-hosted, linux] |
| 663 | steps: |
| 664 | - run: echo "gate=\${{ needs.gate.result }}" |
| 665 | leaf: |
| 666 | needs: middle |
| 667 | runs-on: [self-hosted, linux] |
| 668 | steps: |
| 669 | - run: echo leaf |
| 670 | rescued: |
| 671 | needs: middle |
| 672 | if: \${{ !cancelled() && !failure() }} |
| 673 | runs-on: [self-hosted, linux] |
| 674 | steps: |
| 675 | - run: echo "middle=\${{ needs.middle.result }}" |
| 676 | lanes: |
| 677 | name: lane \${{ matrix.lane }} |
| 678 | strategy: |
| 679 | matrix: |
| 680 | include: |
| 681 | - lane: red |
| 682 | runner: [self-hosted, e2e-red] |
| 683 | - lane: blue |
| 684 | runner: [self-hosted, e2e-blue] |
| 685 | runs-on: \${{ matrix.runner }} |
| 686 | continue-on-error: \${{ matrix.lane == 'red' }} |
| 687 | environment: e2e-\${{ matrix.lane }}-\${{ strategy.job-total }} |
| 688 | steps: |
| 689 | - run: echo "lane=\${{ matrix.lane }}" && test "\${{ matrix.lane }}" = blue |
| 690 | after-lanes: |
| 691 | needs: lanes |
| 692 | runs-on: [self-hosted, linux] |
| 693 | steps: |
| 694 | - run: echo "lanes=\${{ needs.lanes.result }}" |
| 695 | deploy: |
| 696 | strategy: |
| 697 | matrix: |
| 698 | target: [prod, staging] |
| 699 | concurrency: e2e-\${{ matrix.target }} |
| 700 | runs-on: [self-hosted, e2e-deploy] |
| 701 | steps: |
| 702 | - run: echo "deploy \${{ matrix.target }}" |
| 703 | hotfix: |
| 704 | strategy: |
| 705 | matrix: |
| 706 | target: [prod] |
| 707 | concurrency: e2e-\${{ matrix.target }} |
| 708 | runs-on: [self-hosted, e2e-deploy] |
| 709 | steps: |
| 710 | - run: echo "hotfix \${{ matrix.target }}" |
| 711 | `, |
| 712 | ); |
| 713 | git(src, ["init", "-q", "-b", "main"]); |
| 714 | git(src, ["add", "."]); |
| 715 | git(src, ["commit", "-q", "-m", "graph"]); |
| 716 | git(src, ["remote", "add", "origin", `${base}/${owner}/graph.git`]); |
| 717 | git(src, ["push", "-q", "origin", "main"]); |
| 718 | |
| 719 | const run = await until("graph run", async () => (await ok("GET", `/repos/${owner}/graph/actions/runs`)).workflow_runs[0]); |
| 720 | const logOf = async (id) => (await fetch(`${base}/api/v3/repos/${owner}/graph/actions/jobs/${id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 721 | const jobs = async () => (await ok("GET", `/repos/${owner}/graph/actions/runs/${run.id}/jobs`)).jobs; |
| 722 | |
| 723 | // Jobs start when their own needs finish: after-fast and rescued run while slow (no runner offers e2e-slow) still waits. |
| 724 | const linux = {}; |
| 725 | for (let i = 0; i < 4; i++) { |
| 726 | const j = await runOneJob(owner, "graph"); |
| 727 | linux[j.claim.name] = j.claim.job_id; |
| 728 | } |
| 729 | assert.deepEqual(Object.keys(linux).sort(), ["after-fast", "fast", "middle", "rescued"]); |
| 730 | assert.equal((await jobs()).find((j) => j.name === "slow").status, "waiting"); |
| 731 | assert.match(await logOf(linux.fast), /private=false visibility=public/); |
| 732 | assert.match(await logOf(linux.middle), /gate=skipped/); |
| 733 | assert.match(await logOf(linux.rescued), /middle=success/); |
| 734 | |
| 735 | // runs-on per matrix leg; the red leg fails but continues on error, so fail-fast spares the blue one. |
| 736 | const red = await runOneJob(owner, "graph", { labels: ["e2e-red"], allowFailure: true }); |
| 737 | assert.equal(red.claim.name, "lane red"); |
| 738 | const blue = await runOneJob(owner, "graph", { labels: ["e2e-blue"] }); |
| 739 | assert.equal(blue.claim.name, "lane blue"); |
| 740 | const afterLanes = await runOneJob(owner, "graph"); |
| 741 | assert.equal(afterLanes.claim.name, "after-lanes"); |
| 742 | assert.match(await logOf(afterLanes.claim.job_id), /lanes=success/); |
| 743 | const envs = (await ok("GET", `/repos/${owner}/graph/environments`)).environments.map((e) => e.name); |
| 744 | assert.ok(envs.includes("e2e-red-2") && envs.includes("e2e-blue-2"), `per-leg environments: ${envs}`); |
| 745 | |
| 746 | // Job-level concurrency, evaluated per leg: one of the two prod jobs waits as pending until the other finishes. |
| 747 | const pending = await until("a pending prod job", async () => (await jobs()).find((j) => j.status === "pending")); |
| 748 | assert.ok(["deploy (prod)", "hotfix (prod)"].includes(pending.name), pending.name); |
| 749 | const first = await runOneJob(owner, "graph", { labels: ["e2e-deploy"] }); |
| 750 | const second = await runOneJob(owner, "graph", { labels: ["e2e-deploy"] }); |
| 751 | assert.deepEqual([first.claim.name, second.claim.name].sort(), ["deploy (prod)", "deploy (staging)", "hotfix (prod)"].filter((n) => n !== pending.name).sort()); |
| 752 | const third = await runOneJob(owner, "graph", { labels: ["e2e-deploy"] }); |
| 753 | assert.equal(third.claim.name, pending.name); |
| 754 | |
| 755 | const slow = await runOneJob(owner, "graph", { labels: ["e2e-slow"] }); |
| 756 | assert.equal(slow.claim.name, "slow"); |
| 757 | const done = await until("graph run to complete", async () => { |
| 758 | const r = await ok("GET", `/repos/${owner}/graph/actions/runs/${run.id}`); |
| 759 | return r.status === "completed" ? r : null; |
| 760 | }); |
| 761 | assert.equal(done.conclusion, "success", "a failed leg with continue-on-error does not fail the run"); |
| 762 | const byName = Object.fromEntries((await jobs()).map((j) => [j.name, j])); |
| 763 | assert.equal(byName.gate.conclusion, "skipped"); |
| 764 | assert.equal(byName.leaf.conclusion, "skipped", "a skipped ancestor skips the jobs below it"); |
| 765 | assert.equal(byName["lane red"].conclusion, "failure"); |
| 766 | assert.equal(byName["lane blue"].conclusion, "success"); |
| 767 | }); |
| 768 | |
| 769 | test("re-runs: failed jobs only keep what passed, and one job re-runs with what depends on it", async () => { |
| 770 | const owner = (await ok("GET", "/user")).login; |
| 771 | await ok("POST", "/user/repos", { name: "rerun", private: true }); |
| 772 | const src = join(work, "rerun"); |
| 773 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 774 | writeFileSync( |
| 775 | join(src, ".github/workflows/rerun.yml"), |
| 776 | `name: Rerun |
| 777 | on: push |
| 778 | jobs: |
| 779 | build: |
| 780 | runs-on: [self-hosted, linux] |
| 781 | steps: |
| 782 | - run: echo "built in attempt \${{ github.run_attempt }}" |
| 783 | test: |
| 784 | needs: build |
| 785 | strategy: |
| 786 | matrix: |
| 787 | n: [1, 2] |
| 788 | runs-on: [self-hosted, linux] |
| 789 | steps: |
| 790 | - run: echo "leg \${{ matrix.n }}" && { test "\${{ matrix.n }}" = 1 || test "\${{ github.run_attempt }}" != 1; } |
| 791 | report: |
| 792 | needs: test |
| 793 | runs-on: [self-hosted, linux] |
| 794 | steps: |
| 795 | - run: echo "report in attempt \${{ github.run_attempt }}" |
| 796 | `, |
| 797 | ); |
| 798 | git(src, ["init", "-q", "-b", "main"]); |
| 799 | git(src, ["add", "."]); |
| 800 | git(src, ["commit", "-q", "-m", "rerun"]); |
| 801 | git(src, ["remote", "add", "origin", `${base}/${owner}/rerun.git`]); |
| 802 | git(src, ["push", "-q", "origin", "main"]); |
| 803 | const R = `/repos/${owner}/rerun`; |
| 804 | const finished = (id) => |
| 805 | until(`run ${id} to complete`, async () => { |
| 806 | const r = await ok("GET", `${R}/actions/runs/${id}`); |
| 807 | return r.status === "completed" ? r : null; |
| 808 | }); |
| 809 | const jobsOf = async (id) => Object.fromEntries((await ok("GET", `${R}/actions/runs/${id}/jobs`)).jobs.map((j) => [j.name, j])); |
| 810 | const logOf = async (id) => (await fetch(`${base}/api/v3${R}/actions/jobs/${id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 811 | |
| 812 | // Attempt 1: leg 2 fails, so report is skipped. |
| 813 | const first = await until("the first run", async () => (await ok("GET", `${R}/actions/runs`)).workflow_runs[0]); |
| 814 | const claimed = []; |
| 815 | for (let i = 0; i < 3; i++) claimed.push((await runOneJob(owner, "rerun", { allowFailure: true })).claim.name); |
| 816 | assert.deepEqual(claimed, ["build", "test (1)", "test (2)"]); |
| 817 | assert.equal((await finished(first.id)).conclusion, "failure"); |
| 818 | assert.equal((await jobsOf(first.id)).report.conclusion, "skipped"); |
| 819 | |
| 820 | // Re-run failed jobs: build and leg 1 are kept as they were; leg 2 and report run again. |
| 821 | const second = (await api("POST", `${R}/actions/runs/${first.id}/rerun-failed-jobs`, { enable_debug_logging: false })).json.run_id; |
| 822 | assert.ok(second); |
| 823 | const again = []; |
| 824 | for (let i = 0; i < 2; i++) again.push((await runOneJob(owner, "rerun")).claim.name); |
| 825 | assert.deepEqual(again, ["test (2)", "report"]); |
| 826 | const done = await finished(second); |
| 827 | assert.deepEqual([done.conclusion, done.run_attempt, done.run_number], ["success", 2, first.run_number]); |
| 828 | const kept = await jobsOf(second); |
| 829 | const before = await jobsOf(first.id); |
| 830 | assert.equal(kept.build.carriedFrom, before.build.id); |
| 831 | assert.equal(kept["test (1)"].carriedFrom, before["test (1)"].id); |
| 832 | assert.equal(kept["test (2)"].carriedFrom, null); |
| 833 | assert.match(await logOf(kept.build.id), /built in attempt 1/); |
| 834 | assert.match(await logOf(kept.report.id), /report in attempt 2/); |
| 835 | assert.equal((await runnerClaim(R, ["linux"])).status, 204, "nothing else waits for a runner"); |
| 836 | |
| 837 | // Nothing failed any more; re-running one job runs it and what depends on it (nothing here), keeping the rest. |
| 838 | const none = await api("POST", `${R}/actions/runs/${second}/rerun-failed-jobs`); |
| 839 | assert.deepEqual([none.status, none.json.message], [409, "this run has no failed jobs to re-run"]); |
| 840 | const third = (await ok("POST", `${R}/actions/jobs/${kept.report.id}/rerun`)).run_id; |
| 841 | assert.equal((await runOneJob(owner, "rerun")).claim.name, "report"); |
| 842 | assert.equal((await finished(third)).conclusion, "success"); |
| 843 | const last = await jobsOf(third); |
| 844 | assert.equal(last.build.carriedFrom, before.build.id, "a job kept twice points at the job that ran"); |
| 845 | assert.equal(last["test (2)"].carriedFrom, kept["test (2)"].id); |
| 846 | assert.equal(last.report.carriedFrom, null); |
| 847 | }); |
| 848 | |
| 849 | test("workflow_run: a workflow follows another one on the default branch", async () => { |
| 850 | const owner = (await ok("GET", "/user")).login; |
| 851 | await ok("POST", "/user/repos", { name: "chain" }); |
| 852 | const src = join(work, "chain"); |
| 853 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 854 | writeFileSync(join(src, ".github/workflows/ci.yml"), `name: CI\non: push\njobs:\n build:\n runs-on: [self-hosted, linux]\n steps:\n - run: echo built\n`); |
| 855 | writeFileSync( |
| 856 | join(src, ".github/workflows/after.yml"), |
| 857 | `name: After CI |
| 858 | on: |
| 859 | workflow_run: |
| 860 | workflows: [CI] |
| 861 | types: [completed] |
| 862 | branches: [main] |
| 863 | jobs: |
| 864 | report: |
| 865 | if: \${{ github.event.workflow_run.conclusion == 'success' }} |
| 866 | runs-on: [self-hosted, linux] |
| 867 | steps: |
| 868 | - run: echo "followed \${{ github.event.workflow_run.name }} on \${{ github.event.workflow_run.head_branch }} (\${{ github.event.action }}) ref=\${{ github.ref }}" |
| 869 | `, |
| 870 | ); |
| 871 | git(src, ["init", "-q", "-b", "main"]); |
| 872 | git(src, ["add", "."]); |
| 873 | git(src, ["commit", "-q", "-m", "chain"]); |
| 874 | git(src, ["remote", "add", "origin", `${base}/${owner}/chain.git`]); |
| 875 | git(src, ["push", "-q", "origin", "main"]); |
| 876 | |
| 877 | const runs = async () => (await ok("GET", `/repos/${owner}/chain/actions/runs`)).workflow_runs; |
| 878 | await until("CI run", async () => (await runs()).find((r) => r.name === "CI")); |
| 879 | assert.equal((await runs()).length, 1, "only CI runs before it completes"); |
| 880 | const build = await runOneJob(owner, "chain"); |
| 881 | assert.equal(build.claim.name, "build"); |
| 882 | const follower = await until("follower run", async () => (await runs()).find((r) => r.event === "workflow_run")); |
| 883 | assert.equal(follower.name, "After CI"); |
| 884 | assert.equal(follower.head_branch, "main"); |
| 885 | const report = await runOneJob(owner, "chain"); |
| 886 | assert.equal(report.claim.name, "report"); |
| 887 | const log = await (await fetch(`${base}/api/v3/repos/${owner}/chain/actions/jobs/${report.claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 888 | assert.match(log, /followed CI on main \(completed\) ref=refs\/heads\/main/); |
| 889 | const done = await until("follower to complete", async () => { |
| 890 | const r = await ok("GET", `/repos/${owner}/chain/actions/runs/${follower.id}`); |
| 891 | return r.status === "completed" ? r : null; |
| 892 | }); |
| 893 | assert.equal(done.conclusion, "success"); |
| 894 | assert.equal((await runs()).length, 2, "the follower does not trigger itself"); |
| 895 | |
| 896 | // A run whose jobs were all skipped is skipped, as on GitHub, so a follower that waits for success doesn't run. |
| 897 | writeFileSync(join(src, ".github/workflows/ci.yml"), `name: CI\non: push\njobs:\n build:\n if: \${{ false }}\n runs-on: [self-hosted, linux]\n steps:\n - run: echo built\n`); |
| 898 | git(src, ["commit", "-q", "-am", "skip the build"]); |
| 899 | git(src, ["push", "-q", "origin", "main"]); |
| 900 | const settled = await until("skipped CI and its follower", async () => { |
| 901 | const all = await runs(); |
| 902 | return all.length === 4 && all.every((r) => r.status === "completed") ? all : null; |
| 903 | }); |
| 904 | const sha = git(src, ["rev-parse", "HEAD"]).trim(); |
| 905 | const skippedCi = settled.find((r) => r.name === "CI" && r.head_sha === sha); |
| 906 | assert.equal(skippedCi?.conclusion, "skipped"); |
| 907 | assert.equal(settled.find((r) => r.event === "workflow_run" && r.id !== follower.id)?.conclusion, "skipped"); |
| 908 | }); |
| 909 | |
| 910 | test("triggers from workflows: a job's repository_dispatch starts runs, a follower of agent code stays an agent's", { timeout: 600_000 }, async () => { |
| 911 | const owner = (await ok("GET", "/user")).login; |
| 912 | await ok("POST", "/user/repos", { name: "relay" }); |
| 913 | const bot = (await ok("POST", "/diffinery/agents", { login: "relay-bot" }, null)).token.token; |
| 914 | const src = join(work, "relay"); |
| 915 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 916 | writeFileSync( |
| 917 | join(src, ".github/workflows/send.yml"), |
| 918 | `name: Send |
| 919 | on: workflow_dispatch |
| 920 | jobs: |
| 921 | send: |
| 922 | runs-on: [self-hosted, linux] |
| 923 | permissions: { contents: write } |
| 924 | steps: |
| 925 | - env: { GH_TOKEN: "\${{ github.token }}" } |
| 926 | run: | |
| 927 | code=$(curl -s -o out.txt -w '%{http_code}' -X POST -H "authorization: Bearer $GH_TOKEN" -H 'content-type: application/json' -d '{"event_type":"ping","client_payload":{"from":"a job"}}' "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/dispatches") |
| 928 | echo "dispatch: $code $(cat out.txt)" |
| 929 | `, |
| 930 | ); |
| 931 | writeFileSync( |
| 932 | join(src, ".github/workflows/pinged.yml"), |
| 933 | `name: Pinged |
| 934 | on: |
| 935 | repository_dispatch: |
| 936 | types: [ping] |
| 937 | jobs: |
| 938 | pinged: |
| 939 | runs-on: [self-hosted, linux] |
| 940 | steps: |
| 941 | - run: echo "pinged from \${{ github.event.client_payload.from }}" |
| 942 | `, |
| 943 | ); |
| 944 | writeFileSync(join(src, ".github/workflows/ci.yml"), `name: CI\non: pull_request\njobs:\n build:\n runs-on: [self-hosted, linux]\n steps:\n - run: echo built\n`); |
| 945 | writeFileSync( |
| 946 | join(src, ".github/workflows/after.yml"), |
| 947 | `name: After CI |
| 948 | on: |
| 949 | workflow_run: |
| 950 | workflows: [CI] |
| 951 | types: [completed] |
| 952 | jobs: |
| 953 | follow: |
| 954 | runs-on: [self-hosted, linux] |
| 955 | permissions: { contents: write } |
| 956 | steps: |
| 957 | - uses: actions/checkout@v4 |
| 958 | with: { ref: main } |
| 959 | - env: |
| 960 | GIT_AUTHOR_NAME: bot |
| 961 | GIT_AUTHOR_EMAIL: bot@example.com |
| 962 | GIT_COMMITTER_NAME: bot |
| 963 | GIT_COMMITTER_EMAIL: bot@example.com |
| 964 | run: | |
| 965 | echo "after \${{ github.event.workflow_run.head_branch }}" >> followed.txt |
| 966 | git add followed.txt && git commit -qm followed |
| 967 | if git push -q origin HEAD:main 2>err.txt; then echo "main push: ok"; else echo "main push: refused $(tr '\\n' ' ' < err.txt)"; fi |
| 968 | `, |
| 969 | ); |
| 970 | git(src, ["init", "-q", "-b", "main"]); |
| 971 | git(src, ["add", "."]); |
| 972 | git(src, ["commit", "-q", "-m", "relay"]); |
| 973 | git(src, ["remote", "add", "origin", `${base}/${owner}/relay.git`]); |
| 974 | git(src, ["push", "-q", "origin", "main"]); |
| 975 | |
| 976 | const runs = async () => (await ok("GET", `/repos/${owner}/relay/actions/runs`)).workflow_runs; |
| 977 | const logOf = async (claim) => (await (await fetch(`${base}/api/v3/repos/${owner}/relay/actions/jobs/${claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text()).replace(/^\S+Z /gm, ""); |
| 978 | |
| 979 | // Like GitHub, a job's GITHUB_TOKEN may send repository_dispatch, and that starts runs. |
| 980 | await ok("POST", `/repos/${owner}/relay/actions/workflows/send.yml/dispatches`, { ref: "main" }); |
| 981 | const send = await runOneJob(owner, "relay"); |
| 982 | assert.equal(send.claim.name, "send"); |
| 983 | assert.match(await logOf(send.claim), /dispatch: 204/); |
| 984 | const pingedRun = await until("repository_dispatch run", async () => (await runs()).find((r) => r.event === "repository_dispatch")); |
| 985 | assert.equal(pingedRun.name, "Pinged"); |
| 986 | const pinged = await runOneJob(owner, "relay"); |
| 987 | assert.equal(pinged.claim.name, "pinged"); |
| 988 | assert.match(await logOf(pinged.claim), /pinged from a job/); |
| 989 | |
| 990 | /** Opens a pull request from `branch` as a person, runs its CI, then the follower; returns the follower's log. */ |
| 991 | const follow = async (branch) => { |
| 992 | const seen = new Set((await runs()).map((r) => r.id)); |
| 993 | await ok("POST", `/repos/${owner}/relay/pulls`, { title: branch, head: branch, base: "main" }); |
| 994 | await until("CI run", async () => (await runs()).find((r) => !seen.has(r.id) && r.name === "CI")); |
| 995 | const build = await runOneJob(owner, "relay"); |
| 996 | assert.equal(build.claim.name, "build"); |
| 997 | await until("follower run", async () => (await runs()).find((r) => !seen.has(r.id) && r.event === "workflow_run")); |
| 998 | const f = await runOneJob(owner, "relay"); |
| 999 | assert.equal(f.claim.name, "follow"); |
| 1000 | return logOf(f.claim); |
| 1001 | }; |
| 1002 | |
| 1003 | // A person's pull request: the follower writes to main with the token it was granted. |
| 1004 | git(src, ["checkout", "-q", "-b", "person-work"]); |
| 1005 | writeFileSync(join(src, "person.txt"), "from a person\n"); |
| 1006 | git(src, ["add", "person.txt"]); |
| 1007 | git(src, ["commit", "-q", "-m", "person change"]); |
| 1008 | git(src, ["push", "-q", "origin", "person-work"]); |
| 1009 | assert.match(await follow("person-work"), /main push: ok/); |
| 1010 | |
| 1011 | // A person opens a pull request from a branch an agent pushed. CI runs the agent's code, so |
| 1012 | // the follower, which acts on what CI produced, is an agent's too and may not push to main. |
| 1013 | const agentClone = join(work, "relay-agent"); |
| 1014 | git(work, ["clone", "-q", `${base}/${owner}/relay.git`, agentClone], bot); |
| 1015 | git(agentClone, ["checkout", "-q", "-b", "agent-work"], bot); |
| 1016 | writeFileSync(join(agentClone, "agent.txt"), "from the agent\n"); |
| 1017 | git(agentClone, ["add", "agent.txt"], bot); |
| 1018 | git(agentClone, ["commit", "-q", "-m", "agent change"], bot); |
| 1019 | git(agentClone, ["push", "-q", "origin", "agent-work"], bot); |
| 1020 | assert.match(await follow("agent-work"), /main push: refused .*agents may not push to protected or default branches/); |
| 1021 | }); |
| 1022 | |
| 1023 | test("pull requests: test merge commits, pull_request_target and the merge queue", async () => { |
| 1024 | const owner = (await ok("GET", "/user")).login; |
| 1025 | await ok("POST", "/user/repos", { name: "queue" }); |
| 1026 | await api("PUT", `/repos/${owner}/queue/actions/secrets/TOPSECRET`, { value: "s3cret" }); |
| 1027 | const src = join(work, "queue"); |
| 1028 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1029 | writeFileSync(join(src, "app.txt"), "a\nb\nc\n"); |
| 1030 | writeFileSync( |
| 1031 | join(src, ".github/workflows/ci.yml"), |
| 1032 | `name: CI |
| 1033 | on: |
| 1034 | pull_request: |
| 1035 | merge_group: |
| 1036 | jobs: |
| 1037 | test: |
| 1038 | runs-on: [self-hosted, linux] |
| 1039 | steps: |
| 1040 | - uses: actions/checkout@v4 |
| 1041 | - run: | |
| 1042 | echo "event=\${{ github.event_name }} ref=\${{ github.ref }} sha=\${{ github.sha }} head=$(git rev-parse HEAD)" |
| 1043 | echo "parents=$(git rev-list --parents -n1 HEAD)" |
| 1044 | echo "app=$(tr '\\n' ' ' < app.txt)" |
| 1045 | if [ "\${{ github.event_name }}" = merge_group ] && grep -qs fail notes.txt; then echo "merge group failed"; exit 1; fi |
| 1046 | `, |
| 1047 | ); |
| 1048 | writeFileSync( |
| 1049 | join(src, ".github/workflows/target.yml"), |
| 1050 | `name: Target |
| 1051 | on: pull_request_target |
| 1052 | jobs: |
| 1053 | t: |
| 1054 | runs-on: [self-hosted, linux] |
| 1055 | steps: |
| 1056 | - run: echo "target from base secret=\${{ secrets.TOPSECRET == 's3cret' }} ref=\${{ github.ref }} head=\${{ github.event.pull_request.head.sha }}" |
| 1057 | `, |
| 1058 | ); |
| 1059 | git(src, ["init", "-q", "-b", "main"]); |
| 1060 | git(src, ["add", "."]); |
| 1061 | git(src, ["commit", "-q", "-m", "queue"]); |
| 1062 | git(src, ["remote", "add", "origin", `${base}/${owner}/queue.git`]); |
| 1063 | git(src, ["push", "-q", "origin", "main"]); |
| 1064 | const root = git(src, ["rev-parse", "HEAD"]).trim(); |
| 1065 | |
| 1066 | // f1 changes the first line and tries to rewrite the pull_request_target workflow. |
| 1067 | git(src, ["checkout", "-q", "-b", "f1"]); |
| 1068 | writeFileSync(join(src, "app.txt"), "A\nb\nc\n"); |
| 1069 | writeFileSync(join(src, ".github/workflows/target.yml"), `name: Target\non: pull_request_target\njobs:\n t:\n runs-on: [self-hosted, linux]\n steps:\n - run: echo "PWNED \${{ secrets.TOPSECRET }}"\n`); |
| 1070 | git(src, ["commit", "-q", "-am", "f1"]); |
| 1071 | git(src, ["push", "-q", "origin", "f1"]); |
| 1072 | const f1 = git(src, ["rev-parse", "HEAD"]).trim(); |
| 1073 | // main moves on, so the test merge commit differs from both sides. |
| 1074 | git(src, ["checkout", "-q", "main"]); |
| 1075 | writeFileSync(join(src, "app.txt"), "a\nb\nC\n"); |
| 1076 | git(src, ["commit", "-q", "-am", "main moves"]); |
| 1077 | git(src, ["push", "-q", "origin", "main"]); |
| 1078 | const main1 = git(src, ["rev-parse", "HEAD"]).trim(); |
| 1079 | |
| 1080 | const pr1 = await ok("POST", `/repos/${owner}/queue/pulls`, { title: "Capitalise a", head: "f1", base: "main" }); |
| 1081 | const runs = async (q = "") => (await ok("GET", `/repos/${owner}/queue/actions/runs${q}`)).workflow_runs; |
| 1082 | const prRun = await until("pull_request run", async () => (await runs("?event=pull_request"))[0]); |
| 1083 | assert.equal(prRun.ref, "refs/pull/1/merge"); |
| 1084 | assert.equal(prRun.head_sha, f1, "pull_request runs report on the PR head"); |
| 1085 | assert.notEqual(prRun.sha, f1, "pull_request runs check out the test merge commit"); |
| 1086 | assert.equal(prRun.head_branch, "f1"); |
| 1087 | const targetRun = (await runs("?event=pull_request_target"))[0]; |
| 1088 | assert.equal(targetRun.ref, "refs/heads/main"); |
| 1089 | assert.equal(targetRun.sha, main1, "pull_request_target runs on the base commit"); |
| 1090 | assert.equal(targetRun.head_sha, f1); |
| 1091 | // The merge ref is fetchable and its parents are [base, head]; refs/pull is read-only. |
| 1092 | git(src, ["fetch", "-q", "origin", "+refs/pull/1/merge:refs/remotes/pr/1"]); |
| 1093 | assert.equal(git(src, ["rev-parse", "refs/remotes/pr/1"]).trim(), prRun.sha); |
| 1094 | assert.equal(git(src, ["rev-list", "--parents", "-n1", "refs/remotes/pr/1"]).trim(), `${prRun.sha} ${main1} ${f1}`); |
| 1095 | assert.match(gitFails(src, ["push", "origin", `${root}:refs/pull/1/head`]), /refs\/pull|managed/); |
| 1096 | |
| 1097 | const logOf = async (id) => (await fetch(`${base}/api/v3/repos/${owner}/queue/actions/jobs/${id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 1098 | const ran = {}; |
| 1099 | for (let i = 0; i < 2; i++) { |
| 1100 | const j = await runOneJob(owner, "queue"); |
| 1101 | ran[j.claim.name] = await logOf(j.claim.job_id); |
| 1102 | } |
| 1103 | assert.match(ran.test, new RegExp(`event=pull_request ref=refs/pull/1/merge sha=${prRun.sha} head=${prRun.sha}`)); |
| 1104 | assert.match(ran.test, /app=A b C/); |
| 1105 | assert.match(ran.t, /target from base secret=true ref=refs\/heads\/main/); |
| 1106 | assert.match(ran.t, new RegExp(`head=${f1}`)); |
| 1107 | assert.doesNotMatch(ran.t, /PWNED/); |
| 1108 | await until("PR #1 checks", async () => { |
| 1109 | const c = (await ok("GET", `/repos/${owner}/queue/commits/${f1}/check-runs`)).check_runs; |
| 1110 | return c.length === 2 && c.every((x) => x.conclusion === "success") ? c : null; |
| 1111 | }); |
| 1112 | |
| 1113 | // ------------------------------------------------------------ merge queue |
| 1114 | await ok("PUT", `/repos/${owner}/queue/branches/main/protection`, { requirePullRequest: true, requiredChecks: ["CI / test"], mergeQueue: { method: "merge", buildConcurrency: 5, checkTimeoutMinutes: 30 } }); |
| 1115 | const direct = await api("PUT", `/repos/${owner}/queue/pulls/1/merge`, {}); |
| 1116 | assert.equal(direct.status, 405); |
| 1117 | assert.match(JSON.stringify(direct.json), /merge queue/); |
| 1118 | assert.equal((await ok("GET", `/repos/${owner}/queue/pulls/1/merge`)).mergeQueue.method, "merge"); |
| 1119 | |
| 1120 | // Two more pull requests: one that merges cleanly and one that fails only in its merge group. |
| 1121 | const branch = async (name, file, content) => { |
| 1122 | git(src, ["checkout", "-q", "-b", name, main1]); |
| 1123 | writeFileSync(join(src, file), content); |
| 1124 | git(src, ["add", "."]); |
| 1125 | git(src, ["commit", "-q", "-m", name]); |
| 1126 | git(src, ["push", "-q", "origin", name]); |
| 1127 | const sha = git(src, ["rev-parse", "HEAD"]).trim(); |
| 1128 | const pr = await ok("POST", `/repos/${owner}/queue/pulls`, { title: name, head: name, base: "main" }); |
| 1129 | for (let i = 0; i < 2; i++) await runOneJob(owner, "queue"); |
| 1130 | await until(`${name} checks`, async () => (await ok("GET", `/repos/${owner}/queue/commits/${sha}/check-runs`)).check_runs.filter((x) => x.conclusion === "success").length === 2); |
| 1131 | return { pr, sha }; |
| 1132 | }; |
| 1133 | const f2 = await branch("f2", "two.txt", "two\n"); |
| 1134 | const f3 = await branch("f3", "notes.txt", "fail\n"); |
| 1135 | |
| 1136 | const q1 = await ok("PUT", `/repos/${owner}/queue/pulls/1/merge-queue`, { delete_branch: true }); |
| 1137 | assert.equal(q1.state, "queued"); |
| 1138 | assert.equal(q1.entry.position, 1); |
| 1139 | assert.equal(q1.entry.state, "checking"); |
| 1140 | const q2 = await ok("PUT", `/repos/${owner}/queue/pulls/${f2.pr.number}/merge-queue`, {}); |
| 1141 | const q3 = await ok("PUT", `/repos/${owner}/queue/pulls/${f3.pr.number}/merge-queue`, {}); |
| 1142 | assert.equal(q2.entry.merge_group.base_sha, q1.entry.merge_group.head_sha, "each group builds on the one ahead"); |
| 1143 | assert.equal(q3.entry.merge_group.base_sha, q2.entry.merge_group.head_sha); |
| 1144 | assert.equal(q1.entry.merge_group.head_ref, `refs/heads/gh-readonly-queue/main/pr-1-${f1}`); |
| 1145 | const queue = await ok("GET", `/repos/${owner}/queue/merge-queue/main`); |
| 1146 | assert.deepEqual(queue.entries.map((e) => e.number), [1, f2.pr.number, f3.pr.number]); |
| 1147 | assert.match(gitFails(src, ["push", "origin", `${root}:refs/heads/gh-readonly-queue/main/x`]), /read-only/); |
| 1148 | |
| 1149 | // Merge groups run merge_group workflows, oldest first. |
| 1150 | const groupLogs = []; |
| 1151 | for (let i = 0; i < 3; i++) { |
| 1152 | const j = await runOneJob(owner, "queue", { allowFailure: i === 2 }); |
| 1153 | groupLogs.push(await logOf(j.claim.job_id)); |
| 1154 | } |
| 1155 | assert.match(groupLogs[0], new RegExp(`event=merge_group ref=refs/heads/gh-readonly-queue/main/pr-1-${f1} sha=${q1.entry.merge_group.head_sha}`)); |
| 1156 | assert.match(groupLogs[2], /merge group failed/); |
| 1157 | const mainNow = await until("the queue to merge #1 and #2", async () => { |
| 1158 | const b = await ok("GET", `/repos/${owner}/queue/branches/main`); |
| 1159 | return b.commit.sha === q2.entry.merge_group.head_sha ? b : null; |
| 1160 | }); |
| 1161 | assert.ok(mainNow); |
| 1162 | assert.equal((await ok("GET", `/repos/${owner}/queue/pulls/1`)).merged, true); |
| 1163 | assert.equal((await ok("GET", `/repos/${owner}/queue/pulls/${f2.pr.number}`)).merged, true); |
| 1164 | assert.equal((await ok("GET", `/repos/${owner}/queue/pulls/1`)).merge_commit_sha, q1.entry.merge_group.head_sha, "the tested commit is what merged"); |
| 1165 | const f3After = await until("#3 to leave the queue", async () => { |
| 1166 | const q = await ok("GET", `/repos/${owner}/queue/merge-queue/main`); |
| 1167 | return q.entries.length === 0 ? q : null; |
| 1168 | }); |
| 1169 | assert.ok(f3After); |
| 1170 | assert.equal((await ok("GET", `/repos/${owner}/queue/pulls/${f3.pr.number}`)).merged, false); |
| 1171 | const comments = await ok("GET", `/repos/${owner}/queue/issues/${f3.pr.number}/comments`); |
| 1172 | assert.match(JSON.stringify(comments), /removed from the merge queue: the required check 'CI \/ test' failed on its merge group/); |
| 1173 | git(src, ["fetch", "-q", "origin"]); |
| 1174 | assert.equal(git(src, ["show", "origin/main:app.txt"]), "A\nb\nC\n"); |
| 1175 | assert.equal(git(src, ["show", "origin/main:two.txt"]), "two\n"); |
| 1176 | assert.ok(!(await ok("GET", `/repos/${owner}/queue/branches`)).some((b) => b.name.startsWith("gh-readonly-queue/") || b.name === "f1"), "group branches and f1 are gone"); |
| 1177 | |
| 1178 | // A pull request that conflicts with its base runs no pull_request workflows (like GitHub), only pull_request_target. |
| 1179 | git(src, ["checkout", "-q", "-b", "f4", root]); |
| 1180 | writeFileSync(join(src, "app.txt"), "x\nb\nc\n"); |
| 1181 | git(src, ["commit", "-q", "-am", "f4"]); |
| 1182 | git(src, ["push", "-q", "origin", "f4"]); |
| 1183 | const pr4 = await ok("POST", `/repos/${owner}/queue/pulls`, { title: "conflicts", head: "f4", base: "main" }); |
| 1184 | const t4 = await until("pull_request_target run for #4", async () => (await runs("?event=pull_request_target")).find((r) => r.pull_request_number === pr4.number)); |
| 1185 | assert.ok(t4); |
| 1186 | assert.equal((await runs("?event=pull_request")).filter((r) => r.pull_request_number === pr4.number).length, 0); |
| 1187 | await runOneJob(owner, "queue"); |
| 1188 | |
| 1189 | // The web UI shows the queue. |
| 1190 | const page = async (path) => { |
| 1191 | const res = await fetch(`${base}${path}`); |
| 1192 | assert.equal(res.status, 200, path); |
| 1193 | return res.text(); |
| 1194 | }; |
| 1195 | assert.match(await page(`/${owner}/queue/queue/main`), /The queue is empty/); |
| 1196 | assert.match(await page(`/${owner}/queue/pull/${pr4.number}`), /Merge when ready/); |
| 1197 | assert.match(await page(`/${owner}/queue/pull/${f3.pr.number}`), /removed from the merge queue/); |
| 1198 | assert.match(await page(`/${owner}/queue/settings/branches`), /merge queue/); |
| 1199 | }); |
| 1200 | |
| 1201 | const POLICY_WORKFLOW = `name: Policy |
| 1202 | on: |
| 1203 | push: |
| 1204 | branches: [main, agent-work] |
| 1205 | pull_request: |
| 1206 | jobs: |
| 1207 | probe: |
| 1208 | runs-on: [self-hosted, linux] |
| 1209 | permissions: { contents: write, pull-requests: write } |
| 1210 | steps: |
| 1211 | - uses: actions/checkout@v4 |
| 1212 | with: { ref: main } |
| 1213 | - name: Push to main, push a workflow change, approve the pull request |
| 1214 | env: |
| 1215 | GH_TOKEN: \${{ github.token }} |
| 1216 | PR: \${{ github.event.pull_request.number }} |
| 1217 | GIT_AUTHOR_NAME: bot |
| 1218 | GIT_AUTHOR_EMAIL: bot@example.com |
| 1219 | GIT_COMMITTER_NAME: bot |
| 1220 | GIT_COMMITTER_EMAIL: bot@example.com |
| 1221 | run: | |
| 1222 | echo "run \${{ github.run_id }}" > note.txt |
| 1223 | git add note.txt && git commit -qm note |
| 1224 | if git push -q origin HEAD:main 2>err.txt; then echo "main push: ok"; else echo "main push: refused $(tr '\\n' ' ' < err.txt)"; fi |
| 1225 | git reset -q --hard HEAD~1 |
| 1226 | echo "# edited" >> .github/workflows/policy.yml |
| 1227 | git commit -qam "edit the workflow" |
| 1228 | if git push -q origin "HEAD:refs/heads/wf-\${{ github.run_id }}" 2>err.txt; then echo "workflow push: ok"; else echo "workflow push: refused $(tr '\\n' ' ' < err.txt)"; fi |
| 1229 | if [ -n "$PR" ]; then |
| 1230 | code=$(curl -s -o out.json -w '%{http_code}' -X POST -H "authorization: Bearer $GH_TOKEN" -H 'content-type: application/json' -d '{"event":"APPROVE","body":"from a workflow"}' "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/pulls/$PR/reviews") |
| 1231 | echo "approve: $code $(cat out.json)" |
| 1232 | fi |
| 1233 | defaults: |
| 1234 | if: github.event_name == 'push' && github.ref == 'refs/heads/main' |
| 1235 | runs-on: [self-hosted, linux] |
| 1236 | steps: |
| 1237 | - env: { GH_TOKEN: "\${{ github.token }}" } |
| 1238 | run: | |
| 1239 | code=$(curl -s -o out.json -w '%{http_code}' -X POST -H "authorization: Bearer $GH_TOKEN" -H 'content-type: application/json' -d '{"title":"from the default token"}' "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/issues") |
| 1240 | echo "default token issue write: $code $(cat out.json)" |
| 1241 | code=$(curl -s -o /dev/null -w '%{http_code}' -H "authorization: Bearer $GH_TOKEN" "$GITHUB_API_URL/repos/$GITHUB_REPOSITORY/contents/README.md") |
| 1242 | echo "default token contents read: $code" |
| 1243 | `; |
| 1244 | |
| 1245 | test("workflow tokens: read-only default, no approvals or workflow edits, agent code stays an agent", { timeout: 600_000 }, async () => { |
| 1246 | const owner = (await ok("GET", "/user")).login; |
| 1247 | await ok("POST", "/user/repos", { name: "policy" }); |
| 1248 | const settingsPath = `/repos/${owner}/policy/actions/permissions/workflow`; |
| 1249 | // New repositories start like GitHub's: read-only token, no pull request approvals. |
| 1250 | assert.deepEqual(await ok("GET", settingsPath), { default_workflow_permissions: "read", can_approve_pull_request_reviews: false, agent_runs_use_secrets: false }); |
| 1251 | const bot = (await ok("POST", "/diffinery/agents", { login: "policy-bot" }, null)).token.token; |
| 1252 | assert.equal((await api("PUT", settingsPath, { can_approve_pull_request_reviews: true }, bot)).status, 403, "agents cannot change what workflows may do"); |
| 1253 | assert.equal((await api("PUT", settingsPath, { default_workflow_permissions: "admin" })).status, 422); |
| 1254 | // The same settings on the repository's Settings → Actions page. |
| 1255 | const actionsPage = `/${owner}/policy/settings/actions`; |
| 1256 | const form = async (fields) => { |
| 1257 | const res = await fetch(`${base}${actionsPage}`); |
| 1258 | const html = await res.text(); |
| 1259 | assert.match(html, /Allow GitHub Actions to create and approve pull requests/); |
| 1260 | const csrf = /name="_csrf" value="([^"]+)"/.exec(html)?.[1]; |
| 1261 | const cookie = /dfy_csrf=([^;]+)/.exec(res.headers.get("set-cookie") ?? "")?.[1]; |
| 1262 | const r = await fetch(`${base}${actionsPage}`, { method: "POST", body: new URLSearchParams({ _csrf: csrf, ...fields }), redirect: "manual", headers: { cookie: `dfy_csrf=${cookie}`, "sec-fetch-site": "same-origin" } }); |
| 1263 | assert.equal(r.status, 303); |
| 1264 | }; |
| 1265 | await form({ default_permissions: "write", can_approve: "1" }); |
| 1266 | assert.deepEqual(await ok("GET", settingsPath), { default_workflow_permissions: "write", can_approve_pull_request_reviews: true, agent_runs_use_secrets: false }); |
| 1267 | await form({ default_permissions: "read" }); |
| 1268 | assert.deepEqual(await ok("GET", settingsPath), { default_workflow_permissions: "read", can_approve_pull_request_reviews: false, agent_runs_use_secrets: false }); |
| 1269 | |
| 1270 | const src = join(work, "policy"); |
| 1271 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1272 | writeFileSync(join(src, "README.md"), "# policy\n"); |
| 1273 | writeFileSync(join(src, ".github/workflows/policy.yml"), POLICY_WORKFLOW); |
| 1274 | git(src, ["init", "-q", "-b", "main"]); |
| 1275 | git(src, ["add", "-A"]); |
| 1276 | git(src, ["commit", "-q", "-m", "policy"]); |
| 1277 | git(src, ["remote", "add", "origin", `${base}/${owner}/policy.git`]); |
| 1278 | git(src, ["push", "-q", "origin", "main"]); |
| 1279 | |
| 1280 | /** Runs `count` jobs; returns their logs by job name. */ |
| 1281 | const runJobs = async (count) => { |
| 1282 | const logs = {}; |
| 1283 | for (let i = 0; i < count; i++) { |
| 1284 | const { claim } = await runOneJob(owner, "policy"); |
| 1285 | const raw = await (await fetch(`${base}/api/v3/repos/${owner}/policy/actions/jobs/${claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 1286 | logs[claim.name] = raw.replace(/^\S+Z /gm, ""); |
| 1287 | } |
| 1288 | return logs; |
| 1289 | }; |
| 1290 | const refused = (path) => new RegExp(`workflow push: refused .*refusing to allow a GitHub App to create or update workflow \`${path.replace(/\./g, "\\.")}\` without \`workflows\` permission`); |
| 1291 | |
| 1292 | // A person's push: the job token writes what it was granted, but never workflow files; |
| 1293 | // the default token (no permissions:) only reads. |
| 1294 | let logs = await runJobs(2); |
| 1295 | assert.match(logs.probe, /main push: ok/); |
| 1296 | assert.match(logs.probe, refused(".github/workflows/policy.yml")); |
| 1297 | assert.match(logs.defaults, /default token issue write: 403 .*issues: write/); |
| 1298 | assert.match(logs.defaults, /default token contents read: 200/); |
| 1299 | const branches = (await ok("GET", `/repos/${owner}/policy/branches`)).map((b) => b.name); |
| 1300 | assert.ok(!branches.some((b) => b.startsWith("wf-")), JSON.stringify(branches)); |
| 1301 | git(src, ["pull", "-q", "--ff-only", "origin", "main"]); |
| 1302 | assert.match(readFileSync(join(src, "note.txt"), "utf8"), /^run \d+/); |
| 1303 | |
| 1304 | // An agent's push: the run executes the agent's code, so its token is an agent's and may not push to the default branch. |
| 1305 | const agentClone = join(work, "policy-agent"); |
| 1306 | git(work, ["clone", "-q", `${base}/${owner}/policy.git`, agentClone], bot); |
| 1307 | git(agentClone, ["checkout", "-q", "-b", "agent-work"], bot); |
| 1308 | writeFileSync(join(agentClone, "agent.txt"), "from the agent\n"); |
| 1309 | git(agentClone, ["add", "agent.txt"], bot); |
| 1310 | git(agentClone, ["commit", "-q", "-m", "agent change"], bot); |
| 1311 | git(agentClone, ["push", "-q", "origin", "agent-work"], bot); |
| 1312 | logs = await runJobs(1); |
| 1313 | assert.match(logs.probe, /main push: refused .*agents may not push to protected or default branches/); |
| 1314 | assert.match(logs.probe, refused(".github/workflows/policy.yml")); |
| 1315 | |
| 1316 | // A person opens a pull request from the branch the agent pushed: still the agent's code. |
| 1317 | // Workflows may not approve pull requests unless the repository allows it. |
| 1318 | const pr1 = await ok("POST", `/repos/${owner}/policy/pulls`, { title: "Agent work", head: "agent-work", base: "main" }); |
| 1319 | logs = await runJobs(1); |
| 1320 | assert.match(logs.probe, /main push: refused .*agents may not push/); |
| 1321 | assert.match(logs.probe, /approve: 403 .*GitHub Actions is not permitted to approve pull requests/); |
| 1322 | assert.equal((await ok("GET", `/repos/${owner}/policy/pulls/${pr1.number}/reviews`)).length, 0); |
| 1323 | |
| 1324 | // Allowed: the approval is recorded, but only people's approvals count toward the required ones. |
| 1325 | await ok("PUT", settingsPath, { can_approve_pull_request_reviews: true }); |
| 1326 | assert.deepEqual(await ok("GET", settingsPath), { default_workflow_permissions: "read", can_approve_pull_request_reviews: true, agent_runs_use_secrets: false }); |
| 1327 | await ok("PUT", `/repos/${owner}/policy/branches/main/protection`, { |
| 1328 | requirePullRequest: true, |
| 1329 | requiredApprovals: 1, |
| 1330 | dismissStaleReviews: false, |
| 1331 | requiredChecks: [], |
| 1332 | allowForcePush: false, |
| 1333 | allowDeletion: false, |
| 1334 | requireLinearHistory: false, |
| 1335 | enforceAdmins: true, |
| 1336 | agentsMayPush: false, |
| 1337 | agentsMayMerge: false, |
| 1338 | }); |
| 1339 | git(src, ["checkout", "-q", "-b", "feature"]); |
| 1340 | writeFileSync(join(src, "feature.txt"), "a person's change\n"); |
| 1341 | git(src, ["add", "feature.txt"]); |
| 1342 | git(src, ["commit", "-q", "-m", "feature"]); |
| 1343 | git(src, ["push", "-q", "origin", "feature"]); |
| 1344 | const pr2 = await ok("POST", `/repos/${owner}/policy/pulls`, { title: "Feature", head: "feature", base: "main" }); |
| 1345 | logs = await runJobs(1); |
| 1346 | assert.match(logs.probe, /main push: refused .*pull request/); |
| 1347 | assert.match(logs.probe, /approve: 20[01] /); |
| 1348 | const reviews = await ok("GET", `/repos/${owner}/policy/pulls/${pr2.number}/reviews`); |
| 1349 | assert.deepEqual(reviews.map((r) => r.state), ["APPROVED"]); |
| 1350 | const m = await ok("GET", `/repos/${owner}/policy/pulls/${pr2.number}/merge`); |
| 1351 | assert.equal(m.mergeable, false, JSON.stringify(m)); |
| 1352 | assert.equal(m.approvals, 0, JSON.stringify(m)); |
| 1353 | assert.match(JSON.stringify(m.blockers), /needs 1 more approving review/); |
| 1354 | }); |
| 1355 | |
| 1356 | const SCOPES_WORKFLOW = `name: Scopes |
| 1357 | on: |
| 1358 | push: |
| 1359 | branches: [main, a, b] |
| 1360 | pull_request: |
| 1361 | jobs: |
| 1362 | c: |
| 1363 | runs-on: [self-hosted, linux] |
| 1364 | steps: |
| 1365 | - uses: actions/cache@v4 |
| 1366 | with: { path: built, key: "b-\${{ github.run_id }}", restore-keys: "b-" } |
| 1367 | - uses: actions/cache@v4 |
| 1368 | env: { DIFFINERY_BUILTINS: "false" } |
| 1369 | with: { path: real, key: "r-\${{ github.run_id }}", restore-keys: "r-" } |
| 1370 | - run: | |
| 1371 | echo "restored built=[$(cat built/from 2>/dev/null)] real=[$(cat real/from 2>/dev/null)]" |
| 1372 | mkdir -p built real && echo "$GITHUB_REF" > built/from && echo "$GITHUB_REF" > real/from |
| 1373 | - if: github.ref == 'refs/heads/main' |
| 1374 | uses: actions/upload-artifact@v4 |
| 1375 | with: { name: "builtin-\${{ github.run_id }}", path: built/from, retention-days: 1 } |
| 1376 | - if: github.ref == 'refs/heads/main' |
| 1377 | uses: actions/upload-artifact@v4 |
| 1378 | env: { DIFFINERY_BUILTINS: "false" } |
| 1379 | with: { name: "real-\${{ github.run_id }}", path: built/from, retention-days: 3 } |
| 1380 | - if: github.ref == 'refs/heads/main' |
| 1381 | uses: actions/upload-artifact@v4 |
| 1382 | with: { name: "capped-\${{ github.run_id }}", path: built/from, retention-days: 400 } |
| 1383 | `; |
| 1384 | |
| 1385 | test("caches are scoped to branches and pull requests; artifacts keep their retention-days", { timeout: 900_000 }, async () => { |
| 1386 | const owner = (await ok("GET", "/user")).login; |
| 1387 | await ok("POST", "/user/repos", { name: "scopes" }); |
| 1388 | const src = join(work, "scopes"); |
| 1389 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1390 | writeFileSync(join(src, ".github/workflows/scopes.yml"), SCOPES_WORKFLOW); |
| 1391 | writeFileSync(join(src, "README.md"), "# scopes\n"); |
| 1392 | git(src, ["init", "-q", "-b", "main"]); |
| 1393 | git(src, ["add", "-A"]); |
| 1394 | git(src, ["commit", "-q", "-m", "scopes"]); |
| 1395 | git(src, ["remote", "add", "origin", `${base}/${owner}/scopes.git`]); |
| 1396 | /** Runs the job a push or pull request started; returns what its caches restored. */ |
| 1397 | const restored = async () => { |
| 1398 | const { claim } = await runOneJob(owner, "scopes", { timeoutMs: 300_000 }); |
| 1399 | const log = await (await fetch(`${base}/api/v3/repos/${owner}/scopes/actions/jobs/${claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 1400 | const m = /^\S+ restored built=\[(.*?)\] real=\[(.*?)\]$/m.exec(log); |
| 1401 | assert.ok(m, log); |
| 1402 | return { builtin: m[1], real: m[2] }; |
| 1403 | }; |
| 1404 | const branch = (name, from, file) => { |
| 1405 | git(src, ["checkout", "-q", "-b", name, from]); |
| 1406 | if (file) { |
| 1407 | writeFileSync(join(src, file), `${name}\n`); |
| 1408 | git(src, ["add", file]); |
| 1409 | git(src, ["commit", "-q", "-m", name]); |
| 1410 | } |
| 1411 | git(src, ["push", "-q", "origin", name]); |
| 1412 | }; |
| 1413 | |
| 1414 | // The default branch starts empty and saves its own entries. |
| 1415 | git(src, ["push", "-q", "origin", "main"]); |
| 1416 | assert.deepEqual(await restored(), { builtin: "", real: "" }); |
| 1417 | // A branch falls back to the default branch's caches... |
| 1418 | branch("a", "main", "a.txt"); |
| 1419 | assert.deepEqual(await restored(), { builtin: "refs/heads/main", real: "refs/heads/main" }); |
| 1420 | // ...but never sees another branch's, though a's are newer and match the restore key. |
| 1421 | branch("b", "main", "b.txt"); |
| 1422 | assert.deepEqual(await restored(), { builtin: "refs/heads/main", real: "refs/heads/main" }); |
| 1423 | // A pull request reads its base branch's caches. |
| 1424 | branch("c", "a", "c.txt"); |
| 1425 | await ok("POST", `/repos/${owner}/scopes/pulls`, { title: "c into a", head: "c", base: "a" }); |
| 1426 | assert.deepEqual(await restored(), { builtin: "refs/heads/a", real: "refs/heads/a" }); |
| 1427 | // Another pull request doesn't see the first one's (saved under refs/pull/<n>/merge). |
| 1428 | branch("d", "main", "d.txt"); |
| 1429 | await ok("POST", `/repos/${owner}/scopes/pulls`, { title: "d into main", head: "d", base: "main" }); |
| 1430 | assert.deepEqual(await restored(), { builtin: "refs/heads/main", real: "refs/heads/main" }); |
| 1431 | const caches = await ok("GET", `/repos/${owner}/scopes/actions/caches?per_page=100`); |
| 1432 | assert.deepEqual([...new Set(caches.actions_caches.map((c) => c.ref))].sort(), ["refs/heads/a", "refs/heads/b", "refs/heads/main", "refs/pull/1/merge", "refs/pull/2/merge"]); |
| 1433 | // A page at a time, with GitHub's Link header. |
| 1434 | const page = await fetch(`${base}/api/v3/repos/${owner}/scopes/actions/caches?per_page=2`, { headers: { authorization: `Bearer ${token}` } }); |
| 1435 | assert.equal((await page.json()).actions_caches.length, 2); |
| 1436 | assert.match(page.headers.get("link") ?? "", /[?&]page=2>; rel="next".*rel="last"/); |
| 1437 | |
| 1438 | // retention-days (the built-in and the real upload-artifact), capped at 90 days. |
| 1439 | const days = (iso) => Math.round((Date.parse(iso) - Date.now()) / 86_400_000); |
| 1440 | const artifacts = (await ok("GET", `/repos/${owner}/scopes/actions/runs/1/artifacts`)).artifacts; |
| 1441 | const byName = Object.fromEntries(artifacts.map((a) => [a.name.replace(/-\d+$/, ""), a])); |
| 1442 | assert.equal(days(byName.builtin.expires_at), 1, JSON.stringify(byName.builtin)); |
| 1443 | assert.equal(days(byName.real.expires_at), 3, JSON.stringify(byName.real)); |
| 1444 | assert.equal(days(byName.capped.expires_at), 90, JSON.stringify(byName.capped)); |
| 1445 | assert.ok(artifacts.every((a) => a.expired === false)); |
| 1446 | }); |
| 1447 | |
| 1448 | test("an older runner's cache API is scoped the same way", async () => { |
| 1449 | const owner = (await ok("GET", "/user")).login; |
| 1450 | await ok("POST", "/user/repos", { name: "legacy-cache" }); |
| 1451 | const src = join(work, "legacy-cache"); |
| 1452 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1453 | writeFileSync(join(src, ".github/workflows/w.yml"), "on:\n push:\n branches: [main, a, b]\njobs:\n j:\n runs-on: [self-hosted, linux]\n steps: [{ run: echo }]\n"); |
| 1454 | git(src, ["init", "-q", "-b", "main"]); |
| 1455 | git(src, ["add", "-A"]); |
| 1456 | git(src, ["commit", "-q", "-m", "legacy"]); |
| 1457 | git(src, ["remote", "add", "origin", `${base}/${owner}/legacy-cache.git`]); |
| 1458 | |
| 1459 | /** Claims the job a push started and starts it, as an older runner would, without running steps. */ |
| 1460 | const startJob = async () => { |
| 1461 | const claim = await until("a job to claim", async () => { |
| 1462 | const r = await runnerClaim(`/repos/${owner}/legacy-cache`, ["linux"]); |
| 1463 | return r.status === 200 ? r.json : null; |
| 1464 | }); |
| 1465 | const h = { authorization: `Bearer ${claim.token}` }; |
| 1466 | const at = `${base}/runner/jobs/${claim.job_id}`; |
| 1467 | assert.equal((await fetch(at, { headers: h })).status, 200); |
| 1468 | return { |
| 1469 | put: async (key, body) => (await fetch(`${at}/cache/${encodeURIComponent(key)}`, { method: "PUT", headers: { ...h, "content-length": String(body.length) }, body })).status, |
| 1470 | lookup: async (key, restore = "") => { |
| 1471 | const r = await fetch(`${at}/cache?key=${encodeURIComponent(key)}&restore=${encodeURIComponent(restore)}`, { headers: h }); |
| 1472 | if (r.status !== 200) return { status: r.status }; |
| 1473 | const hit = await r.json(); |
| 1474 | const blob = await fetch(hit.url, { headers: h }); |
| 1475 | return { key: hit.key, content: blob.status === 200 ? await blob.text() : blob.status }; |
| 1476 | }, |
| 1477 | blob: async (stored) => (await fetch(`${at}/cache/blob/${encodeURIComponent(stored)}`, { headers: h })).status, |
| 1478 | done: async () => assert.equal((await fetch(`${at}/complete`, { method: "POST", headers: { ...h, "content-type": "application/json" }, body: JSON.stringify({ conclusion: "success", outputs: {} }) })).status, 200), |
| 1479 | }; |
| 1480 | }; |
| 1481 | |
| 1482 | const branch = (name) => { |
| 1483 | git(src, ["checkout", "-q", "-b", name, "main"]); |
| 1484 | writeFileSync(join(src, `${name}.txt`), `${name}\n`); |
| 1485 | git(src, ["add", "-A"]); |
| 1486 | git(src, ["commit", "-q", "-m", name]); |
| 1487 | git(src, ["push", "-q", "origin", name]); |
| 1488 | }; |
| 1489 | |
| 1490 | git(src, ["push", "-q", "origin", "main"]); |
| 1491 | let job = await startJob(); |
| 1492 | assert.equal(await job.put("lk-main", "from main"), 200); |
| 1493 | await job.done(); |
| 1494 | |
| 1495 | branch("a"); |
| 1496 | job = await startJob(); |
| 1497 | assert.deepEqual(await job.lookup("lk-a", "lk-"), { key: "lk-main", content: "from main" }); |
| 1498 | assert.equal(await job.put("lk-a", "from a"), 200); |
| 1499 | assert.deepEqual(await job.lookup("lk-a"), { key: "lk-a", content: "from a" }); |
| 1500 | await job.done(); |
| 1501 | |
| 1502 | branch("b"); |
| 1503 | job = await startJob(); |
| 1504 | assert.deepEqual(await job.lookup("lk-a"), { status: 404 }); |
| 1505 | assert.deepEqual(await job.lookup("lk-x", "lk-"), { key: "lk-main", content: "from main" }); |
| 1506 | // a's entry can't be fetched directly either |
| 1507 | assert.equal(await job.blob("refs/heads/a\nlk-a"), 404); |
| 1508 | assert.equal(await job.blob("lk-a"), 404); |
| 1509 | await job.done(); |
| 1510 | }); |
| 1511 | |
| 1512 | test("an older runner's artifact API needs the job's own token", async () => { |
| 1513 | const owner = (await ok("GET", "/user")).login; |
| 1514 | await ok("POST", "/user/repos", { name: "legacy-artifacts" }); |
| 1515 | const src = join(work, "legacy-artifacts"); |
| 1516 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1517 | writeFileSync(join(src, ".github/workflows/w.yml"), "on: push\njobs:\n j:\n runs-on: [self-hosted, linux]\n steps: [{ run: echo }]\n"); |
| 1518 | git(src, ["init", "-q", "-b", "main"]); |
| 1519 | git(src, ["add", "-A"]); |
| 1520 | git(src, ["commit", "-q", "-m", "legacy"]); |
| 1521 | git(src, ["remote", "add", "origin", `${base}/${owner}/legacy-artifacts.git`]); |
| 1522 | git(src, ["push", "-q", "origin", "main"]); |
| 1523 | const claim = await until("a job to claim", async () => { |
| 1524 | const r = await runnerClaim(`/repos/${owner}/legacy-artifacts`, ["linux"]); |
| 1525 | return r.status === 200 ? r.json : null; |
| 1526 | }); |
| 1527 | const at = `${base}/runner/jobs/${claim.job_id}/artifacts/out`; |
| 1528 | const real = { authorization: `Bearer ${claim.token}` }; |
| 1529 | // Same shape (djt_<repo>_<job>_...) as the real token, but not it. |
| 1530 | const forged = { authorization: `Bearer ${claim.token.split("_").slice(0, 3).join("_")}_${"0".repeat(48)}` }; |
| 1531 | assert.equal((await fetch(`${base}/runner/jobs/${claim.job_id}`, { headers: real })).status, 200); |
| 1532 | const put = async (h, body) => (await fetch(at, { method: "PUT", headers: { ...h, "content-length": String(body.length) }, body })).status; |
| 1533 | const get = async (h) => { |
| 1534 | const r = await fetch(at, { headers: h }); |
| 1535 | return r.status === 200 ? await r.text() : r.status; |
| 1536 | }; |
| 1537 | assert.equal(await put(real, "real bytes"), 200); |
| 1538 | assert.equal(await put(forged, "forged bytes"), 401); |
| 1539 | assert.equal(await get(forged), 401); |
| 1540 | assert.equal(await get({}), 401); |
| 1541 | // The forged upload never reached storage. |
| 1542 | assert.equal(await get(real), "real bytes"); |
| 1543 | assert.equal((await fetch(`${base}/runner/jobs/${claim.job_id}/complete`, { method: "POST", headers: { ...real, "content-type": "application/json" }, body: JSON.stringify({ conclusion: "success", outputs: {} }) })).status, 200); |
| 1544 | // A finished job's token no longer reads or writes. |
| 1545 | assert.equal(await get(real), 401); |
| 1546 | }); |
| 1547 | |
| 1548 | test("an older runner's artifacts are listed, downloaded and deleted through the REST API", async () => { |
| 1549 | const owner = (await ok("GET", "/user")).login; |
| 1550 | await ok("POST", "/user/repos", { name: "legacy-rest" }); |
| 1551 | const src = join(work, "legacy-rest"); |
| 1552 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1553 | writeFileSync(join(src, ".github/workflows/w.yml"), "on: push\njobs:\n j:\n runs-on: [self-hosted, linux]\n steps: [{ run: echo }]\n"); |
| 1554 | git(src, ["init", "-q", "-b", "main"]); |
| 1555 | git(src, ["add", "-A"]); |
| 1556 | git(src, ["commit", "-q", "-m", "legacy"]); |
| 1557 | git(src, ["remote", "add", "origin", `${base}/${owner}/legacy-rest.git`]); |
| 1558 | git(src, ["push", "-q", "origin", "main"]); |
| 1559 | const claim = await until("a job to claim", async () => { |
| 1560 | const r = await runnerClaim(`/repos/${owner}/legacy-rest`, ["linux"]); |
| 1561 | return r.status === 200 ? r.json : null; |
| 1562 | }); |
| 1563 | const h = { authorization: `Bearer ${claim.token}` }; |
| 1564 | const at = `${base}/runner/jobs/${claim.job_id}`; |
| 1565 | assert.equal((await fetch(at, { headers: h })).status, 200); |
| 1566 | for (const name of ["one", "two", "three"]) { |
| 1567 | assert.equal((await fetch(`${at}/artifacts/${name}`, { method: "PUT", headers: { ...h, "content-length": String(name.length) }, body: name })).status, 200); |
| 1568 | } |
| 1569 | const rest = (path, init = {}) => fetch(`${base}/api/v3/repos/${owner}/legacy-rest${path}`, { ...init, headers: { authorization: `Bearer ${token}` } }); |
| 1570 | |
| 1571 | // Listed with numeric ids, across the repository and for the run, a page at a time. |
| 1572 | let r = await rest("/actions/artifacts?per_page=2"); |
| 1573 | let body = await r.json(); |
| 1574 | assert.equal(body.total_count, 3, JSON.stringify(body)); |
| 1575 | assert.equal(body.artifacts.length, 2); |
| 1576 | assert.ok(body.artifacts.every((a) => Number.isSafeInteger(a.id) && a.expired === false), JSON.stringify(body)); |
| 1577 | assert.match(r.headers.get("link") ?? "", /[?&]page=2>; rel="next"/); |
| 1578 | const runId = body.artifacts[0].workflow_run.id; |
| 1579 | r = await rest(`/actions/runs/${runId}/artifacts?per_page=2&page=2`); |
| 1580 | body = await r.json(); |
| 1581 | assert.equal(body.total_count, 3); |
| 1582 | assert.equal(body.artifacts.length, 1); |
| 1583 | assert.match(r.headers.get("link") ?? "", /rel="prev"/); |
| 1584 | assert.doesNotMatch(r.headers.get("link") ?? "", /rel="next"/); |
| 1585 | const two = (await (await rest(`/actions/runs/${runId}/artifacts?name=two`)).json()).artifacts[0]; |
| 1586 | assert.equal((await (await rest(`/actions/artifacts/${two.id}`)).json()).name, "two"); |
| 1587 | |
| 1588 | // The download is the tarball the runner uploaded. |
| 1589 | const zip = await rest(`/actions/artifacts/${two.id}/zip`, { redirect: "manual" }); |
| 1590 | assert.equal(zip.status, 302); |
| 1591 | const blob = await fetch(zip.headers.get("location")); |
| 1592 | assert.equal(blob.status, 200); |
| 1593 | assert.equal(blob.headers.get("content-type"), "application/gzip"); |
| 1594 | assert.match(blob.headers.get("content-disposition") ?? "", /two\.tar\.gz/); |
| 1595 | assert.equal(await blob.text(), "two"); |
| 1596 | |
| 1597 | // Deleting it removes it for the runner too. |
| 1598 | assert.equal((await rest(`/actions/artifacts/${two.id}`, { method: "DELETE" })).status, 204); |
| 1599 | assert.equal((await rest(`/actions/artifacts/${two.id}`)).status, 404); |
| 1600 | assert.equal((await fetch(`${at}/artifacts/two`, { headers: h })).status, 404); |
| 1601 | assert.equal(await (await fetch(`${at}/artifacts/one`, { headers: h })).text(), "one"); |
| 1602 | assert.equal((await (await rest("/actions/artifacts")).json()).total_count, 2); |
| 1603 | assert.equal((await fetch(`${at}/complete`, { method: "POST", headers: { ...h, "content-type": "application/json" }, body: JSON.stringify({ conclusion: "success", outputs: {} }) })).status, 200); |
| 1604 | }); |
| 1605 | |
| 1606 | const hasDocker = spawnSync("docker", ["info"], { stdio: "ignore", timeout: 15_000 }).status === 0; |
| 1607 | |
| 1608 | test("containers: a job container and service containers on a self-hosted runner", { skip: !hasDocker && "Docker is not available" }, async () => { |
| 1609 | const owner = (await ok("GET", "/user")).login; |
| 1610 | await ok("POST", "/user/repos", { name: "boxes" }); |
| 1611 | const src = join(work, "boxes"); |
| 1612 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1613 | // Docker Hub rate-limits anonymous pulls; mirror.gcr.io serves the same official images. |
| 1614 | writeFileSync( |
| 1615 | join(src, ".github/workflows/ci.yml"), |
| 1616 | `on: push |
| 1617 | jobs: |
| 1618 | boxed: |
| 1619 | runs-on: [self-hosted, linux] |
| 1620 | container: |
| 1621 | image: mirror.gcr.io/library/debian:\${{ github.event_name == 'push' && 'bookworm-slim' || 'nope' }} |
| 1622 | services: |
| 1623 | cache: |
| 1624 | image: mirror.gcr.io/library/redis:7-alpine |
| 1625 | options: --health-cmd "redis-cli ping" --health-interval 1s --health-retries 30 |
| 1626 | steps: |
| 1627 | - run: | |
| 1628 | grep -q bookworm /etc/os-release && echo "inside debian" |
| 1629 | exec 3<>/dev/tcp/cache/6379 |
| 1630 | printf 'PING\\r\\n' >&3 |
| 1631 | read -r line <&3 |
| 1632 | echo "cache answered \${line%$'\\r'}" |
| 1633 | `, |
| 1634 | ); |
| 1635 | git(src, ["init", "-q", "-b", "main"]); |
| 1636 | git(src, ["add", "."]); |
| 1637 | git(src, ["commit", "-q", "-m", "containers"]); |
| 1638 | git(src, ["remote", "add", "origin", `${base}/${owner}/boxes.git`]); |
| 1639 | git(src, ["push", "-q", "origin", "main"]); |
| 1640 | |
| 1641 | const run = await until("boxes run", async () => (await ok("GET", `/repos/${owner}/boxes/actions/runs`)).workflow_runs[0]); |
| 1642 | const boxed = await runOneJob(owner, "boxes"); |
| 1643 | assert.equal(boxed.claim.name, "boxed"); |
| 1644 | const log = await (await fetch(`${base}/api/v3/repos/${owner}/boxes/actions/jobs/${boxed.claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 1645 | assert.match(log, /inside debian/); |
| 1646 | assert.match(log, /cache answered \+PONG/); |
| 1647 | const done = await until("boxes run to complete", async () => { |
| 1648 | const r = await ok("GET", `/repos/${owner}/boxes/actions/runs/${run.id}`); |
| 1649 | return r.status === "completed" ? r : null; |
| 1650 | }); |
| 1651 | assert.equal(done.conclusion, "success"); |
| 1652 | }); |
| 1653 | |
| 1654 | // ---------------------------------------------------------------- the official actions, unmodified |
| 1655 | |
| 1656 | // No workflow-level permissions: jobs get the repository default (write), like GitHub's permissive default. |
| 1657 | const TOOLKIT_WORKFLOW = `name: Toolkit |
| 1658 | on: push |
| 1659 | jobs: |
| 1660 | produce: |
| 1661 | runs-on: [self-hosted, linux] |
| 1662 | steps: |
| 1663 | - uses: actions/checkout@v4 |
| 1664 | with: { lfs: true, submodules: true, persist-credentials: false } |
| 1665 | - name: LFS, submodules, no persisted credentials |
| 1666 | run: | |
| 1667 | cat big.bin lib/lib.txt |
| 1668 | git config --get-regexp 'extraheader' || echo "no persisted credentials" |
| 1669 | - uses: actions/checkout@v4 |
| 1670 | with: { path: sparse, sparse-checkout: docs } |
| 1671 | - run: echo "sparse=$(ls sparse | tr '\\n' ' ')" |
| 1672 | - id: real-cache |
| 1673 | uses: actions/cache@v4 |
| 1674 | env: { DIFFINERY_BUILTINS: "false" } |
| 1675 | with: { path: deps, key: "deps-\${{ hashFiles('lock.txt') }}" } |
| 1676 | - if: steps.real-cache.outputs.cache-hit != 'true' |
| 1677 | run: mkdir -p deps && echo "saved by run \${{ github.run_id }}" > deps/marker |
| 1678 | - run: echo "real-hit=\${{ steps.real-cache.outputs.cache-hit }} $(cat deps/marker)" |
| 1679 | - id: builtin-cache |
| 1680 | uses: actions/cache/restore@v4 |
| 1681 | with: { path: deps, key: "deps-\${{ hashFiles('lock.txt') }}", lookup-only: true } |
| 1682 | - id: v3-cache |
| 1683 | uses: actions/cache/restore@v3 |
| 1684 | env: { DIFFINERY_BUILTINS: "false" } |
| 1685 | with: { path: deps, key: "deps-\${{ hashFiles('lock.txt') }}", lookup-only: true } |
| 1686 | - run: echo "builtin-hit=\${{ steps.builtin-cache.outputs.cache-hit }} v3-hit=\${{ steps.v3-cache.outputs.cache-hit }}" |
| 1687 | - run: mkdir -p dist/sub && echo one > dist/one.txt && echo two > dist/sub/two.txt && echo hidden > dist/.hidden |
| 1688 | - id: up-real |
| 1689 | uses: actions/upload-artifact@v4 |
| 1690 | env: { DIFFINERY_BUILTINS: "false" } |
| 1691 | with: { name: dist-real, path: dist/, include-hidden-files: true, retention-days: 3 } |
| 1692 | - id: up-builtin |
| 1693 | uses: actions/upload-artifact@v4 |
| 1694 | with: { name: dist-builtin, path: dist/, compression-level: 0 } |
| 1695 | - run: echo "ids=\${{ steps.up-real.outputs.artifact-id }},\${{ steps.up-builtin.outputs.artifact-id }} url=\${{ steps.up-real.outputs.artifact-url }}" |
| 1696 | consume: |
| 1697 | needs: produce |
| 1698 | runs-on: [self-hosted, linux] |
| 1699 | steps: |
| 1700 | - uses: actions/download-artifact@v4 |
| 1701 | env: { DIFFINERY_BUILTINS: "false" } |
| 1702 | with: { path: all } |
| 1703 | - uses: actions/download-artifact@v4 |
| 1704 | with: { pattern: dist-*, path: merged, merge-multiple: true } |
| 1705 | - uses: actions/download-artifact@v4 |
| 1706 | env: { DIFFINERY_BUILTINS: "false" } |
| 1707 | with: { name: dist-builtin, path: one } |
| 1708 | - run: find all merged one -type f | LC_ALL=C sort | tr '\\n' ' ' |
| 1709 | - uses: actions/checkout@v4 |
| 1710 | with: { path: src } |
| 1711 | - id: prev |
| 1712 | run: echo "id=$(cat src/prev-run.txt 2>/dev/null)" >> "$GITHUB_OUTPUT" |
| 1713 | - if: steps.prev.outputs.id != '' |
| 1714 | uses: actions/download-artifact@v4 |
| 1715 | env: { DIFFINERY_BUILTINS: "false" } |
| 1716 | with: { name: dist-real, path: prev-real, run-id: "\${{ steps.prev.outputs.id }}", github-token: "\${{ github.token }}", repository: "\${{ github.repository }}" } |
| 1717 | - if: steps.prev.outputs.id != '' |
| 1718 | uses: actions/download-artifact@v4 |
| 1719 | with: { pattern: "dist-*", path: prev-builtin, run-id: "\${{ steps.prev.outputs.id }}", github-token: "\${{ github.token }}" } |
| 1720 | - if: steps.prev.outputs.id != '' |
| 1721 | run: echo "previous run $(cat prev-real/sub/two.txt) $(find prev-builtin -type f | LC_ALL=C sort | tr '\\n' ' ')" |
| 1722 | script: |
| 1723 | runs-on: [self-hosted, linux] |
| 1724 | steps: |
| 1725 | - id: gs |
| 1726 | uses: actions/github-script@v7 |
| 1727 | with: |
| 1728 | result-encoding: string |
| 1729 | script: | |
| 1730 | const { owner, repo } = context.repo; |
| 1731 | const label = \`from-run-\${context.runId}\`; |
| 1732 | await github.rest.issues.createLabel({ owner, repo, name: label, color: "ff0000" }); |
| 1733 | const { data: issue } = await github.rest.issues.create({ owner, repo, title: \`Opened by run \${context.runId}\`, body: "hello", labels: [label] }); |
| 1734 | await github.rest.issues.createComment({ owner, repo, issue_number: issue.number, body: "commented by github-script" }); |
| 1735 | const { data: comments } = await github.rest.issues.listComments({ owner, repo, issue_number: issue.number }); |
| 1736 | const { data: got } = await github.rest.issues.get({ owner, repo, issue_number: issue.number }); |
| 1737 | const { data: pulls } = await github.rest.pulls.list({ owner, repo, state: "all" }); |
| 1738 | await github.rest.repos.createCommitStatus({ owner, repo, sha: context.sha, state: "success", context: "github-script", description: "set with the default GITHUB_TOKEN" }); |
| 1739 | return \`issue=\${issue.number} labels=\${got.labels.map((l) => l.name)} comments=\${comments.length} pulls=\${pulls.length} event=\${context.eventName} repo=\${context.payload.repository.full_name} sender=\${context.payload.sender.login}\`; |
| 1740 | - run: echo "script says $RESULT" |
| 1741 | env: { RESULT: "\${{ steps.gs.outputs.result }}" } |
| 1742 | restricted: |
| 1743 | runs-on: [self-hosted, linux] |
| 1744 | permissions: { contents: read } |
| 1745 | steps: |
| 1746 | - id: gs |
| 1747 | uses: actions/github-script@v7 |
| 1748 | with: |
| 1749 | result-encoding: string |
| 1750 | script: | |
| 1751 | const { owner, repo } = context.repo; |
| 1752 | const { data: meta } = await github.rest.repos.get({ owner, repo }); |
| 1753 | try { |
| 1754 | await github.rest.issues.create({ owner, repo, title: "must not be created" }); |
| 1755 | return "issue write allowed"; |
| 1756 | } catch (e) { |
| 1757 | return \`repo=\${meta.name} issue write -> \${e.status}: \${e.message}\`; |
| 1758 | } |
| 1759 | - run: echo "restricted says $RESULT" |
| 1760 | env: { RESULT: "\${{ steps.gs.outputs.result }}" } |
| 1761 | node: |
| 1762 | runs-on: [self-hosted, linux] |
| 1763 | steps: |
| 1764 | - uses: actions/checkout@v4 |
| 1765 | - id: node |
| 1766 | uses: actions/setup-node@v4 |
| 1767 | with: { node-version: 20, cache: npm } |
| 1768 | - run: npm ci --no-audit --no-fund && echo "node=$(node --version) npm-cache-hit=\${{ steps.node.outputs.cache-hit }}" |
| 1769 | java: |
| 1770 | runs-on: [self-hosted, linux] |
| 1771 | steps: |
| 1772 | - uses: actions/checkout@v4 |
| 1773 | - id: java |
| 1774 | uses: actions/setup-java@v4 |
| 1775 | with: { distribution: oracle, java-version: "21", cache: maven } |
| 1776 | - run: | |
| 1777 | mkdir -p ~/.m2/repository/e2e && echo "run \${{ github.run_id }}" > ~/.m2/repository/e2e/marker |
| 1778 | echo "java=$(java -version 2>&1 | grep version) maven-cache-hit=\${{ steps.java.outputs.cache-hit }} $(cat ~/.m2/repository/e2e/marker)" |
| 1779 | `; |
| 1780 | |
| 1781 | /** Runs every job of `run` with the real agent; returns their logs by job name. */ |
| 1782 | async function runToolkit(owner, run) { |
| 1783 | const home = join(work, "toolkit-home"); |
| 1784 | const logs = {}; |
| 1785 | for (let i = 0; i < 6; i++) { |
| 1786 | const job = await runOneJob(owner, "toolkit", { |
| 1787 | timeoutMs: 420_000, |
| 1788 | envFor: (claim) => { |
| 1789 | // A fresh HOME per job (at the same path, as on hosted runners): caches must come from the cache service. |
| 1790 | rmSync(home, { recursive: true, force: true }); |
| 1791 | mkdirSync(home, { recursive: true }); |
| 1792 | return { |
| 1793 | ...(process.env.E2E_RUNNER_IMAGE ? {} : { HOME: home, RUNNER_TOOL_CACHE: join(work, "toolcache") }), |
| 1794 | // The node job runs every action for real, checkout and setup-node included. |
| 1795 | ...(claim.name === "node" ? { DIFFINERY_DISABLE_BUILTINS: "1" } : {}), |
| 1796 | }; |
| 1797 | }, |
| 1798 | }); |
| 1799 | // Without the timestamps, so assertions can span lines. |
| 1800 | const raw = await (await fetch(`${base}/api/v3/repos/${owner}/toolkit/actions/jobs/${job.claim.job_id}/logs`, { headers: { authorization: `Bearer ${token}` } })).text(); |
| 1801 | logs[job.claim.name] = raw.replace(/^\S+Z /gm, ""); |
| 1802 | // Kept with E2E_KEEP=1 for debugging. |
| 1803 | mkdirSync(join(work, "toolkit-logs"), { recursive: true }); |
| 1804 | writeFileSync(join(work, "toolkit-logs", `run${run.run_number}-${job.claim.name}.log`), `${logs[job.claim.name]}\n--- agent ---\n${job.out}`); |
| 1805 | } |
| 1806 | const done = await until("toolkit run to complete", async () => { |
| 1807 | const r = await ok("GET", `/repos/${owner}/toolkit/actions/runs/${run.id}`); |
| 1808 | return r.status === "completed" ? r : null; |
| 1809 | }); |
| 1810 | assert.equal(done.conclusion, "success", JSON.stringify(logs, null, 2).slice(-8000)); |
| 1811 | return logs; |
| 1812 | } |
| 1813 | |
| 1814 | const toolkit = {}; |
| 1815 | |
| 1816 | test("official actions: checkout, cache, artifacts, github-script and setup-* run unmodified", { timeout: 1_200_000 }, async () => { |
| 1817 | const owner = (await ok("GET", "/user")).login; |
| 1818 | toolkit.owner = owner; |
| 1819 | // A submodule repository, then the main one (LFS object, submodule, sparse-checkout target). |
| 1820 | // Public: like GITHUB_TOKEN, the job token reads only its own repository and public ones. |
| 1821 | await ok("POST", "/user/repos", { name: "toolkit-lib", visibility: "public" }); |
| 1822 | const lib = join(work, "toolkit-lib"); |
| 1823 | mkdirSync(lib, { recursive: true }); |
| 1824 | writeFileSync(join(lib, "lib.txt"), "from the submodule\n"); |
| 1825 | git(lib, ["init", "-q", "-b", "main"]); |
| 1826 | git(lib, ["add", "."]); |
| 1827 | git(lib, ["commit", "-q", "-m", "lib"]); |
| 1828 | git(lib, ["remote", "add", "origin", `${base}/${owner}/toolkit-lib.git`]); |
| 1829 | git(lib, ["push", "-q", "origin", "main"]); |
| 1830 | const libSha = git(lib, ["rev-parse", "HEAD"]).trim(); |
| 1831 | |
| 1832 | await ok("POST", "/user/repos", { name: "toolkit" }); |
| 1833 | // Jobs without permissions: write with the default token (the setting older repositories keep). |
| 1834 | await ok("PUT", `/repos/${owner}/toolkit/actions/permissions/workflow`, { default_workflow_permissions: "write" }); |
| 1835 | const src = join(work, "toolkit"); |
| 1836 | toolkit.src = src; |
| 1837 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 1838 | mkdirSync(join(src, "docs")); |
| 1839 | writeFileSync(join(src, ".github/workflows/toolkit.yml"), TOOLKIT_WORKFLOW); |
| 1840 | writeFileSync(join(src, "docs/guide.md"), "# guide\n"); |
| 1841 | writeFileSync(join(src, "README.md"), "# toolkit\n"); |
| 1842 | writeFileSync(join(src, "lock.txt"), "v1\n"); |
| 1843 | writeFileSync(join(src, "package.json"), JSON.stringify({ name: "toolkit", version: "1.0.0", private: true }, null, 2) + "\n"); |
| 1844 | writeFileSync(join(src, "package-lock.json"), JSON.stringify({ name: "toolkit", version: "1.0.0", lockfileVersion: 3, requires: true, packages: { "": { name: "toolkit", version: "1.0.0" } } }, null, 2) + "\n"); |
| 1845 | writeFileSync(join(src, "pom.xml"), "<project><modelVersion>4.0.0</modelVersion><groupId>e2e</groupId><artifactId>toolkit</artifactId><version>1</version></project>\n"); |
| 1846 | writeFileSync(join(src, ".gitmodules"), '[submodule "lib"]\n\tpath = lib\n\turl = ../toolkit-lib.git\n'); |
| 1847 | git(src, ["init", "-q", "-b", "main"]); |
| 1848 | git(src, ["lfs", "install", "--local"]); |
| 1849 | git(src, ["lfs", "track", "*.bin"]); |
| 1850 | writeFileSync(join(src, "big.bin"), "the real LFS content\n"); |
| 1851 | git(src, ["add", "-A"]); |
| 1852 | git(src, ["update-index", "--add", "--cacheinfo", `160000,${libSha},lib`]); |
| 1853 | git(src, ["commit", "-q", "-m", "toolkit"]); |
| 1854 | git(src, ["remote", "add", "origin", `${base}/${owner}/toolkit.git`]); |
| 1855 | git(src, ["push", "-q", "origin", "main"]); |
| 1856 | assert.match(git(src, ["show", "HEAD:big.bin"]), /git-lfs/, "big.bin is stored as an LFS pointer"); |
| 1857 | |
| 1858 | const run = await until("toolkit run", async () => (await ok("GET", `/repos/${owner}/toolkit/actions/runs`)).workflow_runs[0]); |
| 1859 | toolkit.firstRun = run; |
| 1860 | const logs = await runToolkit(owner, run); |
| 1861 | |
| 1862 | // checkout: LFS content, submodule over HTTP with the job token, credentials not persisted, sparse |
| 1863 | assert.match(logs.produce, /the real LFS content\nfrom the submodule/); |
| 1864 | assert.match(logs.produce, /no persisted credentials/); |
| 1865 | assert.match(logs.produce, /sparse=README\.md big\.bin docs lib lock\.txt package-lock\.json package\.json pom\.xml /); |
| 1866 | // cache: a miss on the first run, for the real action and the built-in alike |
| 1867 | assert.match(logs.produce, /real-hit= saved by run 1\b/); |
| 1868 | assert.match(logs.produce, /builtin-hit= v3-hit=/); |
| 1869 | assert.match(logs.produce, /Cache saved with (the )?key: deps-[0-9a-f]{64}/); |
| 1870 | // artifacts: ids from both uploads, the URL points back at this server |
| 1871 | assert.match(logs.produce, new RegExp(`ids=\\d+,\\d+ url=${base}/${owner}/toolkit/actions/runs/1/artifacts/\\d+`)); |
| 1872 | assert.match( |
| 1873 | logs.consume, |
| 1874 | /all\/dist-builtin\/one\.txt all\/dist-builtin\/sub\/two\.txt all\/dist-real\/\.hidden all\/dist-real\/one\.txt all\/dist-real\/sub\/two\.txt merged\/\.hidden merged\/one\.txt merged\/sub\/two\.txt one\/one\.txt one\/sub\/two\.txt/, |
| 1875 | ); |
| 1876 | // github-script: REST calls with the job token |
| 1877 | assert.match(logs.script, /script says issue=1 labels=from-run-1 comments=1 pulls=0 event=push repo=[^/]+\/toolkit sender=\S+/); |
| 1878 | const issue = await ok("GET", `/repos/${owner}/toolkit/issues/1`); |
| 1879 | assert.equal(issue.title, "Opened by run 1"); |
| 1880 | assert.deepEqual(issue.labels.map((l) => l.name), ["from-run-1"]); |
| 1881 | assert.equal((await ok("GET", `/repos/${owner}/toolkit/issues/1/comments`))[0].body, "commented by github-script"); |
| 1882 | const statuses = await ok("GET", `/repos/${owner}/toolkit/commits/${run.head_sha}/statuses`); |
| 1883 | assert.equal(statuses.find((s) => s.context === "github-script")?.state, "success", JSON.stringify(statuses)); |
| 1884 | // `permissions: { contents: read }`: reading works, issue writes are refused |
| 1885 | assert.match(logs.restricted, /restricted says repo=toolkit issue write -> 403: .*issues/); |
| 1886 | assert.equal((await ok("GET", `/repos/${owner}/toolkit/issues?state=all`)).filter((i) => i.title === "must not be created").length, 0); |
| 1887 | // setup-node (the real action, DIFFINERY_DISABLE_BUILTINS=1) and setup-java: installed, caches saved |
| 1888 | assert.match(logs.node, /node=v20\.\d+\.\d+ npm-cache-hit=false/); |
| 1889 | assert.match(logs.node, /Cache saved with (the )?key: node-cache-Linux-x64-npm-[0-9a-f]{64}/); |
| 1890 | assert.match(logs.java, /java=java version "21[^\n]* maven-cache-hit=false run 1/); |
| 1891 | assert.match(logs.java, /Cache saved with (the )?key: setup-java-Linux-x64-maven-[0-9a-f]{64}/); |
| 1892 | |
| 1893 | // the REST API lists the run's artifacts and caches |
| 1894 | const artifacts = await ok("GET", `/repos/${owner}/toolkit/actions/runs/1/artifacts`); |
| 1895 | assert.deepEqual(artifacts.artifacts.map((a) => a.name).sort(), ["dist-builtin", "dist-real"]); |
| 1896 | const caches = await ok("GET", `/repos/${owner}/toolkit/actions/caches`); |
| 1897 | assert.equal(caches.total_count, 3, JSON.stringify(caches)); |
| 1898 | const zip = await fetch(`${base}/api/v3/repos/${owner}/toolkit/actions/artifacts/${artifacts.artifacts[0].id}/zip`, { headers: { authorization: `Bearer ${token}` }, redirect: "manual" }); |
| 1899 | assert.equal(zip.status, 302); |
| 1900 | assert.equal((await fetch(zip.headers.get("location"))).headers.get("content-type"), "application/zip"); |
| 1901 | }); |
| 1902 | |
| 1903 | test("official actions: the second run hits every cache and reads the first run's artifacts", { timeout: 1_200_000 }, async () => { |
| 1904 | const { owner, src, firstRun } = toolkit; |
| 1905 | assert.ok(firstRun, "the first toolkit run must have completed"); |
| 1906 | writeFileSync(join(src, "prev-run.txt"), `${firstRun.run_number}\n`); |
| 1907 | git(src, ["add", "prev-run.txt"]); |
| 1908 | git(src, ["commit", "-q", "-m", "second run"]); |
| 1909 | git(src, ["push", "-q", "origin", "main"]); |
| 1910 | const run = await until("second toolkit run", async () => (await ok("GET", `/repos/${owner}/toolkit/actions/runs`)).workflow_runs.find((r) => r.run_number === 2)); |
| 1911 | const logs = await runToolkit(owner, run); |
| 1912 | |
| 1913 | // The entry the real action saved restores with the real action (v4 and v3) and the built-in. |
| 1914 | assert.match(logs.produce, /real-hit=true saved by run 1\b/); |
| 1915 | assert.match(logs.produce, /builtin-hit=true v3-hit=true/); |
| 1916 | assert.match(logs.produce, /Cache hit occurred on the primary key deps-[0-9a-f]{64}, not saving cache\./); |
| 1917 | assert.match(logs.node, /npm-cache-hit=true/); |
| 1918 | assert.match(logs.java, /maven-cache-hit=true run 2/); |
| 1919 | // Artifacts of another run, through the REST API with github-token (real action and built-in). |
| 1920 | assert.match(logs.consume, /previous run two prev-builtin\/dist-builtin\/one\.txt prev-builtin\/dist-builtin\/sub\/two\.txt prev-builtin\/dist-real\/\.hidden prev-builtin\/dist-real\/one\.txt prev-builtin\/dist-real\/sub\/two\.txt/); |
| 1921 | assert.match(logs.script, /script says issue=2 labels=from-run-2 comments=1/); |
| 1922 | }); |
| 1923 | |
| 1924 | test("objects of every size: files from 0 bytes to 1.2 MB, read cold after a restart, come back intact from contents, diffs, pushes and clones", async () => { |
| 1925 | // A server of its own, restarted after the first push so nothing is in memory: reads then inflate |
| 1926 | // every object from storage, as in production. |
| 1927 | let server = await startServer("cold-sizes"); |
| 1928 | try { |
| 1929 | let c = client(server); |
| 1930 | const me = await c.ok("GET", "/user"); |
| 1931 | await c.ok("POST", "/user/repos", { name: "cold-sizes" }); |
| 1932 | const src = join(work, "cold-sizes"); |
| 1933 | mkdirSync(src, { recursive: true }); |
| 1934 | await c.git(src, ["init", "-q", "-b", "main"]); |
| 1935 | // Around zlib's and the runtime's buffer steps, as text and as random base64. Workers' own zlib once |
| 1936 | // refused 41,271, 65,536 and 101,330 bytes when told the exact size (#277), which Node's zlib, and so |
| 1937 | // the unit tests, never do. |
| 1938 | const sizes = [0, 1, 1000, 16_384, 16_385, 32_769, 41_271, 49_153, 65_535, 65_536, 65_537, 98_304, 101_330, 131_073, 200_000, 524_289, 1_048_576, 1_048_577, 1_200_000]; |
| 1939 | const words = "the quick brown fox jumps over a lazy dog while git packs objects into zlib streams".split(" "); |
| 1940 | const text = (n) => { |
| 1941 | let t = ""; |
| 1942 | for (let i = 0; t.length < n; i++) t += words[(i * 7 + (i >> 3)) % words.length] + (i % 11 === 10 ? "\n" : " "); |
| 1943 | return Buffer.from(t.slice(0, n)); |
| 1944 | }; |
| 1945 | const random = (n) => Buffer.from(randomBytes(Math.ceil(n * 0.75) + 3).toString("base64").slice(0, n)); |
| 1946 | const files = new Map(sizes.flatMap((n) => [[`text${n}.txt`, text(n)], [`b64-${n}.txt`, random(n)]])); |
| 1947 | for (const [name, data] of files) writeFileSync(join(src, name), data); |
| 1948 | await c.git(src, ["add", "-A"]); |
| 1949 | await c.git(src, ["commit", "-q", "-m", "every size"]); |
| 1950 | const first = (await c.git(src, ["rev-parse", "HEAD"])).trim(); |
| 1951 | await c.git(src, ["-c", "http.postBuffer=1048576", "push", "-q", `${c.gitBase}/${me.login}/cold-sizes.git`, "main"]); |
| 1952 | |
| 1953 | server.stop(); |
| 1954 | while (await fetch(`${server.base}/healthz`).then(() => true, () => false)) await sleep(500); |
| 1955 | await sleep(1000); |
| 1956 | server = await startServer("cold-sizes", { restart: true }); |
| 1957 | c = client(server); |
| 1958 | |
| 1959 | // A second push changes each file by a line: git sends deltas against the objects the server has, |
| 1960 | // so taking it in reads each base cold. |
| 1961 | for (const [name, data] of files) writeFileSync(join(src, name), Buffer.concat([data, Buffer.from("\nchanged\n")])); |
| 1962 | await c.git(src, ["add", "-A"]); |
| 1963 | await c.git(src, ["commit", "-q", "-m", "every size, changed"]); |
| 1964 | await c.git(src, ["-c", "http.postBuffer=1048576", "push", "-q", `${c.gitBase}/${me.login}/cold-sizes.git`, "main"]); |
| 1965 | // The commit's diff reads both versions of each file. |
| 1966 | const commit = await c.ok("GET", `/repos/${me.login}/cold-sizes/commits/main`); |
| 1967 | assert.equal(commit.files.length, files.size); |
| 1968 | const read = async (path, accept) => { |
| 1969 | const res = await fetch(`${server.base}/api/v3${path}`, { headers: { authorization: `Bearer ${server.token}`, accept } }); |
| 1970 | assert.equal(res.status, 200, `${path}: ${res.status} ${(await res.clone().text()).slice(0, 300)}`); |
| 1971 | return res; |
| 1972 | }; |
| 1973 | for (const [name, data] of files) { |
| 1974 | const res = await read(`/repos/${me.login}/cold-sizes/contents/${name}?ref=${first}`, "application/vnd.github.raw"); |
| 1975 | assert.ok(Buffer.from(await res.arrayBuffer()).equals(data), `${name} reads back as first pushed`); |
| 1976 | if (data.length <= 1024 * 1024) { |
| 1977 | const json = await (await read(`/repos/${me.login}/cold-sizes/contents/${name}?ref=${first}`, "application/json")).json(); |
| 1978 | assert.ok(Buffer.from(json.content, "base64").equals(data), `${name} in JSON reads back as first pushed`); |
| 1979 | } |
| 1980 | } |
| 1981 | const out = join(work, "cold-sizes-clone"); |
| 1982 | await c.git(work, ["clone", "-q", `${c.gitBase}/${me.login}/cold-sizes.git`, out]); |
| 1983 | await c.git(out, ["fsck", "--full", "--strict"]); |
| 1984 | assert.equal((await c.git(out, ["rev-parse", "HEAD"])).trim(), (await c.git(src, ["rev-parse", "HEAD"])).trim()); |
| 1985 | } finally { |
| 1986 | server.stop(); |
| 1987 | } |
| 1988 | }); |
| 1989 | |
| 1990 | test("D1 sessions: bookmarks for token clients and browsers", async () => { |
| 1991 | // Token clients get the bookmark in a header and can send it back to read their own writes. |
| 1992 | const r = await fetch(`${base}/api/v3/user`, { headers: { authorization: `Bearer ${token}` } }); |
| 1993 | assert.equal(r.status, 200); |
| 1994 | const bookmark = r.headers.get("x-d1-bookmark"); |
| 1995 | assert.match(bookmark ?? "", /^[0-9a-f]+(-[0-9a-f]+)+$/i); |
| 1996 | assert.equal(r.headers.get("set-cookie"), null); |
| 1997 | const again = await fetch(`${base}/api/v3/user`, { headers: { authorization: `Bearer ${token}`, "x-d1-bookmark": bookmark } }); |
| 1998 | assert.equal(again.status, 200); |
| 1999 | // A junk bookmark is ignored rather than breaking the request. |
| 2000 | assert.equal((await fetch(`${base}/api/v3/user`, { headers: { authorization: `Bearer ${token}`, "x-d1-bookmark": "nope" } })).status, 200); |
| 2001 | // Anonymous page views don't get a cookie; a browser that writes (any non-GET) does. |
| 2002 | assert.equal((await fetch(`${base}/`)).headers.get("set-cookie")?.includes("__Host-dfy_d1") ?? false, false); |
| 2003 | const post = await fetch(`${base}/auth/logout`, { method: "POST", redirect: "manual", headers: { "sec-fetch-site": "same-origin" } }); |
| 2004 | assert.match(post.headers.get("set-cookie") ?? "", /__Host-dfy_d1=[0-9a-f-]+; Max-Age=86400; Path=\/; HttpOnly; Secure; SameSite=Lax/); |
| 2005 | }); |
| 2006 | |
| 2007 | test("GitHub REST compatibility: Octokit against releases, statuses, git data, issues, deployments and search", async () => { |
| 2008 | const { Octokit } = await import("@octokit/rest"); |
| 2009 | const gh = new Octokit({ auth: token, baseUrl: `${base}/api/v3`, userAgent: "diffinery-e2e", request: { retries: 0 } }); |
| 2010 | const owner = (await gh.rest.users.getAuthenticated()).data.login; |
| 2011 | const repo = "compat"; |
| 2012 | const R = { owner, repo }; |
| 2013 | |
| 2014 | // ------------------------------------------------------------ repository, pushed with git |
| 2015 | const created = await gh.rest.repos.createForAuthenticatedUser({ name: repo, description: "Octokit compatibility" }); |
| 2016 | assert.equal(created.status, 201); |
| 2017 | assert.equal(created.data.full_name, `${owner}/${repo}`); |
| 2018 | assert.match(created.data.node_id, /\S/); |
| 2019 | assert.equal(created.data.owner.login, owner); |
| 2020 | const src = join(work, "compat"); |
| 2021 | mkdirSync(join(src, "src"), { recursive: true }); |
| 2022 | writeFileSync(join(src, "README.md"), "# compat\n"); |
| 2023 | writeFileSync(join(src, "src/main.ts"), "export const x = 1;\n"); |
| 2024 | git(src, ["init", "-q", "-b", "main"]); |
| 2025 | git(src, ["add", "-A"]); |
| 2026 | git(src, ["commit", "-q", "-m", "initial"]); |
| 2027 | git(src, ["remote", "add", "origin", `${base}/${owner}/${repo}.git`]); |
| 2028 | git(src, ["push", "-q", "origin", "main"]); |
| 2029 | const mainSha = git(src, ["rev-parse", "HEAD"]).trim(); |
| 2030 | |
| 2031 | const r = await gh.rest.repos.get(R); |
| 2032 | assert.equal(r.data.default_branch, "main"); |
| 2033 | assert.equal(r.data.permissions.admin, true); |
| 2034 | assert.equal(r.headers["x-github-media-type"], "github.v3; format=json"); |
| 2035 | const rate = await gh.rest.rateLimit.get(); |
| 2036 | assert.ok(rate.data.resources.core.remaining > 0); |
| 2037 | |
| 2038 | // GitHub error bodies: message and documentation_url, surfaced by Octokit's RequestError. |
| 2039 | await assert.rejects(gh.rest.repos.getContent({ ...R, path: "missing.txt" }), (e) => e.status === 404 && typeof e.response.data.message === "string" && typeof e.response.data.documentation_url === "string"); |
| 2040 | |
| 2041 | // ------------------------------------------------------------ contents |
| 2042 | const readme = await gh.rest.repos.getContent({ ...R, path: "README.md" }); |
| 2043 | assert.equal(readme.data.type, "file"); |
| 2044 | assert.equal(Buffer.from(readme.data.content, "base64").toString(), "# compat\n"); |
| 2045 | const rawReadme = await gh.rest.repos.getContent({ ...R, path: "README.md", mediaType: { format: "raw" } }); |
| 2046 | assert.equal(rawReadme.data, "# compat\n"); |
| 2047 | const dir = await gh.rest.repos.getContent({ ...R, path: "src" }); |
| 2048 | assert.deepEqual(dir.data.map((e) => e.name), ["main.ts"]); |
| 2049 | await assert.rejects(gh.rest.repos.createOrUpdateFileContents({ ...R, path: "README.md", message: "stale", content: Buffer.from("x").toString("base64"), sha: "0".repeat(40) }), (e) => e.status === 409); |
| 2050 | const put = await gh.rest.repos.createOrUpdateFileContents({ ...R, path: "README.md", message: "Update README", content: Buffer.from("# compat\n\nUpdated.\n").toString("base64"), sha: readme.data.sha, committer: { name: "Release Bot", email: "bot@example.com" } }); |
| 2051 | assert.equal(put.status, 200); |
| 2052 | assert.equal(put.data.commit.committer.name, "Release Bot"); |
| 2053 | const added = await gh.rest.repos.createOrUpdateFileContents({ ...R, path: "docs/guide.md", message: "Add guide", content: Buffer.from("guide\n").toString("base64") }); |
| 2054 | assert.equal(added.status, 201); |
| 2055 | const readmeApi = await gh.rest.repos.getReadme(R); |
| 2056 | assert.equal(readmeApi.data.path, "README.md"); |
| 2057 | |
| 2058 | // ------------------------------------------------------------ git data: blob -> tree -> commit -> ref, then git sees it |
| 2059 | const headSha = (await gh.rest.git.getRef({ ...R, ref: "heads/main" })).data.object.sha; |
| 2060 | assert.equal(headSha, added.data.commit.sha); |
| 2061 | const blob = await gh.rest.git.createBlob({ ...R, content: Buffer.from("made with the git data API\n").toString("base64"), encoding: "base64" }); |
| 2062 | assert.equal(blob.status, 201); |
| 2063 | assert.equal(Buffer.from((await gh.rest.git.getBlob({ ...R, file_sha: blob.data.sha })).data.content, "base64").toString(), "made with the git data API\n"); |
| 2064 | const headCommit = await gh.rest.git.getCommit({ ...R, commit_sha: headSha }); |
| 2065 | const tree = await gh.rest.git.createTree({ ...R, base_tree: headCommit.data.tree.sha, tree: [{ path: "gitdata/hello.txt", mode: "100644", type: "blob", sha: blob.data.sha }, { path: "inline.txt", mode: "100644", type: "blob", content: "inline\n" }] }); |
| 2066 | const full = await gh.rest.git.getTree({ ...R, tree_sha: tree.data.sha, recursive: "true" }); |
| 2067 | assert.ok(full.data.tree.some((e) => e.path === "gitdata/hello.txt" && e.type === "blob")); |
| 2068 | assert.ok(full.data.tree.some((e) => e.path === "src/main.ts")); |
| 2069 | const commit = await gh.rest.git.createCommit({ ...R, message: "Commit from the git data API", tree: tree.data.sha, parents: [headSha], author: { name: "Diffy", email: "diffy@example.com", date: "2024-05-06T07:08:09Z" } }); |
| 2070 | assert.equal(commit.data.author.name, "Diffy"); |
| 2071 | assert.deepEqual(commit.data.parents.map((p) => p.sha), [headSha]); |
| 2072 | const ref = await gh.rest.git.createRef({ ...R, ref: "refs/heads/feature", sha: commit.data.sha }); |
| 2073 | assert.equal(ref.data.ref, "refs/heads/feature"); |
| 2074 | assert.equal(ref.data.object.type, "commit"); |
| 2075 | await assert.rejects(gh.rest.git.createRef({ ...R, ref: "refs/heads/feature", sha: commit.data.sha }), (e) => e.status === 422); |
| 2076 | const matching = await gh.rest.git.listMatchingRefs({ ...R, ref: "heads/f" }); |
| 2077 | assert.deepEqual(matching.data.map((x) => x.ref), ["refs/heads/feature"]); |
| 2078 | const tag = await gh.rest.git.createTag({ ...R, tag: "v0.1.0", message: "Annotated tag", object: commit.data.sha, type: "commit", tagger: { name: "Diffy", email: "diffy@example.com", date: "2024-05-06T07:08:09Z" } }); |
| 2079 | await gh.rest.git.createRef({ ...R, ref: "refs/tags/v0.1.0", sha: tag.data.sha }); |
| 2080 | assert.equal((await gh.rest.git.getTag({ ...R, tag_sha: tag.data.sha })).data.object.sha, commit.data.sha); |
| 2081 | assert.equal((await gh.rest.git.getRef({ ...R, ref: "tags/v0.1.0" })).data.object.type, "tag"); |
| 2082 | git(src, ["fetch", "-q", "origin", "feature", "--tags"]); |
| 2083 | assert.equal(git(src, ["show", "FETCH_HEAD:gitdata/hello.txt"]), "made with the git data API\n"); |
| 2084 | assert.equal(git(src, ["cat-file", "-t", "v0.1.0"]).trim(), "tag"); |
| 2085 | |
| 2086 | // ------------------------------------------------------------ commit statuses count as checks, also for required checks |
| 2087 | await ok("PUT", `/repos/${owner}/${repo}/branches/main/protection`, { requirePullRequest: true, requiredChecks: ["ci/external"] }); |
| 2088 | const pr = await gh.rest.pulls.create({ ...R, title: "Feature", head: "feature", base: "main", body: "Adds files" }); |
| 2089 | assert.equal(pr.status, 201); |
| 2090 | const pending = await until("mergeability", async () => { |
| 2091 | const p = (await gh.rest.pulls.get({ ...R, pull_number: pr.data.number })).data; |
| 2092 | return typeof p.mergeable === "boolean" ? p : null; |
| 2093 | }); |
| 2094 | assert.equal(pending.mergeable, true); |
| 2095 | assert.equal(pending.mergeable_state, "blocked"); |
| 2096 | assert.equal(pending.head.sha, commit.data.sha); |
| 2097 | const st = await gh.rest.repos.createCommitStatus({ ...R, sha: commit.data.sha, state: "success", context: "ci/external", description: "External CI passed", target_url: "https://ci.example.com/build/1" }); |
| 2098 | assert.equal(st.status, 201); |
| 2099 | assert.equal(st.data.creator.login, owner); |
| 2100 | await assert.rejects(gh.rest.repos.createCommitStatus({ ...R, sha: commit.data.sha, state: "great" }), (e) => e.status === 422); |
| 2101 | const combined = await gh.rest.repos.getCombinedStatusForRef({ ...R, ref: "feature" }); |
| 2102 | assert.equal(combined.data.state, "success"); |
| 2103 | assert.equal(combined.data.total_count, 1); |
| 2104 | assert.equal(combined.data.statuses[0].context, "ci/external"); |
| 2105 | assert.equal((await gh.rest.repos.listCommitStatusesForRef({ ...R, ref: commit.data.sha })).data.length, 1); |
| 2106 | const clean = (await gh.rest.pulls.get({ ...R, pull_number: pr.data.number })).data; |
| 2107 | assert.equal(clean.mergeable_state, "clean"); |
| 2108 | const prPage = await (await fetch(`${base}/${owner}/${repo}/pull/${pr.data.number}/checks`)).text(); |
| 2109 | assert.match(prPage, /ci\/external/); |
| 2110 | assert.match(prPage, /https:\/\/ci\.example\.com\/build\/1/); |
| 2111 | |
| 2112 | // ------------------------------------------------------------ pull request sub-resources |
| 2113 | const byHead = await gh.rest.pulls.list({ ...R, head: `${owner}:feature`, state: "open" }); |
| 2114 | assert.deepEqual(byHead.data.map((p) => p.number), [pr.data.number]); |
| 2115 | assert.equal((await gh.rest.pulls.list({ ...R, base: "other" })).data.length, 0); |
| 2116 | const assoc = await gh.rest.repos.listPullRequestsAssociatedWithCommit({ ...R, commit_sha: commit.data.sha }); |
| 2117 | assert.deepEqual(assoc.data.map((p) => p.number), [pr.data.number]); |
| 2118 | const rc = await gh.rest.pulls.createReviewComment({ ...R, pull_number: pr.data.number, body: "Why this file?", commit_id: commit.data.sha, path: "inline.txt", line: 1, side: "RIGHT" }); |
| 2119 | assert.equal(rc.status, 201); |
| 2120 | assert.equal(rc.data.path, "inline.txt"); |
| 2121 | const reply = await gh.rest.pulls.createReplyForReviewComment({ ...R, pull_number: pr.data.number, comment_id: rc.data.id, body: "For the test." }); |
| 2122 | assert.equal(reply.data.in_reply_to_id, rc.data.id); |
| 2123 | // Numeric ids, like GitHub's: actions parse them with Number() and pass them back. |
| 2124 | assert.equal(typeof rc.data.id, "number"); |
| 2125 | assert.equal(typeof rc.data.pull_request_review_id, "number"); |
| 2126 | assert.equal((await gh.rest.pulls.listReviewComments({ ...R, pull_number: pr.data.number })).data.length, 2); |
| 2127 | await gh.rest.reactions.createForPullRequestReviewComment({ ...R, comment_id: rc.data.id, content: "eyes" }); |
| 2128 | const merged = await gh.rest.pulls.merge({ ...R, pull_number: pr.data.number, merge_method: "squash" }); |
| 2129 | assert.equal(merged.data.merged, true); |
| 2130 | const merges = await gh.rest.issues.listEventsForTimeline({ ...R, issue_number: pr.data.number }); |
| 2131 | assert.ok(merges.data.some((e) => e.event === "merged")); |
| 2132 | |
| 2133 | // ------------------------------------------------------------ releases and assets |
| 2134 | const rel = await gh.rest.repos.createRelease({ ...R, tag_name: "v1.0.0", target_commitish: "main", name: "First", generate_release_notes: true }); |
| 2135 | assert.equal(rel.status, 201); |
| 2136 | assert.match(rel.data.body, /Full Changelog/); |
| 2137 | assert.match(rel.data.body, /Feature by @/); |
| 2138 | assert.equal(rel.data.author.login, owner); |
| 2139 | assert.match(rel.data.upload_url, /\/api\/uploads\/repos\/.+\/releases\/\d+\/assets\{\?name,label\}$/); |
| 2140 | assert.match(git(src, ["ls-remote", "--tags", "origin"]), /refs\/tags\/v1\.0\.0/); |
| 2141 | const bytes = Buffer.from("hello from a release asset\n"); |
| 2142 | const up = await gh.rest.repos.uploadReleaseAsset({ ...R, release_id: rel.data.id, name: "hello.txt", label: "Greeting", data: bytes, url: rel.data.upload_url, headers: { "content-type": "text/plain", "content-length": bytes.length } }); |
| 2143 | assert.equal(up.status, 201); |
| 2144 | assert.equal(up.data.name, "hello.txt"); |
| 2145 | assert.equal(up.data.size, bytes.length); |
| 2146 | assert.equal(up.data.state, "uploaded"); |
| 2147 | const bin = Buffer.from(randomBytes(70_000)); |
| 2148 | const up2 = await gh.rest.repos.uploadReleaseAsset({ ...R, release_id: rel.data.id, name: "tool linux.bin", data: bin, baseUrl: `${base}/api/uploads`, headers: { "content-type": "application/octet-stream", "content-length": bin.length } }); |
| 2149 | assert.equal(up2.data.name, "tool.linux.bin"); |
| 2150 | await assert.rejects(gh.rest.repos.uploadReleaseAsset({ ...R, release_id: rel.data.id, name: "hello.txt", data: bytes, url: rel.data.upload_url, headers: { "content-type": "text/plain", "content-length": bytes.length } }), (e) => e.status === 422); |
| 2151 | const viaApi = await gh.rest.repos.getReleaseAsset({ ...R, asset_id: up2.data.id, headers: { accept: "application/octet-stream" } }); |
| 2152 | assert.deepEqual(Buffer.from(viaApi.data), bin); |
| 2153 | const browser = await fetch(up.data.browser_download_url); |
| 2154 | assert.equal(browser.status, 200); |
| 2155 | assert.equal(await browser.text(), bytes.toString()); |
| 2156 | assert.match(browser.headers.get("content-disposition") ?? "", /attachment/); |
| 2157 | assert.equal((await gh.rest.repos.getReleaseAsset({ ...R, asset_id: up.data.id })).data.download_count, 1); |
| 2158 | assert.deepEqual((await gh.rest.repos.listReleaseAssets({ ...R, release_id: rel.data.id })).data.map((a) => a.name), ["hello.txt", "tool.linux.bin"]); |
| 2159 | assert.equal((await gh.rest.repos.updateReleaseAsset({ ...R, asset_id: up.data.id, label: "Hello" })).data.label, "Hello"); |
| 2160 | assert.equal((await gh.rest.repos.getLatestRelease(R)).data.id, rel.data.id); |
| 2161 | assert.equal((await gh.rest.repos.getReleaseByTag({ ...R, tag: "v1.0.0" })).data.assets.length, 2); |
| 2162 | const notes = await gh.rest.repos.generateReleaseNotes({ ...R, tag_name: "v1.1.0", previous_tag_name: "v1.0.0" }); |
| 2163 | assert.match(notes.data.body, /compare\/v1\.0\.0\.\.\.v1\.1\.0/); |
| 2164 | await gh.rest.reactions.createForRelease({ ...R, release_id: rel.data.id, content: "rocket" }); |
| 2165 | assert.equal((await gh.rest.repos.getRelease({ ...R, release_id: rel.data.id })).data.reactions.rocket, 1); |
| 2166 | |
| 2167 | // Drafts are for writers only; a read-only token sees neither the draft nor its tag lookup. |
| 2168 | const draft = await gh.rest.repos.createRelease({ ...R, tag_name: "v2.0.0-rc.1", name: "Next", draft: true, prerelease: true }); |
| 2169 | assert.equal(draft.data.draft, true); |
| 2170 | const reader = await ok("POST", "/diffinery/tokens", { name: "reader", scopes: ["read"] }, null); |
| 2171 | const ro = new Octokit({ auth: reader.token, baseUrl: `${base}/api/v3`, request: { retries: 0 } }); |
| 2172 | assert.deepEqual((await ro.rest.repos.listReleases(R)).data.map((x) => x.tag_name), ["v1.0.0"]); |
| 2173 | await assert.rejects(ro.rest.repos.getRelease({ ...R, release_id: draft.data.id }), (e) => e.status === 404); |
| 2174 | await assert.rejects(ro.rest.repos.createRelease({ ...R, tag_name: "v9" }), (e) => e.status === 403 || e.status === 404); |
| 2175 | const published = await gh.rest.repos.updateRelease({ ...R, release_id: draft.data.id, draft: false, make_latest: "false" }); |
| 2176 | assert.equal(published.data.draft, false); |
| 2177 | assert.equal((await gh.rest.repos.getLatestRelease(R)).data.tag_name, "v1.0.0"); |
| 2178 | await gh.rest.repos.deleteReleaseAsset({ ...R, asset_id: up2.data.id }); |
| 2179 | assert.equal((await fetch(`${base}/${owner}/${repo}/releases/download/v1.0.0/tool.linux.bin`)).status, 404); |
| 2180 | await gh.rest.repos.deleteRelease({ ...R, release_id: published.data.id }); |
| 2181 | await assert.rejects(gh.rest.repos.getReleaseByTag({ ...R, tag: "v2.0.0-rc.1" }), (e) => e.status === 404); |
| 2182 | |
| 2183 | // Archives |
| 2184 | const zipball = await gh.rest.repos.downloadZipballArchive({ ...R, ref: "v1.0.0" }); |
| 2185 | assert.equal(Buffer.from(zipball.data).subarray(0, 2).toString(), "PK"); |
| 2186 | const tarball = await gh.rest.repos.downloadTarballArchive({ ...R, ref: "main" }); |
| 2187 | assert.deepEqual([...Buffer.from(tarball.data).subarray(0, 2)], [0x1f, 0x8b]); |
| 2188 | const tgz = join(work, "compat.tar.gz"); |
| 2189 | writeFileSync(tgz, Buffer.from(tarball.data)); |
| 2190 | assert.match(execFileSync("tar", ["tzf", tgz], { encoding: "utf8" }), /\/gitdata\/hello\.txt/); |
| 2191 | |
| 2192 | // The web UI: releases list, the release page and the form (CSRF, Kumo controls, multipart upload). |
| 2193 | const list = await (await fetch(`${base}/${owner}/${repo}/releases`)).text(); |
| 2194 | assert.match(list, /First/); |
| 2195 | assert.match(list, /hello\.txt/); |
| 2196 | const form = await fetch(`${base}/${owner}/${repo}/releases/new`); |
| 2197 | const html = await form.text(); |
| 2198 | assert.doesNotMatch(html, /<select|type="checkbox"|type="radio"/); |
| 2199 | const csrf = /name="_csrf" value="([^"]+)"/.exec(html)?.[1]; |
| 2200 | const cookie = /dfy_csrf=([^;]+)/.exec(form.headers.get("set-cookie") ?? "")?.[1]; |
| 2201 | assert.ok(csrf && cookie); |
| 2202 | const fd = new FormData(); |
| 2203 | for (const [k, v] of Object.entries({ _csrf: csrf, tag: "", new_tag: "v3.0.0", target: "main", name: "From the web", body: "Notes from the form.", latest: "1", action: "publish" })) fd.append(k, v); |
| 2204 | fd.append("assets", new Blob(["web upload\n"], { type: "text/plain" }), "web.txt"); |
| 2205 | const submit = await fetch(`${base}/${owner}/${repo}/releases`, { method: "POST", body: fd, redirect: "manual", headers: { cookie: `dfy_csrf=${cookie}` } }); |
| 2206 | assert.equal(submit.status, 303, await submit.text()); |
| 2207 | assert.equal(submit.headers.get("location"), `/${owner}/${repo}/releases/tag/v3.0.0`); |
| 2208 | assert.equal(await (await fetch(`${base}/${owner}/${repo}/releases/download/v3.0.0/web.txt`)).text(), "web upload\n"); |
| 2209 | assert.equal((await gh.rest.repos.getLatestRelease(R)).data.tag_name, "v3.0.0"); |
| 2210 | assert.match(await (await fetch(`${base}/${owner}/${repo}`)).text(), /From the web/); |
| 2211 | |
| 2212 | // ------------------------------------------------------------ General settings: website and topics, and what search engines see |
| 2213 | const before = (await gh.rest.repos.get(R)).data; |
| 2214 | const settingsPage = await fetch(`${base}/${owner}/${repo}/settings`); |
| 2215 | const settingsHtml = await settingsPage.text(); |
| 2216 | assert.match(settingsHtml, /id="repo-homepage"/); |
| 2217 | assert.match(settingsHtml, /id="repo-topics"/); |
| 2218 | const saveGeneral = async (fields) => { |
| 2219 | const r = await fetch(`${base}/${owner}/${repo}/settings/general`, { |
| 2220 | method: "POST", |
| 2221 | body: new URLSearchParams({ _csrf: /name="_csrf" value="([^"]+)"/.exec(settingsHtml)[1], name: repo, description: before.description ?? "", default_branch: before.default_branch, ...fields }), |
| 2222 | redirect: "manual", |
| 2223 | headers: { cookie: `dfy_csrf=${/dfy_csrf=([^;]+)/.exec(settingsPage.headers.get("set-cookie") ?? "")[1]}`, "sec-fetch-site": "same-origin" }, |
| 2224 | }); |
| 2225 | assert.equal(r.status, 303, await r.text()); |
| 2226 | return (await gh.rest.repos.get(R)).data; |
| 2227 | }; |
| 2228 | let saved = await saveGeneral({ visibility: "public", homepage: "https://example.com/project", topics: "Cloudflare ci, git ci" }); |
| 2229 | assert.equal(saved.homepage, "https://example.com/project"); |
| 2230 | assert.deepEqual(saved.topics, ["cloudflare", "ci", "git"]); |
| 2231 | const homePage = await (await fetch(`${base}/${owner}/${repo}`)).text(); |
| 2232 | assert.match(homePage, /<a href="https:\/\/example.com\/project" rel="noopener nofollow ugc">example.com\/project<\/a>/); |
| 2233 | const ld = JSON.stringify(JSON.parse(/<script type="application\/ld\+json"[^>]*>([^<]+)<\/script>/.exec(homePage)[1])); |
| 2234 | assert.match(ld, /"@type":"SoftwareSourceCode"/); |
| 2235 | assert.match(ld, /"keywords":"cloudflare, ci, git"/); |
| 2236 | // A form without the fields leaves them alone; bad values are refused. |
| 2237 | saved = await saveGeneral({ visibility: before.visibility }); |
| 2238 | assert.deepEqual([saved.homepage, saved.topics, saved.visibility], ["https://example.com/project", ["cloudflare", "ci", "git"], before.visibility]); |
| 2239 | assert.doesNotMatch(await (await fetch(`${base}/${owner}/${repo}`)).text(), /SoftwareSourceCode/); |
| 2240 | await assert.rejects(gh.rest.repos.update({ ...R, homepage: "javascript:alert(1)" }), (e) => e.status === 422); |
| 2241 | assert.equal((await gh.rest.repos.update({ ...R, homepage: "" })).data.homepage, null); |
| 2242 | assert.equal((await gh.rest.repos.replaceAllTopics({ ...R, names: [] })).data.names.length, 0); |
| 2243 | |
| 2244 | // ------------------------------------------------------------ issues: labels, assignees, lock, reactions, events, pagination |
| 2245 | await gh.rest.issues.createLabel({ ...R, name: "bug", color: "d73a4a", description: "Something is broken" }); |
| 2246 | await assert.rejects(gh.rest.issues.createLabel({ ...R, name: "bug", color: "d73a4a" }), (e) => e.status === 422); |
| 2247 | const issue = await gh.rest.issues.create({ ...R, title: "Crash on start", body: "It crashes.", labels: ["bug"], assignees: [owner] }); |
| 2248 | assert.deepEqual(issue.data.labels.map((l) => [l.name, l.color]), [["bug", "d73a4a"]]); |
| 2249 | assert.equal(issue.data.assignees[0].login, owner); |
| 2250 | assert.equal(issue.data.user.login, owner); |
| 2251 | assert.match(issue.data.html_url, new RegExp(`/${owner}/${repo}/issues/${issue.data.number}$`)); |
| 2252 | const n = issue.data.number; |
| 2253 | await gh.rest.issues.addLabels({ ...R, issue_number: n, labels: ["triage"] }); |
| 2254 | assert.deepEqual((await gh.rest.issues.listLabelsOnIssue({ ...R, issue_number: n })).data.map((l) => l.name).sort(), ["bug", "triage"]); |
| 2255 | await gh.rest.issues.removeLabel({ ...R, issue_number: n, name: "triage" }); |
| 2256 | await gh.rest.issues.removeAssignees({ ...R, issue_number: n, assignees: [owner] }); |
| 2257 | await gh.rest.issues.addAssignees({ ...R, issue_number: n, assignees: [owner] }); |
| 2258 | assert.equal((await gh.rest.issues.checkUserCanBeAssigned({ ...R, assignee: owner })).status, 204); |
| 2259 | const comment = await gh.rest.issues.createComment({ ...R, issue_number: n, body: "Same here." }); |
| 2260 | assert.equal(typeof comment.data.id, "number"); |
| 2261 | assert.equal((await gh.rest.issues.getComment({ ...R, comment_id: comment.data.id })).data.body, "Same here."); |
| 2262 | await gh.rest.issues.updateComment({ ...R, comment_id: comment.data.id, body: "Same here!" }); |
| 2263 | assert.equal((await gh.rest.issues.getComment({ ...R, comment_id: String(comment.data.id) })).data.body, "Same here!"); |
| 2264 | await gh.rest.reactions.createForIssue({ ...R, issue_number: n, content: "+1" }); |
| 2265 | const heart = await gh.rest.reactions.createForIssueComment({ ...R, comment_id: comment.data.id, content: "heart" }); |
| 2266 | assert.equal(heart.status, 201); |
| 2267 | assert.equal((await gh.rest.reactions.createForIssueComment({ ...R, comment_id: comment.data.id, content: "heart" })).status, 200); |
| 2268 | assert.deepEqual((await gh.rest.reactions.listForIssue({ ...R, issue_number: n })).data.map((x) => x.content), ["+1"]); |
| 2269 | assert.equal((await gh.rest.issues.get({ ...R, issue_number: n })).data.reactions["+1"], 1); |
| 2270 | await gh.rest.issues.lock({ ...R, issue_number: n, lock_reason: "resolved" }); |
| 2271 | const locked = await gh.rest.issues.get({ ...R, issue_number: n }); |
| 2272 | assert.equal(locked.data.locked, true); |
| 2273 | assert.equal(locked.data.active_lock_reason, "resolved"); |
| 2274 | await gh.rest.issues.unlock({ ...R, issue_number: n }); |
| 2275 | await gh.rest.issues.update({ ...R, issue_number: n, title: "Crash on startup", state: "closed", state_reason: "completed" }); |
| 2276 | const events = (await gh.rest.issues.listEvents({ ...R, issue_number: n })).data.map((e) => e.event); |
| 2277 | for (const e of ["labeled", "unlabeled", "unassigned", "assigned", "locked", "unlocked", "renamed", "closed"]) assert.ok(events.includes(e), `${e} in ${events}`); |
| 2278 | const timeline = (await gh.rest.issues.listEventsForTimeline({ ...R, issue_number: n })).data; |
| 2279 | assert.ok(timeline.some((e) => e.event === "commented" && e.body === "Same here!" && e.id === comment.data.id)); |
| 2280 | assert.ok((await gh.rest.issues.listCommentsForRepo(R)).data.some((x) => x.id === comment.data.id)); |
| 2281 | await gh.rest.issues.deleteComment({ ...R, comment_id: comment.data.id }); |
| 2282 | await assert.rejects(gh.rest.issues.getComment({ ...R, comment_id: comment.data.id }), (e) => e.status === 404); |
| 2283 | |
| 2284 | for (let i = 0; i < 3; i++) await gh.rest.issues.create({ ...R, title: `Paged ${i}` }); |
| 2285 | const page1 = await gh.rest.issues.listForRepo({ ...R, state: "all", per_page: 2 }); |
| 2286 | assert.equal(page1.data.length, 2); |
| 2287 | assert.match(page1.headers.link ?? "", /rel="next"/); |
| 2288 | const all = await gh.paginate(gh.rest.issues.listForRepo, { ...R, state: "all", per_page: 2 }); |
| 2289 | assert.equal(new Set(all.map((x) => x.number)).size, all.length); |
| 2290 | assert.ok(all.length >= 5, `paginated ${all.length} issues and pull requests`); |
| 2291 | |
| 2292 | // ------------------------------------------------------------ search, over readable repositories only |
| 2293 | const found = await until("issue search", async () => { |
| 2294 | const s = await gh.rest.search.issuesAndPullRequests({ q: `repo:${owner}/${repo} is:issue label:bug crash` }); |
| 2295 | return s.data.total_count ? s.data : null; |
| 2296 | }); |
| 2297 | assert.deepEqual(found.items.map((x) => x.number), [n]); |
| 2298 | assert.equal((await gh.rest.search.issuesAndPullRequests({ q: `repo:${owner}/${repo} is:pr is:merged` })).data.items[0].number, pr.data.number); |
| 2299 | assert.equal((await gh.rest.search.issuesAndPullRequests({ q: `repo:${owner}/${repo} is:issue -label:bug Paged`, sort: "created", order: "asc" })).data.items.map((x) => x.title).join(","), "Paged 0,Paged 1,Paged 2"); |
| 2300 | await gh.rest.repos.replaceAllTopics({ ...R, names: ["octokit", "e2e"] }); |
| 2301 | assert.deepEqual((await gh.rest.repos.getAllTopics(R)).data.names, ["octokit", "e2e"]); |
| 2302 | const repos = await gh.rest.search.repos({ q: `topic:octokit user:${owner}` }); |
| 2303 | assert.deepEqual(repos.data.items.map((x) => x.full_name), [`${owner}/${repo}`]); |
| 2304 | assert.ok("TypeScript" in (await gh.rest.repos.listLanguages(R)).data); |
| 2305 | |
| 2306 | // ------------------------------------------------------------ deployments and environments |
| 2307 | const env = await gh.rest.repos.createOrUpdateEnvironment({ ...R, environment_name: "staging" }); |
| 2308 | assert.equal(env.data.name, "staging"); |
| 2309 | const dep = await gh.rest.repos.createDeployment({ ...R, ref: "main", environment: "staging", auto_merge: false, required_contexts: [], payload: { region: "eu" }, description: "Deploy to staging" }); |
| 2310 | assert.equal(dep.status, 201); |
| 2311 | assert.equal(dep.data.sha, (await gh.rest.git.getRef({ ...R, ref: "heads/main" })).data.object.sha); |
| 2312 | assert.deepEqual(dep.data.payload, { region: "eu" }); |
| 2313 | await gh.rest.repos.createDeploymentStatus({ ...R, deployment_id: dep.data.id, state: "in_progress" }); |
| 2314 | const ds = await gh.rest.repos.createDeploymentStatus({ ...R, deployment_id: dep.data.id, state: "success", environment_url: "https://staging.example.com", log_url: "https://ci.example.com/deploy/1" }); |
| 2315 | assert.equal(ds.data.state, "success"); |
| 2316 | assert.deepEqual((await gh.rest.repos.listDeploymentStatuses({ ...R, deployment_id: dep.data.id })).data.map((x) => x.state), ["success", "in_progress"]); |
| 2317 | assert.equal((await gh.rest.repos.listDeployments({ ...R, environment: "staging" })).data.length, 1); |
| 2318 | assert.ok((await gh.rest.repos.getAllEnvironments(R)).data.environments.some((e) => e.name === "staging")); |
| 2319 | await gh.rest.actions.createEnvironmentVariable({ ...R, environment_name: "staging", name: "REGION", value: "eu" }); |
| 2320 | assert.equal((await gh.rest.actions.getEnvironmentVariable({ ...R, environment_name: "staging", name: "REGION" })).data.value, "eu"); |
| 2321 | // The environment page sets deployment branches, their patterns and the environment's secrets. |
| 2322 | const envUrl = `${base}/${owner}/${repo}/settings/environments/staging`; |
| 2323 | const envForm = async (path, fields) => { |
| 2324 | const pageRes = await fetch(envUrl); |
| 2325 | const html = await pageRes.text(); |
| 2326 | const r = await fetch(`${envUrl}${path}`, { |
| 2327 | method: "POST", |
| 2328 | body: new URLSearchParams({ _csrf: /name="_csrf" value="([^"]+)"/.exec(html)[1], ...fields }), |
| 2329 | redirect: "manual", |
| 2330 | headers: { cookie: `dfy_csrf=${/dfy_csrf=([^;]+)/.exec(pageRes.headers.get("set-cookie") ?? "")[1]}`, "sec-fetch-site": "same-origin" }, |
| 2331 | }); |
| 2332 | assert.equal(r.status, 303, await r.text()); |
| 2333 | return html; |
| 2334 | }; |
| 2335 | const envState = async () => (await gh.request("GET /repos/{owner}/{repo}/environments/{environment_name}", { ...R, environment_name: "staging" })).data.deployment_branch_policy; |
| 2336 | const envSecrets = async () => (await gh.request("GET /repos/{owner}/{repo}/environments/{environment_name}/secrets", { ...R, environment_name: "staging" })).data.secrets.map((x) => x.name); |
| 2337 | assert.match(await envForm("/secrets", { name: "deploy_key", value: "env-s3cret" }), /<input type="hidden" name="branch_policy" value="all"\/>/); |
| 2338 | assert.deepEqual(await envSecrets(), ["DEPLOY_KEY"]); |
| 2339 | assert.deepEqual(await envState(), { protected_branches: true, custom_branch_policies: false }, "the first secret limits the environment to protected branches"); |
| 2340 | await envForm("", { reviewers: "", wait_timer: "0", branch_policy: "selected" }); |
| 2341 | assert.deepEqual(await envState(), { protected_branches: false, custom_branch_policies: true }); |
| 2342 | await envForm("/branch-policies", { name: "release/*", type: "tag" }); |
| 2343 | const patterns = (await gh.request("GET /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies", { ...R, environment_name: "staging" })).data.branch_policies; |
| 2344 | assert.deepEqual(patterns.map((x) => [x.name, x.type]), [["release/*", "tag"]]); |
| 2345 | const envHtml = await envForm("/branch-policies/delete", { id: String(patterns[0].id) }); |
| 2346 | assert.match(envHtml, /release\/\*/); |
| 2347 | assert.match(envHtml, /DEPLOY_KEY/); |
| 2348 | assert.doesNotMatch(envHtml, /env-s3cret/); |
| 2349 | assert.equal((await gh.request("GET /repos/{owner}/{repo}/environments/{environment_name}/deployment-branch-policies", { ...R, environment_name: "staging" })).data.total_count, 0); |
| 2350 | await envForm("", { reviewers: "", wait_timer: "0", branch_policy: "all" }); |
| 2351 | assert.equal(await envState(), null); |
| 2352 | await envForm("/secrets/delete", { name: "DEPLOY_KEY" }); |
| 2353 | assert.deepEqual(await envSecrets(), []); |
| 2354 | await gh.rest.repos.deleteAnEnvironment({ ...R, environment_name: "staging" }); |
| 2355 | |
| 2356 | // ------------------------------------------------------------ repository_dispatch and repository variables |
| 2357 | assert.equal((await gh.rest.repos.createDispatchEvent({ ...R, event_type: "deploy", client_payload: { env: "prod" } })).status, 204); |
| 2358 | await gh.rest.actions.createRepoVariable({ ...R, name: "COLOR", value: "blue" }); |
| 2359 | await gh.rest.actions.updateRepoVariable({ ...R, name: "COLOR", value: "green" }); |
| 2360 | assert.equal((await gh.rest.actions.getRepoVariable({ ...R, name: "COLOR" })).data.value, "green"); |
| 2361 | }); |
| 2362 | |
| 2363 | /** A small opaque RGBA PNG (a gradient), built by hand so the test needs no image tools. */ |
| 2364 | function pngGradient(width, height) { |
| 2365 | const chunk = (type, body) => { |
| 2366 | const out = Buffer.alloc(12 + body.length); |
| 2367 | out.writeUInt32BE(body.length, 0); |
| 2368 | out.write(type, 4, "latin1"); |
| 2369 | body.copy(out, 8); |
| 2370 | out.writeUInt32BE(crc32(out.subarray(4, 8 + body.length)), 8 + body.length); |
| 2371 | return out; |
| 2372 | }; |
| 2373 | const ihdr = Buffer.alloc(13); |
| 2374 | ihdr.writeUInt32BE(width, 0); |
| 2375 | ihdr.writeUInt32BE(height, 4); |
| 2376 | ihdr.set([8, 6, 0, 0, 0], 8); |
| 2377 | const raw = Buffer.alloc((width * 4 + 1) * height); |
| 2378 | for (let y = 0; y < height; y++) |
| 2379 | for (let x = 0; x < width; x++) raw.set([(x * 255) / width, (y * 255) / height, 128, 255], y * (width * 4 + 1) + 1 + x * 4); |
| 2380 | return Buffer.concat([Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]), chunk("IHDR", ihdr), chunk("IDAT", deflateSync(raw)), chunk("IEND", Buffer.alloc(0))]); |
| 2381 | } |
| 2382 | |
| 2383 | /** Width and height of a lossy or extended WebP, or null when it is not one. */ |
| 2384 | function webpSize(b) { |
| 2385 | if (b.toString("latin1", 0, 4) !== "RIFF" || b.toString("latin1", 8, 12) !== "WEBP") return null; |
| 2386 | const chunk = b.toString("latin1", 12, 16); |
| 2387 | if (chunk === "VP8 ") return { width: b.readUInt16LE(26) & 0x3fff, height: b.readUInt16LE(28) & 0x3fff }; |
| 2388 | if (chunk === "VP8X") return { width: b.readUIntLE(24, 3) + 1, height: b.readUIntLE(27, 3) + 1 }; |
| 2389 | return null; |
| 2390 | } |
| 2391 | |
| 2392 | test("people and profiles: visibility, repository invitations, blank names and usernames", async () => { |
| 2393 | const me = await ok("GET", "/user"); |
| 2394 | // A form post as the signed-in dev user, with the CSRF double-submit pair taken from `page`. |
| 2395 | const submit = async (page, action, fields) => { |
| 2396 | const res = await fetch(`${base}${page}`); |
| 2397 | const csrf = /name="_csrf" value="([^"]+)"/.exec(await res.text())?.[1]; |
| 2398 | const cookie = /dfy_csrf=([^;]+)/.exec(res.headers.get("set-cookie") ?? "")?.[1]; |
| 2399 | assert.ok(csrf && cookie, `no CSRF pair on ${page}`); |
| 2400 | return fetch(`${base}${action}`, { method: "POST", body: new URLSearchParams({ _csrf: csrf, ...fields }), redirect: "manual", headers: { cookie: `dfy_csrf=${cookie}`, "sec-fetch-site": "same-origin" } }); |
| 2401 | }; |
| 2402 | const flashOf = (res) => decodeURIComponent(decodeURIComponent(res.headers.get("set-cookie") ?? "")); |
| 2403 | |
| 2404 | // "internal" is gone: creating and updating refuse it the same way. |
| 2405 | const created = await api("POST", "/user/repos", { name: "people-rules", visibility: "internal" }); |
| 2406 | assert.equal(created.status, 422); |
| 2407 | await ok("POST", "/user/repos", { name: "people-rules" }); |
| 2408 | const patched = await api("PATCH", `/repos/${me.login}/people-rules`, { visibility: "internal" }); |
| 2409 | assert.equal(patched.status, 422); |
| 2410 | assert.equal(created.json.message, patched.json.message); |
| 2411 | assert.deepEqual(created.json.errors, patched.json.errors); |
| 2412 | |
| 2413 | // Inviting the same email to a repository twice (any letter case) is refused. |
| 2414 | const access = `/${me.login}/people-rules/settings/access`; |
| 2415 | const first = await submit(access, `${access}/people`, { who: "newcomer@example.com", role: "read" }); |
| 2416 | assert.equal(first.status, 200, flashOf(first)); |
| 2417 | assert.match(await first.text(), /newcomer@example\.com/); |
| 2418 | const second = await submit(access, `${access}/people`, { who: "NewComer@Example.com", role: "write" }); |
| 2419 | assert.equal(second.status, 303); |
| 2420 | assert.match(flashOf(second), /already has a pending invitation/); |
| 2421 | |
| 2422 | // ...and it can be revoked: on the access page, in the API and over MCP. Then the address can be invited again. |
| 2423 | const mcp = async (name, args) => { |
| 2424 | const r = await fetch(`${base}/mcp`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name, arguments: args } }) }); |
| 2425 | const { result } = await r.json(); |
| 2426 | assert.equal(result.isError, false, result.content[0].text); |
| 2427 | return result.structuredContent; |
| 2428 | }; |
| 2429 | const repoInvites = async () => ok("GET", `/repos/${me.login}/people-rules/invitations`); |
| 2430 | const [pending] = await repoInvites(); |
| 2431 | assert.equal(pending.email, "newcomer@example.com"); |
| 2432 | assert.match(await (await fetch(`${base}${access}`)).text(), new RegExp(`action="${access}/invites/${pending.id}/revoke"`)); |
| 2433 | const revoked = await submit(access, `${access}/invites/${pending.id}/revoke`, {}); |
| 2434 | assert.match(flashOf(revoked), /Invitation revoked/); |
| 2435 | assert.deepEqual(await repoInvites(), []); |
| 2436 | assert.equal((await submit(access, `${access}/people`, { who: "newcomer@example.com", role: "read" })).status, 200); |
| 2437 | const [again] = await repoInvites(); |
| 2438 | assert.equal((await api("DELETE", `/repos/${me.login}/people-rules/invitations/${again.id}`)).status, 204); |
| 2439 | assert.equal((await api("DELETE", `/repos/${me.login}/people-rules/invitations/${again.id}`)).status, 404); |
| 2440 | await submit(access, `${access}/people`, { who: "newcomer@example.com", role: "read" }); |
| 2441 | const listed = (await mcp("list_invitations", { owner: me.login, repo: "people-rules" })).result; |
| 2442 | assert.deepEqual(listed.map((i) => i.email), ["newcomer@example.com"]); |
| 2443 | assert.equal((await mcp("revoke_invitation", { owner: me.login, repo: "people-rules", invitation_id: listed[0].id })).revoked, true); |
| 2444 | assert.deepEqual(await repoInvites(), []); |
| 2445 | |
| 2446 | // Organization and site invitations too; each revoke only reaches its own kind. |
| 2447 | await ok("POST", "/user/orgs", { login: "revokers" }); |
| 2448 | const orgInvite = await ok("POST", "/orgs/revokers/invitations", { email: "joiner@example.com" }); |
| 2449 | const siteInvite = await ok("POST", "/invitations", { email: "visitor@example.com" }); |
| 2450 | assert.deepEqual((await ok("GET", "/orgs/revokers/invitations")).map((i) => i.id), [orgInvite.id]); |
| 2451 | const site = await ok("GET", "/invitations"); |
| 2452 | assert.ok(site.some((i) => i.id === siteInvite.id) && !site.some((i) => i.id === orgInvite.id)); |
| 2453 | assert.equal((await api("DELETE", `/invitations/${orgInvite.id}`)).status, 404); |
| 2454 | assert.equal((await api("DELETE", `/orgs/revokers/invitations/${siteInvite.id}`)).status, 404); |
| 2455 | assert.equal((await api("DELETE", `/orgs/revokers/invitations/${orgInvite.id}`)).status, 204); |
| 2456 | await ok("POST", "/orgs/revokers/invitations", { email: "joiner@example.com" }); |
| 2457 | const [orgAgain] = (await mcp("list_invitations", { org: "revokers" })).result; |
| 2458 | assert.equal((await mcp("revoke_invitation", { org: "revokers", invitation_id: orgAgain.id })).revoked, true); |
| 2459 | assert.match(await (await fetch(`${base}/settings/admin`)).text(), new RegExp(`/settings/admin/invites/${siteInvite.id}/revoke`)); |
| 2460 | assert.match(flashOf(await submit("/settings/admin", `/settings/admin/invites/${siteInvite.id}/revoke`, {})), /Invitation revoked/); |
| 2461 | assert.ok(!(await ok("GET", "/invitations")).some((i) => i.id === siteInvite.id)); |
| 2462 | |
| 2463 | // A blank display name falls back to the username. |
| 2464 | const saved = await submit("/settings", "/settings/profile", { login: me.login, name: " " }); |
| 2465 | assert.match(flashOf(saved), /Profile saved\./); |
| 2466 | assert.equal((await ok("GET", `/users/${me.login}`)).name, null); |
| 2467 | assert.match(await (await fetch(`${base}/${me.login}`)).text(), new RegExp(`<h1[^>]*>${me.login}</h1>`)); |
| 2468 | |
| 2469 | // Usernames: the browser's pattern and the server agree that dots aren't allowed. |
| 2470 | const settings = await (await fetch(`${base}/settings`)).text(); |
| 2471 | const pattern = /name="login"[^>]*pattern="([^"]+)"/.exec(settings)?.[1]; |
| 2472 | assert.ok(pattern, "the username field has a pattern"); |
| 2473 | assert.equal(new RegExp(`^(?:${pattern.replaceAll("&", "&")})$`, "v").test("dot.name"), false); |
| 2474 | assert.equal((await api("POST", "/user/orgs", { login: "dot.name" })).status, 422); |
| 2475 | }); |
| 2476 | |
| 2477 | test("profile pictures: upload, resize, serve, cache headers and removal", async () => { |
| 2478 | const me = await ok("GET", "/user"); |
| 2479 | const avatar = async (login, query = "") => { |
| 2480 | const r = await fetch(`${base}/api/avatars/${login}${query}`); |
| 2481 | assert.equal(r.status, 200); |
| 2482 | return { type: r.headers.get("content-type"), cache: r.headers.get("cache-control"), csp: r.headers.get("content-security-policy"), body: Buffer.from(await r.arrayBuffer()) }; |
| 2483 | }; |
| 2484 | const put = (path, body, tok = token) => fetch(`${base}/api/v3${path}`, { method: "PUT", headers: { authorization: `Bearer ${tok}`, "content-type": "image/png" }, body }); |
| 2485 | |
| 2486 | // Before any upload: generated initials, also for logins that don't exist. |
| 2487 | const initials = await avatar(me.login, "?s=64"); |
| 2488 | assert.match(initials.type, /^image\/svg\+xml/); |
| 2489 | assert.equal(initials.csp, "default-src 'none'; style-src 'unsafe-inline'"); |
| 2490 | assert.match((await avatar("no-such-user-here")).type, /^image\/svg\+xml/); |
| 2491 | |
| 2492 | // REST upload: a 600×400 PNG becomes centred square WebPs. |
| 2493 | const up = await put("/user/avatar", pngGradient(600, 400)); |
| 2494 | assert.equal(up.status, 200, await up.clone().text()); |
| 2495 | const { avatar_url } = await up.json(); |
| 2496 | const version = new URL(avatar_url).searchParams.get("v"); |
| 2497 | assert.match(version ?? "", /^\d+$/); |
| 2498 | for (const s of [64, 128, 256]) { |
| 2499 | const a = await avatar(me.login, `?s=${s}&v=${version}`); |
| 2500 | assert.equal(a.type, "image/webp"); |
| 2501 | assert.deepEqual(webpSize(a.body), { width: s, height: s }); |
| 2502 | assert.ok(a.body.length < 10_000, `${s}px WebP is ${a.body.length} bytes`); |
| 2503 | assert.match(a.cache, /immutable/); |
| 2504 | } |
| 2505 | const current = await avatar(me.login, "?s=64"); |
| 2506 | assert.equal(current.type, "image/webp"); |
| 2507 | assert.match(current.cache, /max-age=600/); |
| 2508 | |
| 2509 | // Only JPEG, PNG and WebP, sniffed from the bytes. |
| 2510 | const gif = await put("/user/avatar", Buffer.from("GIF89a\x01\x00\x01\x00\x00\x00\x00;", "latin1")); |
| 2511 | assert.equal(gif.status, 422); |
| 2512 | assert.equal((await gif.json()).message, "Use a JPEG, PNG or WebP image."); |
| 2513 | // Read-only tokens can't change a profile. |
| 2514 | const ro = (await ok("POST", "/diffinery/tokens", { name: "ro", scopes: ["read"] }, null)).token; |
| 2515 | assert.equal((await put("/user/avatar", pngGradient(8, 8), ro)).status, 403); |
| 2516 | |
| 2517 | // The web form (multipart, CSRF double-submit) takes the original file and the server resizes it. |
| 2518 | const page = await fetch(`${base}/settings`); |
| 2519 | const csrf = /name="_csrf" value="([^"]+)"/.exec(await page.text())?.[1]; |
| 2520 | const cookie = /dfy_csrf=([^;]+)/.exec(page.headers.get("set-cookie") ?? "")?.[1]; |
| 2521 | assert.ok(csrf && cookie); |
| 2522 | const form = new FormData(); |
| 2523 | form.set("_csrf", csrf); |
| 2524 | form.set("avatar", new Blob([pngGradient(300, 500)], { type: "image/png" }), "me.png"); |
| 2525 | const posted = await fetch(`${base}/settings/avatar`, { method: "POST", body: form, redirect: "manual", headers: { cookie: `dfy_csrf=${cookie}`, "sec-fetch-site": "same-origin" } }); |
| 2526 | assert.equal(posted.status, 303); |
| 2527 | assert.match(decodeURIComponent(decodeURIComponent(posted.headers.get("set-cookie") ?? "")), /"kind":"notice","message":"Profile picture updated\."/); |
| 2528 | const settings = await (await fetch(`${base}/settings`)).text(); |
| 2529 | const newVersion = /\/api\/avatars\/[^"?]+\?s=256&v=(\d+)/.exec(settings)?.[1]; |
| 2530 | assert.ok(newVersion && newVersion !== version, "the settings page links the new version"); |
| 2531 | // An old version's URL gets the current picture, but not as an immutable response. |
| 2532 | assert.match((await avatar(me.login, `?s=64&v=${version}`)).cache, /max-age=600/); |
| 2533 | |
| 2534 | // Organizations: owners (admins get a 403; see the security tests). |
| 2535 | await ok("POST", "/user/orgs", { login: "picture-org" }); |
| 2536 | const org = await put("/orgs/picture-org/avatar", pngGradient(64, 64)); |
| 2537 | assert.equal(org.status, 200, await org.clone().text()); |
| 2538 | assert.equal((await avatar("picture-org", "?s=128")).type, "image/webp"); |
| 2539 | assert.equal((await fetch(`${base}/api/v3/orgs/picture-org/avatar`, { method: "DELETE", headers: { authorization: `Bearer ${token}` } })).status, 200); |
| 2540 | assert.match((await avatar("picture-org", "?s=128")).type, /^image\/svg\+xml/); |
| 2541 | |
| 2542 | // Removal falls back to initials straight away (the cached copies are purged). |
| 2543 | const del = await fetch(`${base}/api/v3/user/avatar`, { method: "DELETE", headers: { authorization: `Bearer ${token}` } }); |
| 2544 | assert.deepEqual(await del.json(), { avatar_url: `${base}/api/avatars/${me.login}` }); |
| 2545 | assert.match((await avatar(me.login, "?s=64")).type, /^image\/svg\+xml/); |
| 2546 | }); |
| 2547 | |
| 2548 | // ---------------------------------------------------------------- GitHub imports |
| 2549 | |
| 2550 | /** Build the fake GitHub's repositories with real git: history with a merged pull request, a draft, a fork head, tags, LFS and an old secret. */ |
| 2551 | function githubFixture(root) { |
| 2552 | const g = (cwd, ...args) => execFileSync("git", args, { cwd, encoding: "utf8", env: { ...process.env, GIT_AUTHOR_NAME: "Octo", GIT_AUTHOR_EMAIL: "octo@example.com", GIT_COMMITTER_NAME: "Octo", GIT_COMMITTER_EMAIL: "octo@example.com" } }).trim(); |
| 2553 | const src = join(root, "work-app"); |
| 2554 | mkdirSync(join(src, ".github/workflows"), { recursive: true }); |
| 2555 | g(root, "init", "-q", "-b", "main", src); |
| 2556 | const asset = lfsObject(`binary asset ${randomBytes(8).toString("hex")}\n`); |
| 2557 | writeFileSync(join(src, "README.md"), "# app\n"); |
| 2558 | writeFileSync(join(src, ".gitattributes"), "*.bin filter=lfs diff=lfs merge=lfs -text\n"); |
| 2559 | writeFileSync(join(src, "asset.bin"), asset.pointer); |
| 2560 | // Would start a run on every push if imports fired workflows. |
| 2561 | writeFileSync(join(src, ".github/workflows/ci.yml"), "name: CI\non: [push, pull_request]\njobs:\n build:\n runs-on: [self-hosted, linux]\n steps:\n - run: echo hi\n"); |
| 2562 | // A credential committed long ago (built here so this file has none). |
| 2563 | writeFileSync(join(src, "settings.env"), `AWS_ACCESS_KEY_ID=${"AKIA"}${"Q3EGRK5V7XJZ2MWN"}\n`); |
| 2564 | g(src, "add", "-A"); |
| 2565 | g(src, "-c", "core.hooksPath=/dev/null", "commit", "-q", "-m", "initial"); |
| 2566 | g(src, "tag", "v0.1"); |
| 2567 | g(src, "rm", "-q", "settings.env"); |
| 2568 | g(src, "commit", "-q", "-m", "remove credentials"); |
| 2569 | // Enough data that a small push budget needs several chunks. |
| 2570 | for (let i = 0; i < 4; i++) { |
| 2571 | writeFileSync(join(src, `data${i}.bin.txt`), randomBytes(90 * 1024).toString("base64")); |
| 2572 | g(src, "add", "-A"); |
| 2573 | g(src, "commit", "-q", "-m", `data ${i}`); |
| 2574 | } |
| 2575 | g(src, "checkout", "-q", "-b", "feature"); |
| 2576 | writeFileSync(join(src, "feature.txt"), "feature\n"); |
| 2577 | g(src, "add", "-A"); |
| 2578 | g(src, "commit", "-q", "-m", "feature"); |
| 2579 | const featureSha = g(src, "rev-parse", "HEAD"); |
| 2580 | g(src, "checkout", "-q", "main"); |
| 2581 | g(src, "merge", "-q", "--no-ff", "-m", "Merge pull request #2 from octo/feature", "feature"); |
| 2582 | const mergeSha = g(src, "rev-parse", "HEAD"); |
| 2583 | g(src, "tag", "-a", "v1.0", "-m", "Version 1.0"); |
| 2584 | g(src, "checkout", "-q", "-b", "draft-work"); |
| 2585 | writeFileSync(join(src, "draft.txt"), "wip\n"); |
| 2586 | g(src, "add", "-A"); |
| 2587 | g(src, "commit", "-q", "-m", "draft"); |
| 2588 | const draftSha = g(src, "rev-parse", "HEAD"); |
| 2589 | // A fork's commit: only GitHub's refs/pull/6/head points at it. |
| 2590 | g(src, "checkout", "-q", "--detach", "main"); |
| 2591 | writeFileSync(join(src, "fork.txt"), "from a fork\n"); |
| 2592 | g(src, "add", "-A"); |
| 2593 | g(src, "commit", "-q", "-m", "fork change"); |
| 2594 | const forkSha = g(src, "rev-parse", "HEAD"); |
| 2595 | g(src, "checkout", "-q", "main"); |
| 2596 | const bare = join(root, "octo", "app.git"); |
| 2597 | mkdirSync(join(root, "octo"), { recursive: true }); |
| 2598 | g(root, "clone", "-q", "--bare", src, bare); |
| 2599 | g(bare, "update-ref", "refs/pull/2/head", featureSha); |
| 2600 | g(bare, "update-ref", "refs/pull/2/merge", mergeSha); |
| 2601 | g(bare, "update-ref", "refs/pull/5/head", draftSha); |
| 2602 | g(bare, "update-ref", "refs/pull/6/head", forkSha); |
| 2603 | g(bare, "update-ref", "refs/heads/gh-readonly-queue/main/pr-9-0123456789abcdef", mergeSha); |
| 2604 | // A second repository for the organization import. |
| 2605 | const lib = join(root, "work-lib"); |
| 2606 | g(root, "init", "-q", "-b", "trunk", lib); |
| 2607 | writeFileSync(join(lib, "lib.txt"), "lib\n"); |
| 2608 | g(lib, "add", "-A"); |
| 2609 | g(lib, "commit", "-q", "-m", "lib"); |
| 2610 | g(root, "clone", "-q", "--bare", lib, join(root, "octo", "lib.git")); |
| 2611 | return { asset, featureSha, mergeSha, draftSha, forkSha, mainSha: mergeSha }; |
| 2612 | } |
| 2613 | |
| 2614 | test("import from GitHub: git data, issues and pull requests with their numbers, no side effects", async () => { |
| 2615 | const me = await ok("GET", "/user"); |
| 2616 | const owner = me.login; |
| 2617 | const ghRoot = join(work, "github"); |
| 2618 | const fx = githubFixture(ghRoot); |
| 2619 | const GH_TOKEN = `ghfake${randomBytes(12).toString("hex")}`; |
| 2620 | const t = "2024-01-02T03:04:05Z"; |
| 2621 | const u = (login, id) => ({ login, id, type: "User" }); |
| 2622 | const octoDev = u("octo-dev", 1001); |
| 2623 | const alice = u("alice", 2001); |
| 2624 | const bob = u("bob", 3001); |
| 2625 | let ghBase; |
| 2626 | let linkNonce = ""; // set from the link challenge, then served back in octo-dev's public gist |
| 2627 | let listHuge = false; // the organization lists an oversized repository once set |
| 2628 | const pr = (number, extra) => ({ number, title: `PR ${number}`, body: `Body of #${number}`, state: "open", user: alice, labels: [], assignees: [], milestone: null, locked: false, created_at: t, updated_at: t, closed_at: null, draft: false, merged_at: null, merge_commit_sha: null, requested_reviewers: [], head: { ref: "x", sha: fx.mainSha, label: "octo:x", repo: { full_name: "octo/app" } }, base: { ref: "main", repo: { full_name: "octo/app" } }, ...extra }); |
| 2629 | const issue = (number, extra) => ({ number, title: `Issue ${number}`, body: `Body of #${number}`, state: "open", state_reason: null, user: octoDev, labels: [], assignees: [], milestone: null, locked: false, created_at: t, updated_at: t, closed_at: null, ...extra }); |
| 2630 | const pulls = [ |
| 2631 | pr(2, { title: "Add feature", state: "closed", closed_at: "2024-02-01T00:00:00Z", merged_at: "2024-02-01T00:00:00Z", merge_commit_sha: fx.mergeSha, labels: [{ name: "docs" }], head: { ref: "feature", sha: fx.featureSha, label: "octo:feature", repo: { full_name: "octo/app" } } }), |
| 2632 | pr(5, { title: "Work in progress", draft: true, requested_reviewers: [bob], head: { ref: "draft-work", sha: fx.draftSha, label: "octo:draft-work", repo: { full_name: "octo/app" } } }), |
| 2633 | pr(6, { title: "From a fork", state: "closed", closed_at: "2024-03-01T00:00:00Z", head: { ref: "patch", sha: fx.forkSha, label: "forker:patch", repo: { full_name: "forker/app" } } }), |
| 2634 | ]; |
| 2635 | const issues = [ |
| 2636 | issue(1, { title: "First bug", labels: [{ name: "bug", color: "d73a4a" }], assignees: [octoDev], milestone: { number: 1 } }), |
| 2637 | ...pulls.map((p) => ({ ...p, pull_request: { url: "x" } })), |
| 2638 | issue(3, { title: "Won't fix", user: alice, state: "closed", state_reason: "not_planned", closed_at: "2024-01-05T00:00:00Z", locked: true }), |
| 2639 | // #4 was deleted on GitHub: the number stays unused. |
| 2640 | issue(7, { title: "Latest", user: { login: "ghost-user", id: 9999 } }), |
| 2641 | ]; |
| 2642 | const repoJson = (name, extra) => ({ name, full_name: `octo/${name}`, owner: { login: "octo" }, private: true, visibility: "private", fork: false, archived: false, description: `The ${name}`, homepage: "https://app.example.com", topics: ["demo", "import"], default_branch: "main", ...extra }); |
| 2643 | const empty = []; |
| 2644 | const routes = { |
| 2645 | "GET /user": octoDev, |
| 2646 | // Gist-proof account linking: octo-dev's public profile, their gists, and the gist body with the nonce. |
| 2647 | "GET /users/octo-dev": octoDev, |
| 2648 | "GET /users/octo-dev/gists": () => [{ id: "proofgist", public: true, description: "", files: { "proof.md": { raw_url: `${ghBase}/raw/proofgist` } } }], |
| 2649 | "GET /gists/proofgist": () => ({ files: { "proof.md": { content: `linking to diffinery: ${linkNonce}` } } }), |
| 2650 | "GET /users/octo": { login: "octo", type: "Organization" }, |
| 2651 | "GET /orgs/octo/repos": () => [repoJson("app"), repoJson("lib", { default_branch: "trunk", topics: [] }), ...(listHuge ? [repoJson("huge", { size: 99_999_999 })] : [])].map((r) => ({ ...r, clone_url: `${ghBase}/${r.full_name}.git` })), |
| 2652 | "GET /repos/octo/huge": () => ({ ...repoJson("huge", { size: 99_999_999 }), clone_url: `${ghBase}/octo/huge.git` }), |
| 2653 | // Only ORG_REGION reaches every repository on GitHub; Diffinery's organization variables always do. |
| 2654 | "GET /orgs/octo/actions/variables": { variables: [{ name: "ORG_REGION", value: "eu-west", visibility: "all" }, { name: "ORG_PRIVATE_HOST", value: "internal.example", visibility: "private" }, { name: "ORG_SELECTED_KEY", value: "few", visibility: "selected" }] }, |
| 2655 | "GET /orgs/octo/actions/secrets": { secrets: [{ name: "ORG_DEPLOY_KEY" }] }, |
| 2656 | "GET /repos/octo/app": () => ({ ...repoJson("app"), clone_url: `${ghBase}/octo/app.git`, html_url: `${ghBase}/octo/app` }), |
| 2657 | "GET /repos/octo/app/labels": [{ name: "bug", color: "d73a4a", description: "Something isn't working" }, { name: "docs", color: "0075ca", description: "Documentation" }, { name: "wontfix", color: "ffffff", description: "" }], |
| 2658 | "GET /repos/octo/app/milestones": [{ number: 1, title: "v1", description: "First release", state: "open", due_on: "2030-01-01T00:00:00Z", created_at: t, updated_at: t, closed_at: null, creator: octoDev }], |
| 2659 | "GET /repos/octo/app/pulls": pulls, |
| 2660 | "GET /repos/octo/app/issues": issues, |
| 2661 | "GET /repos/octo/app/issues/comments": () => [ |
| 2662 | { id: 11, body: "I can reproduce this", user: alice, created_at: "2024-01-03T00:00:00Z", updated_at: "2024-01-03T00:00:00Z", issue_url: `${ghBase}/api/v3/repos/octo/app/issues/1` }, |
| 2663 | { id: 12, body: "Thanks!", user: octoDev, created_at: "2024-01-04T00:00:00Z", updated_at: "2024-01-04T00:00:00Z", issue_url: `${ghBase}/api/v3/repos/octo/app/issues/1` }, |
| 2664 | { id: 13, body: "Merging this", user: octoDev, created_at: "2024-01-31T00:00:00Z", updated_at: "2024-01-31T00:00:00Z", issue_url: `${ghBase}/api/v3/repos/octo/app/issues/2` }, |
| 2665 | ], |
| 2666 | "GET /repos/octo/app/pulls/2/reviews": [{ id: 501, user: bob, state: "APPROVED", body: "Looks good", commit_id: fx.featureSha, submitted_at: "2024-01-30T00:00:00Z" }], |
| 2667 | "GET /repos/octo/app/pulls/5/reviews": [{ id: 505, user: octoDev, state: "APPROVED", body: "looks fine", commit_id: fx.draftSha, submitted_at: "2024-01-20T00:00:00Z" }], |
| 2668 | "GET /repos/octo/app/pulls/6/reviews": [{ id: 502, user: alice, state: "PENDING", body: "", commit_id: fx.forkSha, submitted_at: null }], |
| 2669 | "GET /repos/octo/app/pulls/2": { ...pulls[0], merged_by: octoDev }, |
| 2670 | "GET /repos/octo/app/pulls/comments": () => [ |
| 2671 | { id: 601, pull_request_review_id: 501, pull_request_url: `${ghBase}/api/v3/repos/octo/app/pulls/2`, user: bob, body: "Nice file", path: "feature.txt", line: 1, side: "RIGHT", commit_id: fx.featureSha, created_at: "2024-01-30T00:00:00Z", updated_at: "2024-01-30T00:00:00Z" }, |
| 2672 | { id: 602, pull_request_review_id: 503, pull_request_url: `${ghBase}/api/v3/repos/octo/app/pulls/2`, in_reply_to_id: 601, user: alice, body: "Agreed", path: "feature.txt", line: 1, side: "RIGHT", commit_id: fx.featureSha, created_at: "2024-01-30T01:00:00Z", updated_at: "2024-01-30T01:00:00Z" }, |
| 2673 | ], |
| 2674 | "GET /repos/octo/app/branches": [{ name: "main" }], |
| 2675 | "GET /repos/octo/app/branches/main/protection": { required_status_checks: { contexts: ["build"] }, required_pull_request_reviews: { required_approving_review_count: 1, dismiss_stale_reviews: true }, enforce_admins: { enabled: false }, allow_force_pushes: { enabled: false } }, |
| 2676 | "GET /repos/octo/app/actions/variables": { variables: [{ name: "DEPLOY_ENV", value: "staging" }, { name: "REGION", value: "us" }] }, |
| 2677 | "GET /repos/octo/app/actions/secrets": { secrets: [{ name: "NPM_TOKEN" }, { name: "SLACK_WEBHOOK" }] }, |
| 2678 | "GET /repos/octo/app/releases": [{ id: 1, tag_name: "v1.0.0", name: "1.0.0", body: "first release", draft: false, prerelease: false, created_at: "2024-01-01T00:00:00Z", published_at: "2024-01-01T00:00:00Z", author: { login: "octocat", id: 1 }, assets: [] }], |
| 2679 | "GET /repos/octo/lib": () => ({ ...repoJson("lib", { default_branch: "trunk", topics: [] }), clone_url: `${ghBase}/octo/lib.git` }), |
| 2680 | "GET /repos/octo/lib/labels": empty, |
| 2681 | "GET /repos/octo/lib/milestones": empty, |
| 2682 | "GET /repos/octo/lib/pulls": empty, |
| 2683 | "GET /repos/octo/lib/issues": [issue(1, { title: "Lib issue" })], |
| 2684 | "GET /repos/octo/lib/issues/comments": empty, |
| 2685 | "GET /repos/octo/lib/pulls/comments": empty, |
| 2686 | "GET /repos/octo/lib/branches": empty, |
| 2687 | "GET /repos/octo/lib/actions/variables": { variables: [] }, |
| 2688 | "GET /repos/octo/lib/actions/secrets": { secrets: [] }, |
| 2689 | "GET /repos/octo/lib/releases": empty, |
| 2690 | }; |
| 2691 | const gh = fakeGitHub({ root: ghRoot, token: GH_TOKEN, routes, lfs: new Map([[fx.asset.oid, fx.asset.data]]), anonymous: ["octo/lib"] }); |
| 2692 | ghBase = await gh.listen(); |
| 2693 | const apiUrl = `${ghBase}/api/v3`; |
| 2694 | |
| 2695 | /** Run the import agent (the runner image's import mode) for an import waiting for one, like the container would. */ |
| 2696 | const transfer = async (id) => { |
| 2697 | await until(`import ${id} to wait for an agent`, async () => (await ok("GET", `/diffinery/imports/${id}`)).git?.phase === "waiting", 120_000); |
| 2698 | const { token: pushToken } = await ok("POST", `/diffinery/imports/${id}/claim`); |
| 2699 | const r = await spawnAsync(process.execPath, [AGENT], { DIFFINERY_API_URL: base, DIFFINERY_IMPORT_ID: id, DIFFINERY_IMPORT_TOKEN: pushToken, DIFFINERY_IMPORT_CHUNK_BYTES: String(160 * 1024), DIFFINERY_IMPORT_LARGE_BLOB_BYTES: String(100 * 1024), DIFFINERY_WORK_ROOT: join(work, `import-${id}`) }, 240_000); |
| 2700 | const out = `${r.stdout}${r.stderr}`; |
| 2701 | assert.ok(!out.includes(GH_TOKEN) && !out.includes(pushToken), "the agent never prints tokens"); |
| 2702 | return { ...r, out, pushToken }; |
| 2703 | }; |
| 2704 | const finished = (id, timeout = 180_000) => |
| 2705 | until(`import ${id} to finish`, async () => { |
| 2706 | const r = await ok("GET", `/diffinery/imports/${id}`); |
| 2707 | return ["succeeded", "failed", "cancelled"].includes(r.status) ? r : null; |
| 2708 | }, timeout); |
| 2709 | |
| 2710 | // ------------------------------------------------------------ people: link a GitHub account by gist proof (no token) |
| 2711 | const challenge = await ok("POST", "/diffinery/user/linked-accounts/github/challenge", { api_url: apiUrl }); |
| 2712 | assert.match(challenge.nonce, /^diffinery-link-[0-9a-f]+$/); |
| 2713 | // Before the gist carries the nonce, linking is refused. |
| 2714 | assert.equal((await api("POST", "/diffinery/user/linked-accounts/github", { login: "octo-dev", api_url: apiUrl })).status, 422); |
| 2715 | linkNonce = challenge.nonce; // now octo-dev's public gist contains it |
| 2716 | const link = await ok("POST", "/diffinery/user/linked-accounts/github", { login: "octo-dev", api_url: apiUrl }); |
| 2717 | assert.equal(link.login, "octo-dev"); |
| 2718 | |
| 2719 | // ------------------------------------------------------------ input validation and permissions |
| 2720 | assert.equal((await api("POST", "/diffinery/imports", { source: "octo/app", owner, options: { nope: true }, github_api_url: apiUrl })).status, 422); |
| 2721 | assert.equal((await api("POST", "/diffinery/imports", { source: "octo/app", owner: "nobody-here", github_api_url: apiUrl })).status, 422); |
| 2722 | assert.equal((await api("POST", "/diffinery/imports", { source: "octo/app", owner, github_token: "wrong", github_api_url: apiUrl })).status, 422); |
| 2723 | const bot = (await ok("POST", "/diffinery/agents", { login: "import-bot" }, null)).token.token; |
| 2724 | // An agent may import where its owner may, its owner's own account included: this one gets past |
| 2725 | // the permission check and stops only because the GitHub repository does not exist. |
| 2726 | const botTry = await api("POST", "/diffinery/imports", { source: "octo/missing", owner, github_token: GH_TOKEN, github_api_url: apiUrl }, bot); |
| 2727 | assert.equal(botTry.status, 422, JSON.stringify(botTry.json)); |
| 2728 | assert.match(botTry.json.message, /octo\/missing was not found/); |
| 2729 | |
| 2730 | // ------------------------------------------------------------ one repository |
| 2731 | const started = await ok("POST", "/diffinery/imports", { source: "octo/app", owner, name: "imported-app", github_token: GH_TOKEN, github_api_url: apiUrl }); |
| 2732 | assert.equal(started.status, "queued"); |
| 2733 | assert.ok(!JSON.stringify(started).includes(GH_TOKEN), "the token is never returned"); |
| 2734 | // While the import holds the repository it created, nothing else writes to it but settings such as |
| 2735 | // secrets: the REST API, the web UI and MCP get 423 Locked (reads still work). The import's own |
| 2736 | // writes go through below. |
| 2737 | await until(`import ${started.id} to wait for an agent`, async () => (await ok("GET", `/diffinery/imports/${started.id}`)).git?.phase === "waiting", 120_000); |
| 2738 | const L = `/repos/${owner}/imported-app`; |
| 2739 | for (const [method, path, body] of [ |
| 2740 | ["POST", `${L}/hooks`, { config: { url: "https://hooks.invalid/import-sink" }, events: ["*"] }], |
| 2741 | ["POST", `${L}/issues`, { title: "too early" }], |
| 2742 | ["PUT", `${L}/contents/early.txt`, { message: "too early", content: Buffer.from("early\n").toString("base64") }], |
| 2743 | ["POST", `${L}/labels`, { name: "early", color: "ffffff" }], |
| 2744 | ["PATCH", L, { description: "changed during the import" }], |
| 2745 | ]) { |
| 2746 | const r = await api(method, path, body); |
| 2747 | assert.equal(r.status, 423, `${method} ${path} -> ${r.status} ${JSON.stringify(r.json)}`); |
| 2748 | assert.match(r.json.message, /locked by an import/); |
| 2749 | } |
| 2750 | assert.equal((await api("GET", `${L}/issues`)).status, 200, "reads are not locked"); |
| 2751 | // Settings that don't touch Git data, issues or pull requests can be set up while it imports. |
| 2752 | assert.equal((await api("PUT", `${L}/actions/secrets/EARLY_SECRET`, { value: "set during the import" })).status, 204); |
| 2753 | assert.ok((await ok("GET", `${L}/actions/secrets`)).secrets.some((x) => x.name === "EARLY_SECRET")); |
| 2754 | assert.equal((await api("DELETE", `${L}/actions/secrets/EARLY_SECRET`)).status, 204); |
| 2755 | const csrf = randomBytes(16).toString("hex"); |
| 2756 | const webWrite = await fetch(`${base}/${owner}/imported-app/issues`, { method: "POST", headers: { cookie: `dfy_csrf=${csrf}`, "content-type": "application/x-www-form-urlencoded" }, body: new URLSearchParams({ _csrf: csrf, title: "too early" }), redirect: "manual" }); |
| 2757 | assert.equal(webWrite.status, 423, await webWrite.text()); |
| 2758 | const mcpWrite = await fetch(`${base}/mcp`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/json", accept: "application/json, text/event-stream" }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "tools/call", params: { name: "create_issue", arguments: { owner, repo: "imported-app", title: "too early" } } }) }).then((r) => r.json()); |
| 2759 | assert.equal(mcpWrite.result.isError, true, JSON.stringify(mcpWrite)); |
| 2760 | assert.match(mcpWrite.result.content[0].text, /locked by an import/); |
| 2761 | const agent = await transfer(started.id); |
| 2762 | assert.equal(agent.status, 0, agent.out); |
| 2763 | const done = await finished(started.id); |
| 2764 | assert.equal(done.status, "succeeded", JSON.stringify(done, null, 2)); |
| 2765 | // No side effects: no workflow runs (ci.yml runs on push and pull_request), no webhook deliveries. |
| 2766 | // The lock is released: a webhook can be added now, and nothing the import queued reaches it. |
| 2767 | const hook = await ok("POST", `/repos/${owner}/imported-app/hooks`, { config: { url: "https://hooks.invalid/import-sink" }, events: ["*"] }); |
| 2768 | await sleep(2000); |
| 2769 | assert.equal((await ok("GET", `/repos/${owner}/imported-app/actions/runs`)).total_count, 0); |
| 2770 | const sink = (await ok("GET", `/repos/${owner}/imported-app/hooks`)).find((h) => h.id === hook.id); |
| 2771 | assert.equal(sink.last_response.code, null, "no webhook was delivered"); |
| 2772 | assert.equal(sink.last_response.status, "unused"); |
| 2773 | assert.ok(done.steps.every((s) => s.status === "done" || s.status === "skipped"), JSON.stringify(done.steps)); |
| 2774 | assert.ok(done.summary.git.chunks >= 3, `history went in chunks: ${JSON.stringify(done.summary.git)}`); |
| 2775 | // The data files count as large here (DIFFINERY_IMPORT_LARGE_BLOB_BYTES): they went first, whole, |
| 2776 | // under temporary refs naming trees, which the server takes and the import removes at the end. |
| 2777 | assert.match(agent.out, /4 version\(s\) of large files/); |
| 2778 | assert.equal(done.summary.git.branches, 3); |
| 2779 | assert.equal(done.summary.git.tags, 2); |
| 2780 | assert.equal(done.summary.git.lfsObjects, 1); |
| 2781 | assert.deepEqual(done.summary.secretNames, ["NPM_TOKEN", "SLACK_WEBHOOK"]); |
| 2782 | assert.ok(done.summary.findings.some((f) => f.kind === "AWS access key"), "bypassed findings are listed"); |
| 2783 | assert.ok(done.summary.people.imported.includes("alice") && done.summary.people.linked.includes("octo-dev")); |
| 2784 | // Releases are imported over the REST API (not just their tags). |
| 2785 | assert.equal(done.summary.releases?.imported, 1, JSON.stringify(done.summary.releases)); |
| 2786 | const releases = await ok("GET", `/repos/${owner}/imported-app/releases`); |
| 2787 | assert.deepEqual(releases.map((r) => r.tag_name), ["v1.0.0"]); |
| 2788 | |
| 2789 | // Repository settings |
| 2790 | const repo = await ok("GET", `/repos/${owner}/imported-app`); |
| 2791 | assert.equal(repo.visibility, "private"); |
| 2792 | assert.equal(repo.default_branch, "main"); |
| 2793 | assert.equal(repo.description, "The app"); |
| 2794 | assert.equal(repo.homepage, "https://app.example.com"); |
| 2795 | assert.deepEqual(repo.topics, ["demo", "import"]); |
| 2796 | |
| 2797 | // Git: branches, tags and pull request heads; no merge queue branches, merge refs or import refs. |
| 2798 | const refs = Object.fromEntries( |
| 2799 | git(work, ["ls-remote", `${base}/${owner}/imported-app.git`]) |
| 2800 | .trim() |
| 2801 | .split("\n") |
| 2802 | .map((l) => l.split("\t").reverse()), |
| 2803 | ); |
| 2804 | assert.equal(refs["refs/heads/main"], fx.mainSha); |
| 2805 | assert.equal(refs["refs/heads/feature"], fx.featureSha); |
| 2806 | assert.equal(refs["refs/heads/draft-work"], fx.draftSha); |
| 2807 | assert.ok(refs["refs/tags/v1.0"] && refs["refs/tags/v0.1"]); |
| 2808 | assert.equal(refs["refs/pull/2/head"], fx.featureSha); |
| 2809 | assert.equal(refs["refs/pull/5/head"], fx.draftSha); |
| 2810 | assert.equal(refs["refs/pull/6/head"], fx.forkSha); |
| 2811 | assert.ok(!Object.keys(refs).some((r) => r.startsWith("refs/heads/gh-readonly-queue/") || r.startsWith("refs/diffinery-import/") || r === "refs/pull/2/merge"), Object.keys(refs).join("\n")); |
| 2812 | const copy = join(work, "imported-app"); |
| 2813 | git(work, ["clone", "-q", "--mirror", `${base}/${owner}/imported-app.git`, copy], token, { env: { ...process.env, GIT_LFS_SKIP_SMUDGE: "1", GIT_TERMINAL_PROMPT: "0" } }); |
| 2814 | git(copy, ["fsck", "--no-progress"]); |
| 2815 | git(copy, ["cat-file", "-e", fx.forkSha]); |
| 2816 | // The LFS object is stored in Diffinery. |
| 2817 | const lfsRes = await fetch(`${base}/${owner}/imported-app.git/info/lfs/objects/batch`, { method: "POST", headers: { authorization: `Bearer ${token}`, "content-type": "application/vnd.git-lfs+json" }, body: JSON.stringify({ operation: "download", objects: [{ oid: fx.asset.oid, size: fx.asset.data.length }] }) }); |
| 2818 | const lfsBody = await lfsRes.json(); |
| 2819 | assert.ok(lfsBody.objects[0].actions?.download, `${JSON.stringify(lfsBody)}\n${JSON.stringify(done.warnings)}\n${agent.out}\n${devLog().split("\n").filter((l) => /lfs|rror/i.test(l)).slice(-40).join("\n")}`); |
| 2820 | const blob = await fetch(lfsBody.objects[0].actions.download.href, { headers: { authorization: `Bearer ${token}` } }); |
| 2821 | assert.equal(Buffer.from(await blob.arrayBuffer()).toString(), fx.asset.data.toString()); |
| 2822 | // The push token is revoked. |
| 2823 | assert.equal((await api("GET", "/user", undefined, agent.pushToken)).status, 401); |
| 2824 | |
| 2825 | // Issues keep their numbers, people, state and dates. |
| 2826 | const i1 = await ok("GET", `/repos/${owner}/imported-app/issues/1`); |
| 2827 | assert.equal(i1.title, "First bug"); |
| 2828 | assert.equal(i1.user.login, owner, "a linked GitHub account maps to its Diffinery account"); |
| 2829 | assert.deepEqual(i1.assignees.map((a) => a.login), [owner]); |
| 2830 | assert.deepEqual(i1.labels.map((l) => l.name), ["bug"]); |
| 2831 | assert.equal(i1.milestone.title, "v1"); |
| 2832 | assert.equal(i1.created_at, t); |
| 2833 | const i3 = await ok("GET", `/repos/${owner}/imported-app/issues/3`); |
| 2834 | assert.equal(i3.state, "closed"); |
| 2835 | assert.equal(i3.state_reason, "not_planned"); |
| 2836 | assert.equal(i3.user.login, "github:alice"); |
| 2837 | assert.equal((await api("GET", `/repos/${owner}/imported-app/issues/4`)).status, 404, "a deleted GitHub issue leaves its number unused"); |
| 2838 | const c1 = await ok("GET", `/repos/${owner}/imported-app/issues/1/comments`); |
| 2839 | assert.deepEqual(c1.map((c) => [c.user.login, c.body, c.created_at]), [["github:alice", "I can reproduce this", "2024-01-03T00:00:00Z"], [owner, "Thanks!", "2024-01-04T00:00:00Z"]]); |
| 2840 | // Pull requests: merged state, who merged, reviews and line comments with replies. |
| 2841 | const p2 = await ok("GET", `/repos/${owner}/imported-app/pulls/2`); |
| 2842 | assert.equal(p2.merged, true); |
| 2843 | assert.equal(p2.merged_by.login, owner); |
| 2844 | assert.equal(p2.merge_commit_sha, fx.mergeSha); |
| 2845 | assert.equal(p2.head.ref, "feature"); |
| 2846 | assert.equal(p2.head.sha, fx.featureSha); |
| 2847 | // Its commits and files are counted from where main was when it merged, not from main now (which contains it). |
| 2848 | assert.deepEqual([p2.commits, p2.changed_files], [1, 1]); |
| 2849 | assert.deepEqual((await ok("GET", `/repos/${owner}/imported-app/pulls/2/commits`)).map((c) => c.sha), [fx.featureSha]); |
| 2850 | assert.deepEqual((await ok("GET", `/repos/${owner}/imported-app/pulls/2/files`)).map((f) => f.filename), ["feature.txt"]); |
| 2851 | const reviews = await ok("GET", `/repos/${owner}/imported-app/pulls/2/reviews`); |
| 2852 | assert.deepEqual(reviews.map((r) => [r.user.login, r.state, r.body]), [["github:bob", "APPROVED", "Looks good"]]); |
| 2853 | const lineComments = await ok("GET", `/repos/${owner}/imported-app/pulls/2/comments`); |
| 2854 | assert.equal(lineComments.length, 2); |
| 2855 | assert.equal(lineComments[0].path, "feature.txt"); |
| 2856 | assert.equal(lineComments[1].in_reply_to_id, lineComments[0].id); |
| 2857 | const p5 = await ok("GET", `/repos/${owner}/imported-app/pulls/5`); |
| 2858 | assert.equal(p5.draft, true); |
| 2859 | assert.equal(p5.state, "open"); |
| 2860 | assert.deepEqual(p5.requested_reviewers.map((r) => r.login), ["github:bob"]); |
| 2861 | // M1: an imported review is history, not a vote — even when its author is a linked Diffinery account. |
| 2862 | const r5 = await ok("GET", `/repos/${owner}/imported-app/pulls/5/reviews`); |
| 2863 | assert.deepEqual(r5.map((r) => [r.user.login, r.state]), [[owner, "APPROVED"]], "the imported approval is kept as history"); |
| 2864 | assert.equal(p5.approvals, 0, "an imported approval does not count toward the required approvals"); |
| 2865 | assert.equal(p5.required_approvals, 1); |
| 2866 | const p6 = await ok("GET", `/repos/${owner}/imported-app/pulls/6`); |
| 2867 | assert.equal(p6.head.ref, "forker:patch"); |
| 2868 | assert.equal(p6.head.sha, fx.forkSha); |
| 2869 | assert.equal(p6.merged, false); |
| 2870 | // A pull request from a fork runs the fork's code, so its jobs get what GitHub gives them: |
| 2871 | // a read-only token and no secrets. |
| 2872 | assert.equal((await api("PUT", `/repos/${owner}/imported-app/actions/secrets/DEPLOY_KEY`, { value: "never-for-forks" })).status, 204); |
| 2873 | await ok("PATCH", `/repos/${owner}/imported-app/pulls/6`, { state: "open" }); |
| 2874 | const forkJob = await until("the fork pull request's job", async () => { |
| 2875 | const r = await runnerClaim(`/repos/${owner}/imported-app`, ["linux"]); |
| 2876 | return r.status === 200 ? r.json : null; |
| 2877 | }); |
| 2878 | const forkSpec = await (await fetch(`${base}/runner/jobs/${forkJob.job_id}`, { headers: { authorization: `Bearer ${forkJob.token}` } })).json(); |
| 2879 | assert.equal(forkSpec.github.event_name, "pull_request"); |
| 2880 | assert.deepEqual(Object.keys(forkSpec.secrets).sort(), ["DIFFINERY_TOKEN", "GITHUB_TOKEN"]); |
| 2881 | assert.equal((await api("POST", `/repos/${owner}/imported-app/issues`, { title: "from a fork's job" }, forkJob.token)).status, 403); |
| 2882 | await fetch(`${base}/runner/jobs/${forkJob.job_id}/complete`, { method: "POST", headers: { authorization: `Bearer ${forkJob.token}`, "content-type": "application/json" }, body: JSON.stringify({ conclusion: "success", outputs: {} }) }); |
| 2883 | await ok("PATCH", `/repos/${owner}/imported-app/pulls/6`, { state: "closed" }); |
| 2884 | // New issues continue after the highest imported number. |
| 2885 | assert.equal((await ok("POST", `/repos/${owner}/imported-app/issues`, { title: "After import" })).number, 8); |
| 2886 | // Labels, milestones, protection, variables. |
| 2887 | const labels = await ok("GET", `/repos/${owner}/imported-app/labels`); |
| 2888 | { |
| 2889 | const bug = labels.find((l) => l.name === "bug"); |
| 2890 | assert.deepEqual([bug.name, bug.color, bug.description], ["bug", "d73a4a", "Something isn't working"]); |
| 2891 | } |
| 2892 | const milestones = await ok("GET", `/repos/${owner}/imported-app/milestones?state=all`); |
| 2893 | assert.equal(milestones.length, 1); |
| 2894 | assert.equal(milestones[0].due_on, "2030-01-01T00:00:00Z"); |
| 2895 | assert.equal(milestones[0].open_issues, 1); |
| 2896 | const protection = await ok("GET", `/repos/${owner}/imported-app/branches/main/protection`); |
| 2897 | assert.match(JSON.stringify(protection), /CI \/ build/); |
| 2898 | const vars = await ok("GET", `/repos/${owner}/imported-app/actions/variables`); |
| 2899 | assert.deepEqual(vars.variables.map((v) => [v.name, v.value]).sort(), [["DEPLOY_ENV", "staging"], ["REGION", "us"]]); |
| 2900 | assert.ok((await ok("GET", "/diffinery/imports")).some((x) => x.id === started.id)); |
| 2901 | |
| 2902 | // ------------------------------------------------------------ secrets policy "stop" fails with a clear message |
| 2903 | const strict = await ok("POST", "/diffinery/imports", { source: "octo/app", owner, name: "strict-app", github_token: GH_TOKEN, github_api_url: apiUrl, options: { secrets: "stop", issues: false } }); |
| 2904 | const strictAgent = await transfer(strict.id); |
| 2905 | assert.equal(strictAgent.status, 1, strictAgent.out); |
| 2906 | assert.match(strictAgent.out, /secret/i); |
| 2907 | const strictDone = await finished(strict.id); |
| 2908 | assert.equal(strictDone.status, "failed"); |
| 2909 | assert.match(strictDone.error, /secrets/); |
| 2910 | |
| 2911 | // ------------------------------------------------------------ a whole organization, into an organization |
| 2912 | await ok("POST", "/user/orgs", { login: "acme-imports" }); |
| 2913 | const bulk = await ok("POST", "/diffinery/imports", { source_owner: "octo", owner: "acme-imports", github_token: GH_TOKEN, github_api_url: apiUrl, concurrency: 2, options: { lfs: false } }); |
| 2914 | assert.equal(bulk.kind, "organization"); |
| 2915 | assert.deepEqual(bulk.children.map((ch) => ch.source).sort(), ["octo/app", "octo/lib"]); |
| 2916 | for (const ch of bulk.children) assert.equal((await transfer(ch.id)).status, 0); |
| 2917 | const bulkDone = await finished(bulk.id, 300_000); |
| 2918 | assert.equal(bulkDone.status, "succeeded", JSON.stringify(bulkDone, null, 2)); |
| 2919 | assert.deepEqual(bulkDone.summary.orgSecretNames, ["ORG_DEPLOY_KEY"]); |
| 2920 | const orgVars = await ok("GET", "/orgs/acme-imports/actions/variables"); |
| 2921 | assert.deepEqual(orgVars.variables.map((v) => [v.name, v.value]), [["ORG_REGION", "eu-west"]]); |
| 2922 | assert.ok(bulkDone.warnings.some((w) => /ORG_PRIVATE_HOST/.test(w)) && bulkDone.warnings.some((w) => /ORG_SELECTED_KEY/.test(w)), JSON.stringify(bulkDone.warnings)); |
| 2923 | assert.equal((await ok("GET", "/repos/acme-imports/lib")).default_branch, "trunk"); |
| 2924 | assert.equal((await ok("GET", "/repos/acme-imports/lib/issues/1")).title, "Lib issue"); |
| 2925 | assert.equal((await ok("GET", "/repos/acme-imports/app/pulls/2")).merged, true); |
| 2926 | |
| 2927 | // ------------------------------------------------------------ without a token: everything public comes across, variables are skipped |
| 2928 | // GitHub answers Actions variables, secrets and environments only with a token (401 otherwise). |
| 2929 | const anon = await ok("POST", "/diffinery/imports", { source: "octo/lib", owner, name: "anon-lib", github_api_url: apiUrl, options: { lfs: false } }); |
| 2930 | assert.equal((await transfer(anon.id)).status, 0); |
| 2931 | const anonDone = await finished(anon.id); |
| 2932 | assert.equal(anonDone.status, "succeeded", JSON.stringify(anonDone, null, 2)); |
| 2933 | const anonVars = anonDone.steps.find((st) => st.name === "variables"); |
| 2934 | assert.equal(anonVars.status, "skipped", JSON.stringify(anonDone.steps)); |
| 2935 | assert.match(anonVars.message, /needs a GitHub token/); |
| 2936 | assert.ok(anonDone.warnings.some((w) => /variables, secret names and environments were not imported/.test(w)), JSON.stringify(anonDone.warnings)); |
| 2937 | assert.equal((await ok("GET", `/repos/${owner}/anon-lib/issues/1`)).title, "Lib issue"); |
| 2938 | assert.equal((await ok("GET", `/repos/${owner}/anon-lib`)).default_branch, "trunk"); |
| 2939 | |
| 2940 | // ------------------------------------------------------------ an agent's organization import cannot touch variables (H1) |
| 2941 | const agentBulk = await api("POST", "/diffinery/imports", { source_owner: "octo", owner: "acme-imports", github_token: GH_TOKEN, github_api_url: apiUrl, concurrency: 1, options: { lfs: false } }, bot); |
| 2942 | assert.equal(agentBulk.status, 201, JSON.stringify(agentBulk.json)); |
| 2943 | const agentBulkDone = await finished(agentBulk.json.id, 180_000); |
| 2944 | const orgStep = agentBulkDone.steps.find((s) => s.name === "organization"); |
| 2945 | assert.equal(orgStep.status, "skipped", JSON.stringify(agentBulkDone.steps)); |
| 2946 | assert.match(orgStep.message, /agents cannot manage variables/); |
| 2947 | // The organization's variable is untouched by the agent import. |
| 2948 | assert.deepEqual((await ok("GET", "/orgs/acme-imports/actions/variables")).variables.map((v) => v.name), ["ORG_REGION"]); |
| 2949 | |
| 2950 | // ------------------------------------------------------------ a retry onto a repository adopted since is refused (H2) |
| 2951 | await ok("POST", `/repos/${owner}/strict-app/issues`, { title: "adopted after the failed import" }); |
| 2952 | await ok("POST", `/diffinery/imports/${strict.id}/retry`, { github_token: GH_TOKEN }); |
| 2953 | const reDone = await finished(strict.id); |
| 2954 | assert.equal(reDone.status, "failed"); |
| 2955 | assert.match(reDone.error, /holds content|start a new import/); |
| 2956 | |
| 2957 | // ------------------------------------------------------------ a stale (revoked) import push token cannot push in import mode (M4) |
| 2958 | const stalePush = gitFails(copy, ["push", `${base}/${owner}/imported-app.git`, "--push-option=diffinery-import", "refs/heads/main:refs/heads/stale-attempt"], agent.pushToken); |
| 2959 | assert.match(stalePush, /401|403|denied|forbidden|authentication|fatal/i, stalePush); |
| 2960 | |
| 2961 | // ------------------------------------------------------------ an organization import holds each repository to the size limit |
| 2962 | listHuge = true; |
| 2963 | const hugeBulk = await ok("POST", "/diffinery/imports", { source_owner: "octo", repositories: ["huge"], owner: "acme-imports", github_token: GH_TOKEN, github_api_url: apiUrl, concurrency: 1, options: { lfs: false } }); |
| 2964 | const hugeChild = await until("the oversized repository to be refused", async () => { |
| 2965 | const ch = (await ok("GET", `/diffinery/imports/${hugeBulk.id}`)).children?.find((c) => c.source === "octo/huge"); |
| 2966 | return ch?.status === "failed" ? ch : ch?.git?.phase === "waiting" ? Promise.reject(new Error("the oversized repository was queued for transfer")) : null; |
| 2967 | }, 120_000); |
| 2968 | assert.match(hugeChild.error ?? (await ok("GET", `/diffinery/imports/${hugeChild.id}`)).error, /import limit/); |
| 2969 | assert.equal((await api("GET", "/repos/acme-imports/huge")).status, 404); |
| 2970 | await finished(hugeBulk.id); |
| 2971 | |
| 2972 | // ------------------------------------------------------------ the web UI (signed in via DEV_AUTH) |
| 2973 | for (const path of ["/new/import", "/imports", `/imports/${started.id}`, `/imports/${started.id}/secrets`, `/imports/${bulk.id}`, "/settings/linked-accounts", `/${owner}/imported-app/issues/1`, `/${owner}/imported-app/pull/2`]) { |
| 2974 | const res = await fetch(`${base}${path}`); |
| 2975 | const html = await res.text(); |
| 2976 | assert.equal(res.status, 200, `${path} -> ${res.status}\n${html.slice(0, 500)}`); |
| 2977 | assert.ok(!/<select|type="checkbox"|type="radio"/.test(html.replace(/<template[\s\S]*?<\/template>/g, "")) || path.startsWith(`/${owner}`), `${path} renders Kumo controls, not native ones`); |
| 2978 | } |
| 2979 | assert.match(await (await fetch(`${base}/imports/${started.id}`)).text(), /NPM_TOKEN/); |
| 2980 | assert.match(await (await fetch(`${base}/${owner}/imported-app/issues/1`)).text(), /v1/); |
| 2981 | assert.match(await (await fetch(`${base}/${owner}/imported-app/issues/3`)).text(), /alice/); |
| 2982 | await gh.close(); |
| 2983 | }); |
| 2984 | |
| 2985 | test("scheduled workflows: the schedule fires without a cron trigger", async () => { |
| 2986 | assert.ok(tickRepo, "the schedule was registered"); |
| 2987 | const runs = await until("a scheduled run", async () => { |
| 2988 | const r = await ok("GET", `/repos/${tickRepo}/actions/runs?event=schedule`); |
| 2989 | return r.total_count > 0 && r.workflow_runs; |
| 2990 | }, 150_000); |
| 2991 | assert.equal(runs[0].event, "schedule"); |
| 2992 | assert.equal(runs[0].path, ".github/workflows/tick.yml"); |
| 2993 | assert.equal(runs[0].actor.login, "diffinery-scheduler"); |
| 2994 | // One run per tick, never two for the same minute. |
| 2995 | const minutes = runs.map((r) => r.created_at.slice(0, 16)); |
| 2996 | assert.equal(new Set(minutes).size, minutes.length, `two runs in one tick: ${minutes.join(", ")}`); |
| 2997 | // Site admins can see the scheduler's state (the live test reports it when no run comes). |
| 2998 | const state = await ok("GET", "/diffinery/admin/scheduler"); |
| 2999 | assert.equal(state.tick_minutes, 1); |
| 3000 | assert.ok(state.last_tick_at && state.covered_until, JSON.stringify(state)); |
| 3001 | assert.ok(Date.parse(state.next_tick_at) > Date.parse(state.last_tick_at), JSON.stringify(state)); |
| 3002 | // And the newest unexpected server errors; a request id that isn't kept is a 404. |
| 3003 | const errors = await ok("GET", "/diffinery/admin/errors?per_page=5"); |
| 3004 | assert.ok(Array.isArray(errors) && errors.length <= 5, JSON.stringify(errors)); |
| 3005 | assert.equal((await api("GET", "/diffinery/admin/errors/not-a-request-id")).status, 404); |
| 3006 | // IndexNow is off on a local origin; its page and API still answer for site admins. |
| 3007 | const indexnow = await ok("GET", "/diffinery/admin/indexnow"); |
| 3008 | assert.equal(indexnow.enabled, false, JSON.stringify(indexnow)); |
| 3009 | assert.match(indexnow.off_reason, /public https/); |
| 3010 | assert.match(await (await fetch(`${base}/settings/admin/search-engines`)).text(), /Search engines[\s\S]*PUBLIC_URL isn/); |
| 3011 | assert.equal((await ok("GET", "/meta")).indexnow.enabled, false); |
| 3012 | // A push without the schedule removes it. |
| 3013 | const src = join(work, "schedules"); |
| 3014 | rmSync(join(src, ".github/workflows/tick.yml")); |
| 3015 | git(src, ["commit", "-q", "-am", "no schedule"]); |
| 3016 | git(src, ["push", "-q", `${base}/${tickRepo}.git`, "main"]); |
| 3017 | }); |
| 3018 | |
| 3019 | /** |
| 3020 | * `git` without blocking this process, so the tests below run side by side; it rejects with git's |
| 3021 | * output and, on a server error or after 10 minutes without finishing (the command is killed, so a |
| 3022 | * stuck push fails the test instead of the whole job), the tail of `log` and what `more` says. |
| 3023 | * A `tok` of null sends no credentials (for GitHub). |
| 3024 | */ |
| 3025 | function gitAsync(cwd, args, tok = token, log = devLog, more = () => "") { |
| 3026 | const auth = tok === null ? [] : ["-c", `http.extraHeader=Authorization: Basic ${Buffer.from(`x-token:${tok}`).toString("base64")}`]; |
| 3027 | return new Promise((done, fail) => { |
| 3028 | const child = spawn("git", [...auth, "-c", "protocol.version=2", ...args], { |
| 3029 | cwd, |
| 3030 | stdio: ["ignore", "pipe", "pipe"], |
| 3031 | env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GIT_AUTHOR_NAME: "E2E", GIT_AUTHOR_EMAIL: "e2e@example.com", GIT_COMMITTER_NAME: "E2E", GIT_COMMITTER_EMAIL: "e2e@example.com" }, |
| 3032 | }); |
| 3033 | let stdout = ""; |
| 3034 | let stderr = ""; |
| 3035 | let timedOut = false; |
| 3036 | const timer = setTimeout(() => { |
| 3037 | timedOut = true; |
| 3038 | child.kill("SIGKILL"); |
| 3039 | }, 600_000); |
| 3040 | child.stdout.on("data", (d) => (stdout += d)); |
| 3041 | child.stderr.on("data", (d) => (stderr += d)); |
| 3042 | child.on("error", fail); |
| 3043 | child.on("close", (status) => { |
| 3044 | clearTimeout(timer); |
| 3045 | if (status === 0) return done(stdout); |
| 3046 | let message = `git ${args.join(" ")} ${timedOut ? "didn't finish in 10 minutes" : `exited ${status}`}\n${stderr}`; |
| 3047 | if (timedOut || /HTTP 5\d\d|Failed to connect/.test(stderr)) message += `\n${more()}\n--- wrangler log tail ---\n${log().slice(-4000)}`; |
| 3048 | fail(Object.assign(new Error(message), { stdout, stderr })); |
| 3049 | }); |
| 3050 | }); |
| 3051 | } |
| 3052 | |
| 3053 | /** |
| 3054 | * `ok`, `git` and `gitFails` against `server` (startServer's, or the shared one), with git that doesn't |
| 3055 | * block this process: for the tests below that run side by side. |
| 3056 | */ |
| 3057 | function client(server) { |
| 3058 | const ok = async (method, path, body) => { |
| 3059 | const send = () => fetch(`${server.base}/api/v3${path}`, { method, headers: { authorization: `Bearer ${server.token}`, "content-type": "application/json" }, body: body === undefined ? undefined : JSON.stringify(body) }); |
| 3060 | // A GET goes again on a kept-alive connection the server closed meanwhile, as in api(). |
| 3061 | const r = await send().catch((e) => (method === "GET" ? sleep(1000).then(send) : Promise.reject(e))); |
| 3062 | const text = await r.text(); |
| 3063 | assert.ok(r.ok, `${method} ${path} -> ${r.status} ${text}`); |
| 3064 | return JSON.parse(text); |
| 3065 | }; |
| 3066 | const git = (cwd, args) => gitAsync(cwd, args, server.token, server.log, () => `the Worker was at ${server.direct}; wrangler dev now names ${server.workerUrl?.() ?? "(the shared server)"}`); |
| 3067 | const gitFails = async (cwd, args) => { |
| 3068 | try { |
| 3069 | await git(cwd, args); |
| 3070 | } catch (e) { |
| 3071 | return `${e.stdout ?? ""}${e.stderr ?? ""}`; |
| 3072 | } |
| 3073 | assert.fail(`expected git ${args.join(" ")} to fail`); |
| 3074 | }; |
| 3075 | // Git goes straight to the Worker: wrangler dev's own proxy drops large pushes and clones streaming |
| 3076 | // through it (see workerUrl), which isn't Diffinery and doesn't exist in production. |
| 3077 | assert.ok(server.direct, `the Worker's own address wasn't found in wrangler dev's debug log`); |
| 3078 | return { ...server, gitBase: server.direct, ok, git, gitFails }; |
| 3079 | } |
| 3080 | const shared = () => client({ base, direct, token, log: devLog }); |
| 3081 | |
| 3082 | /** Runs `fn` with a client of a server of its own, stopped afterwards. */ |
| 3083 | async function onOwnServer(name, fn, ready = Promise.resolve()) { |
| 3084 | const [server] = await Promise.all([startServer(name), ready]); |
| 3085 | try { |
| 3086 | assert.ok(server.direct && (await fetch(`${server.direct}/healthz`)).ok, `the Worker's own address (${server.direct}) doesn't answer`); |
| 3087 | return await fn(client(server)); |
| 3088 | } finally { |
| 3089 | server.stop(); |
| 3090 | } |
| 3091 | } |
| 3092 | |
| 3093 | /** |
| 3094 | * A real project's history, pushed whole to a new repository on `server` and cloned back. Pinned |
| 3095 | * commits, so the packs are the same each run; cloned from GitHub without the Diffinery token. |
| 3096 | */ |
| 3097 | async function pushHistory({ gitBase, ok, git }, name, upstream) { |
| 3098 | const me = await ok("GET", "/user"); |
| 3099 | await ok("POST", "/user/repos", { name }); |
| 3100 | const remote = `${gitBase}/${me.login}/${name}.git`; |
| 3101 | // Test fixtures in these histories look like credentials (tornado has a test private key): pushed as an admin's bypass. |
| 3102 | await git(upstream, ["-c", "http.postBuffer=1048576", "push", "-q", "-o", "allow-secrets=e2e of a public history", remote, "main"]); |
| 3103 | const out = join(work, `${name}-clone`); |
| 3104 | await git(work, ["clone", "-q", "--bare", remote, out]); |
| 3105 | assert.equal((await git(out, ["rev-parse", "main"])).trim(), (await git(upstream, ["rev-parse", "main"])).trim()); |
| 3106 | assert.equal((await git(out, ["rev-list", "--count", "main"])).trim(), (await git(upstream, ["rev-list", "--count", "main"])).trim()); |
| 3107 | await git(out, ["fsck", "--full"]); |
| 3108 | } |
| 3109 | |
| 3110 | // Two at a time, each on a server of its own (the lockfile's on the shared one): one big push keeps |
| 3111 | // one server process busy, so on separate processes they use the runner's other cores; jquery's push |
| 3112 | // takes about as long as the other three's together. Not all four: a local server's memory grows to |
| 3113 | // 2 to 4 GB during such a push, and four at once ran a runner out of memory (the Workers runtime |
| 3114 | // crashed mid-push); two at a time peak about 1 GB above one at a time. Git goes past wrangler dev's |
| 3115 | // proxy (see client). |
| 3116 | describe("real histories: whole projects, side by side", { concurrency: 2 }, () => { |
| 3117 | for (const [name, url, sha] of [ |
| 3118 | // 37 MB: staged in R2 and unpacked in pieces. |
| 3119 | ["jquery", "https://github.com/jquery/jquery", "c2f27864e4ffc2d1409c289fcb9a42b6165dfeaf"], |
| 3120 | // About 12 MB each: too many objects for parsing whole in memory, so unpacked in pieces too. |
| 3121 | ["tornado", "https://github.com/tornadoweb/tornado", "056cacaefc7de31bda28363378c4ffc86b97d7e4"], |
| 3122 | ["flask", "https://github.com/pallets/flask", "d086db856be187255b8ec61ef409357393020f32"], |
| 3123 | ]) { |
| 3124 | test(`real histories: ${name}'s whole history goes in one push and clones back intact`, async () => { |
| 3125 | const upstream = join(work, `${name}-upstream.git`); |
| 3126 | const download = gitAsync(work, ["clone", "-q", "--bare", "--no-tags", url, upstream], null).then(() => gitAsync(upstream, ["update-ref", "refs/heads/main", sha], null)); |
| 3127 | await onOwnServer(name, (c) => pushHistory(c, name, upstream), download); |
| 3128 | }); |
| 3129 | } |
| 3130 | |
| 3131 | test("real histories: a 2.6 MB lockfile's 60 versions, a chain of deltas, go in one small push", async () => { |
| 3132 | const upstream = join(work, "lockfile-upstream"); |
| 3133 | mkdirSync(upstream, { recursive: true }); |
| 3134 | git(upstream, ["init", "-q", "-b", "main"]); |
| 3135 | const versions = new Map(); |
| 3136 | const pkgs = Array.from({ length: 11_000 }, (_, i) => `pkg-${String(i).padStart(5, "0")}`); |
| 3137 | pkgs.forEach((p, i) => versions.set(p, `1.${i % 51}.${i % 97}`)); |
| 3138 | for (let c = 0; c < 60; c++) { |
| 3139 | for (let k = 0; k < 40; k++) versions.set(pkgs[(c * 7919 + k * 104729) % pkgs.length], `2.${c}.${k}`); |
| 3140 | const body = pkgs.map((p) => { |
| 3141 | const v = versions.get(p); |
| 3142 | return ` "node_modules/${p}": {\n "version": "${v}",\n "resolved": "https://registry.npmjs.org/${p}/-/${p}-${v}.tgz",\n "integrity": "sha512-${createHash("sha512").update(p + v).digest("hex").slice(0, 64)}"\n },\n`; |
| 3143 | }); |
| 3144 | writeFileSync(join(upstream, "package-lock.json"), `{\n "lockfileVersion": 3,\n "packages": {\n${body.join("")} }\n}\n`); |
| 3145 | git(upstream, ["add", "-A"]); |
| 3146 | git(upstream, ["commit", "-q", "-m", `lockfile ${c}`]); |
| 3147 | } |
| 3148 | assert.ok(readFileSync(join(upstream, "package-lock.json")).length > 2_500_000); |
| 3149 | const packBytes = Number(execFileSync("bash", ["-c", "git pack-objects --revs --stdout -q <<<main | wc -c"], { cwd: upstream, encoding: "utf8" }).trim()); |
| 3150 | assert.ok(packBytes < 16 * 1024 * 1024, `the pack is ${packBytes} bytes`); |
| 3151 | await pushHistory(shared(), "lockfile", upstream); |
| 3152 | }); |
| 3153 | }); |
| 3154 | |
| 3155 | // Side by side, each on a server of its own, as the real histories above: about as long as the 40 MB |
| 3156 | // history's pushes alone instead of the sum. |
| 3157 | describe("large pushes: side by side, each on a server of its own", { concurrency: true }, () => { |
| 3158 | test("large files: a 40 MB file is pushed and cloned intact, and push protection still reads big text files", () => |
| 3159 | onOwnServer("bigfile", async ({ gitBase, ok, git, gitFails }) => { |
| 3160 | const me = await ok("GET", "/user"); |
| 3161 | await ok("POST", "/user/repos", { name: "bigfile" }); |
| 3162 | const src = join(work, "bigfile"); |
| 3163 | mkdirSync(src, { recursive: true }); |
| 3164 | await git(src, ["init", "-q", "-b", "main"]); |
| 3165 | // Random bytes don't compress: the push is over 16 MB, so it goes through R2 on its way in. |
| 3166 | const data = randomBytes(40 * 1024 * 1024); |
| 3167 | writeFileSync(join(src, "big.bin"), data); |
| 3168 | await git(src, ["add", "-A"]); |
| 3169 | await git(src, ["commit", "-q", "-m", "a large file"]); |
| 3170 | await git(src, ["-c", "http.postBuffer=1048576", "push", "-q", `${gitBase}/${me.login}/bigfile.git`, "main"]); |
| 3171 | const out = join(work, "bigfile-clone"); |
| 3172 | await git(work, ["clone", "-q", `${gitBase}/${me.login}/bigfile.git`, out]); |
| 3173 | assert.ok(readFileSync(join(out, "big.bin")).equals(data), "the cloned file is the pushed file"); |
| 3174 | const wantBlob = (await git(src, ["rev-parse", "HEAD:big.bin"])).trim(); |
| 3175 | assert.equal((await git(out, ["rev-parse", "HEAD:big.bin"])).trim(), wantBlob); |
| 3176 | // A large text file (scanned slice by slice) with a key near its end is still refused. |
| 3177 | // diffinery:allow-secret (a fake key) |
| 3178 | writeFileSync(join(src, "dump.sql"), `${"-- row\n".repeat(1024 * 1024)}aws_key = AKIAIOSFODNN7EXAMPL3\n`); |
| 3179 | await git(src, ["add", "-A"]); |
| 3180 | await git(src, ["commit", "-q", "-m", "a large dump"]); |
| 3181 | assert.match(await gitFails(src, ["push", "-q", `${gitBase}/${me.login}/bigfile.git`, "main"]), /possible secrets detected/); |
| 3182 | })); |
| 3183 | |
| 3184 | test("large pushes: three large pushes into one repository at once all go in, and a fetch meanwhile works", () => |
| 3185 | onOwnServer("pushes-at-once", async ({ gitBase, ok, git }) => { |
| 3186 | const me = await ok("GET", "/user"); |
| 3187 | await ok("POST", "/user/repos", { name: "pushes-at-once" }); |
| 3188 | const remote = `${gitBase}/${me.login}/pushes-at-once.git`; |
| 3189 | const src = join(work, "pushes-at-once"); |
| 3190 | mkdirSync(src, { recursive: true }); |
| 3191 | await git(src, ["init", "-q", "-b", "main"]); |
| 3192 | writeFileSync(join(src, "README.md"), "base\n"); |
| 3193 | await git(src, ["add", "-A"]); |
| 3194 | await git(src, ["commit", "-q", "-m", "base"]); |
| 3195 | await git(src, ["push", "-q", remote, "main"]); |
| 3196 | // Each branch: many medium files over a few commits, so unpacking holds a lot. Two packs stay under |
| 3197 | // 16 MB (handed over in memory), one goes over (staged in R2), as when several agents push at once. |
| 3198 | const sizes = { a: 180, b: 180, c: 420 }; |
| 3199 | const tips = {}; |
| 3200 | for (const [branch, count] of Object.entries(sizes)) { |
| 3201 | await git(src, ["checkout", "-q", "-b", branch, "main"]); |
| 3202 | const files = Array.from({ length: count }, () => randomBytes(48 * 1024).toString("base64")); |
| 3203 | mkdirSync(join(src, branch), { recursive: true }); |
| 3204 | for (let c = 0; c < 3; c++) { |
| 3205 | files.forEach((f, i) => writeFileSync(join(src, branch, `f${i}.txt`), `${f}\nrevision ${c}\n${randomBytes(1024).toString("hex")}\n`)); |
| 3206 | await git(src, ["add", "-A"]); |
| 3207 | await git(src, ["commit", "-q", "-m", `${branch} ${c}`]); |
| 3208 | } |
| 3209 | tips[branch] = (await git(src, ["rev-parse", "HEAD"])).trim(); |
| 3210 | } |
| 3211 | await git(src, ["checkout", "-q", "main"]); |
| 3212 | const push = (branch) => git(src, ["-c", "http.postBuffer=1048576", "push", "-q", remote, branch]); |
| 3213 | const fetchWhile = git(work, ["clone", "-q", remote, join(work, "pushes-at-once-early")]); |
| 3214 | await Promise.all([push("a"), push("b"), push("c"), fetchWhile]); |
| 3215 | const branches = (await ok("GET", `/repos/${me.login}/pushes-at-once/branches`)).map((b) => b.name).sort(); |
| 3216 | assert.deepEqual(branches, ["a", "b", "c", "main"]); |
| 3217 | const out = join(work, "pushes-at-once-clone"); |
| 3218 | await git(work, ["clone", "-q", "--mirror", remote, out]); |
| 3219 | for (const [branch, tip] of Object.entries(tips)) assert.equal((await git(out, ["rev-parse", `refs/heads/${branch}`])).trim(), tip); |
| 3220 | await git(out, ["fsck", "--full", "--strict"]); |
| 3221 | })); |
| 3222 | |
| 3223 | test("large pushes: a 40 MB history of many files goes in one push, read from R2 in ranges, and is still checked", () => |
| 3224 | onOwnServer("bighistory", async ({ gitBase, ok, git, gitFails }) => { |
| 3225 | const me = await ok("GET", "/user"); |
| 3226 | await ok("POST", "/user/repos", { name: "bighistory" }); |
| 3227 | const remote = `${gitBase}/${me.login}/bighistory.git`; |
| 3228 | const src = join(work, "bighistory"); |
| 3229 | mkdirSync(join(src, "files"), { recursive: true }); |
| 3230 | await git(src, ["init", "-q", "-b", "main"]); |
| 3231 | // Hundreds of medium files, each changed a little in later commits: a pack of deltas over 16 MB, |
| 3232 | // as a fork's pull request heads made one in jekyll/minima's import. |
| 3233 | const files = Array.from({ length: 400 }, () => randomBytes(96 * 1024).toString("base64")); |
| 3234 | for (let c = 0; c < 6; c++) { |
| 3235 | files.forEach((f, i) => writeFileSync(join(src, "files", `f${i}.txt`), `${f}\nrevision ${c} of file ${i}\n${randomBytes(2048).toString("hex")}\n`)); |
| 3236 | await git(src, ["add", "-A"]); |
| 3237 | await git(src, ["commit", "-q", "-m", `revision ${c}`]); |
| 3238 | } |
| 3239 | const packBytes = Number(execFileSync("bash", ["-c", "git pack-objects --revs --stdout -q <<<main | wc -c"], { cwd: src, encoding: "utf8" }).trim()); |
| 3240 | assert.ok(packBytes > 30 * 1024 * 1024, `the pack is ${packBytes} bytes`); |
| 3241 | await git(src, ["-c", "http.postBuffer=1048576", "push", "-q", remote, "main"]); |
| 3242 | const out = join(work, "bighistory-clone"); |
| 3243 | await git(work, ["clone", "-q", remote, out]); |
| 3244 | assert.equal((await git(out, ["rev-parse", "HEAD"])).trim(), (await git(src, ["rev-parse", "HEAD"])).trim()); |
| 3245 | await git(out, ["fsck", "--full", "--strict"]); |
| 3246 | |
| 3247 | // A second large push builds on objects the repository has (a thin pack, its bases read from storage). |
| 3248 | files.forEach((f, i) => writeFileSync(join(src, "files", `f${i}.txt`), `${f}\nrevision 6 of file ${i}\n${randomBytes(2048).toString("hex")}\n`)); |
| 3249 | writeFileSync(join(src, "new.bin"), randomBytes(20 * 1024 * 1024)); |
| 3250 | await git(src, ["add", "-A"]); |
| 3251 | await git(src, ["commit", "-q", "-m", "revision 6"]); |
| 3252 | await git(src, ["-c", "http.postBuffer=1048576", "push", "-q", remote, "main"]); |
| 3253 | await git(out, ["pull", "-q", "--ff-only"]); |
| 3254 | assert.equal((await git(out, ["rev-parse", "HEAD"])).trim(), (await git(src, ["rev-parse", "HEAD"])).trim()); |
| 3255 | await git(out, ["fsck", "--full", "--strict"]); |
| 3256 | |
| 3257 | // A large push is checked like a small one: a key in one small file, or an unsafe .gitmodules, refuses all of it. |
| 3258 | await git(src, ["checkout", "-q", "-b", "leak"]); |
| 3259 | writeFileSync(join(src, "more.bin"), randomBytes(20 * 1024 * 1024)); |
| 3260 | writeFileSync(join(src, "config.txt"), "aws_key = AKIAIOSFODNN7EXAMPL3\n"); // diffinery:allow-secret (a fake key; the pushed file doesn't carry the marker) |
| 3261 | await git(src, ["add", "-A"]); |
| 3262 | await git(src, ["commit", "-q", "-m", "a key"]); |
| 3263 | assert.match(await gitFails(src, ["-c", "http.postBuffer=1048576", "push", "-q", remote, "leak"]), /possible secrets detected/); |
| 3264 | await git(src, ["checkout", "-q", "main"]); |
| 3265 | await git(src, ["checkout", "-q", "-b", "modules"]); |
| 3266 | writeFileSync(join(src, "more.bin"), randomBytes(20 * 1024 * 1024)); |
| 3267 | writeFileSync(join(src, ".gitmodules"), '[submodule "x"]\n\tpath = x\n\turl = --upload-pack=touch /tmp/pwned\n'); |
| 3268 | await git(src, ["add", "-A"]); |
| 3269 | await git(src, ["commit", "-q", "-m", "an unsafe submodule"]); |
| 3270 | assert.match(await gitFails(src, ["-c", "http.postBuffer=1048576", "push", "-q", remote, "modules"]), /fsck error: \.gitmodules .*starts with '-'/); |
| 3271 | const branches = (await ok("GET", `/repos/${me.login}/bighistory/branches`)).map((b) => b.name); |
| 3272 | assert.deepEqual(branches, ["main"]); |
| 3273 | })); |
| 3274 | }); |