forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidate-commits.ts
153 lines (130 loc) · 4.16 KB
/
validate-commits.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
// tslint:disable:no-implicit-dependencies
import { logging } from '@angular-devkit/core';
import { execSync } from 'child_process';
import { packages } from '../lib/packages';
const blacklist = [
'9ce1aed331ad0742463b587f1f5555486ccc202f',
'de7a44f23514594274394322adaf40ac87c38d8b',
];
export interface ValidateCommitsOptions {
ci?: boolean;
base?: string;
head?: string;
}
export default function (argv: ValidateCommitsOptions, logger: logging.Logger) {
logger.info('Getting merge base...');
const prNumber = process.env['CIRCLE_PR_NUMBER'] || '';
let baseSha = '';
let sha = '';
if (prNumber) {
const url = `https://api.github.com/repos/angular/angular-cli/pulls/${prNumber}`;
const prJson = JSON.parse(execSync(`curl "${url}"`, {
stdio: ['ignore', 'pipe', 'ignore'],
encoding: 'utf8',
}).toString());
baseSha = prJson['base']['sha'];
sha = prJson['head']['sha'];
} else if (argv.base) {
baseSha = argv.base;
sha = argv.head || 'HEAD';
} else {
const parentRemote = process.env['GIT_REMOTE'] ? process.env['GIT_REMOTE'] + '/' : '';
const parentBranch = process.env['GIT_BRANCH'] || 'master';
baseSha = execSync(`git merge-base --fork-point "${parentRemote}${parentBranch}"`)
.toString().trim();
sha = 'HEAD';
}
logger.createChild('sha').info(`Base: ${baseSha}\nHEAD: ${sha}`);
const log = execSync(`git log --oneline "${baseSha}..${sha}"`).toString().trim();
logger.debug('Commits:');
logger.createChild('commits').debug(log);
logger.debug('');
const commits = log.split(/\n/)
.map(i => i.match(/(^[0-9a-f]+) (.+)$/))
.map(x => x ? Array.from(x).slice(1) : null)
.filter(x => !!x) as [string, string][];
logger.info(`Found ${commits.length} commits...`);
const output = logger.createChild('check');
let invalidCount = 0;
function _invalid(sha: string, message: string, error: string) {
invalidCount++;
output.error(`The following commit ${error}:`);
output.error(` ${sha} ${message}`);
}
for (const [sha, message] of commits) {
if (blacklist.find(i => i.startsWith(sha))) {
// Some commits are better ignored.
continue;
}
const subject = message.match(/^([^:(]+)(?:\((.*?)\))?:/);
if (!subject) {
_invalid(sha, message, 'does not have a subject');
continue;
}
const [type, scope] = subject.slice(1);
switch (type) {
// Types that can contain both a scope or no scope.
case 'docs':
case 'refactor':
case 'style':
case 'test':
if (scope && !packages[scope]) {
_invalid(sha, message, 'has a scope that does not exist');
continue;
}
break;
// Types that MUST contain a scope.
case 'feat':
case 'fix':
if (!scope) {
_invalid(sha, message, 'should always have a scope');
continue;
}
if (!packages[scope]) {
_invalid(sha, message, 'has a scope that does not exist');
continue;
}
break;
// Types that MUST NOT contain a scope.
case 'build':
case 'revert':
case 'ci':
if (scope) {
_invalid(sha, message, 'should not have a scope');
continue;
}
break;
case 'release':
if (scope) {
_invalid(sha, message, 'should not have a scope');
continue;
}
if (argv.ci && commits.length > 1) {
_invalid(sha, message, 'release should always be alone in a PR');
continue;
}
break;
case 'wip':
if (argv.ci) {
_invalid(sha, message, 'wip are not allowed in a PR');
}
break;
// Unknown types.
default:
_invalid(sha, message, 'has an unknown type. You can use wip: to avoid this.');
}
}
if (invalidCount > 0) {
logger.fatal(`${invalidCount} commits were found invalid...`);
} else {
logger.info('All green. Thank you, come again.');
}
return invalidCount;
}