Skip to content

Commit

Permalink
feat(github-actions): ensure pull requests with deprecation commits h…
Browse files Browse the repository at this point in the history
…ave 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
  • Loading branch information
josephperrott committed Oct 18, 2021
1 parent 3648f59 commit 6fb5d71
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 54 deletions.
71 changes: 41 additions & 30 deletions github-actions/commit-message-based-labels/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,59 @@ 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} from '../../../ng-dev/pr/config';
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);
// Create authenticated Github client.
const client = new Octokit({auth: token});

const {number, owner, repo} = context.issue;

const hasBreakingChangeLabel = await (
/** Labels currently applied to the PR. */
const labels = await (
await client.issues.listLabelsOnIssue({issue_number: number, owner, repo})
).data.find((label) => label.name === breakingChangeLabel);
console.log('hasBreakingChangeLabel', hasBreakingChangeLabel);

const hasBreakingChangeCommit = (
).data;
/** Parsed commit message for every commit on the PR. */
const commits = await (
await client.paginate(client.pulls.listCommits, {owner, pull_number: number, repo})
).some((commit) => {
return parseCommitMessage(commit.commit.message).breakingChanges.length > 0;
});
).map(({commit: {message}}) => parseCommitMessage(message));

if (hasBreakingChangeCommit && !hasBreakingChangeLabel) {
await client.issues.addLabels({
repo,
owner,
issue_number: number,
labels: [breakingChangeLabel],
});
console.log(`Added ${breakingChangeLabel} label to PR #${number}`);
}
if (!hasBreakingChangeCommit && hasBreakingChangeLabel) {
await client.issues.removeLabel({
repo,
owner,
issue_number: number,
name: breakingChangeLabel,
});
console.log(`Removed ${breakingChangeLabel} label from PR #${number}`);
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 with is in the Angular org. This is in place
// 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();
Expand Down
56 changes: 32 additions & 24 deletions github-actions/commit-message-based-labels/main.js

Large diffs are not rendered by default.

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';

0 comments on commit 6fb5d71

Please sign in to comment.