mirror of
https://github.com/actions/labeler.git
synced 2025-12-13 21:17:02 +00:00
Added ability to pass in an optional PR number as a parameter (#349)
* Adding pr-number as an optional parameter * Updated README * Tests on the pr-number parameter * Added missing | to table * re-built script * Adding support for multiple pr-numbers * excluded .idea * Updated readme to reflect that there might be more than one PR * Additional warning * Removed unused * Reformatted and re-built * Corrected message * Removed required check * Added (s) to pull request numbers in the description Co-authored-by: MaksimZhukov <46996400+MaksimZhukov@users.noreply.github.com> * Reworded PR-number parameter description Co-authored-by: MaksimZhukov <46996400+MaksimZhukov@users.noreply.github.com> * adding getMultilineInput into the tests * Fixing tests for single pr * Fixing tests for multiple prs * Updated README.md to make it more obvious that it can take a list of PRs * Added example that labels PR's 1-3 * Handled no pull requests better (from code review) * Handled no pull requests better (from code review) * Handled missing pull request better (from code review) * Back out suggested change as it broke the tests * Rebuilt dist * Update src/labeler.ts Co-authored-by: MaksimZhukov <46996400+MaksimZhukov@users.noreply.github.com> * Added Emphasis to the note Co-authored-by: MaksimZhukov <46996400+MaksimZhukov@users.noreply.github.com> * Changed mockInput for pr-number to be string[] --------- Co-authored-by: MaksimZhukov <46996400+MaksimZhukov@users.noreply.github.com>
This commit is contained in:
154
src/labeler.ts
154
src/labeler.ts
@@ -22,75 +22,83 @@ export async function run() {
|
||||
const syncLabels = !!core.getInput('sync-labels');
|
||||
const dot = core.getBooleanInput('dot');
|
||||
|
||||
const prNumber = getPrNumber();
|
||||
if (!prNumber) {
|
||||
core.info('Could not get pull request number from context, exiting');
|
||||
const prNumbers = getPrNumbers();
|
||||
if (!prNumbers.length) {
|
||||
core.warning('Could not get pull request number(s), exiting');
|
||||
return;
|
||||
}
|
||||
|
||||
const client: ClientType = github.getOctokit(token, {}, pluginRetry.retry);
|
||||
|
||||
const {data: pullRequest} = await client.rest.pulls.get({
|
||||
owner: github.context.repo.owner,
|
||||
repo: github.context.repo.repo,
|
||||
pull_number: prNumber
|
||||
});
|
||||
|
||||
core.debug(`fetching changed files for pr #${prNumber}`);
|
||||
const changedFiles: string[] = await getChangedFiles(client, prNumber);
|
||||
const labelGlobs: Map<string, StringOrMatchConfig[]> = await getLabelGlobs(
|
||||
client,
|
||||
configPath
|
||||
);
|
||||
|
||||
const preexistingLabels = pullRequest.labels.map(l => l.name);
|
||||
const allLabels: Set<string> = new Set<string>(preexistingLabels);
|
||||
|
||||
for (const [label, globs] of labelGlobs.entries()) {
|
||||
core.debug(`processing ${label}`);
|
||||
if (checkGlobs(changedFiles, globs, dot)) {
|
||||
allLabels.add(label);
|
||||
} else if (syncLabels) {
|
||||
allLabels.delete(label);
|
||||
}
|
||||
}
|
||||
|
||||
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
|
||||
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
|
||||
|
||||
try {
|
||||
let newLabels: string[] = [];
|
||||
|
||||
if (!isListEqual(labelsToAdd, preexistingLabels)) {
|
||||
await setLabels(client, prNumber, labelsToAdd);
|
||||
newLabels = labelsToAdd.filter(l => !preexistingLabels.includes(l));
|
||||
for (const prNumber of prNumbers) {
|
||||
core.debug(`looking for pr #${prNumber}`);
|
||||
let pullRequest: any;
|
||||
try {
|
||||
const result = await client.rest.pulls.get({
|
||||
owner: github.context.repo.owner,
|
||||
repo: github.context.repo.repo,
|
||||
pull_number: prNumber
|
||||
});
|
||||
pullRequest = result.data;
|
||||
} catch (error: any) {
|
||||
core.warning(`Could not find pull request #${prNumber}, skipping`);
|
||||
continue;
|
||||
}
|
||||
|
||||
core.setOutput('new-labels', newLabels.join(','));
|
||||
core.setOutput('all-labels', labelsToAdd.join(','));
|
||||
core.debug(`fetching changed files for pr #${prNumber}`);
|
||||
const changedFiles: string[] = await getChangedFiles(client, prNumber);
|
||||
const labelGlobs: Map<string, StringOrMatchConfig[]> =
|
||||
await getLabelGlobs(client, configPath);
|
||||
|
||||
if (excessLabels.length) {
|
||||
core.warning(
|
||||
`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(
|
||||
', '
|
||||
)}`,
|
||||
{title: 'Label limit for a PR exceeded'}
|
||||
);
|
||||
const preexistingLabels = pullRequest.labels.map(l => l.name);
|
||||
const allLabels: Set<string> = new Set<string>(preexistingLabels);
|
||||
|
||||
for (const [label, globs] of labelGlobs.entries()) {
|
||||
core.debug(`processing ${label}`);
|
||||
if (checkGlobs(changedFiles, globs, dot)) {
|
||||
allLabels.add(label);
|
||||
} else if (syncLabels) {
|
||||
allLabels.delete(label);
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (
|
||||
error.name === 'HttpError' &&
|
||||
error.message === 'Resource not accessible by integration'
|
||||
) {
|
||||
core.warning(
|
||||
`The action requires write permission to add labels to pull requests. For more information please refer to the action documentation: https://github.com/actions/labeler#permissions`,
|
||||
{
|
||||
title: `${process.env['GITHUB_ACTION_REPOSITORY']} running under '${github.context.eventName}' is misconfigured`
|
||||
}
|
||||
);
|
||||
core.setFailed(error.message);
|
||||
} else {
|
||||
throw error;
|
||||
|
||||
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
|
||||
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
|
||||
|
||||
try {
|
||||
let newLabels: string[] = [];
|
||||
|
||||
if (!isListEqual(labelsToAdd, preexistingLabels)) {
|
||||
await setLabels(client, prNumber, labelsToAdd);
|
||||
newLabels = labelsToAdd.filter(l => !preexistingLabels.includes(l));
|
||||
}
|
||||
|
||||
core.setOutput('new-labels', newLabels.join(','));
|
||||
core.setOutput('all-labels', labelsToAdd.join(','));
|
||||
|
||||
if (excessLabels.length) {
|
||||
core.warning(
|
||||
`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(
|
||||
', '
|
||||
)}`,
|
||||
{title: 'Label limit for a PR exceeded'}
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (
|
||||
error.name === 'HttpError' &&
|
||||
error.message === 'Resource not accessible by integration'
|
||||
) {
|
||||
core.warning(
|
||||
`The action requires write permission to add labels to pull requests. For more information please refer to the action documentation: https://github.com/actions/labeler#permissions`,
|
||||
{
|
||||
title: `${process.env['GITHUB_ACTION_REPOSITORY']} running under '${github.context.eventName}' is misconfigured`
|
||||
}
|
||||
);
|
||||
core.setFailed(error.message);
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (error: any) {
|
||||
@@ -99,13 +107,29 @@ export async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
function getPrNumber(): number | undefined {
|
||||
const pullRequest = github.context.payload.pull_request;
|
||||
if (!pullRequest) {
|
||||
return undefined;
|
||||
function getPrNumbers(): number[] {
|
||||
const pullRequestNumbers = core.getMultilineInput('pr-number');
|
||||
if (pullRequestNumbers && pullRequestNumbers.length) {
|
||||
const prNumbers: number[] = [];
|
||||
|
||||
for (const prNumber of pullRequestNumbers) {
|
||||
const prNumberInt = parseInt(prNumber, 10);
|
||||
if (isNaN(prNumberInt) || prNumberInt <= 0) {
|
||||
core.warning(`'${prNumber}' is not a valid pull request number`);
|
||||
} else {
|
||||
prNumbers.push(prNumberInt);
|
||||
}
|
||||
}
|
||||
|
||||
return prNumbers;
|
||||
}
|
||||
|
||||
return pullRequest.number;
|
||||
const pullRequest = github.context.payload.pull_request;
|
||||
if (!pullRequest) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [pullRequest.number];
|
||||
}
|
||||
|
||||
async function getChangedFiles(
|
||||
|
||||
Reference in New Issue
Block a user