Skip to content

Commit 24e85c8

Browse files
authored
ci: update danger to do more stuff (#1200)
1 parent 1e81a25 commit 24e85c8

File tree

1 file changed

+51
-7
lines changed

1 file changed

+51
-7
lines changed

dangerfile.ts

+51-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,57 @@
1-
import { danger } from 'danger';
1+
import { parse } from 'path';
2+
import { danger, warn } from 'danger';
23

34
// Ensure that people include a description on their PRs
45
if (danger.github.pr.body.length === 0) {
56
fail('Please include a body for your PR');
67
}
78

8-
if (
9-
danger.git.created_files.find(filename => filename.startsWith('rules/')) &&
10-
!danger.git.modified_files.includes('README.md')
11-
) {
12-
fail('Please update the README when new rules are added');
13-
}
9+
const createOrAddLabelSafely = async (name: string, color: string) => {
10+
try {
11+
await danger.github.utils.createOrAddLabel({
12+
name,
13+
color,
14+
description: '',
15+
});
16+
} catch (error) {
17+
console.warn(error);
18+
warn(`Was unable to create or add label "${name}"`);
19+
}
20+
};
21+
22+
const labelBasedOnRules = async () => {
23+
const affectedRules = [
24+
...danger.git.created_files,
25+
...danger.git.modified_files,
26+
...danger.git.deleted_files,
27+
]
28+
.filter(filename => {
29+
const { dir, ext } = parse(filename);
30+
31+
return dir === 'src/rules' && ext === '.ts';
32+
})
33+
.map(filename => parse(filename).name);
34+
35+
await Promise.all(
36+
affectedRules.map(rule =>
37+
createOrAddLabelSafely(`rule: ${rule}`, '#7d3abc'),
38+
),
39+
);
40+
};
41+
42+
const labelBasedOnCommits = async () => {
43+
const commits = danger.github.commits.map(commits => commits.commit.message);
44+
45+
if (commits.some(commit => commit.startsWith('fix'))) {
46+
await createOrAddLabelSafely('bug', '#ee0701');
47+
}
48+
49+
if (commits.some(commit => commit.startsWith('feat'))) {
50+
await createOrAddLabelSafely('enhancement', '#84b6eb');
51+
}
52+
};
53+
54+
Promise.all([labelBasedOnRules(), labelBasedOnCommits()]).catch(error => {
55+
console.error(error);
56+
fail(`Something went very wrong: ${error}`);
57+
});

0 commit comments

Comments
 (0)