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

Handle PR labels #4

Open
wants to merge 15 commits into
base: trunk
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { getInput } = require( '@actions/core' );
const debug = require( '../../utils/debug' );
const getFiles = require( '../../utils/get-files' );
const getLabels = require( '../../utils/labels/get-labels' );

/* global GitHub, WebhookPayloadPullRequest */

Expand Down Expand Up @@ -48,18 +49,18 @@
);
}

/**

Check failure on line 52 in projects/github-actions/repo-gardening/src/tasks/add-labels/index.js

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Expected JSDoc block lines to be aligned
* Build a list of labels to add to the issue, based off our file list.
* Build a list of labels to add to the pull request, based off our file list.
*
* @param {GitHub} octokit - Initialized Octokit REST client.
* @param {string} owner - Repository owner.
* @param {string} repo - Repository name.
* @param {string} number - PR number.
* @param {boolean} isDraft - Whether the pull request is a draft.
* @param {boolean} isRevert - Whether the pull request is a revert.
* @param {GitHub} octokit - Initialized Octokit REST client.
* @param {string} owner - Repository owner.
* @param {string} repo - Repository name.
* @param {string} number - PR number.
* @param {boolean} isDraft - Whether the pull request is a draft.
* @param {boolean} isRevert - Whether the pull request is a revert.
* @return {Promise<Array>} Promise resolving to an array of keywords we'll search for.
*/
async function getLabelsToAdd( octokit, owner, repo, number, isDraft, isRevert ) {
async function getFileDerivedLabels( octokit, owner, repo, number, isDraft, isRevert ) {
const keywords = new Set();

// Get next valid milestone.
Expand Down Expand Up @@ -320,7 +321,7 @@
}

/**
* Assigns any issues that are being worked to the author of the matching PR.
* Adds appropriate labels to the specified PR.
*
* @param {WebhookPayloadPullRequest} payload - Pull request event payload.
* @param {GitHub} octokit - Initialized Octokit REST client.
Expand All @@ -330,26 +331,83 @@
const { owner, name } = repository;
const { draft, title } = pull_request;

// GitHub allows 100 labels on a PR.
// Limit to less than that to allow a buffer for future manual labels.
const maxLabels = 5;
const bigProjectLabel = '[Project] All the things!';

// Get labels to add to the PR.
const isDraft = !! ( pull_request && draft );

// If the PR title includes the word "revert", mark it as such.
const isRevert = title.toLowerCase().includes( 'revert' );

const labels = await getLabelsToAdd( octokit, owner.login, name, number, isDraft, isRevert );
let fileDerivedLabels = await getFileDerivedLabels(

Check failure on line 345 in projects/github-actions/repo-gardening/src/tasks/add-labels/index.js

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

'fileDerivedLabels' is never reassigned. Use 'const' instead
octokit,
owner.login,
name,
number,
isDraft,
isRevert
);

// Grab current labels on the PR.
// We can't rely on payload, as it could be outdated by the time this runs.
const currentLabels = await getLabels( octokit, owner.login, name, number );

// This is an array of labels that GitHub doesn't already have.
let labelsToAdd = fileDerivedLabels.filter( label => ! currentLabels.includes( label ) );

if ( ! labels.length ) {
debug( 'add-labels: Could not find labels to add to that PR. Aborting' );
// Nothing new was added, so abort.
if ( labelsToAdd.length === 0 ) {
debug( 'add-labels: No new labels to add to that PR. Aborting.' );
return;
}

debug( `add-labels: Adding labels ${ labels } to PR #${ number }` );
// Determine how many labels can safely be added.
let maxLabelsToAdd = Math.max( 0, maxLabels - currentLabels.length );

// Overkill, but let's prevent this label from counting toward the max.
const hasBigProjectLabel = currentLabels.includes( bigProjectLabel );
if ( hasBigProjectLabel ) {
maxLabelsToAdd++;
}

// If there are too many labels, we need to reduce the label count to keep GitHub happy.
if ( labelsToAdd.length > maxLabelsToAdd ) {
debug( `add-labels: Too many labels! Grouping project labels into '${bigProjectLabel}'.` );

Check failure on line 378 in projects/github-actions/repo-gardening/src/tasks/add-labels/index.js

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Replace `bigProjectLabel` with `·bigProjectLabel·`

// Filter out project-type labels in deference to bigProjectLabel.
// In theory we could also remove any existing project-type labels here, but for now
// let's not as that would prevent manually adding specific project labels.
const projectLabelRegex = /^(\[Action\]|\[Package\]|\[Plugin\]|\[JS Package\])/;
labelsToAdd = labelsToAdd.filter( label => ! projectLabelRegex.test( label ) );

if ( ! hasBigProjectLabel ) {
// Add to the beginning of the labels array in case the array gets truncated later on.
labelsToAdd.unshift( bigProjectLabel );
}
} else if ( hasBigProjectLabel ) {
await octokit.rest.issues.removeLabel( {
owner: owner.login,
repo: name,
issue_number: number,
name: bigProjectLabel,
} );
}
// In the rare chance there would still be too many labels...
if ( labelsToAdd.length > maxLabelsToAdd ) {
debug( `add-labels: Limiting to the first ${maxLabels}.` );

Check failure on line 400 in projects/github-actions/repo-gardening/src/tasks/add-labels/index.js

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Replace `↹debug(·`add-labels:·Limiting·to·the·first·${maxLabels` with `debug(·`add-labels:·Limiting·to·the·first·${·maxLabels·`
labelsToAdd.splice( maxLabelsToAdd );

Check failure on line 401 in projects/github-actions/repo-gardening/src/tasks/add-labels/index.js

View workflow job for this annotation

GitHub Actions / ESLint (non-excluded files only)

Delete `↹`
}

debug( `add-labels: Adding labels ${ labelsToAdd } to PR #${ number }` );

await octokit.rest.issues.addLabels( {
owner: owner.login,
repo: name,
issue_number: number,
labels,
labels: labelsToAdd,
} );
}

Expand Down
Loading