Refine PR label bot (#13245)

This commit is contained in:
Ian Bassi
2026-04-16 14:04:27 -03:00
committed by GitHub
parent 883c3f82cd
commit 30265e4f98

View File

@@ -176,37 +176,9 @@ jobs:
invalidLabels.push(rawLabel); invalidLabels.push(rawLabel);
} }
} }
if (invalidLabels.length) {
const allowedText = allowedLabels.map((label) => `\`${label}\``).join(', ');
const invalidText = invalidLabels.map((label) => `\`${label}\``).join(', ');
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body:
`@${commenter} invalid label(s): ${invalidText}.\n\n` +
`Allowed labels: ${allowedText}\n\n` +
`Use:\n` +
`- \`/bot add-label label\`\n` +
`- \`/bot remove-label label\`\n` +
`- \`/bot add-label label1, label2\``
});
} catch (error) {
if (isPermissionDenied(error)) {
core.warning('Cannot post invalid-label feedback because token cannot write comments.');
return;
}
throw error;
}
return;
}
const uniqueRequestedLabels = [...new Set(resolvedLabels)]; const uniqueRequestedLabels = [...new Set(resolvedLabels)];
if (action === 'add') { if (action === 'add' && uniqueRequestedLabels.length) {
try { try {
await github.rest.issues.addLabels({ await github.rest.issues.addLabels({
owner: context.repo.owner, owner: context.repo.owner,
@@ -227,41 +199,77 @@ jobs:
} }
core.info(`Added labels: ${uniqueRequestedLabels.join(', ')}`); core.info(`Added labels: ${uniqueRequestedLabels.join(', ')}`);
return;
} }
let removedCount = 0; if (action === 'remove' && uniqueRequestedLabels.length) {
try { let removedCount = 0;
for (const label of uniqueRequestedLabels) { try {
try { for (const label of uniqueRequestedLabels) {
await github.rest.issues.removeLabel({ try {
owner: context.repo.owner, await github.rest.issues.removeLabel({
repo: context.repo.repo, owner: context.repo.owner,
issue_number: issue.number, repo: context.repo.repo,
name: label issue_number: issue.number,
}); name: label
removedCount += 1; });
} catch (error) { removedCount += 1;
if (isPermissionDenied(error)) { } catch (error) {
core.warning( if (isPermissionDenied(error)) {
'Cannot remove labels because token cannot write. Enable Actions write permissions, ' + core.warning(
'or run with a token that has issues:write and pull_requests:write.' 'Cannot remove labels because token cannot write. Enable Actions write permissions, ' +
); 'or run with a token that has issues:write and pull_requests:write.'
return; );
} return;
}
if (error.status === 404) { if (error.status === 404) {
core.info(`Label is not currently applied: ${label}`); core.info(`Label is not currently applied: ${label}`);
continue; continue;
} }
throw error; throw error;
}
} }
}
core.info(`Removed labels count: ${removedCount}`); core.info(`Removed labels count: ${removedCount}`);
} catch (error) { } catch (error) {
throw error; throw error;
}
} }
core.info(`Processed label command: ${action} ${uniqueRequestedLabels.join(', ')}`); if (!uniqueRequestedLabels.length) {
core.info('No valid labels were provided in the command.');
}
if (invalidLabels.length) {
const allowedText = allowedLabels.map((label) => `\`${label}\``).join(', ');
const invalidText = invalidLabels.map((label) => `\`${label}\``).join(', ');
const validText = uniqueRequestedLabels.length
? `\n\nProcessed valid label(s): ${uniqueRequestedLabels.map((label) => `\`${label}\``).join(', ')}`
: '';
try {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
body:
`@${commenter} invalid label(s): ${invalidText}.${validText}\n\n` +
`Allowed labels: ${allowedText}\n\n` +
`Use:\n` +
`- \`/bot add-label label\`\n` +
`- \`/bot remove-label label\`\n` +
`- \`/bot add-label label1, label2\``
});
} catch (error) {
if (isPermissionDenied(error)) {
core.warning('Cannot post invalid-label feedback because token cannot write comments.');
return;
}
throw error;
}
}
const processedLabelsText = uniqueRequestedLabels.length ? uniqueRequestedLabels.join(', ') : '(none)';
core.info(`Processed label command: ${action} ${processedLabelsText}`);