Skip to content

Commit 2605f5a

Browse files
committed
Meta: add action to check commit messages
1 parent 618479a commit 2605f5a

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

.github/workflows/check-commit.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: 262 Commit Checker
2+
3+
on: pull_request
4+
5+
jobs:
6+
test:
7+
name: Check commit messages
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v1
11+
with:
12+
ref: ${{ github.event.pull_request.head.sha }}
13+
- run: git checkout HEAD^2 # to get the actual PR head
14+
- uses: actions/setup-node@v1
15+
with:
16+
node-version: '12.x'
17+
- run: node scripts/check-commit

scripts/check-commit.js

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/usr/bin/env node
2+
3+
'use strict';
4+
5+
const exec = require('child_process').execSync;
6+
7+
const oldestAncestor = String(exec(`bash -c 'diff -u <(git rev-list --first-parent "\${1:-origin/master}") <(git rev-list --first-parent "\${2:-HEAD}") | sed -ne "s/^ //p" | head -1' -`)).trim();
8+
9+
console.log(`Oldest ancestor SHA: ${oldestAncestor}`);
10+
11+
const messages = String(exec(`git log --format=%B ${oldestAncestor}..HEAD | sed '/^[[:space:]]*\$/d'`)).trim().split('\n').reverse();
12+
13+
const [first, ...rest] = messages;
14+
15+
const prefixes = ['Normative', 'Editorial', 'Meta', 'Layering'];
16+
const firstPrefixRE = new RegExp(`^(?:${prefixes.join('|')}): `);
17+
const prefixRE = new RegExp(`^(?:${prefixes.concat('fixup', 'squash').join('|')}): `);
18+
19+
const errors = [];
20+
if (!firstPrefixRE.test(first)) {
21+
errors.push(`First commit must start with a valid prefix (${prefixes.join(', ')}), followed by “: ”`);
22+
}
23+
24+
rest.forEach((msg) => {
25+
if (!prefixRE.test(msg)) {
26+
errors.push(`Commit “${msg}” must start with a valid prefix (${prefixes.join(', ')}) or “fixup” or “squash”, followed by “: ”`);
27+
}
28+
});
29+
30+
if (errors.length === 0) {
31+
console.log('Commit messages are valid!');
32+
} else {
33+
console.error(`Errors:
34+
${errors.map(x => ` - ${x}`).join('\n')}
35+
`);
36+
process.exitCode = errors.length;
37+
}

0 commit comments

Comments
 (0)