Skip to content

Commit

Permalink
Avoid an additional call to get all labels
Browse files Browse the repository at this point in the history
  • Loading branch information
jeherve committed Dec 3, 2024
1 parent 163c2d4 commit d65c407
Showing 1 changed file with 21 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +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 Type 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 hasTypeLabels( octokit, owner, repo, number ) {
const labels = await getLabels( octokit, owner, repo, number );
return !! labels.find( label => label.match( /^\[Type\]/ ) );
}

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

const hasLongDescription = body?.length > 200;
const isLabeled = await hasStatusLabels( octokit, ownerLogin, repo, number );
const hasType = await hasTypeLabels( 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,
hasTesting,
hasPrivacy,
projectsWithoutChangelog,
hasChangelogEntries: projectsWithoutChangelog.length === 0,
isFromContributor,
hasType,
hasTypeLabels,
};
}

Expand All @@ -304,19 +288,19 @@ 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 has a Status label: ${ 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.hasType }` );
debug( `check-description: this PR has a Type label: ${ statusChecks.hasTypeLabels }` );
checks += statusEntry(
! statusChecks.hasType,
! statusChecks.hasTypeLabels,
'Add a "[Type]" label (Bug, Enhancement, Janitorial, Task).'
);
}
Expand Down

0 comments on commit d65c407

Please sign in to comment.