Skip to content

Commit

Permalink
ci: fixes for validate-commit-message
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed May 2, 2017
1 parent 646c1b0 commit 6015a03
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 16 deletions.
30 changes: 17 additions & 13 deletions scripts/test-commit-messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,47 +41,51 @@ execSync('git fetch origin');
// Find the branch
const branchRefs = {};
for (const name of config['branches']) {
const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' });
if (output) {
branchRefs[name] = output;
try {
const output = execSync(`git show-ref --hash ${name}`, { encoding: 'utf-8' });
if (output) {
branchRefs[name] = output.replace(/\n/g, '').trim();
}
} catch (e) {
// Ignore.
}
}
logger.info(`Found refs for branches:\n ${Object.entries(branchRefs).forEach(([key, value]) => {
return `${key} => ${value}`;
logger.info(`Found refs for branches:\n ${Object.keys(branchRefs).map(key => {
return `${key} => ${JSON.stringify(branchRefs[key])}`;
}).join('\n ')}`);


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

if (output.length === 0) {
logger.warn('There are zero new commits between this HEAD and master');
return;
process.exit(0);
}

const commitByLines = [];
const commitsByLine = [];
let branch = null;

// Finding the closest branch marker.
for (const line of output.split(/n/)) {
const [hash, ...messageArray] = line.split(/ /);
for (const line of output.split(/\n/)) {
const [hash, ...messageArray] = line.split(' ');
const message = messageArray.join(' ');

const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] == hash);
const maybeBranch = Object.keys(branchRefs).find(branchName => branchRefs[branchName] === hash);
if (maybeBranch) {
branch = maybeBranch;
break;
}
commitByLines.push(message);
commitsByLine.push(message);
}

if (!branch) {
logger.fatal('Something wrong happened.');
return;
process.exit(1);
}

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

const someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
const someCommitsInvalid = !commitsByLine.every(message => validateCommitMessage(message, branch));

if (someCommitsInvalid) {
logger.error('Please fix the failing commit messages before continuing...');
Expand Down
6 changes: 3 additions & 3 deletions scripts/validate-commit-message/validate-commit-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ module.exports = function(commitSubject, branch) {

const type = match[2];
const types = Object.keys(config['types']);
if (!(type in types)) {
error(`${type} is not an allowed type.\n => TYPES: ${types.join(', ')}`, commitSubject);
if (types.indexOf(type) === -1) {
error(`"${type}" is not an allowed type.\n => TYPES: "${types.join('", "')}"`, commitSubject);
return false;
}
if (types[type] !== "" && types[type] !== branch) {
if (config['types'][type] !== '' && config['types'][type] !== branch) {
error(`${type} is not allowed to be on branch ${branch}.`, commitSubject);
return false;
}
Expand Down

0 comments on commit 6015a03

Please sign in to comment.