@@ -6,16 +6,20 @@ if (danger.github.pr.body.length === 0) {
6
6
fail ( 'Please include a body for your PR' ) ;
7
7
}
8
8
9
- const createOrAddLabelSafely = async ( name : string , color : string ) => {
9
+ const createOrAddLabelSafely = async ( name : string , color : string ) : boolean => {
10
10
try {
11
11
await danger . github . utils . createOrAddLabel ( {
12
12
name,
13
13
color : color . replace ( '#' , '' ) ,
14
14
description : '' ,
15
15
} ) ;
16
+
17
+ return true ;
16
18
} catch ( error ) {
17
19
console . warn ( error ) ;
18
20
warn ( `Was unable to create or add label "${ name } "` ) ;
21
+
22
+ return false ;
19
23
}
20
24
} ;
21
25
@@ -39,6 +43,18 @@ const labelBasedOnRules = async () => {
39
43
) ;
40
44
} ;
41
45
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
+
42
58
const labelBasedOnCommits = async ( ) => {
43
59
const commits = danger . github . commits . map ( commits => commits . commit . message ) ;
44
60
@@ -51,7 +67,20 @@ const labelBasedOnCommits = async () => {
51
67
}
52
68
} ;
53
69
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