From cc8bda1e89d48a453fb3a49dafd96a57e13d8a74 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 21:20:43 +0100 Subject: [PATCH] feat(pr-checks): no-co-authors fails on Co-authored-by trailers in the description or commits no-co-authors (G4) reads the description and every commit message through the API and names each trailer it finds with how to remove it. --- .github/pr-checks/no-co-authors.js | 36 +++++++++++++++++++++++++ .github/pr-checks/no-co-authors.test.js | 33 +++++++++++++++++++++++ .github/pr-checks/pull-request.js | 10 +++++++ .github/pr-checks/run.js | 6 ++++- .github/pr-checks/testing.js | 13 ++++++--- .github/workflows/pr-checks.yml | 2 +- crates/sourceorigin-standard/README.md | 2 ++ 7 files changed, 97 insertions(+), 5 deletions(-) create mode 100644 .github/pr-checks/no-co-authors.js create mode 100644 .github/pr-checks/no-co-authors.test.js diff --git a/.github/pr-checks/no-co-authors.js b/.github/pr-checks/no-co-authors.js new file mode 100644 index 0000000..54428ee --- /dev/null +++ b/.github/pr-checks/no-co-authors.js @@ -0,0 +1,36 @@ +'use strict'; + +/** A line that credits a co-author, in any case. */ +const TRAILER = /^\s*co-authored-by:.*$/im; + +/** + * G4: neither the description nor any commit message carries a + * `Co-authored-by` trailer. The description counts as written, comments + * included, as a squash merge may copy it into the commit on `main`. + */ +const noCoAuthors = { + name: 'no-co-authors', + + async findings(pr) { + const findings = []; + const body = TRAILER.exec(pr.body); + if (body) { + findings.push({ + message: `the description credits a co-author with \`${body[0].trim()}\`: delete that line.`, + }); + } + for (const { sha, message } of await pr.commits()) { + const trailer = TRAILER.exec(message); + if (!trailer) continue; + findings.push({ + message: + `commit ${sha.slice(0, 7)} ("${message.split('\n')[0]}") credits a co-author with ` + + `\`${trailer[0].trim()}\`: reword it without that line (\`git rebase -i\`), then push ` + + `with \`--force-with-lease\`.`, + }); + } + return findings; + }, +}; + +module.exports = noCoAuthors; diff --git a/.github/pr-checks/no-co-authors.test.js b/.github/pr-checks/no-co-authors.test.js new file mode 100644 index 0000000..f3f6de9 --- /dev/null +++ b/.github/pr-checks/no-co-authors.test.js @@ -0,0 +1,33 @@ +'use strict'; + +const test = require('node:test'); +const assert = require('node:assert/strict'); +const noCoAuthors = require('./no-co-authors.js'); +const { Fake } = require('./testing.js'); + +const commit = (sha, message) => ({ sha, commit: { message } }); + +test('a pull request with no co-author passes', async () => { + const commits = [commit('a'.repeat(40), 'feat: a\n\nMentions co-authored-by in prose.')]; + assert.deepEqual(await noCoAuthors.findings(await Fake.pr({ body: 'Body.' }, { commits })), []); +}); + +test('a trailer in the description fails, in any case and inside a comment', async () => { + for (const body of ['x\n\nCo-Authored-By: A ', '']) { + const [finding] = await noCoAuthors.findings(await Fake.pr({ body })); + assert.match(finding.message, /^the description credits a co-author with `co-authored-by: A `/i); + } +}); + +test('each commit with a trailer fails, named by its short SHA and subject', async () => { + const commits = [ + commit('1'.repeat(40), 'feat: a\n\nCo-authored-by: A '), + commit('2'.repeat(40), 'fix: b'), + commit('3'.repeat(40), 'docs: c\n\nCo-authored-by: C '), + ]; + const findings = await noCoAuthors.findings(await Fake.pr({}, { commits })); + assert.deepEqual( + findings.map(({ message }) => message.split(' credits')[0]), + ['commit 1111111 ("feat: a")', 'commit 3333333 ("docs: c")'], + ); +}); diff --git a/.github/pr-checks/pull-request.js b/.github/pr-checks/pull-request.js index 2afcb77..809bbb1 100644 --- a/.github/pr-checks/pull-request.js +++ b/.github/pr-checks/pull-request.js @@ -27,6 +27,16 @@ class PullRequest { return new PullRequest(data, { github, repo: context.repo }); } + /** Each commit's SHA and message, oldest first. */ + async commits() { + const commits = await this.github.paginate(this.github.rest.pulls.listCommits, { + ...this.repo, + pull_number: this.number, + per_page: 100, + }); + return commits.map(({ sha, commit }) => ({ sha, message: commit.message })); + } + /** The text of the file at `path` in commit `sha`, or `null` if it has * none. */ async fileAt(sha, path) { diff --git a/.github/pr-checks/run.js b/.github/pr-checks/run.js index 2abeeac..6616fe3 100644 --- a/.github/pr-checks/run.js +++ b/.github/pr-checks/run.js @@ -11,7 +11,11 @@ class Runner { /** Every check, each a job of pr-checks.yml's matrix: an object with a * `name`, `findings(pr, inputs)` and, optionally, `waiver(pr, inputs)`, * the reason findings shouldn't fail the job, or nothing. */ - static CHECKS = [require('./pr-title.js'), require('./pr-body.js')]; + static CHECKS = [ + require('./pr-title.js'), + require('./pr-body.js'), + require('./no-co-authors.js'), + ]; /** Runs the check named in `CHECK` with the inputs in `INPUTS`. Each * finding becomes an error annotation and a line of the job summary, and diff --git a/.github/pr-checks/testing.js b/.github/pr-checks/testing.js index 9769403..4589904 100644 --- a/.github/pr-checks/testing.js +++ b/.github/pr-checks/testing.js @@ -30,8 +30,9 @@ class Fake { /** A `github` and `context` for pull request number 1, from base commit * `base` to head commit `head`, with the fields of `pr` over a passing - * default, and `files[sha][path]` the text of each file. */ - static github(pr = {}, { files = {} } = {}) { + * default, `files[sha][path]` the text of each file and `commits` its + * commits, as `{ sha, commit: { message } }`. */ + static github(pr = {}, { files = {}, commits = [] } = {}) { const data = { number: 1, title: 'feat: a change', @@ -47,7 +48,13 @@ class Fake { return { data: text }; }; return { - github: { rest: { pulls: { get: async () => ({ data }) }, repos: { getContent } } }, + github: { + rest: { + pulls: { get: async () => ({ data }), listCommits: async () => ({ data: commits }) }, + repos: { getContent }, + }, + paginate: async (method, parameters) => (await method(parameters)).data, + }, context: { repo: { owner: 'o', repo: 'r' }, payload: { pull_request: { number: 1 } } }, }; } diff --git a/.github/workflows/pr-checks.yml b/.github/workflows/pr-checks.yml index d221c6e..e910e52 100644 --- a/.github/workflows/pr-checks.yml +++ b/.github/workflows/pr-checks.yml @@ -37,7 +37,7 @@ jobs: strategy: fail-fast: false matrix: - check: [pr-title, pr-body] + check: [pr-title, pr-body, no-co-authors] steps: - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 with: diff --git a/crates/sourceorigin-standard/README.md b/crates/sourceorigin-standard/README.md index 43fc385..2d4d5fa 100644 --- a/crates/sourceorigin-standard/README.md +++ b/crates/sourceorigin-standard/README.md @@ -136,6 +136,8 @@ check: `.github/pull_request_template.md`, each with more than the template's comments under it. An issue link is optional; one that closes an issue reads `Fixes #123`, or `Fixes PQD-123` where issues live in Linear. +- `no-co-authors`: neither the description nor a commit message has a + `Co-authored-by` trailer. A repository calls it from `.github/workflows/pr.yml`, pinned to a commit of this repository's `main`. Reviews and labels re-run it, as some checks