1
1
import * as core from '@actions/core' ;
2
2
import { context } from '@actions/github' ;
3
- import { Octokit } from '@octokit/rest' ;
3
+ import { Octokit , RestEndpointMethodTypes } from '@octokit/rest' ;
4
4
import { 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' ;
6
6
import { ANGULAR_ROBOT , getAuthTokenFor , revokeActiveInstallationToken } from '../../utils.js' ;
7
7
import { ManagedRepositories } from '../../../ng-dev/pr/common/labels/base.js' ;
8
8
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
+
9
14
class PullRequestLabeling {
10
15
/** Run the commit message based labelling process. */
11
16
static run = async ( ) => {
@@ -26,6 +31,8 @@ class PullRequestLabeling {
26
31
labels = new Set < string > ( ) ;
27
32
/** All commits in the PR */
28
33
commits : Commit [ ] = [ ] ;
34
+ /** The pull request information from the github API. */
35
+ pullRequestMetadata ?: PullRequestGetData ;
29
36
30
37
private constructor ( private git : Octokit ) { }
31
38
@@ -35,6 +42,14 @@ class PullRequestLabeling {
35
42
await this . initialize ( ) ;
36
43
core . info ( `PR #${ context . issue . number } ` ) ;
37
44
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 ( ) {
38
53
// Add or Remove label as appropriate for each of the supported label and commit messaage
39
54
// combinations.
40
55
for ( const { commitCheck, name, repositories} of Object . values ( managedLabels ) ) {
@@ -62,6 +77,31 @@ class PullRequestLabeling {
62
77
}
63
78
}
64
79
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
+
65
105
/** Add the provided label to the pull request. */
66
106
async addLabel ( label : string ) {
67
107
const { number : issue_number , owner, repo} = context . issue ;
@@ -97,6 +137,10 @@ class PullRequestLabeling {
97
137
await this . git . issues
98
138
. listLabelsOnIssue ( { issue_number : number , owner, repo} )
99
139
. 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
+ } ) ;
100
144
}
101
145
}
102
146
0 commit comments