ci: validate the message of each new commit as part of the CI linting
This patch adds the gulp command of `validate-commit-messages` which will validate the range of commits messages present in the active branch. This check now runs on CI as part of the linting checks. Allowed commit message types and scopes are controlled via commit-message.json file and documented at https://github.com/angular/angular/blob/master/CONTRIBUTING.md#-commit-message-guidelines This solution is based on old Vojta's code that he wrote for angular/angular.js, that was later adjusted by @matsko in #13815. Ideally we should switch over to something like https://www.npmjs.com/package/commitplease as suggested in #9953 but that package currently doesn't support strict scope checking, which is one of the primarily goal of this PR. Note that this PR removes support for "chore" which was previously overused by everyone on the team. Closes #13815 Fixes #3337
This commit is contained in:

committed by
Alex Rickabaugh

parent
ba52b2e08c
commit
dff6ee3272
52
tools/validate-commit-message/validate-commit-message.js
Normal file
52
tools/validate-commit-message/validate-commit-message.js
Normal file
@ -0,0 +1,52 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
/**
|
||||
* GIT commit message format enforcement
|
||||
*
|
||||
* Note: this script was originally written by Vojta for AngularJS :-)
|
||||
*/
|
||||
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const configPath = path.resolve(__dirname, './commit-message.json');
|
||||
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
|
||||
const PATTERN = /^(revert\: )?(\w+)(?:\(([^)]+)\))?\: (.+)$/;
|
||||
|
||||
module.exports = function(commitSubject) {
|
||||
if (commitSubject.length > config['maxLength']) {
|
||||
error(`The commit message is longer than ${config['maxLength']} characters`, commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
const match = PATTERN.exec(commitSubject);
|
||||
if (!match || match[2] === 'revert') {
|
||||
error(
|
||||
`The commit message does not match the format of "<type>(<scope>): <subject> OR revert: type(<scope>): <subject>"`,
|
||||
commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
const type = match[2];
|
||||
if (config['types'].indexOf(type) === -1) {
|
||||
error(
|
||||
`${type} is not an allowed type.\n => TYPES: ${config['types'].join(', ')}`, commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
const scope = match[3];
|
||||
|
||||
if (scope && !config['scopes'].includes(scope)) {
|
||||
error(
|
||||
`"${scope}" is not an allowed scope.\n => SCOPES: ${config['scopes'].join(', ')}`,
|
||||
commitSubject);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
};
|
||||
|
||||
function error(errorMessage, commitMessage) {
|
||||
console.error(`INVALID COMMIT MSG: "${commitMessage}"\n => ERROR: ${errorMessage}`);
|
||||
}
|
Reference in New Issue
Block a user