Skip to content

Commit

Permalink
refactor(ng-dev): centralize the configuration/definition of labels t…
Browse files Browse the repository at this point in the history
…hat are controlled by tooling (#837)

Centralize the way that we configure tooling managed labels in preparation for label restructuring and
allowing for general expansion of managed labels.

PR Close #837
  • Loading branch information
josephperrott committed Sep 23, 2022
1 parent 6ca5640 commit 9046496
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 809 deletions.
2 changes: 1 addition & 1 deletion github-actions/commit-message-based-labels/lib/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ ts_library(
deps = [
"//github-actions:utils",
"//ng-dev/commit-message",
"//ng-dev/pr/config",
"//ng-dev/pr/common",
"@npm//@actions/core",
"@npm//@actions/github",
"@npm//@octokit/rest",
Expand Down
40 changes: 12 additions & 28 deletions github-actions/commit-message-based-labels/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,9 @@ import * as core from '@actions/core';
import {context} from '@actions/github';
import {Octokit} from '@octokit/rest';
import {Commit, parseCommitMessage} from '../../../ng-dev/commit-message/parse.js';
import {COMMIT_TYPES} from '../../../ng-dev/commit-message/config.js';
import {breakingChangeLabel, deprecationLabel} from '../../../ng-dev/pr/config/index.js';
import {ToolingPullRequestLabels} from '../../../ng-dev/pr/common/labels.js';
import {ANGULAR_ROBOT, getAuthTokenFor, revokeActiveInstallationToken} from '../../utils.js';

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

/** Label for docs changes. */
const compDocsLabel = 'comp: docs';

class CommitMessageBasedLabelManager {
/** Run the commit message based labelling process. */
static run = async () => {
Expand All @@ -30,8 +20,8 @@ class CommitMessageBasedLabelManager {

/** Labels currently applied to the PR. */
labels = new Set<string>();
/** Parsed commit message for every commit on the PR. */
commits = new Set<Commit>();
/** All commits in the PR */
commits: Commit[] = [];

private constructor(private git: Octokit) {}

Expand All @@ -43,10 +33,14 @@ class CommitMessageBasedLabelManager {

// Add or Remove label as appropriate for each of the supported label and commit messaage
// combinations.
for (const [label, commitProperty] of supportedLabels) {
const hasCommit = [...this.commits].some((commit) => commit[commitProperty].length > 0);
for (const {commitCheck, label} of Object.values(ToolingPullRequestLabels)) {
// If the commit check is set to false, no labeling is applied automatically.
if (commitCheck === false) {
continue;
}
const hasCommit = this.commits.some(commitCheck);
const hasLabel = this.labels.has(label);
core.info(`${commitProperty} | hasLabel: ${hasLabel} | hasCommit: ${hasCommit}`);
core.info(`${label} | hasLabel: ${hasLabel} | hasCommit: ${hasCommit}`);

if (hasCommit && !hasLabel) {
await this.addLabel(label);
Expand All @@ -55,16 +49,6 @@ class CommitMessageBasedLabelManager {
await this.removeLabel(label);
}
}

// Add 'comp: docs' label for changes which contain a docs commit.
if (!this.labels.has(compDocsLabel)) {
for (const commit of this.commits) {
if (commit.type === COMMIT_TYPES['docs'].name) {
await this.addLabel(compDocsLabel);
break;
}
}
}
}

/** Add the provided label to the pull request. */
Expand Down Expand Up @@ -99,8 +83,8 @@ class CommitMessageBasedLabelManager {

await this.git
.paginate(this.git.pulls.listCommits, {owner, pull_number: number, repo})
.then((commits) =>
commits.forEach(({commit}) => this.commits.add(parseCommitMessage(commit.message))),
.then(
(commits) => (this.commits = commits.map(({commit}) => parseCommitMessage(commit.message))),
);

await this.git.issues
Expand Down
Loading

0 comments on commit 9046496

Please sign in to comment.