diff --git a/.github/scripts/retarget-prs.sh b/.github/scripts/retarget-prs.sh index f9358b4efc..8f42c6fde6 100755 --- a/.github/scripts/retarget-prs.sh +++ b/.github/scripts/retarget-prs.sh @@ -100,6 +100,14 @@ for number in "$@"; do continue fi + # Canary is an intentional integration target. Keep it untouched even if a caller passes + # its number to the sweep or overrides RELEASE_BASE, TARGET_BASE or EXPLAIN_MISSING. + if [ "$base_ref" = "canary" ]; then + echo "#$number: skipped — canary is an explicit target" + skipped=$((skipped + 1)) + continue + fi + if [ "$base_ref" = "$TARGET_BASE" ] && [ "$EXPLAIN_MISSING" = "true" ] && [ "$DRY_RUN" != "true" ]; then echo "#$number: already on $TARGET_BASE — posting any missing explanation" post_explanation "$number" diff --git a/.github/scripts/retarget-prs.test.mjs b/.github/scripts/retarget-prs.test.mjs new file mode 100644 index 0000000000..03f18b5e21 --- /dev/null +++ b/.github/scripts/retarget-prs.test.mjs @@ -0,0 +1,104 @@ +import assert from 'node:assert/strict'; +import { spawnSync } from 'node:child_process'; +import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs'; +import { dirname, join } from 'node:path'; +import { test } from 'node:test'; +import { fileURLToPath } from 'node:url'; + +const scriptsDir = dirname(fileURLToPath(import.meta.url)); +const script = join(scriptsDir, 'retarget-prs.sh'); + +function runRetarget(t, prs, overrides = {}) { + const scratch = mkdtempSync(join(scriptsDir, '.retarget-prs-test-')); + t.after(() => rmSync(scratch, { recursive: true, force: true })); + const callsPath = join(scratch, 'calls'); + const fakeGh = join(scratch, 'gh'); + writeFileSync( + fakeGh, + `#!/usr/bin/env bash +set -euo pipefail +printf '%s\\n' "$*" >> "$CALLS_PATH" +case "$1:$2" in + api:*/pulls/*) cat "$FIXTURE_DIR/\${2##*/}.json" ;; + api:*/comments) printf '%s\\n' '[]' ;; + 'pr:edit') exit 0 ;; + 'pr:comment') cat >/dev/null ;; + *) echo "unexpected gh call: $*" >&2; exit 1 ;; +esac +`, + { mode: 0o700 }, + ); + for (const [number, { base, head = 'topic' }] of Object.entries(prs)) { + writeFileSync( + join(scratch, `${number}.json`), + JSON.stringify({ + state: 'open', + base: { ref: base }, + head: { ref: head, repo: { full_name: 'contributor/fork' } }, + labels: [], + }), + ); + } + const result = spawnSync('bash', [script, ...Object.keys(prs)], { + encoding: 'utf8', + env: { + ...process.env, + PATH: `${scratch}:${process.env.PATH}`, + FIXTURE_DIR: scratch, + CALLS_PATH: callsPath, + REPO: 'LibreChat-AI/LibreChat', + RELEASE_BASE: 'main', + TARGET_BASE: 'dev', + DRY_RUN: 'false', + EXPLAIN_MISSING: 'false', + ...overrides, + }, + }); + assert.equal(result.status, 0, `${result.stdout}\n${result.stderr}`); + return { + output: result.stdout, + calls: readFileSync(callsPath, 'utf8').trim().split('\n'), + }; +} + +function writes(calls) { + return calls.filter((call) => call.startsWith('pr edit ') || call.startsWith('pr comment ')); +} + +test('a numbered manual sweep leaves canary alone while main still retargets to dev', (t) => { + const { output, calls } = runRetarget(t, { + 101: { base: 'canary' }, + 102: { base: 'main' }, + }); + assert.match(output, /#101: skipped/); + assert.deepEqual(writes(calls), [ + 'pr edit 102 --repo LibreChat-AI/LibreChat --base dev', + 'pr comment 102 --repo LibreChat-AI/LibreChat --body-file -', + ]); +}); + +test('canary stays protected even if a caller misconfigures the release base', (t) => { + const { output, calls } = runRetarget(t, { 103: { base: 'canary' } }, { RELEASE_BASE: 'canary' }); + assert.match(output, /#103: skipped/); + assert.deepEqual(writes(calls), []); +}); + +test('explanation recovery never posts a dev-target message on a canary PR', (t) => { + const { output, calls } = runRetarget( + t, + { 104: { base: 'canary' } }, + { TARGET_BASE: 'canary', EXPLAIN_MISSING: 'true' }, + ); + assert.match(output, /#104: skipped/); + assert.deepEqual(writes(calls), []); +}); + +test('a dry run cannot propose retargeting canary', (t) => { + const { output, calls } = runRetarget( + t, + { 105: { base: 'canary' } }, + { RELEASE_BASE: 'canary', DRY_RUN: 'true' }, + ); + assert.match(output, /would_retarget=0 skipped=1/); + assert.deepEqual(writes(calls), []); +}); diff --git a/.github/workflows/pr-retarget-dev.yml b/.github/workflows/pr-retarget-dev.yml index da4cf02638..8474f70c62 100644 --- a/.github/workflows/pr-retarget-dev.yml +++ b/.github/workflows/pr-retarget-dev.yml @@ -3,6 +3,7 @@ name: Retarget PRs to dev on: pull_request_target: types: [opened, reopened, synchronize] + # A deliberate canary base is never retargeted; the script also enforces this for sweeps. branches: [main] workflow_dispatch: inputs: @@ -11,7 +12,7 @@ on: type: boolean default: true pr_numbers: - description: 'Space-separated PR numbers (default: every open pull request based on main)' + description: 'Space-separated PR numbers (default: open main-based PRs; canary is excluded)' required: false default: '' @@ -41,6 +42,9 @@ jobs: persist-credentials: false sparse-checkout: .github/scripts + - name: Verify canary base stays untouched + run: node --test .github/scripts/retarget-prs.test.mjs + - name: Retarget onto dev env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} @@ -59,6 +63,9 @@ jobs: persist-credentials: false sparse-checkout: .github/scripts + - name: Verify canary base stays untouched + run: node --test .github/scripts/retarget-prs.test.mjs + - name: Retarget onto dev env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}