Skip to content

Commit

Permalink
Repo Gardening: add check for a [Type] label on PRs (#40428)
Browse files Browse the repository at this point in the history
* Repo Gardening: add check for a [Type] label on PRs

See p1HpG7-uPQ-p2#comment-74890

* Avoid an additional call to get all labels

* Move it up for more readability

See #40428 (comment)

Co-authored-by: anomiex <anomiex@users.noreply.github.com>

---------

Co-authored-by: anomiex <anomiex@users.noreply.github.com>
  • Loading branch information
jeherve and anomiex authored Dec 3, 2024
1 parent 6e57e78 commit eafa13a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

PR checks: add new check to ensure that PRs include a [Type] label.
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,6 @@ const getLabels = require( '../../utils/labels/get-labels' );

/* global GitHub, WebhookPayloadPullRequest */

/**
* Check for status labels on a PR.
*
* @param {GitHub} octokit - Initialized Octokit REST client.
* @param {string} owner - Repository owner.
* @param {string} repo - Repository name.
* @param {string} number - PR number.
* @return {Promise<boolean>} Promise resolving to boolean.
*/
async function hasStatusLabels( octokit, owner, repo, number ) {
const labels = await getLabels( octokit, owner, repo, number );
// We're only interested in status labels, but not the "Needs Reply" label since it can be added by the action.
return !! labels.find( label => label.match( /^\[Status\].*(?<!Author Reply)$/ ) );
}

/**
* Check for a "Need Review" label on a PR.
*
Expand Down Expand Up @@ -255,15 +240,30 @@ async function getStatusChecks( payload, octokit ) {
const ownerLogin = owner.login;

const hasLongDescription = body?.length > 200;
const isLabeled = await hasStatusLabels( octokit, ownerLogin, repo, number );
const hasTesting = !! body?.includes( 'Testing instructions' );
const hasPrivacy = !! body?.includes( 'data or activity we track or use' );
const projectsWithoutChangelog = await getChangelogEntries( octokit, ownerLogin, repo, number );
const isFromContributor = head.repo.full_name === base.repo.full_name;

const prLabels = await getLabels( octokit, ownerLogin, repo, number );
const { hasStatusLabels, hasTypeLabels } = prLabels.reduce(
( acc, label ) => {
// We're only interested in status labels, but not the "Needs Reply" label since it can be added by the action.
if ( label.match( /^\[Status\].*(?<!Author Reply)$/ ) ) {
acc.hasStatusLabels = true;
}
if ( label.match( /^\[Type\]/ ) ) {
acc.hasTypeLabels = true;
}
return acc;
},
{ hasStatusLabels: false, hasTypeLabels: false }
);

return {
hasLongDescription,
isLabeled,
hasStatusLabels,
hasTypeLabels,
hasTesting,
hasPrivacy,
projectsWithoutChangelog,
Expand All @@ -288,13 +288,23 @@ function renderStatusChecks( statusChecks ) {
// Use labels please!
// Only check this for PRs created by a12s. External contributors cannot add labels.
if ( statusChecks.isFromContributor ) {
debug( `check-description: this PR is correctly labeled: ${ statusChecks.isLabeled }` );
debug( `check-description: this PR has a Status label: ${ statusChecks.hasStatusLabels }` );
checks += statusEntry(
! statusChecks.isLabeled,
! statusChecks.hasStatusLabels,
'Add a "[Status]" label (In Progress, Needs Team Review, ...).'
);
}

// Add a [Type] label please.
// Only check this for PRs created by a12s. External contributors cannot add labels.
if ( statusChecks.isFromContributor ) {
debug( `check-description: this PR has a Type label: ${ statusChecks.hasTypeLabels }` );
checks += statusEntry(
! statusChecks.hasTypeLabels,
'Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).'
);
}

// Check for testing instructions.
checks += statusEntry( ! statusChecks.hasTesting, 'Add testing instructions.' );

Expand Down

0 comments on commit eafa13a

Please sign in to comment.