豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content

Commit 6fb5d71

Browse files
committed
feat(github-actions): ensure pull requests with deprecation commits have a deprecation label (#269)
Expand the commit-message-based-labels github action to enforce labeling for both deprecation and breaking changes when indicated by commits in the PR. PR Close #269
1 parent 3648f59 commit 6fb5d71

File tree

3 files changed

+76
-54
lines changed

3 files changed

+76
-54
lines changed

github-actions/commit-message-based-labels/lib/main.ts

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,48 +2,59 @@ import * as core from '@actions/core';
22
import {context} from '@actions/github';
33
import {Octokit} from '@octokit/rest';
44
import {parseCommitMessage} from '../../../ng-dev/commit-message/parse';
5-
import {breakingChangeLabel} from '../../../ng-dev/pr/config';
5+
import {breakingChangeLabel, deprecationLabel} from '../../../ng-dev/pr/config';
66
import {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+
814
async 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.
4859
if (context.repo.owner === 'angular') {
4960
run();

github-actions/commit-message-based-labels/main.js

Lines changed: 32 additions & 24 deletions
Large diffs are not rendered by default.

ng-dev/pr/config/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,6 @@ export function assertValidPullRequestConfig<T>(
7878

7979
/** Label for pull requests containing a breaking change. */
8080
export const breakingChangeLabel = 'flag: breaking change';
81+
82+
/** Label for pull requests containing a deprecation. */
83+
export const deprecationLabel = 'flag: deprecation';

0 commit comments

Comments
 (0)