Skip to content

Commit 6015a03

Browse files
committed
ci: fixes for validate-commit-message
1 parent 646c1b0 commit 6015a03

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

scripts/test-commit-messages.js

+17-13
Original file line numberDiff line numberDiff line change
@@ -41,47 +41,51 @@ execSync('git fetch origin');
4141
// Find the branch
4242
const branchRefs = {};
4343
for (const name of config['branches']) {
44-
const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' });
45-
if (output) {
46-
branchRefs[name] = output;
44+
try {
45+
const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' });
46+
if (output) {
47+
branchRefs[name] = output.replace(/\n/g, '').trim();
48+
}
49+
} catch (e) {
50+
// Ignore.
4751
}
4852
}
49-
logger.info(`Found refs for branches:\n ${Object.entries(branchRefs).forEach(([key, value]) => {
50-
return `${key} => ${value}`;
53+
logger.info(`Found refs for branches:\n ${Object.keys(branchRefs).map(key => {
54+
return `${key} => ${JSON.stringify(branchRefs[key])}`;
5155
}).join('\n ')}`);
5256

5357

5458
const output = execSync('git log --format="%H %s" --no-merges', { encoding: 'utf-8' });
5559

5660
if (output.length === 0) {
5761
logger.warn('There are zero new commits between this HEAD and master');
58-
return;
62+
process.exit(0);
5963
}
6064

61-
const commitByLines = [];
65+
const commitsByLine = [];
6266
let branch = null;
6367

6468
// Finding the closest branch marker.
65-
for (const line of output.split(/n/)) {
66-
const [hash, ...messageArray] = line.split(/ /);
69+
for (const line of output.split(/\n/)) {
70+
const [hash, ...messageArray] = line.split(' ');
6771
const message = messageArray.join(' ');
6872

69-
const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] == hash);
73+
const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] === hash);
7074
if (maybeBranch) {
7175
branch = maybeBranch;
7276
break;
7377
}
74-
commitByLines.push(message);
78+
commitsByLine.push(message);
7579
}
7680

7781
if (!branch) {
7882
logger.fatal('Something wrong happened.');
79-
return;
83+
process.exit(1);
8084
}
8185

8286
logger.info(`Examining ${commitsByLine.length} commit(s) between HEAD and ${branch}`);
8387

84-
const someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
88+
const someCommitsInvalid = !commitsByLine.every(message => validateCommitMessage(message, branch));
8589

8690
if (someCommitsInvalid) {
8791
logger.error('Please fix the failing commit messages before continuing...');

scripts/validate-commit-message/validate-commit-message.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ module.exports = function(commitSubject, branch) {
3939

4040
const type = match[2];
4141
const types = Object.keys(config['types']);
42-
if (!(type in types)) {
43-
error(`${type} is not an allowed type.\n => TYPES: ${types.join(', ')}`, commitSubject);
42+
if (types.indexOf(type) === -1) {
43+
error(`"${type}" is not an allowed type.\n => TYPES: "${types.join('", "')}"`, commitSubject);
4444
return false;
4545
}
46-
if (types[type] !== "" && types[type] !== branch) {
46+
if (config['types'][type] !== '' && config['types'][type] !== branch) {
4747
error(`${type} is not allowed to be on branch ${branch}.`, commitSubject);
4848
return false;
4949
}

0 commit comments

Comments
 (0)