diff --git a/.github/bylaws-protection.md b/.github/bylaws-protection.md new file mode 100644 index 0000000..de3e9ad --- /dev/null +++ b/.github/bylaws-protection.md @@ -0,0 +1,26 @@ +# Bylaws approval policy + +The independent `bylaws-approval` commit status protects `docs/bylaws.md` +(including additions, amendments, removals, and both rename directions) +and this policy, its verifier, its review signal, and its regression tests. + +A protected change on a PR targeting main requires an explicit APPROVED +review of the current head SHA by a different human with repository admin +permission. Write access alone, stale reviews, self approvals, bots, and +dismissed or superseded approvals do not qualify. Unrelated changes +automatically receive a successful bylaws status. API failures fail closed. + +## Temporary exception + +Until December 31, 2026, at 00:00 UTC, a PR authored by the human GitHub +account `JFWooten4` (ID `60260750`) can pass without independent approval +if the author retains admin permission and the PR only modifies +`docs/bylaws.md`. Additions, deletions, renames, verifier changes, and mixed +PRs are excluded. This is a repository review exception, not a substitute +for any legally required governance action. + +The cutoff applies at verification time. The trusted workflow rechecks open +PRs daily at 00:07 UTC and can be dispatched manually from main. GitHub +may delay scheduled execution; an already successful status is not +automatically revoked at the exact cutoff. Recheck outstanding exempt PRs +at expiration and remove the transition exception when no longer needed. diff --git a/.github/scripts/bylaws-review.cjs b/.github/scripts/bylaws-review.cjs new file mode 100644 index 0000000..483984a --- /dev/null +++ b/.github/scripts/bylaws-review.cjs @@ -0,0 +1,102 @@ +'use strict'; + +const STATUS_CONTEXT = 'bylaws-approval'; +// Temporary transition exception; remove after bylaws governance is established. +const BYLAWS_EXCEPTION_UNTIL = Date.parse('2026-12-31T00:00:00Z'); +const POLICY_PATHS = new Set([ + 'docs/bylaws.md', + '.github/bylaws-protection.md', + '.github/workflows/bylaws-review.yml', + '.github/workflows/bylaws-review-signal.yml', + '.github/scripts/bylaws-review.cjs', + '.github/scripts/bylaws-review.test.cjs' +]); + +function bylawsProtectedFiles(files) { + const matches = new Set(); + for (const file of files) { + for (const path of [file.filename, file.previous_filename]) { + if (POLICY_PATHS.has(path)) matches.add(path); + } + } + return [...matches].sort(); +} + +function approvedReviewers(reviews, headSha, author) { + const decisions = new Map(); + for (const review of [...reviews].sort((a, b) => a.id - b.id)) { + const login = review.user?.login; + if (!login || login.toLowerCase() === author.toLowerCase()) continue; + if (['APPROVED', 'CHANGES_REQUESTED', 'DISMISSED'].includes(review.state)) { + decisions.set(login.toLowerCase(), review); + } + } + return [...decisions.values()].filter(review => + review.state === 'APPROVED' && review.commit_id === headSha && + review.user.type === 'User' && !review.user.login.endsWith('[bot]') + ).map(review => review.user.login); +} + +function temporaryBylawsException(pr, files, now) { + return now < BYLAWS_EXCEPTION_UNTIL && + pr.user?.id === 60260750 && + pr.user?.login?.toLowerCase() === 'jfwooten4' && + pr.user?.type === 'User' && + files.length === 1 && + files[0].filename === 'docs/bylaws.md' && + files[0].status === 'modified' && + !files[0].previous_filename; +} + +async function run({ github, context, core, pull_number, now = Date.now() }) { + const { owner, repo } = context.repo; + const prNumber = pull_number || context.payload.pull_request?.number; + if (!prNumber) throw new Error('No PR associated with event'); + const { data: pr } = await github.rest.pulls.get({ owner, repo, pull_number: prNumber }); + if (pr.base.ref !== 'main' || pr.state === 'closed') return; + const sha = pr.head.sha; + async function status(state, description) { + await github.rest.repos.createCommitStatus({ + owner, repo, sha, context: STATUS_CONTEXT, state, + description: description.slice(0, 140), + target_url: context.serverUrl + '/' + owner + '/' + repo + '/actions/runs/' + context.runId + }); + } + async function isAdmin(username) { + const { data } = await github.rest.repos.getCollaboratorPermissionLevel({ owner, repo, username }); + return data.permission === 'admin'; + } + try { + const files = await github.paginate(github.rest.pulls.listFiles, { + owner, repo, pull_number: prNumber, per_page: 100 + }); + if (files.length >= 3000) throw new Error('PR has 3000+ files; cannot verify complete changes'); + const paths = bylawsProtectedFiles(files); + if (!paths.length) { + await status('success', 'No bylaws approval required'); + return; + } + await status('pending', 'Checking bylaws approval'); + if (temporaryBylawsException(pr, files, now) && await isAdmin(pr.user.login)) { + await status('success', 'Temporary JFWooten4 bylaws amendment exception'); + return; + } + const reviews = await github.paginate(github.rest.pulls.listReviews, { + owner, repo, pull_number: prNumber, per_page: 100 + }); + for (const reviewer of approvedReviewers(reviews, sha, pr.user.login)) { + if (await isAdmin(reviewer)) { + await status('success', 'Bylaws changes approved by ' + reviewer); + return; + } + } + await status('failure', 'Bylaws changes require admin approval'); + core.setFailed('Changes to ' + paths.join(', ') + + ' require a fresh APPROVED review from a different human with admin access on SHA ' + sha); + } catch (error) { + await status('failure', 'Unable to verify bylaws approvals'); + throw error; + } +} + +module.exports = { run, bylawsProtectedFiles, approvedReviewers }; diff --git a/.github/scripts/bylaws-review.test.cjs b/.github/scripts/bylaws-review.test.cjs new file mode 100644 index 0000000..6db9106 --- /dev/null +++ b/.github/scripts/bylaws-review.test.cjs @@ -0,0 +1,111 @@ +'use strict'; + +const { test } = require('node:test'); +const assert = require('node:assert/strict'); +const { run, bylawsProtectedFiles } = require('./bylaws-review.cjs'); + +const author = { id: 100, login: 'author', type: 'User' }; +const exceptionAuthor = { id: 60260750, login: 'JFWooten4', type: 'User' }; +const amendment = { filename: 'docs/bylaws.md', status: 'modified' }; +const beforeExpiry = Date.parse('2026-12-30T23:59:59Z'); +const atExpiry = Date.parse('2026-12-31T00:00:00Z'); +const context = { repo: { owner: 'blocktransfer', repo: 'org-docs' }, + payload: { pull_request: { number: 1 } }, serverUrl: 'https://github.com', runId: 10 }; +const approval = { id: 1, user: { login: 'reviewer', type: 'User' }, state: 'APPROVED', commit_id: 'head' }; + +function fake({ files = [amendment], reviews = [], permission = 'admin', + prAuthor = author, throwFiles = false, throwPermissions = false } = {}) { + const statuses = [], failures = []; + const github = { rest: { + pulls: { + get: async () => ({ data: { state: 'open', base: { ref: 'main' }, head: { sha: 'head' }, user: prAuthor } }), + listFiles() {}, listReviews() {} + }, + repos: { + createCommitStatus: async item => { statuses.push(item); }, + getCollaboratorPermissionLevel: async () => { + if (throwPermissions) throw new Error('permission API failed'); + return { data: { permission } }; + } + } + }}; + github.paginate = async method => { + if (method === github.rest.pulls.listFiles) { + if (throwFiles) throw new Error('files API failed'); + return files; + } + return reviews; + }; + return { github, statuses, failures, core: { setFailed: error => failures.push(error) } }; +} +async function check(options, expected, now = beforeExpiry) { + const f = fake(options); + await run({ github: f.github, core: f.core, context, now }); + assert.equal(f.statuses.at(-1).state, expected); + assert.ok(f.statuses.every(s => s.context === 'bylaws-approval' && s.sha === 'head')); + assert.equal(f.failures.length, expected === 'failure' ? 1 : 0); + return f; +} + +test('ordinary docs and archive changes do not require bylaws approval', async () => { + await check({ files: [{ filename: 'docs/intro.md' }, { filename: 'versions/archive-0.1.0/bylaws.md' }] }, 'success'); +}); +test('bylaws path and both rename directions are protected', () => { + for (const file of [ + { filename: 'docs/bylaws.md', status: 'added' }, amendment, + { filename: 'docs/bylaws.md', status: 'removed' }, + { filename: 'docs/relocated.md', previous_filename: 'docs/bylaws.md' }, + { filename: 'docs/bylaws.md', previous_filename: 'docs/old.md' } + ]) assert.deepEqual(bylawsProtectedFiles([file]), ['docs/bylaws.md']); +}); +test('the bylaws verifier and policy protect themselves', async () => { + for (const filename of [ + '.github/bylaws-protection.md', '.github/workflows/bylaws-review.yml', + '.github/workflows/bylaws-review-signal.yml', '.github/scripts/bylaws-review.cjs', + '.github/scripts/bylaws-review.test.cjs' + ]) await check({ files: [{ filename }] }, 'failure'); +}); +test('independent current-head admin approval passes', async () => { + await check({ reviews: [approval] }, 'success'); +}); +for (const [label, options] of [ + ['missing review', {}], + ['write permission', { reviews: [approval], permission: 'write' }], + ['read permission', { reviews: [approval], permission: 'read' }], + ['self approval', { reviews: [{ ...approval, user: author }] }], + ['bot approval', { reviews: [{ ...approval, user: { login: 'bot[bot]', type: 'Bot' } }] }], + ['stale approval', { reviews: [{ ...approval, commit_id: 'old' }] }], + ['dismissed approval', { reviews: [approval, { ...approval, id: 2, state: 'DISMISSED' }] }], + ['changes requested', { reviews: [approval, { ...approval, id: 2, state: 'CHANGES_REQUESTED' }] }] +]) test('rejects ' + label, async () => { await check(options, 'failure'); }); + +test('temporary exception permits the designated admin to amend only the bylaws', async () => { + const f = await check({ prAuthor: exceptionAuthor }, 'success'); + assert.match(f.statuses.at(-1).description, /Temporary JFWooten4/); +}); +for (const [label, options, now = beforeExpiry] of [ + ['different account', { prAuthor: { ...exceptionAuthor, id: 101 } }], + ['non-admin author', { permission: 'write' }], + ['bot author', { prAuthor: { ...exceptionAuthor, type: 'Bot' } }], + ['expiration', {}, atExpiry], + ['deletion', { files: [{ ...amendment, status: 'removed' }] }], + ['addition', { files: [{ ...amendment, status: 'added' }] }], + ['rename', { files: [{ ...amendment, status: 'renamed', previous_filename: 'docs/old.md' }] }], + ['mixed archive changes', { files: [amendment, { filename: 'versions/archive-0.1.0/bylaws.md' }] }], + ['policy changes', { files: [{ filename: '.github/bylaws-protection.md' }] }], + ['mixed ordinary changes', { files: [amendment, { filename: 'docs/intro.md' }] }] +]) test('temporary exception rejects ' + label, async () => { + await check({ prAuthor: exceptionAuthor, ...options }, 'failure', now); +}); + +test('API errors and incomplete changed-file listings fail closed', async () => { + for (const [options, error] of [ + [{ throwFiles: true }, /files API failed/], + [{ reviews: [approval], throwPermissions: true }, /permission API failed/], + [{ files: Array.from({ length: 3000 }, (_, i) => ({ filename: 'docs/' + i })) }, /3000/] + ]) { + const f = fake(options); + await assert.rejects(run({ github: f.github, core: f.core, context }), error); + assert.equal(f.statuses.at(-1).state, 'failure'); + } +}); diff --git a/.github/workflows/bylaws-review-signal.yml b/.github/workflows/bylaws-review-signal.yml new file mode 100644 index 0000000..fd2adf8 --- /dev/null +++ b/.github/workflows/bylaws-review-signal.yml @@ -0,0 +1,23 @@ +name: Bylaws review signal + +on: + pull_request_review: + types: [submitted, dismissed, edited] + +# Review workflows can be changed by a contributor's PR; NEVER give this write access. +permissions: + contents: read + +jobs: + signal: + runs-on: ubuntu-latest + steps: + - name: Record PR number as untrusted data + env: + PR_NUMBER: ${{ github.event.pull_request.number }} + run: printf '%s\n' "$PR_NUMBER" > pr-number.txt + - uses: actions/upload-artifact@v4 + with: + name: bylaws-review-pr + path: pr-number.txt + retention-days: 1 diff --git a/.github/workflows/bylaws-review.yml b/.github/workflows/bylaws-review.yml new file mode 100644 index 0000000..f9c7f80 --- /dev/null +++ b/.github/workflows/bylaws-review.yml @@ -0,0 +1,71 @@ +name: Bylaws review + +on: + pull_request_target: + branches: [main] + types: [opened, reopened, synchronize, ready_for_review, edited] + schedule: + - cron: '7 0 * * *' + workflow_dispatch: + workflow_run: + workflows: [Bylaws review signal] + types: [completed] + +permissions: + contents: read + pull-requests: read + actions: read + statuses: write + +jobs: + verify-bylaws: + if: github.event_name != 'workflow_run' || (github.event.workflow_run.event == 'pull_request_review' && github.event.workflow_run.conclusion == 'success') + runs-on: ubuntu-latest + steps: + # The workflow and checked-out code both originate from trusted main. + - uses: actions/checkout@v4 + with: + ref: main + persist-credentials: false + - uses: actions/github-script@v7 + with: + script: | + const { run } = require('./.github/scripts/bylaws-review.cjs'); + let pull_number = context.payload.pull_request?.number; + if (context.eventName === 'workflow_run') { + // The artifact is untrusted: extract ONLY a decimal PR number. + const { owner, repo } = context.repo; + const run_id = context.payload.workflow_run.id; + const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, { + owner, repo, run_id, per_page: 100 + }); + const artifact = artifacts.find(a => a.name === 'bylaws-review-pr' && !a.expired); + if (!artifact) throw new Error('Review signal artifact is missing'); + const response = await github.rest.actions.downloadArtifact({ + owner, repo, artifact_id: artifact.id, archive_format: 'zip' + }); + const fs = require('node:fs'); + const cp = require('node:child_process'); + const filename = process.env.RUNNER_TEMP + '/review-signal.zip'; + fs.writeFileSync(filename, Buffer.from(response.data)); + const text = cp.execFileSync('unzip', ['-p', filename, 'pr-number.txt'], { + encoding: 'utf8', maxBuffer: 1024 + }).trim(); + if (!/^[1-9][0-9]{0,8}$/.test(text)) throw new Error('Invalid review signal PR number'); + pull_number = Number(text); + } + if (context.eventName === 'schedule' || context.eventName === 'workflow_dispatch') { + const { owner, repo } = context.repo; + const prs = await github.paginate(github.rest.pulls.list, { + owner, repo, state: 'open', base: 'main', per_page: 100 + }); + for (const pr of prs) { + try { + await run({ github, context, core, pull_number: pr.number }); + } catch (error) { + core.setFailed('PR #' + pr.number + ': ' + error.message); + } + } + } else { + await run({ github, context, core, pull_number }); + } diff --git a/.github/workflows/documentation-tests.yml b/.github/workflows/documentation-tests.yml index e5163fc..ccac1c5 100644 --- a/.github/workflows/documentation-tests.yml +++ b/.github/workflows/documentation-tests.yml @@ -23,6 +23,8 @@ jobs: run: node --test .github/scripts/section-sidebars.test.cjs - name: Test archive approval policy run: node --test .github/scripts/version-archive-review.test.cjs + - name: Test bylaws approval policy + run: node --test .github/scripts/bylaws-review.test.cjs - name: Test historical asset link handling run: node --test .github/scripts/remark-archived-assets.test.cjs - name: Test 404 history links