Skip to content

Commit e2d969f

Browse files
WIP
1 parent e126086 commit e2d969f

File tree

6 files changed

+53
-2
lines changed

6 files changed

+53
-2
lines changed

github-actions/pull-request-labeling/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ ts_project(
1818
"//github-actions:utils",
1919
"//ng-dev/commit-message",
2020
"//ng-dev/pr/common/labels",
21+
"//ng-dev/pr/config",
22+
"//ng-dev/utils",
2123
],
2224
)
2325

github-actions/pull-request-labeling/lib/main.ts

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {context} from '@actions/github';
33
import {Octokit, RestEndpointMethodTypes} from '@octokit/rest';
44
import {Commit, parseCommitMessage} from '../../../ng-dev/commit-message/parse.js';
55
import {actionLabels, managedLabels, targetLabels} from '../../../ng-dev/pr/common/labels/index.js';
6+
import {assertValidPullRequestConfig, PullRequestConfig} from '../../../ng-dev/pr/config/index.js';
67
import {ANGULAR_ROBOT, getAuthTokenFor, revokeActiveInstallationToken} from '../../utils.js';
78
import {ManagedRepositories} from '../../../ng-dev/pr/common/labels/base.js';
9+
import {getConfig, NgDevConfig} from '../../../ng-dev/utils/config.js';
810

911
/** The type of the response data for a the pull request get method on from octokit. */
1012
type PullRequestGetData = RestEndpointMethodTypes['pulls']['get']['response']['data'];
@@ -17,7 +19,9 @@ class PullRequestLabeling {
1719
const token = await getAuthTokenFor(ANGULAR_ROBOT);
1820
const git = new Octokit({auth: token});
1921
try {
20-
const inst = new this(git);
22+
const config = await getConfig();
23+
assertValidPullRequestConfig(config);
24+
const inst = new this(git, config);
2125
await inst.run();
2226
} finally {
2327
await revokeActiveInstallationToken(git);
@@ -32,8 +36,13 @@ class PullRequestLabeling {
3236
commits: Commit[] = [];
3337
/** The pull request information from the github API. */
3438
pullRequestMetadata?: PullRequestGetData;
39+
/** The files changed in the pull request */
40+
pullRequestFiles?: RestEndpointMethodTypes['pulls']['listFiles']['response']['data'];
3541

36-
private constructor(private git: Octokit) {}
42+
private constructor(
43+
private git: Octokit,
44+
private config: NgDevConfig<{pullRequest: PullRequestConfig}>,
45+
) {}
3746

3847
/** Run the action, and revoke the installation token on completion. */
3948
async run() {
@@ -43,6 +52,7 @@ class PullRequestLabeling {
4352

4453
await this.commitMessageBasedLabeling();
4554
await this.pullRequestMetadataLabeling();
55+
await this.pathBasedLabeling();
4656
}
4757

4858
/**
@@ -107,6 +117,28 @@ class PullRequestLabeling {
107117
}
108118
}
109119

120+
/**
121+
* Perform labeling based on the paths of the files in the pull request.
122+
*/
123+
async pathBasedLabeling() {
124+
const managedLabelByPath = this.config.pullRequest.managedLabelByPath;
125+
if (managedLabelByPath === undefined || this.pullRequestFiles === undefined) {
126+
return;
127+
}
128+
129+
for (const [path, labels] of Object.entries(managedLabelByPath)) {
130+
for (const file of this.pullRequestFiles) {
131+
if (file.filename.startsWith(path)) {
132+
for (const label of labels) {
133+
if (!this.labels.has(label)) {
134+
await this.addLabel(label);
135+
}
136+
}
137+
}
138+
}
139+
}
140+
}
141+
110142
/** Remove the provided label to the pull request. */
111143
async removeLabel(label: string) {
112144
const {number: issue_number, owner, repo} = context.issue;
@@ -159,6 +191,10 @@ class PullRequestLabeling {
159191
await this.git.pulls.get({owner, repo, pull_number: number}).then(({data}) => {
160192
this.pullRequestMetadata = data;
161193
});
194+
195+
await this.git.pulls.listFiles({owner, repo, pull_number: number}).then(({data}) => {
196+
this.pullRequestFiles = data;
197+
});
162198
}
163199
}
164200

ng-dev/pr/common/labels/managed.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ export const managedLabels = createTypedObject(ManagedLabel)({
8383
commitCheck: (c: Commit) => c.scope === 'localize',
8484
repositories: [ManagedRepositories.ANGULAR],
8585
},
86+
DETECTED_REQUIRES_TGP: {
87+
description: 'This PR requires a passing TGP before merging is allowed',
88+
name: 'requires: TGP',
89+
commitCheck: (c: Commit) => false,
90+
repositories: [ManagedRepositories.ANGULAR],
91+
},
8692
});

ng-dev/pr/config/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ ts_project(
55
srcs = ["index.ts"],
66
visibility = [
77
"//.github/local-actions/branch-manager:__subpackages__",
8+
"//github-actions/pull-request-labeling:__subpackages__",
89
"//ng-dev:__subpackages__",
910
],
1011
deps = [

ng-dev/pr/config/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ export interface PullRequestConfig {
5858
*/
5959
validators?: PullRequestValidationConfig;
6060

61+
/**
62+
* Optional map of paths that have extra labels to apply when PRs are created.
63+
*/
64+
managedLabelByPath?: {[path: string]: string[]};
65+
6166
/**
6267
* Whether target labeling should be disabled. Special option for repositories
6368
* not working with the canonical versioning and branching of Angular projects.

ng-dev/utils/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ ts_project(
3333
"//.github/local-actions/branch-manager:__subpackages__",
3434
"//.github/local-actions/changelog/lib:__subpackages__",
3535
"//github-actions/google-internal-tests:__subpackages__",
36+
"//github-actions/pull-request-labeling:__subpackages__",
3637
"//github-actions/slash-commands/lib:__subpackages__",
3738
"//ng-dev:__subpackages__",
3839
],

0 commit comments

Comments
 (0)