Skip to content
Closed
10 changes: 6 additions & 4 deletions src/github/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ export async function ensurePullRequestLabel(

let created = false;
if (options.createMissingLabel) {
const repoLabels = await octokit.request("GET /repos/{owner}/{repo}/labels", {
const labelLookup = await octokit.request("GET /repos/{owner}/{repo}/labels/{name}", {
owner,
repo,
per_page: 100,
name: labelName,
}).catch((error: { status?: number }) => {
if (error.status === 404) return null;
throw error;
});
const labelExists = (repoLabels.data as GitHubLabel[]).some((label) => label.name?.toLowerCase() === labelName.toLowerCase());
if (!labelExists) {
if (!labelLookup) {
await octokit.request("POST /repos/{owner}/{repo}/labels", {
owner,
repo,
Expand Down
4 changes: 2 additions & 2 deletions test/unit/github-labels.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("GitHub PR labels", () => {
calls.push(`${method} ${url}`);
if (url.includes("/access_tokens")) return Response.json({ token: "installation-token" });
if (url.includes("/issues/4/labels") && method === "GET") return Response.json([]);
if (url.includes("/repos/JSONbored/gittensory/labels") && method === "GET") return Response.json([{ name: "gittensor" }]);
if (url.includes("/labels/gittensor") && method === "GET") return Response.json({ name: "gittensor" });
if (url.includes("/issues/4/labels") && method === "POST") return Response.json([{ name: "gittensor" }]);
return new Response("unexpected", { status: 500 });
});
Expand All @@ -59,7 +59,7 @@ describe("GitHub PR labels", () => {
calls.push(`${method} ${url}`);
if (url.includes("/access_tokens")) return Response.json({ token: "installation-token" });
if (url.includes("/issues/4/labels") && method === "GET") return Response.json([]);
if (url.includes("/repos/JSONbored/gittensory/labels") && method === "GET") return Response.json([]);
if (url.includes("/labels/gittensor") && method === "GET") return new Response("not found", { status: 404 });
if (url.includes("/repos/JSONbored/gittensory/labels") && method === "POST") {
const body = JSON.parse(String(init?.body ?? "{}")) as { name?: string; color?: string };
expect(body).toMatchObject({ name: "gittensor", color: "7ee787" });
Expand Down
Loading