Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add conventional commits enforcement #286

Merged
merged 2 commits into from
Aug 11, 2024
Merged
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
92 changes: 92 additions & 0 deletions .github/workflows/conventional-commits-lint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
"use strict";

const fs = require("fs");

const TITLE_PATTERN =
/^(?<prefix>[^:!(]+)(?<package>\([^)]+\))?(?<breaking>[!])?:.+$/;
const RELEASE_AS_DIRECTIVE = /^\s*Release-As:/im;
const BREAKING_CHANGE_DIRECTIVE = /^\s*BREAKING[ \t]+CHANGE:/im;

const ALLOWED_CONVENTIONAL_COMMIT_PREFIXES = [
"revert",
"feat",
"fix",
"ci",
"docs",
"chore",
"style",
"test",
"refactor",
];

const object = process.argv[2];
const payload = JSON.parse(fs.readFileSync(process.stdin.fd, "utf-8"));

let validate = [];

if (object === "pr") {
validate.push({
title: payload.pull_request.title,
content: payload.pull_request.body,
});
} else if (object === "push") {
validate.push(
...payload.commits
.map((commit) => ({
title: commit.message.split("\n")[0],
content: commit.message,
}))
.filter(({ title }) => !title.startsWith("Merge branch ") && !title.startsWith("Revert ")),
);
} else {
console.error(
`Unknown object for first argument "${object}", use 'pr' or 'push'.`,
);
process.exit(0);
}

let failed = false;

validate.forEach((payload) => {
if (payload.title) {
const match = payload.title.match(TITLE_PATTERN);
if (!match) {
return
}

const { groups } = match

if (groups) {
if (
!ALLOWED_CONVENTIONAL_COMMIT_PREFIXES.find(
(prefix) => prefix === groups.prefix,
)
) {
console.error(
`PR (or a commit in it) is using a disallowed conventional commit prefix ("${groups.prefix}"). Only ${ALLOWED_CONVENTIONAL_COMMIT_PREFIXES.join(", ")} are allowed. Make sure the prefix is lowercase!`,
);
failed = true;
}
} else {
console.error(
"PR or commit title must match conventional commit structure.",
);
failed = true;
}
}

if (payload.content) {
if (payload.content.match(RELEASE_AS_DIRECTIVE)) {
console.error(
"PR descriptions or commit messages must not contain Release-As conventional commit directives.",
);
failed = true;
}
}
});

if (failed) {
process.exit(1);
}

process.exit(0);
43 changes: 43 additions & 0 deletions .github/workflows/conventional-commits.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Check pull requests

on:
push:
branches-ignore: # Run the checks on all branches but the protected ones
- main
- release/*

pull_request_target:
branches:
- main
- release/*
types:
- opened
- edited
- reopened
- ready_for_review

jobs:
check-conventional-commits:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
with:
sparse-checkout: |
.github

- if: ${{ github.event_name == 'pull_request_target' }}
run: |
set -ex

node .github/workflows/conventional-commits-lint.js pr <<EOF
${{ toJSON(github.event) }}
EOF

- if: ${{ github.event_name == 'push' }}
run: |
set -ex

node .github/workflows/conventional-commits-lint.js push <<EOF
${{ toJSON(github.event) }}
EOF
Loading