diff --git a/.github/scripts/check-fork-release.cjs b/.github/scripts/check-fork-release.cjs new file mode 100644 index 0000000000000..1056efdbfa9e6 --- /dev/null +++ b/.github/scripts/check-fork-release.cjs @@ -0,0 +1,22 @@ +const { assertReleaseSource, shouldReleaseNightly } = require("./check-nightly-release.cjs"); + +// Decides whether a Fork Release run publishes, and the UTC date its version +// carries. Manual dispatch skips the six-hour gap but must build main, since a +// published branch build would leave main behind it and stall later scheduled +// checks. The date is when the run was created, so a run that queues past +// midnight or is retried on another day keeps the same tag. +async function resolveForkRelease({ github, context, core }) { + if (context.eventName === "workflow_dispatch") { + await assertReleaseSource({ github, context, releaseChannel: "nightly" }); + } else if (!(await shouldReleaseNightly({ github, context, core }))) { + return { due: false }; + } + + const { data: run } = await github.rest.actions.getWorkflowRun({ + ...context.repo, + run_id: context.runId, + }); + return { due: true, date: run.created_at.slice(0, 10).replaceAll("-", "") }; +} + +module.exports = { resolveForkRelease }; diff --git a/.github/scripts/check-fork-release.test.cjs b/.github/scripts/check-fork-release.test.cjs new file mode 100644 index 0000000000000..a6c54056c05b0 --- /dev/null +++ b/.github/scripts/check-fork-release.test.cjs @@ -0,0 +1,82 @@ +const assert = require("node:assert/strict"); +const test = require("node:test"); +const { resolveForkRelease } = require("./check-fork-release.cjs"); + +const hour = 60 * 60 * 1000; +const core = { info() {} }; + +function fixture({ + eventName = "workflow_dispatch", + ref = "refs/heads/main", + comparisonStatus = "ahead", + nightlyHoursAgo = 7, + createdAt = "2026-09-29T23:59:30Z", +} = {}) { + const runLookups = []; + return { + runLookups, + options: { + core, + context: { + eventName, + payload: { repository: { default_branch: "main" } }, + ref, + repo: { owner: "example", repo: "app" }, + runId: 42, + sha: "candidate", + }, + github: { + async paginate() { + return [ + { + tag_name: "v0.0.43-nightly.20260929.19", + draft: false, + published_at: new Date(Date.now() - nightlyHoursAgo * hour).toISOString(), + }, + ]; + }, + rest: { + actions: { + async getWorkflowRun(params) { + runLookups.push(params); + return { data: { created_at: createdAt } }; + }, + }, + repos: { + async compareCommitsWithBasehead() { + return { data: { status: comparisonStatus } }; + }, + }, + }, + }, + }, + }; +} + +test("rejects a manual release dispatched from a feature branch", async () => { + const { options, runLookups } = fixture({ ref: "refs/heads/feature" }); + await assert.rejects(resolveForkRelease(options), /must be dispatched from main/); + assert.equal(runLookups.length, 0); +}); + +test("rejects a manual release whose commit is not on main", async () => { + const { options } = fixture({ comparisonStatus: "diverged" }); + await assert.rejects(resolveForkRelease(options), /not contained in main \(diverged\)/); +}); + +test("a manual release from main skips the six-hour gap", async () => { + const { options } = fixture({ nightlyHoursAgo: 1 }); + assert.deepEqual(await resolveForkRelease(options), { due: true, date: "20260929" }); +}); + +test("a scheduled run waits for the six-hour gap", async () => { + const { options, runLookups } = fixture({ eventName: "schedule", nightlyHoursAgo: 1 }); + assert.deepEqual(await resolveForkRelease(options), { due: false }); + assert.equal(runLookups.length, 0); +}); + +test("a run created before midnight keeps its date when it runs or retries after", async () => { + const { options, runLookups } = fixture({ eventName: "schedule" }); + assert.deepEqual(await resolveForkRelease(options), { due: true, date: "20260929" }); + assert.equal(runLookups[0].run_id, 42); +}); diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 611d4cf44f754..7e7a662a97266 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -109,6 +109,9 @@ jobs: - name: Test nightly release checks run: node --test .github/scripts/check-nightly-release.test.cjs + - name: Test fork release checks + run: node --test .github/scripts/check-fork-release.test.cjs + - name: Test run: vp run --parallel --concurrency-limit 4 --filter '!t3' --filter '!@t3tools/monorepo' test diff --git a/.github/workflows/fork-release.yml b/.github/workflows/fork-release.yml index 72a023f3daeb1..3f2104fa7dc1c 100644 --- a/.github/workflows/fork-release.yml +++ b/.github/workflows/fork-release.yml @@ -49,8 +49,12 @@ jobs: name: Check if a release is due runs-on: ubuntu-24.04 timeout-minutes: 5 + permissions: + contents: read + actions: read outputs: due: ${{ steps.check.outputs.due }} + date: ${{ steps.check.outputs.date }} steps: - uses: actions/checkout@d23441a48e516b6c34aea4fa41551a30e30af803 # v6 with: @@ -60,9 +64,10 @@ jobs: uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8 with: script: | - const { shouldReleaseNightly } = require('./.github/scripts/check-nightly-release.cjs'); - const due = context.eventName !== 'schedule' || (await shouldReleaseNightly({ github, context, core })); + const { resolveForkRelease } = require('./.github/scripts/check-fork-release.cjs'); + const { due, date } = await resolveForkRelease({ github, context, core }); core.setOutput('due', due); + if (date) core.setOutput('date', date); version: name: Resolve version @@ -102,10 +107,12 @@ jobs: - id: resolve shell: bash + env: + RELEASE_DATE: ${{ needs.due.outputs.date }} run: | set -euo pipefail node scripts/resolve-nightly-release.ts \ - --date "$(date -u -d "${{ github.run_started_at }}" +%Y%m%d)" \ + --date "$RELEASE_DATE" \ --run-number "${{ github.run_number }}" \ --sha "$GITHUB_SHA" \ --github-output