UNPKG

1.47 kBPlain TextView Raw
1import * as commitlint from '@commitlint/cli';
2import * as execa from 'execa';
3
4export default async function commitlintAzurePipelines() {
5 if (process.env.TF_BUILD !== 'True') {
6 throw new Error(
7 'commitlint-azure-pipelines-cli is designed to be used in Azure Pipelines.'
8 );
9 }
10
11 const { from, to } = await getCommitRange();
12
13 await lint(from, to);
14}
15
16function isPR() {
17 return process.env.BUILD_REASON === 'PullRequest';
18}
19
20async function getCommitRange(): Promise<{ from: string; to: string }> {
21 if (isPR()) {
22 return {
23 from: await lastTargetCommit(),
24 to: await lastPrCommit()
25 };
26 }
27
28 const commit = process.env.BUILD_SOURCEVERSION!;
29 return { from: commit, to: commit };
30}
31
32async function lastPrCommit(): Promise<string> {
33 const { stdout } = await execa('git', ['rev-parse', 'HEAD^2']);
34 return stdout;
35}
36
37async function lastTargetCommit(): Promise<string> {
38 const { stdout } = await execa('git', ['rev-parse', 'HEAD^1']);
39 return stdout;
40}
41
42async function lint(fromHash: string, toHash: string): Promise<void> {
43 if (fromHash === toHash) {
44 const { stdout } = await execa('git', [
45 'log',
46 '-n',
47 '1',
48 '--pretty=format:%B',
49 fromHash
50 ]);
51 await execa(commitlint, [], {
52 stdio: ['pipe', 'inherit', 'inherit'],
53 input: stdout
54 });
55 } else {
56 await execa(commitlint, ['--from', fromHash, '--to', toHash], {
57 stdio: ['pipe', 'inherit', 'inherit']
58 });
59 }
60}