@@ -10,84 +10,20 @@ const createEnumerationFromArray = (stringArray) =>
10
10
: stringArray . map ( ( s ) => `\`${ s } \`` ) . join ( '' ) ;
11
11
12
12
const typeLabels = [
13
- 'bug ' ,
14
- 'regression' , // a sub- type of bug but we flatten it.
15
- 'enhancement ' ,
16
- 'new feature ' ,
17
- // Those are not "Types" labels, they are "Kind of work" labels, but we don't want to have to handle the overhead of
18
- // setting “Types” labels with them.
19
- 'release' ,
13
+ 'type: release ' ,
14
+ 'type: bug' ,
15
+ 'type: regression ' ,
16
+ 'type: improvement ' ,
17
+ 'type: new feature' ,
18
+ 'type: general' ,
19
+ // only used by renovate bot so we can ignore the "type: " prefix here
20
20
'dependencies' ,
21
21
] ;
22
22
const labelRegex = new RegExp ( `\\b(${ typeLabels . join ( '|' ) } )\\b` , 'i' ) ;
23
23
24
- const COMMENT_FIRST_LINE = {
25
- NO_LABELS_COMMENT :
26
- 'Please add one type label to categorize the purpose of this PR appropriately:' ,
27
- MULTIPLE_LABELS_COMMENT : 'Multiple type labels found:' ,
28
- SUCCESS_COMMENT : 'Thanks for adding a type label to the PR! 👍' ,
29
- } ;
30
-
31
- /**
32
- * @param {Awaited<ReturnType<ReturnType<import("@actions/github").getOctokit>['rest']['issues']['listComments']>>['data'] } comments
33
- */
34
- function containsAny ( comments ) {
35
- return (
36
- comments . find ( ( comment ) =>
37
- Object . values ( COMMENT_FIRST_LINE ) . some ( ( startLine ) => comment . body ?. startsWith ( startLine ) ) ,
38
- ) ?? false
39
- ) ;
40
- }
41
-
42
- /**
43
- * @param {Object } params
44
- * @param {import("@actions/core") } params.core
45
- * @param {ReturnType<import("@actions/github").getOctokit> } params.github
46
- * @param {import("@actions/github").context } params.context
47
- * @param {Awaited<ReturnType<ReturnType<import("@actions/github").getOctokit>['rest']['issues']['listComments']>>['data'] } params.comments
48
- */
49
- const createCommentHandler =
50
- ( { core, context, github, comments } ) =>
51
- async ( /** @type {string[] } */ commentLines ) => {
52
- const owner = context . repo . owner ;
53
- const repo = context . repo . repo ;
54
- const pullNumber = context . issue . number ;
55
-
56
- const commentFound = containsAny ( comments ) ;
57
-
58
- if ( commentFound ) {
59
- core . info ( `>>> Updating existing comment on PR` ) ;
60
- core . info ( `>>> Comment id: ${ commentFound . id } ` ) ;
61
-
62
- if ( commentFound . body ?. startsWith ( commentLines [ 0 ] ) ) {
63
- core . info ( `>>> PR already has the needed comment.` ) ;
64
- core . info ( `>>> Exiting gracefully! 👍` ) ;
65
- return ;
66
- }
67
-
68
- return await github . rest . issues . updateComment ( {
69
- owner,
70
- repo,
71
- comment_id : commentFound . id ,
72
- body : commentLines . join ( '\n\n' ) ,
73
- } ) ;
74
- }
75
-
76
- // only create a new comment if it's not the success comment
77
- if ( commentLines [ 0 ] === COMMENT_FIRST_LINE . SUCCESS_COMMENT ) {
78
- core . info ( `>>> No need for a comment!` ) ;
79
- core . info ( `>>> Exiting gracefully! 👍` ) ;
80
- return ;
81
- }
82
-
83
- core . info ( `>>> Creating explanatory comment on PR` ) ;
84
- return await github . rest . issues . createComment ( {
85
- owner,
86
- repo,
87
- issue_number : pullNumber ,
88
- body : commentLines . join ( '\n\n' ) ,
89
- } ) ;
90
- } ;
24
+ const NO_LABELS_COMMENT =
25
+ 'Please add one type label to categorize the purpose of this PR appropriately:' ;
26
+ const MULTIPLE_LABELS_COMMENT = 'Multiple type labels found:' ;
91
27
92
28
/**
93
29
* @param {Object } params
@@ -120,51 +56,19 @@ module.exports = async ({ core, context, github }) => {
120
56
per_page : 100 ,
121
57
} ) ;
122
58
123
- const commentHandler = createCommentHandler ( {
124
- core,
125
- context,
126
- github,
127
- comments : prComments ,
128
- } ) ;
129
-
130
- // docs label is handled differently
131
- if ( typeLabelsFound . some ( ( l ) => l === 'docs' ) ) {
132
- core . info ( `>>> 'docs' type label found` ) ;
133
-
134
- await commentHandler ( [ COMMENT_FIRST_LINE . SUCCESS_COMMENT ] ) ;
135
- return ;
136
- }
137
-
138
59
if ( typeLabelsFound . length === 0 ) {
139
60
core . info ( `>>> No type labels found` ) ;
140
-
141
- // Add a comment line explaining that a type label needs to be added
142
- await commentHandler ( [
143
- COMMENT_FIRST_LINE . NO_LABELS_COMMENT ,
144
- createEnumerationFromArray ( typeLabels ) ,
145
- ] ) ;
146
-
147
- core . setFailed ( '>>> Failing workflow to prevent merge without passing this!' ) ;
61
+ core . setFailed ( `>>> ${ NO_LABELS_COMMENT } ${ createEnumerationFromArray ( typeLabels ) } ` ) ;
148
62
return ;
149
63
}
150
64
151
65
if ( typeLabelsFound . length > 1 ) {
152
66
core . info ( `>>> Multiple type labels found: ${ typeLabelsFound . join ( ', ' ) } ` ) ;
153
-
154
- // add a comment line explaining that only one type label is allowed
155
- await commentHandler ( [
156
- COMMENT_FIRST_LINE . MULTIPLE_LABELS_COMMENT ,
157
- typeLabelsFound . map ( ( label ) => `- ${ label } ` ) . join ( '\n' ) ,
158
- 'Only one is allowed. Please remove the extra type labels to ensure the PR is categorized correctly.' ,
159
- ] ) ;
160
-
161
- core . setFailed ( '>>> Failing workflow to prevent merge without passing this!' ) ;
67
+ core . setFailed ( `>>> ${ MULTIPLE_LABELS_COMMENT } ${ typeLabelsFound . join ( ', ' ) } ` ) ;
162
68
return ;
163
69
}
164
70
165
71
core . info ( `>>> Single type label found: ${ typeLabelsFound [ 0 ] } ` ) ;
166
-
167
- await commentHandler ( [ COMMENT_FIRST_LINE . SUCCESS_COMMENT ] ) ;
168
72
} catch ( error ) {
169
73
core . error ( `>>> Workflow failed with: ${ error . message } ` ) ;
170
74
core . setFailed ( error . message ) ;
0 commit comments