Files
labeler/src/api/add-labels.ts
T
Logan RosenandCopilot App b7a8804475 Preserve externally managed labels during synchronization (#957)
* Preserve externally managed pull request labels

Replace whole-set label writes with batched selective additions and removals so sync-labels only manages configured labels.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 44452010-1cd5-4e90-8abe-f03547c04182

* Reconcile ambiguous label addition failures

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 44452010-1cd5-4e90-8abe-f03547c04182

* Handle paginated label reconciliation

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

Copilot-Session: 44452010-1cd5-4e90-8abe-f03547c04182

---------

Copilot-Session: 44452010-1cd5-4e90-8abe-f03547c04182
2026-07-30 21:51:30 -05:00

66 lines
1.4 KiB
TypeScript

import * as github from '@actions/github';
import {ClientType} from './types.js';
const isServerError = (error: unknown): error is {status: number} =>
typeof error === 'object' &&
error !== null &&
'status' in error &&
typeof error.status === 'number' &&
error.status >= 500 &&
error.status < 600;
export const addLabels = async (
client: ClientType,
prNumber: number,
labels: string[]
) => {
const request = {
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber
};
try {
await client.rest.issues.addLabels({
...request,
labels,
request: {retries: 0}
});
} catch (error: unknown) {
if (!isServerError(error)) {
throw error;
}
const currentLabelNames = new Set<string>();
let page = 1;
try {
while (true) {
const currentLabels = await client.rest.issues.listLabelsOnIssue({
...request,
per_page: 100,
page,
request: {retries: 0}
});
for (const label of currentLabels.data) {
currentLabelNames.add(label.name.toLowerCase());
}
if (labels.every(label => currentLabelNames.has(label.toLowerCase()))) {
return;
}
if (!currentLabels.headers.link?.match(/;\s*rel="next"/)) {
break;
}
page++;
}
} catch {
throw error;
}
throw error;
}
};