Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f8e910b

Browse files
authoredJan 17, 2025··
ci: prioritize labelling pull requests based on their title rather than their commits (#246)
1 parent 51ff4af commit f8e910b

File tree

1 file changed

+34
-5
lines changed

1 file changed

+34
-5
lines changed
 

‎dangerfile.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@ if (danger.github.pr.body.length === 0) {
66
fail('Please include a body for your PR');
77
}
88

9-
const createOrAddLabelSafely = async (name: string, color: string) => {
9+
const createOrAddLabelSafely = async (name: string, color: string): boolean => {
1010
try {
1111
await danger.github.utils.createOrAddLabel({
1212
name,
1313
color: color.replace('#', ''),
1414
description: '',
1515
});
16+
17+
return true;
1618
} catch (error) {
1719
console.warn(error);
1820
warn(`Was unable to create or add label "${name}"`);
21+
22+
return false;
1923
}
2024
};
2125

@@ -39,6 +43,18 @@ const labelBasedOnRules = async () => {
3943
);
4044
};
4145

46+
const labelBasedOnTitle = async (): Promise<boolean> => {
47+
if (danger.github.pr.title.startsWith('feat')) {
48+
return createOrAddLabelSafely('enhancement', '#84b6eb');
49+
}
50+
51+
if (danger.github.pr.title.startsWith('fix')) {
52+
return createOrAddLabelSafely('bug', '#ee0701');
53+
}
54+
55+
return false;
56+
};
57+
4258
const labelBasedOnCommits = async () => {
4359
const commits = danger.github.commits.map(commits => commits.commit.message);
4460

@@ -51,7 +67,20 @@ const labelBasedOnCommits = async () => {
5167
}
5268
};
5369

54-
Promise.all([labelBasedOnRules(), labelBasedOnCommits()]).catch(error => {
55-
console.error(error);
56-
fail(`Something went very wrong: ${error}`);
57-
});
70+
const labelBasedOnTitleOrCommits = async () => {
71+
// prioritize labeling based on the title since most pull requests will get
72+
// squashed into a single commit with the title as the subject, but fallback
73+
// to looking through the commits if we can't determine a label from the title
74+
if (await labelBasedOnTitle()) {
75+
return;
76+
}
77+
78+
await labelBasedOnCommits();
79+
};
80+
81+
Promise.all([labelBasedOnRules(), labelBasedOnTitleOrCommits()]).catch(
82+
error => {
83+
console.error(error);
84+
fail(`Something went very wrong: ${error}`);
85+
},
86+
);

0 commit comments

Comments
 (0)
Please sign in to comment.