@@ -13,37 +13,59 @@ You may obtain a copy of the License at
13
13
limitations under the License.
14
14
*/
15
15
16
-
17
16
/**
18
17
* Invoked from labeler.yaml file to add
19
18
* label 'Gemma' to the issue and PR for which have gemma keyword present.
20
19
* @param {!Object.<string,!Object> } github contains pre defined functions.
21
- * context Information about the workflow run.
20
+ * context Information about the workflow run.
22
21
*/
23
22
24
23
module . exports = async ( { github, context } ) => {
25
- const issue_title = context . payload . issue ? context . payload . issue . title : context . payload . pull_request . title
26
- const issue_description = context . payload . issue ? context . payload . issue . body : context . payload . pull_request . body
27
- const issue_number = context . payload . issue ? context . payload . issue . number : context . payload . pull_request . number
24
+ // Determine if the event is an issue or a pull request.
25
+ const isIssue = ! ! context . payload . issue ;
26
+
27
+ // Get the issue/PR title, description, and number from the payload.
28
+ // Use an empty string for the description if it's null to prevent runtime errors.
29
+ const issue_title = isIssue ? context . payload . issue . title : context . payload . pull_request . title ;
30
+ const issue_description = ( isIssue ? context . payload . issue . body : context . payload . pull_request . body ) || '' ;
31
+ const issue_number = isIssue ? context . payload . issue . number : context . payload . pull_request . number ;
32
+
33
+ // Define the keyword-to-label mapping.
28
34
const keyword_label = {
29
- gemma :'Gemma'
30
- }
31
- const labelsToAdd = [ ]
32
- console . log ( issue_title , issue_description , issue_number )
35
+ gemma : 'Gemma'
36
+ } ;
33
37
34
- for ( const [ keyword , label ] of Object . entries ( keyword_label ) ) {
35
- if ( issue_title . toLowerCase ( ) . indexOf ( keyword ) != - 1 || issue_description . toLowerCase ( ) . indexOf ( keyword ) != - 1 ) {
36
- console . log ( `'${ keyword } 'keyword is present inside the title or description. Pushing label '${ label } ' to row.` )
37
- labelsToAdd . push ( label )
38
+ // Array to hold labels that need to be added.
39
+ const labelsToAdd = [ ] ;
40
+
41
+ console . log ( `Processing event for issue/PR #${ issue_number } : "${ issue_title } "` ) ;
42
+
43
+ // Loop through the keywords and check if they exist in the title or description.
44
+ for ( const [ keyword , label ] of Object . entries ( keyword_label ) ) {
45
+ // Use .includes() for a cleaner and more modern check.
46
+ if ( issue_title . toLowerCase ( ) . includes ( keyword ) || issue_description . toLowerCase ( ) . includes ( keyword ) ) {
47
+ console . log ( `'${ keyword } ' keyword is present in the title or description. Pushing label '${ label } ' to the array.` ) ;
48
+ labelsToAdd . push ( label ) ;
49
+ }
50
+ }
51
+
52
+ // Add labels if the labelsToAdd array is not empty.
53
+ if ( labelsToAdd . length > 0 ) {
54
+ console . log ( `Adding labels ${ labelsToAdd } to issue/PR '#${ issue_number } '.` ) ;
55
+
56
+ try {
57
+ // Await the asynchronous API call to ensure it completes.
58
+ await github . rest . issues . addLabels ( {
59
+ owner : context . repo . owner ,
60
+ repo : context . repo . repo ,
61
+ issue_number : issue_number , // Use the correct issue_number variable
62
+ labels : labelsToAdd
63
+ } ) ;
64
+ console . log ( `Successfully added labels.` ) ;
65
+ } catch ( error ) {
66
+ console . error ( `Failed to add labels: ${ error . message } ` ) ;
67
+ }
68
+ } else {
69
+ console . log ( "No matching keywords found. No labels to add." ) ;
38
70
}
39
- }
40
- if ( labelsToAdd . length > 0 ) {
41
- console . log ( `Adding labels ${ labelsToAdd } to the issue '#${ issue_number } '.` )
42
- github . rest . issues . addLabels ( {
43
- owner : context . repo . owner ,
44
- repo : context . repo . repo ,
45
- issue_number : context . issue . number ,
46
- labels : labelsToAdd
47
- } )
48
- }
49
- } ;
71
+ } ;
0 commit comments