-
Notifications
You must be signed in to change notification settings - Fork 236
/
dangerfile.ts
86 lines (70 loc) · 2.21 KB
/
dangerfile.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
import { parse } from 'path';
import { danger, warn } from 'danger';
// Ensure that people include a description on their PRs
if (danger.github.pr.body.length === 0) {
fail('Please include a body for your PR');
}
const createOrAddLabelSafely = async (name: string, color: string): boolean => {
try {
await danger.github.utils.createOrAddLabel({
name,
color: color.replace('#', ''),
description: '',
});
return true;
} catch (error) {
console.warn(error);
warn(`Was unable to create or add label "${name}"`);
return false;
}
};
const labelBasedOnRules = async () => {
const affectedRules = [
...danger.git.created_files,
...danger.git.modified_files,
...danger.git.deleted_files,
]
.filter(filename => {
const { dir, ext } = parse(filename);
return dir === 'src/rules' && ext === '.ts';
})
.map(filename => parse(filename).name);
await Promise.all(
affectedRules.map(rule =>
createOrAddLabelSafely(`rule: ${rule}`, '#7d3abc'),
),
);
};
const labelBasedOnTitle = async (): Promise<boolean> => {
if (danger.github.pr.title.startsWith('feat')) {
return createOrAddLabelSafely('enhancement', '#84b6eb');
}
if (danger.github.pr.title.startsWith('fix')) {
return createOrAddLabelSafely('bug', '#ee0701');
}
return false;
};
const labelBasedOnCommits = async () => {
const commits = danger.github.commits.map(commits => commits.commit.message);
if (commits.some(commit => commit.startsWith('fix'))) {
await createOrAddLabelSafely('bug', '#ee0701');
}
if (commits.some(commit => commit.startsWith('feat'))) {
await createOrAddLabelSafely('enhancement', '#84b6eb');
}
};
const labelBasedOnTitleOrCommits = async () => {
// prioritize labeling based on the title since most pull requests will get
// squashed into a single commit with the title as the subject, but fallback
// to looking through the commits if we can't determine a label from the title
if (await labelBasedOnTitle()) {
return;
}
await labelBasedOnCommits();
};
Promise.all([labelBasedOnRules(), labelBasedOnTitleOrCommits()]).catch(
error => {
console.error(error);
fail(`Something went very wrong: ${error}`);
},
);