From 8df09c779fac758b37b1e181cd27ddb3257c24d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=88=E7=A7=8B?= <49514152+yiqiui@users.noreply.github.com> Date: Sun, 4 Oct 2026 22:18:15 +0800 Subject: [PATCH] ci: restrict mirror publication jobs to the upstream repository --- .github/workflows/sync-mirrors.yml | 5 ++ .../syncMirrorsWorkflowContract.spec.ts | 65 +++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 apps/desktop/src/lib/__tests__/syncMirrorsWorkflowContract.spec.ts diff --git a/.github/workflows/sync-mirrors.yml b/.github/workflows/sync-mirrors.yml index f4a8b3ea55..f40b3e7b59 100644 --- a/.github/workflows/sync-mirrors.yml +++ b/.github/workflows/sync-mirrors.yml @@ -20,6 +20,9 @@ concurrency: jobs: sync-cnb: name: Sync to CNB + # Mirror destinations are fixed to the upstream remotes; forks must never + # publish their refs there, even with mirrored secrets (#10944). + if: github.repository == 't8y2/dbx' runs-on: ubuntu-latest steps: - name: Checkout repository @@ -41,6 +44,7 @@ jobs: sync-atomgit: name: Sync to AtomGit + if: github.repository == 't8y2/dbx' runs-on: ubuntu-latest steps: - name: Checkout repository @@ -62,6 +66,7 @@ jobs: sync-gitee: name: Sync to Gitee + if: github.repository == 't8y2/dbx' runs-on: ubuntu-latest steps: - name: Checkout repository diff --git a/apps/desktop/src/lib/__tests__/syncMirrorsWorkflowContract.spec.ts b/apps/desktop/src/lib/__tests__/syncMirrorsWorkflowContract.spec.ts new file mode 100644 index 0000000000..c832ecaf86 --- /dev/null +++ b/apps/desktop/src/lib/__tests__/syncMirrorsWorkflowContract.spec.ts @@ -0,0 +1,65 @@ +import { readFileSync } from "node:fs"; +import { resolve } from "node:path"; +import { describe, expect, it } from "vitest"; + +const UPSTREAM_REPOSITORY = "t8y2/dbx"; +// Normalize CRLF so line-based matching is checkout-independent. +const workflow = readFileSync(resolve(process.cwd(), ".github/workflows/sync-mirrors.yml"), "utf8").replace(/\r\n/g, "\n"); + +/** Job ids declared under `jobs:`, in source order. */ +function mirrorJobIds(source: string): string[] { + const jobsStart = source.indexOf("\njobs:"); + expect(jobsStart).toBeGreaterThanOrEqual(0); + return [...source.slice(jobsStart).matchAll(/^ ([A-Za-z0-9_-]+):$/gm)].map((match) => match[1]); +} + +/** The raw lines of one job block: from its key line until the next job key. */ +function mirrorJobLines(source: string, jobId: string): string[] { + const jobsStart = source.indexOf("\njobs:"); + const key = ` ${jobId}:`; + const start = source.indexOf(`\n${key}\n`, jobsStart); + expect(start, `job ${jobId} exists under jobs:`).toBeGreaterThanOrEqual(0); + const lines = source.slice(start + 1).split(/\r?\n/); + const block: string[] = [lines[0]]; + for (const line of lines.slice(1)) { + // The next job key (same 2-space indent) ends this block; anything less + // indented than a job key ends the jobs section entirely. + if (/^ [A-Za-z0-9_-]+:$/.test(line)) break; + if (line.trim() !== "" && !line.startsWith(" ")) break; + block.push(line); + } + return block; +} + +describe("sync-mirrors workflow contract (#10944)", () => { + it("keeps the fixed mirror publication jobs", () => { + expect(mirrorJobIds(workflow)).toEqual(["sync-cnb", "sync-atomgit", "sync-gitee"]); + }); + + it("guards every mirror publication job to the upstream repository", () => { + const guard = `if: github.repository == '${UPSTREAM_REPOSITORY}'`; + for (const jobId of mirrorJobIds(workflow)) { + const block = mirrorJobLines(workflow, jobId); + const guardLine = block.find((line) => line.trim().startsWith("if:")); + expect(guardLine, `job ${jobId} has a repository guard`).toBeDefined(); + expect(guardLine?.trim()).toBe(guard); + // Job-level condition: the `if:` sits directly on the job, not inside a step. + expect(guardLine, `job ${jobId} guard is job-level`).toMatch(/^ {4}if:/); + } + }); + + it("preserves the mirror triggers, serialization, and destinations", () => { + // Triggers: tag pushes, ref deletions, scheduled runs, manual dispatch. + expect(workflow).toContain("push:"); + expect(workflow).toContain("tags:"); + expect(workflow).toContain("delete:"); + expect(workflow).toContain("schedule:"); + expect(workflow).toContain("workflow_dispatch:"); + // Serialization so overlapping triggers cannot race the same upstream ref. + expect(workflow).toContain("group: sync-mirrors"); + // Fixed destinations are unchanged. + expect(workflow).toContain("cnb.cool/dbxio.com/dbx.git"); + expect(workflow).toContain("atomgit.com/t8y2/dbx.git"); + expect(workflow).toContain("gitee.com/codetty/dbx.git"); + }); +});