Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(github-actions): ensure pull requests with deprecation commits have a deprecation label #269

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: ./github-actions/breaking-changes-label
- uses: ./github-actions/commit-message-based-labels
with:
angular-robot-key: ${{ secrets.ANGULAR_ROBOT_PRIVATE_KEY }}
2 changes: 1 addition & 1 deletion .ng-dev/commit-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const commitMessage: CommitMessageConfig = {
'remote-execution',
]),
...buildScopesFor('github-actions', [
'breaking-changes-label',
'commit-message-based-labels',
'feature-request',
'lock-closed',
'rebase',
Expand Down
4 changes: 2 additions & 2 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github-actions/feature-request/post.js
github-actions/feature-request/main.js
github-actions/lock-closed/main.js
github-actions/lock-closed/post.js
github-actions/breaking-changes-label/main.js
github-actions/breaking-changes-label/post.js
github-actions/commit-message-based-labels/main.js
github-actions/commit-message-based-labels/post.js
github-actions/slash-commands/main.js
github-actions/slash-commands/post.js
tools/local-actions/changelog/main.js
Expand Down
19 changes: 0 additions & 19 deletions github-actions/breaking-changes-label/BUILD.bazel

This file was deleted.

55 changes: 0 additions & 55 deletions github-actions/breaking-changes-label/lib/main.ts

This file was deleted.

19 changes: 19 additions & 0 deletions github-actions/commit-message-based-labels/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
load("//tools:defaults.bzl", "esbuild_checked_in")

esbuild_checked_in(
name = "post",
entry_point = "//github-actions/commit-message-based-labels/lib:post.ts",
external = ["ts-node"],
deps = [
"//github-actions/commit-message-based-labels/lib",
],
)

esbuild_checked_in(
name = "main",
entry_point = "//github-actions/commit-message-based-labels/lib:main.ts",
external = ["ts-node"],
deps = [
"//github-actions/commit-message-based-labels/lib",
],
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
load("//tools:defaults.bzl", "ts_library")

package(default_visibility = ["//github-actions/breaking-changes-label:__subpackages__"])
package(default_visibility = ["//github-actions/commit-message-based-labels:__subpackages__"])

exports_files([
"main.ts",
Expand Down
66 changes: 66 additions & 0 deletions github-actions/commit-message-based-labels/lib/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import * as core from '@actions/core';
import {context} from '@actions/github';
import {Octokit} from '@octokit/rest';
import {parseCommitMessage} from '../../../ng-dev/commit-message/parse';
import {breakingChangeLabel, deprecationLabel} from '../../../ng-dev/pr/config';
import {ANGULAR_ROBOT, getAuthTokenFor} from '../../utils';

/** List of supported label and commit message attribute combinations. */
const supportedLabels = [
[breakingChangeLabel, 'breakingChanges'],
[deprecationLabel, 'deprecations'],
] as const;

async function run(): Promise<void> {
const token = await getAuthTokenFor(ANGULAR_ROBOT);
const client = new Octokit({auth: token});
const {number, owner, repo} = context.issue;
/** Labels currently applied to the PR. */
const labels = await (
await client.issues.listLabelsOnIssue({issue_number: number, owner, repo})
).data;
josephperrott marked this conversation as resolved.
Show resolved Hide resolved
/** Parsed commit message for every commit on the PR. */
const commits = await (
await client.paginate(client.pulls.listCommits, {owner, pull_number: number, repo})
).map(({commit: {message}}) => parseCommitMessage(message));

console.log(`PR #${number}`);

// Add or Remove label as appropriate for each of the supported label and commit messaage
// combinations.
for (const [label, commitProperty] of supportedLabels) {
const hasCommit = commits.some((commit) => commit[commitProperty].length > 0);
const hasLabel = labels.some(({name}) => name === label);
console.log(`${commitProperty} | hasLabel: ${hasLabel} | hasCommit: ${hasCommit}`);

if (hasCommit && !hasLabel) {
await client.issues.addLabels({
repo,
owner,
issue_number: number,
labels: [label],
});
console.log(`Added ${label} label to PR #${number}`);
}
if (!hasCommit && hasLabel) {
await client.issues.removeLabel({
repo,
owner,
issue_number: number,
name: label,
});
console.log(`Removed ${label} label from PR #${number}`);
}
}
}

// Only run if the action is executed in a repository within the Angular org. This is in place
// to prevent the action from actually running in a fork of a repository with this action set up.
if (context.repo.owner === 'angular') {
run();
} else {
core.warning(
'Automatic labeling was skipped as this action is only meant to run ' +
'in repos belonging to the Angular organization.',
);
}

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion ng-dev/commit-message/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ ts_library(
exclude = ["**/*.spec.ts"],
),
visibility = [
"//github-actions/breaking-changes-label:__subpackages__",
"//github-actions/commit-message-based-labels:__subpackages__",
"//ng-dev:__subpackages__",
],
deps = [
Expand Down
2 changes: 1 addition & 1 deletion ng-dev/pr/config/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ts_library(
name = "config",
srcs = ["index.ts"],
visibility = [
"//github-actions/breaking-changes-label/lib:__pkg__",
"//github-actions/commit-message-based-labels/lib:__pkg__",
"//ng-dev:__subpackages__",
],
deps = [
Expand Down
3 changes: 3 additions & 0 deletions ng-dev/pr/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,6 @@ export function assertValidPullRequestConfig<T>(

/** Label for pull requests containing a breaking change. */
export const breakingChangeLabel = 'flag: breaking change';

/** Label for pull requests containing a deprecation. */
export const deprecationLabel = 'flag: deprecation';