diff --git a/.github/workflows/superdoc-npm-release.yml b/.github/workflows/superdoc-npm-release.yml index f658123..89c4208 100644 --- a/.github/workflows/superdoc-npm-release.yml +++ b/.github/workflows/superdoc-npm-release.yml @@ -3,7 +3,8 @@ name: SuperDoc npm release on: schedule: - cron: '17 0,8,16 * * *' - pull_request: + # Closed-and-merged only: use base-repository credentials even for fork PRs. + pull_request_target: types: [closed] branches: [main] workflow_dispatch: @@ -36,7 +37,7 @@ concurrency: jobs: check: - if: github.event_name != 'pull_request' || github.event.pull_request.merged == true + if: github.event_name != 'pull_request_target' || github.event.pull_request.merged == true runs-on: ubuntu-latest outputs: update: ${{ steps.check.outputs.update }} @@ -56,7 +57,7 @@ jobs: id: check env: DRY_RUN: ${{ github.event_name == 'workflow_dispatch' && (inputs.dry_run || inputs.simulated_latest != '' || inputs.simulated_deployed != '' || inputs.simulate_failure) && 'true' || 'false' }} - DEPLOY_CURRENT: ${{ github.event_name == 'pull_request' || inputs.deploy_current }} + DEPLOY_CURRENT: ${{ github.event_name == 'pull_request_target' || inputs.deploy_current }} SIMULATED_LATEST: ${{ inputs.simulated_latest }} SIMULATED_DEPLOYED: ${{ inputs.simulated_deployed }} SIMULATE_FAILURE: ${{ inputs.simulate_failure }} @@ -121,7 +122,7 @@ jobs: notify: name: Notify Slack needs: [check, update, deploy, record] - if: ${{ always() && ((github.event_name == 'pull_request' && github.event.pull_request.merged == true) || github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && !inputs.dry_run && inputs.simulated_latest == '' && inputs.simulated_deployed == '' && !inputs.simulate_failure)) && (needs.check.result == 'failure' || needs.update.result == 'failure' || needs.deploy.result == 'failure' || needs.record.result == 'failure' || needs.check.result == 'cancelled' || needs.update.result == 'cancelled' || needs.deploy.result == 'cancelled' || needs.record.result == 'cancelled' || needs.deploy.result == 'success') }} + if: ${{ always() && ((github.event_name == 'pull_request_target' && github.event.pull_request.merged == true) || github.event_name == 'schedule' || (github.event_name == 'workflow_dispatch' && !inputs.dry_run && inputs.simulated_latest == '' && inputs.simulated_deployed == '' && !inputs.simulate_failure)) && (needs.check.result == 'failure' || needs.update.result == 'failure' || needs.deploy.result == 'failure' || needs.record.result == 'failure' || needs.check.result == 'cancelled' || needs.update.result == 'cancelled' || needs.deploy.result == 'cancelled' || needs.record.result == 'cancelled' || needs.deploy.result == 'success') }} runs-on: ubuntu-latest timeout-minutes: 5 steps: diff --git a/README.md b/README.md index f66a4d7..49ce1f2 100644 --- a/README.md +++ b/README.md @@ -199,6 +199,10 @@ Each message includes the SuperDoc version, failed stages when applicable, a workflow-log link, and all demo titles and production links from `demos.json`. Adding a demo to that catalog automatically includes it in future notifications. Notifications are sent for merged PR deployments and stable release runs. +Post-merge releases use `pull_request_target` with `types: [closed]` and a merged-only +job guard, so fork PR merges retain repository deployment settings and secrets. +Only the merged base-repository commit is checked out; unmerged fork code is never +run in this privileged release workflow. Routine checks with no release, PR updates, and manual dry runs or simulations stay silent. diff --git a/scripts/notify-superdoc-release.mjs b/scripts/notify-superdoc-release.mjs index e211fec..d05d0cb 100644 --- a/scripts/notify-superdoc-release.mjs +++ b/scripts/notify-superdoc-release.mjs @@ -5,7 +5,7 @@ const stages = { check: "Release detection", update: "Update SuperDoc", deploy: const label = (value) => String(value).replace(/&/g, "&").replace(/[<>|*`\r\n]/g, " ").replace(/\s+/g, " ").trim(); export function shouldNotifyTrigger({ env, event = {} }) { - if (env.GITHUB_EVENT_NAME === "pull_request") { + if (["pull_request", "pull_request_target"].includes(env.GITHUB_EVENT_NAME)) { return event.action === "closed" && event.pull_request?.merged === true; } if (env.GITHUB_EVENT_NAME === "schedule") return true; diff --git a/scripts/notify-superdoc-release.test.mjs b/scripts/notify-superdoc-release.test.mjs index 01a3c14..1ddd577 100644 --- a/scripts/notify-superdoc-release.test.mjs +++ b/scripts/notify-superdoc-release.test.mjs @@ -52,13 +52,13 @@ test("PR titles and branch names cannot inject Slack markup or mentions", () => }); test("only merged PRs and stable release runs can notify", () => { - const env = { ...triggerEnv, GITHUB_EVENT_NAME: "pull_request" }; + const env = { ...triggerEnv, GITHUB_EVENT_NAME: "pull_request_target" }; for (const action of ["opened", "synchronize", "reopened", "edited", "closed"]) { assert.equal(shouldNotifyTrigger({ env, event: { action, pull_request: { ...pullRequest, merged: false } } }), false); } assert.equal(shouldNotifyTrigger({ env, event: { action: "closed", pull_request: { ...pullRequest, merged: true } } }), true); assert.equal(shouldNotifyTrigger({ env: { ...triggerEnv, GITHUB_EVENT_NAME: "schedule" } }), true); - for (const eventName of ["push", "merge_group", "pull_request_target", undefined]) { + for (const eventName of ["push", "merge_group", undefined]) { assert.equal(shouldNotifyTrigger({ env: { ...triggerEnv, GITHUB_EVENT_NAME: eventName } }), false); } const manual = { ...triggerEnv, GITHUB_EVENT_NAME: "workflow_dispatch" }; @@ -71,15 +71,48 @@ test("only merged PRs and stable release runs can notify", () => { test("PR updates never contact Slack, even when pipeline stages fail", async (t) => { const root = await mkdtemp(path.join(tmpdir(), "superdoc-notification-suppression-")); t.after(() => rm(root, { recursive: true, force: true })); - for (const action of ["opened", "synchronize", "reopened", "closed"]) { - await writeFile(`${root}/event.json`, JSON.stringify({ action, pull_request: { ...pullRequest, merged: false } })); - await notifyRelease({ root, env: { - GITHUB_EVENT_NAME: "pull_request", GITHUB_EVENT_PATH: `${root}/event.json`, - RELEASE_RESULTS: JSON.stringify({ check: { result: "failure" } }), - }, fetchImpl: () => { assert.fail("PR updates must not send a notification"); } }); + for (const eventName of ["pull_request", "pull_request_target"]) { + for (const action of ["opened", "synchronize", "reopened", "closed"]) { + await writeFile(`${root}/event.json`, JSON.stringify({ action, pull_request: { ...pullRequest, merged: false } })); + await notifyRelease({ root, env: { + GITHUB_EVENT_NAME: eventName, GITHUB_EVENT_PATH: `${root}/event.json`, + RELEASE_RESULTS: JSON.stringify({ check: { result: "failure" } }), + }, fetchImpl: () => { assert.fail("PR updates must not send a notification"); } }); + } } }); +test("a merged fork PR in the trusted base context notifies with the original PR link", async (t) => { + const root = await mkdtemp(path.join(tmpdir(), "superdoc-fork-merge-")); + t.after(() => rm(root, { recursive: true, force: true })); + await writeFile(`${root}/demos.json`, JSON.stringify(demos)); + await writeFile(`${root}/event.json`, JSON.stringify({ action: "closed", pull_request: { + ...pullRequest, merged: true, head: { ref: "codex/full-reference-preset", repo: { fork: true } }, + } })); + let payload; + await notifyRelease({ root, env: { + ...triggerEnv, GITHUB_EVENT_NAME: "pull_request_target", GITHUB_EVENT_PATH: `${root}/event.json`, + GITHUB_RUN_ID: "123", GITHUB_RUN_ATTEMPT: "1", RELEASE_RESULTS: JSON.stringify(success), + SUPERDOC_VERSION: "2.31.0", RELEASE_DRY_RUN: "false", SLACK_BOT_TOKEN: botToken, SLACK_CHANNEL_ID: channelId, + }, fetchImpl: async (_url, request) => { + payload = JSON.parse(request.body); + return Response.json({ ok: true }); + } }); + assert.ok(payload, "The trusted merged-fork event must reach notification delivery"); + assert.match(payload.text, /Trigger: PR merge.*PR #93/); + assert.match(payload.text, /codex\/full-reference-preset → main/); +}); + +test("the release workflow only grants the trusted merge context after a PR has merged", async () => { + const workflow = await readFile(new URL("../.github/workflows/superdoc-npm-release.yml", import.meta.url), "utf8"); + assert.match(workflow, /^ pull_request_target:\n types: \[closed\]\n branches: \[main\]/m); + assert.doesNotMatch(workflow, /^ pull_request:/m); + assert.match(workflow, /if: github.event_name != 'pull_request_target' \|\| github.event.pull_request.merged == true/); + assert.match(workflow, /DEPLOY_CURRENT: \$\{\{ github.event_name == 'pull_request_target' \|\| inputs.deploy_current \}\}/); + assert.match(workflow, /github.event_name == 'pull_request_target' && github.event.pull_request.merged == true/); + assert.doesNotMatch(workflow, /pull_request\.head\.|refs\/pull\//); +}); + test("successful deployments list every catalog demo with a production link", () => { const { text } = buildNotification(options); assert.match(text, /\[PRODUCTION\].*SUCCESS/);