@@ -2,48 +2,59 @@ import * as core from '@actions/core';
22import { context } from '@actions/github' ;
33import { Octokit } from '@octokit/rest' ;
44import { parseCommitMessage } from '../../../ng-dev/commit-message/parse' ;
5- import { breakingChangeLabel } from '../../../ng-dev/pr/config' ;
5+ import { breakingChangeLabel , deprecationLabel } from '../../../ng-dev/pr/config' ;
66import { ANGULAR_ROBOT , getAuthTokenFor } from '../../utils' ;
77
8+ /** List of supported label and commit message attribute combinations. */
9+ const supportedLabels = [
10+ [ breakingChangeLabel , 'breakingChanges' ] ,
11+ [ deprecationLabel , 'deprecations' ] ,
12+ ] as const ;
13+
814async function run ( ) : Promise < void > {
915 const token = await getAuthTokenFor ( ANGULAR_ROBOT ) ;
10- // Create authenticated Github client.
1116 const client = new Octokit ( { auth : token } ) ;
12-
1317 const { number, owner, repo} = context . issue ;
14-
15- const hasBreakingChangeLabel = await (
18+ /** Labels currently applied to the PR. */
19+ const labels = await (
1620 await client . issues . listLabelsOnIssue ( { issue_number : number , owner, repo} )
17- ) . data . find ( ( label ) => label . name === breakingChangeLabel ) ;
18- console . log ( 'hasBreakingChangeLabel' , hasBreakingChangeLabel ) ;
19-
20- const hasBreakingChangeCommit = (
21+ ) . data ;
22+ /** Parsed commit message for every commit on the PR. */
23+ const commits = await (
2124 await client . paginate ( client . pulls . listCommits , { owner, pull_number : number , repo} )
22- ) . some ( ( commit ) => {
23- return parseCommitMessage ( commit . commit . message ) . breakingChanges . length > 0 ;
24- } ) ;
25+ ) . map ( ( { commit : { message} } ) => parseCommitMessage ( message ) ) ;
2526
26- if ( hasBreakingChangeCommit && ! hasBreakingChangeLabel ) {
27- await client . issues . addLabels ( {
28- repo,
29- owner,
30- issue_number : number ,
31- labels : [ breakingChangeLabel ] ,
32- } ) ;
33- console . log ( `Added ${ breakingChangeLabel } label to PR #${ number } ` ) ;
34- }
35- if ( ! hasBreakingChangeCommit && hasBreakingChangeLabel ) {
36- await client . issues . removeLabel ( {
37- repo,
38- owner,
39- issue_number : number ,
40- name : breakingChangeLabel ,
41- } ) ;
42- console . log ( `Removed ${ breakingChangeLabel } label from PR #${ number } ` ) ;
27+ console . log ( `PR #${ number } ` ) ;
28+
29+ // Add or Remove label as appropriate for each of the supported label and commit messaage
30+ // combinations.
31+ for ( const [ label , commitProperty ] of supportedLabels ) {
32+ const hasCommit = commits . some ( ( commit ) => commit [ commitProperty ] . length > 0 ) ;
33+ const hasLabel = labels . some ( ( { name} ) => name === label ) ;
34+ console . log ( `${ commitProperty } | hasLabel: ${ hasLabel } | hasCommit: ${ hasCommit } ` ) ;
35+
36+ if ( hasCommit && ! hasLabel ) {
37+ await client . issues . addLabels ( {
38+ repo,
39+ owner,
40+ issue_number : number ,
41+ labels : [ label ] ,
42+ } ) ;
43+ console . log ( `Added ${ label } label to PR #${ number } ` ) ;
44+ }
45+ if ( ! hasCommit && hasLabel ) {
46+ await client . issues . removeLabel ( {
47+ repo,
48+ owner,
49+ issue_number : number ,
50+ name : label ,
51+ } ) ;
52+ console . log ( `Removed ${ label } label from PR #${ number } ` ) ;
53+ }
4354 }
4455}
4556
46- // Only run if the action is executed in a repository with is in the Angular org. This is in place
57+ // Only run if the action is executed in a repository within the Angular org. This is in place
4758// to prevent the action from actually running in a fork of a repository with this action set up.
4859if ( context . repo . owner === 'angular' ) {
4960 run ( ) ;
0 commit comments