Skip to content

Commit 186c3f8

Browse files
feat: rename existing aliased labels (#497)
## PR Checklist - [x] Addresses an existing open issue: fixes #455 - [x] That issue was marked as [`status: accepting prs`](https://github.com/JoshuaKGoldberg/template-typescript-node-package/issues?q=is%3Aopen+is%3Aissue+label%3A%22status%3A+accepting+prs%22) - [x] Steps in [CONTRIBUTING.md](https://github.com/JoshuaKGoldberg/template-typescript-node-package/blob/main/.github/CONTRIBUTING.md) were taken ## Overview Adds an `getExistingEquivalentLabel` function to check for common label aliases. See the default labels here: https://docs.github.com/en/issues/using-labels-and-milestones-to-track-work/managing-labels
1 parent 260b055 commit 186c3f8

File tree

4 files changed

+69
-6
lines changed

4 files changed

+69
-6
lines changed

src/setup/setupWithInformation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { InputValuesAndOctokit } from "../shared/inputs.js";
88
import { addOwnerAsAllContributor } from "./settings/addOwnerAsAllContributor.js";
99
import { clearChangelog } from "./steps/clearChangelog.js";
1010
import { hydrateBranchProtectionSettings } from "./steps/hydrateBranchProtectionSettings.js";
11-
import { hydrateRepositoryLabels } from "./steps/hydrateRepositoryLabels.js";
1211
import { hydrateRepositorySettings } from "./steps/hydrateRepositorySettings.js";
12+
import { hydrateRepositoryLabels } from "./steps/labels/hydrateRepositoryLabels.js";
1313
import { removeSetupScripts } from "./steps/removeSetupScripts.js";
1414
import { resetGitTags } from "./steps/resetGitTags.js";
1515
import { uninstallPackages } from "./steps/uninstallPackages.js";
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { describe, expect, it } from "vitest";
2+
3+
import { getExistingEquivalentLabel } from "./getExistingEquivalentLabel.js";
4+
5+
describe("getExistingEquivalentLabel", () => {
6+
it("returns undefined when there are no existing labels", () => {
7+
const actual = getExistingEquivalentLabel([], "abc");
8+
9+
expect(actual).toBe(undefined);
10+
});
11+
12+
it("returns undefined when no existing label matches", () => {
13+
const actual = getExistingEquivalentLabel(["abc"], "def");
14+
15+
expect(actual).toBe(undefined);
16+
});
17+
18+
it("returns the existing label when it matches by name", () => {
19+
const actual = getExistingEquivalentLabel(["def", "abc", "ghi"], "abc");
20+
21+
expect(actual).toBe("abc");
22+
});
23+
24+
it("returns the existing label when it matches excluding prefix", () => {
25+
const actual = getExistingEquivalentLabel(
26+
["abc: def", "abc", "ghi"],
27+
"type: abc"
28+
);
29+
30+
expect(actual).toBe("abc");
31+
});
32+
33+
it("returns the existing label when it matches an alias", () => {
34+
const actual = getExistingEquivalentLabel(
35+
["abc: def", "enhancement", "ghi"],
36+
"type: feature"
37+
);
38+
39+
expect(actual).toBe("enhancement");
40+
});
41+
});
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const aliases = new Map([
2+
["enhancement", "type: feature"],
3+
["help wanted", "status: accepting prs"],
4+
]);
5+
6+
export function getExistingEquivalentLabel(
7+
existingLabels: string[],
8+
outcomeLabel: string
9+
) {
10+
const outcomeTrimmed = outcomeLabel.replace(/\w+: (\w+)/, "$1");
11+
12+
return existingLabels.find(
13+
(existingLabel) =>
14+
aliases.has(existingLabel) || existingLabel === outcomeTrimmed
15+
);
16+
}

src/setup/steps/hydrateRepositoryLabels.ts renamed to src/setup/steps/labels/hydrateRepositoryLabels.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { $ } from "execa";
22

3-
import { readFileAsJSON } from "../readFileAsJSON.js";
3+
import { readFileAsJSON } from "../../readFileAsJSON.js";
4+
import { getExistingEquivalentLabel } from "./getExistingEquivalentLabel.js";
45

56
interface GhLabelData {
67
name: string;
@@ -26,10 +27,15 @@ export async function hydrateRepositoryLabels() {
2627
)) as FileLabelData[];
2728

2829
for (const outcome of outcomeLabels) {
29-
const action = existingLabels.some((existing) => existing === outcome.name)
30-
? "edit"
31-
: "create";
32-
await $`gh label ${action} ${outcome.name} --color ${outcome.color} --description ${outcome.description}`;
30+
const existingEquivalent = getExistingEquivalentLabel(
31+
existingLabels,
32+
outcome.name
33+
);
34+
const [action, label] = existingEquivalent
35+
? ["edit", existingEquivalent]
36+
: ["create", outcome.name];
37+
38+
await $`gh label ${action} ${label} --color ${outcome.color} --description ${outcome.description}`;
3339
}
3440

3541
const allowedLabels = new Set(outcomeLabels.map(getLabelName));

0 commit comments

Comments
 (0)