diff --git a/.github/scripts/is-release-notes-only.cjs b/.github/scripts/is-release-notes-only.cjs new file mode 100644 index 0000000000..44dc1e5157 --- /dev/null +++ b/.github/scripts/is-release-notes-only.cjs @@ -0,0 +1,90 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +// Shared by both milestone jobs. Only a complete, nonempty default-branch +// release-notes change may bypass branch and next-version rules. +module.exports = async ({ github, context, core }) => { + const pr = context.payload.pull_request; + if (pr?.milestone?.state !== 'open') { + throw new Error('An assigned open milestone is required, including for release notes.'); + } + + const defaultBranch = context.payload.repository?.default_branch; + if (!defaultBranch || !pr.base?.ref) { + throw new Error('Missing default or target branch for release-notes classification.'); + } + if (pr.base.ref !== defaultBranch) { + return false; + } + + // GitHub's list-files endpoint returns at most 3,000 files. Never infer + // eligibility from a truncated response or an unavailable expected count. + const count = pr.changed_files; + if (!Number.isSafeInteger(count) || count < 0 || count > 3000) { + throw new Error('Cannot verify the complete changed-file count (must be 0..3000).'); + } + if (count === 0) { + return false; + } + if (!pr.head?.sha || !pr.base.sha) { + throw new Error('Missing PR commit identity for release-notes classification.'); + } + + const args = { ...context.repo, pull_number: pr.number }; + const snapshot = value => JSON.stringify([ + value?.head?.sha, value?.base?.ref, value?.changed_files, + value?.milestone?.id, value?.milestone?.title, value?.milestone?.state, + ]); + const verifySnapshot = async expectedBaseSha => { + const { data } = await github.rest.pulls.get(args); + if (!data?.base?.sha) { + throw new Error('Missing live base commit identity for release-notes classification.'); + } + if (snapshot(data) !== snapshot(pr) + || (expectedBaseSha !== undefined && data.base.sha !== expectedBaseSha)) { + throw new Error('PR changed since this event or during file retrieval; trigger a fresh milestone check.'); + } + return data.base.sha; + }; + + // Reconciliation replays an older event after main may have advanced. + // Match PR identity to the event, but compare base SHA only between live + // snapshots so unrelated merges do not prevent a stable retrieval. + const liveBaseSha = await verifySnapshot(); + const filenames = new Set(); + const statuses = new Set(['added', 'modified', 'removed', 'renamed', 'copied', 'changed', 'unchanged']); + const allowedPath = path => typeof path === 'string' + && !path.includes('\\') + && path.split('/').every(part => part !== '' && part !== '.' && part !== '..') + && (path === 'CHANGELOG.md' || path.startsWith('release-notes/')); + let notesOnly = true; + for (let page = 1; page <= Math.ceil(count / 100); page++) { + const { data } = await github.rest.pulls.listFiles({ ...args, per_page: 100, page }); + if (!Array.isArray(data) || data.length !== Math.min(100, count - (page - 1) * 100)) { + throw new Error(`Incomplete changed-file data on page ${page}.`); + } + for (const file of data) { + if (!file || typeof file.filename !== 'string' || !file.filename || !statuses.has(file.status)) { + throw new Error('Invalid changed-file entry.'); + } + if (filenames.has(file.filename)) { + throw new Error('Duplicate changed-file entry; the complete file set cannot be verified.'); + } + filenames.add(file.filename); + if ((file.status === 'renamed' || file.status === 'copied') && !file.previous_filename) { + throw new Error('Missing previous filename for a renamed or copied file.'); + } + // Check both sides, including deletions and code moved into notes. + if (!allowedPath(file.filename) + || (file.previous_filename !== undefined && !allowedPath(file.previous_filename))) { + notesOnly = false; + } + } + } + await verifySnapshot(liveBaseSha); + if (notesOnly) { + core.notice('Release-notes-only PR on the default branch: skipping milestone branch and next-version rules; the open-milestone requirement still applies.'); + } + return notesOnly; +}; diff --git a/.github/scripts/recheck-milestones-for-release-branch.sh b/.github/scripts/recheck-milestones-for-release-branch.sh index b48ceb4ad6..0c7ab0708b 100755 --- a/.github/scripts/recheck-milestones-for-release-branch.sh +++ b/.github/scripts/recheck-milestones-for-release-branch.sh @@ -21,6 +21,8 @@ # whatever result it last recorded. This script closes that gap by re-running # every semantic-version-milestoned PR targeting the default branch. That covers # both the newly serviced series and any later series whose eligibility changes. +# Release-notes-only PRs are intentionally included: the replayed workflow +# applies the shared exception without duplicating file classification here. # # Re-running is enough because check-milestone-branch.sh queries the live list # of release branches. The replayed event payload still carries the correct @@ -30,8 +32,10 @@ # LIMITATION # ---------- # A re-run replays the original run's commit, which means it executes the -# workflow definition and script as they existed then. Only the release branch -# lookup is evaluated live. +# workflow definition and script as they existed then. API lookups (including +# release branches and release-note file classification) are evaluated live. +# The file classifier rejects stale PR identity but allows the default branch +# to advance before a re-run; its base SHA must remain stable during retrieval. # # So a pull request whose most recent milestone check predates the arrival of # the target-branch rule will replay the older check, pass it, and keep its @@ -40,6 +44,9 @@ # distinguishing a stale replay costs an extra API call per pull request and # the window closes on its own. After the first release branch is cut following # a change to the check itself, review the affected pull requests by hand. +# To activate a new exception on an existing PR, first land the workflow change +# on its base, then update the PR branch with that base and trigger a fresh +# pull_request run. Re-running an old failed run does not load the new scripts. # # REQUIRED ENVIRONMENT VARIABLES # ------------------------------ diff --git a/.github/scripts/tests/README.md b/.github/scripts/tests/README.md index a631609c10..be9ecd3a93 100644 --- a/.github/scripts/tests/README.md +++ b/.github/scripts/tests/README.md @@ -1,5 +1,34 @@ # GitHub Actions Script Tests +## Release-notes milestone exception tests + +`is-release-notes-only.test.cjs` uses the built-in Node.js test runner (Node.js +20 or newer), with no npm dependencies: + +```bash +node --test .github/scripts/tests/is-release-notes-only.test.cjs +``` + +Default-branch PRs with an assigned **open** milestone and a complete, nonempty +file set exclusively under `release-notes/` or at exact `CHANGELOG.md` bypass +only the milestone target-branch and next-version rules. Mixed changes and +non-default targets retain the existing rules. Both names used by required +status checks remain unchanged. API failures, incomplete pages, duplicate +entries, unknown file statuses, and file counts beyond the API's 3,000-file +limit fail the check rather than grant an exception. Renames/copies must have +both paths in the allowlist; deleting code is not a release-notes-only change. + +Tests cover these rules, pagination boundaries, missing/closed milestones, +stale events and concurrent PR changes, and wiring in both workflow jobs. +Reconciliation continues to rerun the same workflow, including notes-only PRs. +Unrelated default-branch updates before a rerun are allowed: the head, target, +milestone, and file count must still match the event, while base-SHA stability +is checked between the live snapshots around file retrieval, not against the +historical event. Base changes during retrieval still fail closed. +After landing changes to the check, update existing PR branches with the new +base and trigger a fresh `pull_request` run; rerunning an old run reuses its old +workflow/scripts and cannot activate the exception. + ## Agentic triage output tests `validate-triage-output.test.cjs` uses the built-in Node.js test runner (Node.js diff --git a/.github/scripts/tests/is-release-notes-only.test.cjs b/.github/scripts/tests/is-release-notes-only.test.cjs new file mode 100644 index 0000000000..871298e8bc --- /dev/null +++ b/.github/scripts/tests/is-release-notes-only.test.cjs @@ -0,0 +1,282 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +const assert = require('node:assert/strict'); +const { test } = require('node:test'); +const { readFileSync } = require('node:fs'); +const classify = require('../is-release-notes-only.cjs'); + +function fixture(files = [ + { filename: 'release-notes/6.1/6.1.8.md', status: 'added' }, + { filename: 'release-notes/6.1/README.md', status: 'modified' }, + { filename: 'CHANGELOG.md', status: 'modified' }, +]) { + const pr = { + number: 4920, + state: 'open', + milestone: { id: 61, title: '6.1.8', state: 'open' }, + head: { sha: 'head-sha' }, + base: { ref: 'main', sha: 'base-sha' }, + changed_files: files.length, + }; + const calls = []; + const notices = []; + const live = structuredClone(pr); + const github = { rest: { pulls: { + get: async (args) => { + calls.push(['get', args]); + return { data: structuredClone(live) }; + }, + listFiles: async (args) => { + calls.push(['listFiles', args]); + return { data: files.slice((args.page - 1) * args.per_page, args.page * args.per_page) }; + }, + } } }; + const context = { + repo: { owner: 'dotnet', repo: 'SqlClient' }, + payload: { pull_request: pr, repository: { default_branch: 'main' } }, + }; + return { github, context, core: { notice: message => notices.push(message) }, pr, live, calls, notices }; +} + +test('PR 4920 release notes bypass branch and version rules with an open servicing milestone', async () => { + const f = fixture(); + assert.equal(await classify(f), true); + assert.equal(f.notices.length, 1); + assert.equal(f.calls.filter(([name]) => name === 'get').length, 2); +}); + +for (const filename of [ + 'src/Driver.cs', '.github/workflows/check-milestone.yml', 'README.md', + 'CHANGELOG.md.old', 'docs/CHANGELOG.md', 'release-notes-other/file.md', + 'release-notes', 'release-notes/../src/Driver.cs', 'release-notes//file.md', + 'release-notes/./file.md', 'release-notes\\file.md', +]) { + test(`mixed changes including ${filename} preserve existing rules`, async () => { + const f = fixture([{ filename: 'release-notes/a.md', status: 'added' }, { filename, status: 'modified' }]); + assert.equal(await classify(f), false); + assert.equal(f.notices.length, 0); + }); +} + +for (const status of ['added', 'modified', 'removed', 'changed', 'unchanged']) { + test(`${status} release-note paths qualify`, async () => { + assert.equal(await classify(fixture([{ filename: 'release-notes/a.md', status }])), true); + }); +} + +for (const status of ['renamed', 'copied']) { + test(`${status} files require both paths inside the allowlist`, async () => { + for (const previous_filename of ['src/Driver.cs', 'README.md']) { + assert.equal(await classify(fixture([ + { filename: 'release-notes/a.md', previous_filename, status }, + ])), false); + } + assert.equal(await classify(fixture([ + { filename: 'release-notes/a.md', previous_filename: 'release-notes/b.md', status }, + ])), true); + await assert.rejects(classify(fixture([ + { filename: 'release-notes/a.md', status }, + ])), /previous filename/); + }); +} + +test('moving notes to code and deleting code cannot bypass rules', async () => { + assert.equal(await classify(fixture([ + { filename: 'src/Driver.cs', previous_filename: 'release-notes/a.md', status: 'renamed' }, + ])), false); + assert.equal(await classify(fixture([ + { filename: 'release-notes/a.md', status: 'added' }, + { filename: 'src/Driver.cs', status: 'removed' }, + ])), false); +}); + +test('a previous path outside the allowlist never qualifies even with a modified status', async () => { + assert.equal(await classify(fixture([ + { filename: 'release-notes/a.md', previous_filename: 'src/Driver.cs', status: 'modified' }, + ])), false); +}); + +test('empty change sets do not qualify', async () => { + const f = fixture([]); + assert.equal(await classify(f), false); + assert.equal(f.calls.length, 0); +}); + +for (const target of ['release/6.1', 'dev/feature']) { + test(`${target} targets preserve existing rules without querying files`, async () => { + const f = fixture(); + f.pr.base.ref = target; + assert.equal(await classify(f), false); + assert.equal(f.calls.length, 0); + }); +} + +test('the default branch is not hardcoded to main', async () => { + const f = fixture(); + f.context.payload.repository.default_branch = f.pr.base.ref = f.live.base.ref = 'trunk'; + assert.equal(await classify(f), true); +}); + +for (const milestone of [null, { id: 61, title: '6.1.8', state: 'closed' }]) { + test(`a ${milestone ? 'closed' : 'missing'} milestone cannot bypass the gate`, async () => { + const f = fixture(); + f.pr.milestone = milestone; + await assert.rejects(classify(f), /open milestone/); + assert.equal(f.calls.length, 0); + }); +} + +for (const count of [undefined, -1, 1.5, '3', 3001]) { + test(`invalid or API-capped file count ${count} fails closed`, async () => { + const f = fixture(); + f.pr.changed_files = count; + await assert.rejects(classify(f), /file count/); + }); +} + +test('reads all pages, including code beyond the first 100 files', async () => { + const files = Array.from({ length: 101 }, (_, i) => ({ filename: `release-notes/${i}.md`, status: 'added' })); + const f = fixture(files); + assert.equal(await classify(f), true); + assert.deepEqual(f.calls.filter(([name]) => name === 'listFiles').map(([, args]) => args), [ + { owner: 'dotnet', repo: 'SqlClient', pull_number: 4920, per_page: 100, page: 1 }, + { owner: 'dotnet', repo: 'SqlClient', pull_number: 4920, per_page: 100, page: 2 }, + ]); + files[100] = { filename: 'src/Driver.cs', status: 'modified' }; + assert.equal(await classify(fixture(files)), false); +}); + +for (const count of [100, 3000]) { + test(`complete ${count}-file sets at page and API boundaries qualify`, async () => { + const f = fixture(Array.from({ length: count }, (_, i) => ({ filename: `release-notes/${i}.md`, status: 'added' }))); + assert.equal(await classify(f), true); + assert.equal(f.calls.filter(([name]) => name === 'listFiles').length, count / 100); + }); +} + +for (const data of [[], {}, null, [{ filename: 'release-notes/a.md', status: 'added' }]]) { + test(`incomplete or malformed page ${JSON.stringify(data)} fails closed`, async () => { + const f = fixture(); + f.github.rest.pulls.listFiles = async () => ({ data }); + await assert.rejects(classify(f), /Incomplete/); + }); +} + +test('a truncated later page fails closed', async () => { + const f = fixture(Array.from({ length: 101 }, (_, i) => ({ filename: `release-notes/${i}.md`, status: 'added' }))); + const listFiles = f.github.rest.pulls.listFiles; + f.github.rest.pulls.listFiles = args => args.page === 2 ? { data: [] } : listFiles(args); + await assert.rejects(classify(f), /Incomplete/); +}); + +for (const method of ['get', 'listFiles']) { + test(`${method} API errors fail the check rather than granting an exception`, async () => { + const f = fixture(); + f.github.rest.pulls[method] = async () => { throw new Error('API unavailable'); }; + await assert.rejects(classify(f), /API unavailable/); + }); +} + +test('later-page API errors are not swallowed', async () => { + const f = fixture(Array.from({ length: 101 }, (_, i) => ({ filename: `release-notes/${i}.md`, status: 'added' }))); + const listFiles = f.github.rest.pulls.listFiles; + f.github.rest.pulls.listFiles = async args => { + if (args.page === 2) throw new Error('page 2 unavailable'); + return listFiles(args); + }; + await assert.rejects(classify(f), /page 2 unavailable/); +}); + +for (const file of [ + null, {}, { status: 'added' }, { filename: 'release-notes/a.md' }, + { filename: 'release-notes/a.md', status: 'unknown' }, +]) { + test(`malformed file entry ${JSON.stringify(file)} fails closed`, async () => { + await assert.rejects(classify(fixture([file])), /Invalid/); + }); +} + +test('duplicate file entries cannot stand in for omitted files', async () => { + const file = { filename: 'release-notes/a.md', status: 'added' }; + await assert.rejects(classify(fixture([file, file])), /Duplicate/); +}); + +for (const change of [ + pr => { pr.head.sha = 'new-head'; }, + pr => { pr.base.ref = 'release/6.1'; }, + pr => { pr.changed_files++; }, + pr => { pr.milestone.state = 'closed'; }, + pr => { pr.milestone = null; }, + pr => { pr.milestone.title = '8.0.0-preview1'; }, +]) { + test(`stale events and concurrent PR changes fail closed: ${change}`, async () => { + const stale = fixture(); + change(stale.live); + await assert.rejects(classify(stale), /changed/); + const concurrent = fixture(); + const listFiles = concurrent.github.rest.pulls.listFiles; + concurrent.github.rest.pulls.listFiles = async args => { + const result = await listFiles(args); + change(concurrent.live); + return result; + }; + await assert.rejects(classify(concurrent), /changed/); + }); +} + +for (const filename of ['release-notes/a.md', 'src/Driver.cs']) { + test(`reconciliation permits main advancing before retrieval for ${filename}`, async () => { + const f = fixture([{ filename, status: 'modified' }]); + f.live.base.sha = 'advanced-main'; + assert.equal(await classify(f), filename.startsWith('release-notes/')); + assert.equal(f.calls.filter(([name]) => name === 'get').length, 2); + }); +} + +for (const initialBase of ['base-sha', 'advanced-main']) { + test(`base advancement during retrieval fails closed starting at ${initialBase}`, async () => { + const f = fixture(); + f.live.base.sha = initialBase; + const listFiles = f.github.rest.pulls.listFiles; + f.github.rest.pulls.listFiles = async args => { + const result = await listFiles(args); + f.live.base.sha = 'changed-during-retrieval'; + return result; + }; + await assert.rejects(classify(f), /changed/); + assert.equal(f.calls.filter(([name]) => name === 'listFiles').length, 1); + assert.equal(f.calls.filter(([name]) => name === 'get').length, 2); + }); +} + +for (const duringRetrieval of [false, true]) { + test(`missing live base SHA fails closed ${duringRetrieval ? 'after' : 'before'} retrieval`, async () => { + const f = fixture(); + if (duringRetrieval) { + const listFiles = f.github.rest.pulls.listFiles; + f.github.rest.pulls.listFiles = async args => { + const result = await listFiles(args); + delete f.live.base.sha; + return result; + }; + } else { + delete f.live.base.sha; + } + await assert.rejects(classify(f), /Missing live base commit identity/); + }); +} + +test('both existing check names and rule steps are retained with explicit true-only bypasses', () => { + const workflow = readFileSync(`${__dirname}/../../workflows/check-milestone.yml`, 'utf8'); + assert.match(workflow, /name: Validate milestone/); + assert.match(workflow, /name: Check milestone matches next SqlClient version/); + assert.equal((workflow.match(/return await classify\(\{ github, context, core \}\);/g) || []).length, 2); + assert.equal((workflow.match(/steps\.release-notes\.outputs\.result != 'true'/g) || []).length, 2); + assert.equal((workflow.match(/pull-requests: read/g) || []).length, 2); + assert.match(workflow, /if: github\.event\.pull_request\.milestone == null/); + assert.match(workflow, /milestone\.state != 'open'/); + assert.match(workflow, /check-milestone-branch\.sh/); + assert.match(workflow, /check-milestone-version\.sh/); +}); diff --git a/.github/workflows/check-milestone.yml b/.github/workflows/check-milestone.yml index 6a03679729..676bc8c8f8 100644 --- a/.github/workflows/check-milestone.yml +++ b/.github/workflows/check-milestone.yml @@ -20,6 +20,11 @@ # # See .github/scripts/check-milestone-branch.sh for the full rule set. # +# Exception: default-branch PRs changing only release-notes/** or CHANGELOG.md +# still require an open milestone, but skip branch and next-version rules. +# Both jobs use is-release-notes-only.cjs to verify the complete file set, +# including previous paths for renames. API errors/incomplete data fail closed. +# # Cutting release/X.Y flips the expected target for X.Y.* milestones but emits # no pull request activity. recheck-milestones.yml reconciles the already open # PRs that the new branch invalidates. @@ -62,8 +67,16 @@ jobs: sparse-checkout: .github/scripts sparse-checkout-cone-mode: false + - name: Classify release-notes-only changes + id: release-notes + uses: actions/github-script@v9 + with: + script: | + const classify = require('./.github/scripts/is-release-notes-only.cjs'); + return await classify({ github, context, core }); + - name: Check milestone matches target branch - if: github.event.pull_request.milestone != null + if: github.event.pull_request.milestone != null && steps.release-notes.outputs.result != 'true' env: # Pass PR data via env to avoid script injection from milestone text. MILESTONE_TITLE: ${{ github.event.pull_request.milestone.title }} @@ -78,6 +91,7 @@ jobs: runs-on: ubuntu-latest permissions: contents: read + pull-requests: read steps: - name: Checkout version check inputs uses: actions/checkout@v6 @@ -87,7 +101,16 @@ jobs: src/Microsoft.Data.SqlClient/Versions.props sparse-checkout-cone-mode: false + - name: Classify release-notes-only changes + id: release-notes + uses: actions/github-script@v9 + with: + script: | + const classify = require('./.github/scripts/is-release-notes-only.cjs'); + return await classify({ github, context, core }); + - name: Validate milestone version + if: steps.release-notes.outputs.result != 'true' env: MILESTONE_TITLE: ${{ github.event.pull_request.milestone.title }} BASE_REF: ${{ github.event.pull_request.base.ref }} diff --git a/.github/workflows/recheck-milestones.yml b/.github/workflows/recheck-milestones.yml index 679b76f144..f02d265ca8 100644 --- a/.github/workflows/recheck-milestones.yml +++ b/.github/workflows/recheck-milestones.yml @@ -14,6 +14,9 @@ # and make the next series eligible, but emits no pull request activity, so # already open pull requests would otherwise keep their previous verdicts. # +# Release-notes-only PRs remain in the reconciliation set. Their re-run uses +# the same file classifier and open-milestone gate as any other PR check. +# # This lives in its own workflow so that check-milestone.yml stays purely pull # request scoped, and so the elevated 'actions: write' permission needed to # re-run checks is isolated from the PR gate.