Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
0x4007 committed Jan 31, 2024
1 parent cd018c8 commit 98c86e9
Show file tree
Hide file tree
Showing 22 changed files with 273 additions and 550 deletions.
6 changes: 2 additions & 4 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"cSpell.words": [
"stablecoins"
]
}
"cSpell.words": ["stablecoins"]
}
9 changes: 3 additions & 6 deletions src/cli/cli-args.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const optionDefinitions = [
name: "color",
type: String,
alias: "c",
description:
"Color to use for the label. Must be a six character CSS color string, without the `#` prefix, like `ff0000`.",
description: "Color to use for the label. Must be a six character CSS color string, without the `#` prefix, like `ff0000`.",
},
{
name: "regex",
Expand All @@ -32,15 +31,13 @@ const optionDefinitions = [
name: "tool",
type: String,
alias: "t",
description:
"Pass in the selected labels and perform actions on those labels. Example: `--tool delete-labels` for `tools/delete-labels.ts`",
description: "Pass in the selected labels and perform actions on those labels. Example: `--tool delete-labels` for `tools/delete-labels.ts`",
},
{
name: "execute",
type: Boolean,
alias: "x",
description:
"Execute destructive command (e.g. delete label). Otherwise, just print the dry run.",
description: "Execute destructive command (e.g. delete label). Otherwise, just print the dry run.",
},
{
name: "to",
Expand Down
8 changes: 3 additions & 5 deletions src/cli/cli-entry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import { log } from "./logging";
export default async function cliEntry() {
// Check for required arguments.
if (!Args.owner || !Args.repository) {
log.error(
"Missing required arguments: `owner`, `repository` (use -o, and -r)"
);
log.error("Missing required arguments: `owner`, `repository` (use -o, and -r)");
process.exit(1);
}

if (!Args.tool) {
log.info("No tool selected. Use `--tool` to select a tool.");
const _tools = fs.readdirSync(`src/tools`);
// remove .ts extension
const tools = _tools.map(tool => {
const tools = _tools.map((tool) => {
if (tool.endsWith(`.ts`)) {
return tool.slice(0, -3);
}
Expand All @@ -35,7 +33,7 @@ export default async function cliEntry() {

const selected = await filterLabels(labels, Args.regex);

const selectedBuffer = selected.map(label => label.name);
const selectedBuffer = selected.map((label) => label.name);
log.ok(`Selected the following labels:\n\n${selectedBuffer.join("\n")}`);

// Select tool
Expand Down
9 changes: 3 additions & 6 deletions src/github-types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { Endpoints } from "@octokit/types";

export type GitHubIssue =
Endpoints["GET /repos/{owner}/{repo}/issues"]["response"]["data"][0];
export type GitHubComment =
Endpoints["GET /repos/{owner}/{repo}/issues/{issue_number}/comments"]["response"]["data"][0];
export type GitHubLabel =
Endpoints["GET /repos/{owner}/{repo}/labels"]["response"]["data"][0];
export type GitHubIssue = Endpoints["GET /repos/{owner}/{repo}/issues"]["response"]["data"][0];
export type GitHubComment = Endpoints["GET /repos/{owner}/{repo}/issues/{issue_number}/comments"]["response"]["data"][0];
export type GitHubLabel = Endpoints["GET /repos/{owner}/{repo}/labels"]["response"]["data"][0];
16 changes: 6 additions & 10 deletions src/network/get-raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as https from "https";
import { githubToken } from "../utils/get-github-token";

export async function getGitHubRaw(path: string) {
const labelsResponse = await new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
const options = {
hostname: "api.github.com",
port: 443,
Expand All @@ -16,23 +16,19 @@ export async function getGitHubRaw(path: string) {
};

const request = https
.get(options, res => {
.get(options, (res) => {
if (res.statusCode !== 200) {
reject(
new Error(`Request failed with status code ${res.statusCode}`)
);
reject(new Error(`Request failed with status code ${res.statusCode}`));
return;
}

let data = "";
res.on("data", chunk => (data += chunk));
res.on("data", (chunk) => (data += chunk));
res.on("end", () => resolve(res));
})
.on("error", error => reject);
.on("error", (error) => reject);

request.on("error", e => console.error);
request.on("error", (e) => console.error);
request.end();
});

return labelsResponse;
}
16 changes: 6 additions & 10 deletions src/network/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as https from "https";
import { githubToken } from "../utils/get-github-token";

export async function getGitHub(path: string) {
const labelsResponse = await new Promise((resolve, reject) => {
return await new Promise((resolve, reject) => {
const options = {
hostname: "api.github.com",
port: 443,
Expand All @@ -16,25 +16,21 @@ export async function getGitHub(path: string) {
};

const request = https
.get(options, res => {
.get(options, (res) => {
if (res.statusCode !== 200) {
reject(
new Error(`Request failed with status code ${res.statusCode}`)
);
reject(new Error(`Request failed with status code ${res.statusCode}`));
return;
}

let data = "";
res.on("data", chunk => (data += chunk));
res.on("data", (chunk) => (data += chunk));
res.on("end", () => {
resolve(JSON.parse(data));
});
})
.on("error", error => reject);
.on("error", (error) => reject);

request.on("error", e => console.error);
request.on("error", (e) => console.error);
request.end();
});

return labelsResponse;
}
8 changes: 3 additions & 5 deletions src/network/remove-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export async function removeLabel(label: string) {
const options = {
hostname: "api.github.com",
port: 443,
path: `/repos/${Args.owner}/${
Args.repository
}/labels/${encodeURIComponent(label)}`,
path: `/repos/${Args.owner}/${Args.repository}/labels/${encodeURIComponent(label)}`,
method: "DELETE",
headers: {
"Content-Type": "application/json",
Expand All @@ -23,7 +21,7 @@ export async function removeLabel(label: string) {
},
};

const req = https.request(options, res => {
const req = https.request(options, (res) => {
if (res.statusCode !== 204) {
reject(new Error(`Request failed with status code ${res.statusCode}`));
return;
Expand All @@ -33,7 +31,7 @@ export async function removeLabel(label: string) {
res.on("end", () => resolve(undefined));
});

req.on("error", error => {
req.on("error", (error) => {
reject(error);
});

Expand Down
16 changes: 3 additions & 13 deletions src/tools/assistive-pricing-normalizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ export default async function assistivePricingLabelsNormalizer() {

Args.color = "ededed";
await resetColors(Object.values(priorityMapping));
await resetColors([
"Time: <1 Hour",
"Time: <2 Hours",
"Time: <4 Hours",
"Time: <1 Day",
"Time: <1 Week",
"Time: <2 Weeks",
"Time: <1 Month",
]);
await resetColors(["Time: <1 Hour", "Time: <2 Hours", "Time: <4 Hours", "Time: <1 Day", "Time: <1 Week", "Time: <2 Weeks", "Time: <1 Month"]);

await deletePriceRangeLabels(allLabels);
// Filter for `Price: ` labels.
Expand All @@ -51,10 +43,8 @@ export default async function assistivePricingLabelsNormalizer() {

async function deletePriceRangeLabels(allLabels: GitHubLabel[]) {
const plusSignLabelRegex = /^Price:\s\d+(\.\d+)?\+\sUSD$/; // regex to match labels with a plus sign
const rangePriceLabels = allLabels.filter(label =>
plusSignLabelRegex.test(label.name)
);
await _deleteLabels(rangePriceLabels.map(label => label.name));
const rangePriceLabels = allLabels.filter((label) => plusSignLabelRegex.test(label.name));
await _deleteLabels(rangePriceLabels.map((label) => label.name));
}

async function resetColors(labels: string[]) {
Expand Down
6 changes: 2 additions & 4 deletions src/tools/clear-unused-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ export default async function clearUnusedLabels(selected: string[]) {
async function getUsedLabelsWithCount(): Promise<Map<string, number>> {
const usedLabels = [] as string[];

const issuesAndPRs = await getGitHub(
`/repos/${Args.owner}/${Args.repository}/issues?per_page=1000`
);
const issuesAndPRs = await getGitHub(`/repos/${Args.owner}/${Args.repository}/issues?per_page=1000`);

for (const item of issuesAndPRs as typeof singleItem[]) {
for (const item of issuesAndPRs as (typeof singleItem)[]) {
item.labels.forEach((label: GitHubLabel) => {
usedLabels.push(label.name);
});
Expand Down
4 changes: 1 addition & 3 deletions src/tools/migrate-priority-labels/_update-issue-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ export async function _updateIssueLabels(oldLabel: string, newLabel: string) {
log.ok(`Successfully updated issue labels from ${oldLabel} to ${newLabel}`);
} catch (error) {
if (error instanceof Error) {
log.error(
`Failed to update issue label ${oldLabel} to ${newLabel}: ${error.message}`
);
log.error(`Failed to update issue label ${oldLabel} to ${newLabel}: ${error.message}`);
} else {
console.error(error);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tools/migrate-priority-labels/check-if-label-exists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { GitHubLabel } from "../../github-types";

export function checkIfLabelExists(labels: GitHubLabel[], newLabel: string) {
log.info(`Checking if label ${newLabel} exists...`);
const labelExists = labels.some(label => label.name === newLabel);
const labelExists = labels.some((label) => label.name === newLabel);

if (labelExists) {
log.info(`Label ${newLabel} exists in the repository.`);
Expand Down
4 changes: 1 addition & 3 deletions src/tools/migrate-priority-labels/create-new-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ export async function createNewLabel(labelExists: boolean, newLabel: string) {
color: "efefef",
});
} else {
log.info(
`Label ${newLabel} already exists in the repository. Skipping creation.`
);
log.info(`Label ${newLabel} already exists in the repository. Skipping creation.`);
}
} else {
log.info("dry run, not creating labels. Use `--execute` to run.");
Expand Down
4 changes: 1 addition & 3 deletions src/tools/migrate-priority-labels/get-all-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ export async function getAllLabels() {
repo: Args.repository,
});

log.info(
`Retrieved ${labels.length} labels from repository ${Args.repository}`
);
log.info(`Retrieved ${labels.length} labels from repository ${Args.repository}`);

return labels;
}
8 changes: 2 additions & 6 deletions src/tools/migrate-priority-labels/update-issue-labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,8 @@ export async function updateIssueLabels(oldLabel: string, newLabel: string) {

// Update each issue to have the new label
for (const issue of issues.data) {
const labels = (issue.labels as GitHubLabel[]).map(label =>
label.name === oldLabel ? newLabel : label.name
);
log.info(
`Updating labels for issue number ${issue.number} from ${oldLabel} to ${newLabel}`
);
const labels = (issue.labels as GitHubLabel[]).map((label) => (label.name === oldLabel ? newLabel : label.name));
log.info(`Updating labels for issue number ${issue.number} from ${oldLabel} to ${newLabel}`);
if (Args.execute) {
await octokit.rest.issues.update({
owner: Args.owner,
Expand Down
4 changes: 1 addition & 3 deletions src/tools/migrate-priority-labels/update-label-name.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ export async function updateLabelName(fromName: string, toName: string) {
if ((error as Response).status === 404) {
throw new Error(`Label ${fromName} not found`);
} else if ((error as Response).status === 422) {
throw new Error(
`Label rename failed: A label with the name '${Args.to}' already exists.`
);
throw new Error(`Label rename failed: A label with the name '${Args.to}' already exists.`);
}

throw error;
Expand Down
6 changes: 1 addition & 5 deletions src/tools/reset-colors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@ import filterLabels from "../utils/filter-labels";
import getLabels from "../utils/get-labels";
import colorizeLabels from "./colorize-labels";

export default async function resetColors(args: {
color?: string;
owner: string;
repository: string;
}) {
export default async function resetColors(args: { color?: string; owner: string; repository: string }) {
// Get all labels.
const allLabels = await getLabels();
// do not select labels with `^Price:.+` regex
Expand Down
4 changes: 1 addition & 3 deletions src/tools/scrub-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ export default async function _scrubComments() {
// Edit the comment

if (!Args.execute) {
log.info(
"dry run, not editing comments. Use `--execute` to edit comments"
);
log.info("dry run, not editing comments. Use `--execute` to edit comments");
return;
}

Expand Down
6 changes: 2 additions & 4 deletions src/tools/toggle-label.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ export default async function _toggleLabel() {
});

for (const issue of issues) {
const currentLabels = (issue.labels as GitHubLabel[]).map(
label => label.name
);
const currentLabels = (issue.labels as GitHubLabel[]).map((label) => label.name);

let updatedLabels;

if (currentLabels.includes(labelToToggle)) {
updatedLabels = currentLabels.filter(label => label !== labelToToggle);
updatedLabels = currentLabels.filter((label) => label !== labelToToggle);
} else {
updatedLabels = [...currentLabels, labelToToggle];
}
Expand Down
Loading

0 comments on commit 98c86e9

Please sign in to comment.