11import * as core from '@actions/core' ;
22import { context } from '@actions/github' ;
3- import { Octokit } from '@octokit/rest' ;
3+ import { Octokit , RestEndpointMethodTypes } from '@octokit/rest' ;
44import { Commit , parseCommitMessage } from '../../../ng-dev/commit-message/parse.js' ;
5- import { managedLabels } from '../../../ng-dev/pr/common/labels/index.js' ;
5+ import { managedLabels , targetLabels } from '../../../ng-dev/pr/common/labels/index.js' ;
66import { ANGULAR_ROBOT , getAuthTokenFor , revokeActiveInstallationToken } from '../../utils.js' ;
77import { ManagedRepositories } from '../../../ng-dev/pr/common/labels/base.js' ;
88
9+ /** The type of the response data for a the pull request get method on from octokit. */
10+ type PullRequestGetData = RestEndpointMethodTypes [ 'pulls' ] [ 'get' ] [ 'response' ] [ 'data' ] ;
11+ /** A Regex matcher to match releasable branch patterns. */
12+ const releasableBranchMatcher = / ( m a i n | \d + \. \d + \. x ) / ;
13+
914class PullRequestLabeling {
1015 /** Run the commit message based labelling process. */
1116 static run = async ( ) => {
@@ -26,6 +31,8 @@ class PullRequestLabeling {
2631 labels = new Set < string > ( ) ;
2732 /** All commits in the PR */
2833 commits : Commit [ ] = [ ] ;
34+ /** The pull request information from the github API. */
35+ pullRequestMetadata ?: PullRequestGetData ;
2936
3037 private constructor ( private git : Octokit ) { }
3138
@@ -35,6 +42,14 @@ class PullRequestLabeling {
3542 await this . initialize ( ) ;
3643 core . info ( `PR #${ context . issue . number } ` ) ;
3744
45+ await this . commitMessageBasedLabeling ( ) ;
46+ await this . pullRequestMetadataLabeling ( ) ;
47+ }
48+
49+ /**
50+ * Perform labeling based on the commit messages for the pull request.
51+ */
52+ async commitMessageBasedLabeling ( ) {
3853 // Add or Remove label as appropriate for each of the supported label and commit messaage
3954 // combinations.
4055 for ( const { commitCheck, name, repositories} of Object . values ( managedLabels ) ) {
@@ -62,6 +77,31 @@ class PullRequestLabeling {
6277 }
6378 }
6479
80+ /**
81+ * Perform labeling based on the metadata for the pull request from the Github API.
82+ */
83+ async pullRequestMetadataLabeling ( ) {
84+ // If we are unable to get pull request metadata, we can shortcut and exit early.
85+ if ( this . pullRequestMetadata === undefined ) {
86+ return ;
87+ }
88+ /** The base reference string, or target branch of the pull request. */
89+ const baseRef = this . pullRequestMetadata . base . ref ;
90+
91+ if ( ! releasableBranchMatcher . test ( baseRef ) ) {
92+ if ( this . labels . has ( targetLabels . TARGET_FEATURE . name ) ) {
93+ core . info (
94+ `The target branch (${ baseRef } ) is not a releasable branch, already has "target: feature" label` ,
95+ ) ;
96+ } else {
97+ core . info (
98+ `The target branch (${ baseRef } ) is not a releasable branch, adding "target: feature" label` ,
99+ ) ;
100+ await this . addLabel ( targetLabels . TARGET_FEATURE . name ) ;
101+ }
102+ }
103+ }
104+
65105 /** Add the provided label to the pull request. */
66106 async addLabel ( label : string ) {
67107 const { number : issue_number , owner, repo} = context . issue ;
@@ -97,6 +137,10 @@ class PullRequestLabeling {
97137 await this . git . issues
98138 . listLabelsOnIssue ( { issue_number : number , owner, repo} )
99139 . then ( ( resp ) => resp . data . forEach ( ( { name} ) => this . labels . add ( name ) ) ) ;
140+
141+ await this . git . pulls . get ( { owner, repo, pull_number : number } ) . then ( ( { data} ) => {
142+ this . pullRequestMetadata = data ;
143+ } ) ;
100144 }
101145}
102146
0 commit comments