Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions .github/pr-checks/no-co-authors.js
Original file line number Diff line number Diff line change
@@ -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;
33 changes: 33 additions & 0 deletions .github/pr-checks/no-co-authors.test.js
Original file line number Diff line number Diff line change
@@ -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 <a@b>', '<!--\nco-authored-by: A <a@b>\n-->']) {
const [finding] = await noCoAuthors.findings(await Fake.pr({ body }));
assert.match(finding.message, /^the description credits a co-author with `co-authored-by: A <a@b>`/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 <a@b>'),
commit('2'.repeat(40), 'fix: b'),
commit('3'.repeat(40), 'docs: c\n\nCo-authored-by: C <c@d>'),
];
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")'],
);
});
10 changes: 10 additions & 0 deletions .github/pr-checks/pull-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion .github/pr-checks/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 10 additions & 3 deletions .github/pr-checks/testing.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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 } } },
};
}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions crates/sourceorigin-standard/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down