Skip to content

Commit b128499

Browse files
authored
ci: add github action and workflow to check pr title for commit message format (#377) (#380)
1 parent bba7273 commit b128499

File tree

5 files changed

+226
-73
lines changed

5 files changed

+226
-73
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name: Check input commit message
2+
description: Check an input string for Conventional Commits format and issue number
3+
inputs:
4+
message:
5+
description: String to check as a commit message
6+
required: true
7+
runs:
8+
using: "node20"
9+
main: "index.js"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const core = require("@actions/core");
2+
3+
async function runCommitlint(message) {
4+
const { default: load } = await import("@commitlint/load");
5+
const { default: lint } = await import("@commitlint/lint");
6+
const { default: format } = await import("@commitlint/format");
7+
8+
console.log("Checking for Conventional Commits format");
9+
10+
const config = await load();
11+
const report = await lint(message, config.rules, {
12+
parserOpts: config.parserPreset?.parserOpts,
13+
});
14+
const reportMessage = format({ results: [report] }, { verbose: true });
15+
16+
if (report.valid) console.log(reportMessage + "\n");
17+
else console.error(reportMessage + "\n");
18+
19+
return report.valid;
20+
}
21+
22+
function checkIssueNumber(message) {
23+
console.log(
24+
"Checking for a '#xxx' reference to a corresponding GitHub issue"
25+
);
26+
if (/#[0-9]/.test(message)) {
27+
console.log("Successfully found issue number\n");
28+
return true;
29+
} else {
30+
console.error("Issue number missing\n");
31+
return false;
32+
}
33+
}
34+
35+
async function checkMessage() {
36+
const message = core.getInput("message");
37+
const failedChecks = [];
38+
if (!checkIssueNumber(message)) failedChecks.push("issue number");
39+
if (!(await runCommitlint(message)))
40+
failedChecks.push("Conventional Commits");
41+
if (failedChecks.length > 0)
42+
core.setFailed(
43+
`Failed ${failedChecks.join(" and ")} check${failedChecks.length > 1 ? "s" : ""}`
44+
);
45+
}
46+
47+
checkMessage().catch((e) => core.setFailed(e.message));

.github/workflows/check-pr-title.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Check pull request title
2+
on:
3+
pull_request:
4+
types: [edited, opened, synchronize, reopened]
5+
6+
jobs:
7+
check-pr-title:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
- uses: actions/setup-node@v4
12+
with:
13+
node-version: "20.10.0"
14+
- name: Cache npm cache
15+
uses: actions/cache@v4
16+
with:
17+
path: ~/.npm
18+
key: ${{ runner.os }}-node-${{ hashFiles('package-lock.json') }}
19+
- name: Install dependencies
20+
run: npm ci
21+
- name: Check pull request title for Conventional Commits format and GitHub issue number
22+
uses: ./.github/actions/check-input-commit-message
23+
with:
24+
message: ${{ github.event.pull_request.title }}

0 commit comments

Comments
 (0)