forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-commit-messages.js
More file actions
92 lines (74 loc) · 2.72 KB
/
Copy pathtest-commit-messages.js
File metadata and controls
92 lines (74 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
require('../lib/bootstrap-local');
const fs = require('fs');
const path = require('path');
const validateCommitMessage = require('./validate-commit-message');
const execSync = require('child_process').execSync;
const chalk = require('chalk');
const Logger = require('@ngtools/logger').Logger;
const configPath = path.resolve(__dirname, './validate-commit-message/commit-message.json');
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
require('rxjs/add/operator/filter');
// Configure logger
const logger = new Logger('test-commit-messages');
logger.subscribe((entry) => {
let color = chalk.white;
let output = process.stdout;
switch (entry.level) {
case 'info': color = chalk.white; break;
case 'warn': color = chalk.yellow; break;
case 'error': color = chalk.red; output = process.stderr; break;
case 'fatal': color = (x) => chalk.bold(chalk.red(x)); output = process.stderr; break;
}
output.write(color(entry.message) + '\n');
});
logger
.filter((entry) => entry.level === 'fatal')
.subscribe(() => {
process.stderr.write('A fatal error happened. See details above.');
process.exit(1);
});
// Note: This is based on the gulp task found in the angular/angular repository
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;
}
}
logger.info(`Found refs for branches:\n ${Object.entries(branchRefs).forEach(([key, value]) => {
return `${key} => ${value}`;
}).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;
}
const commitByLines = [];
let branch = null;
// Finding the closest branch marker.
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);
if (maybeBranch) {
branch = maybeBranch;
break;
}
commitByLines.push(message);
}
if (!branch) {
logger.fatal('Something wrong happened.');
return;
}
logger.info(`Examining ${commitsByLine.length} commit(s) between HEAD and ${branch}`);
const someCommitsInvalid = !commitsByLine.every(validateCommitMessage);
if (someCommitsInvalid) {
logger.error('Please fix the failing commit messages before continuing...');
logger.fatal(
'Commit message guidelines: https://github.com/angular/angular-cli/blob/master/CONTRIBUTING.md#commit');
} else {
logger.info('All commit messages are valid.');
}