Skip to content

Commit

Permalink
chore: format ts (#16838)
Browse files Browse the repository at this point in the history
Co-authored-by: kodster28 <kody@cloudflare.com>
  • Loading branch information
Cherry and kodster28 authored Sep 18, 2024
1 parent 943459f commit 3dd03fd
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 158 deletions.
180 changes: 89 additions & 91 deletions .github/actions/label-products/index.ts
Original file line number Diff line number Diff line change
@@ -1,115 +1,113 @@
// NOTE: This is the source file!
// ~> Run `npm run build` to produce `index.js`

import * as github from '@actions/github';
import * as core from '@actions/core';
import * as github from "@actions/github";
import * as core from "@actions/core";

function getTopLevelFolder(path: string): string {
const parts = path.split('/');
return parts[2];
const parts = path.split("/");
return parts[2];
}


function getSubFolder(path: string): string {
const parts = path.split('/');
return parts[3];
const parts = path.split("/");
return parts[3];
}

function getChangedSubFolders(files: any[]): string[] {
const changedFolders = new Set<string>();
const changedFolders = new Set<string>();

for (const file of files) {
const path = file.filename;
const topLevelFolder = getTopLevelFolder(path);
for (const file of files) {
const path = file.filename;
const topLevelFolder = getTopLevelFolder(path);

// Check if the file is within the top-level /content folder
if (topLevelFolder === 'docs') {
const subFolder = getSubFolder(path);
changedFolders.add(subFolder);
}
}
// Check if the file is within the top-level /content folder
if (topLevelFolder === "docs") {
const subFolder = getSubFolder(path);
changedFolders.add(subFolder);
}
}

return Array.from(changedFolders);
return Array.from(changedFolders);
}

async function run(): Promise<void> {
try {
const ctx = github.context;
const token = core.getInput('GITHUB_TOKEN', { required: true });
const octokit = github.getOctokit(token);
const pr = ctx.payload.pull_request;
const prNumber = pr.number;
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
...ctx.repo,
pull_number: pr.number,
per_page: 100,
});

// Get the changed sub-folders within the top-level /content folder
const changedFolders = getChangedSubFolders(files);

// ...

// Label the PR based on the changed sub-folders
await labelPRSubFolders(octokit, ctx.repo, prNumber, changedFolders);

// ...
} catch (error) {
console.error('An error occurred:', error);
process.exit(1);
}
try {
const ctx = github.context;
const token = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = github.getOctokit(token);
const pr = ctx.payload.pull_request;
const prNumber = pr.number;
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
...ctx.repo,
pull_number: pr.number,
per_page: 100,
});

// Get the changed sub-folders within the top-level /content folder
const changedFolders = getChangedSubFolders(files);

// ...

// Label the PR based on the changed sub-folders
await labelPRSubFolders(octokit, ctx.repo, prNumber, changedFolders);

// ...
} catch (error) {
console.error("An error occurred:", error);
process.exit(1);
}
}

async function labelPRSubFolders(
octokit: any,
repo: any,
prNumber: number,
changedFolders: string[]
octokit: any,
repo: any,
prNumber: number,
changedFolders: string[],
): Promise<void> {
const labelPrefix = 'product:';
const labelsToRemove: string[] = [];
const labelsToAdd: string[] = [];

for (const label of github.context.payload.pull_request.labels) {
if (label.name.startsWith(labelPrefix)) {
labelsToRemove.push(label.name);
}
}

for (const folder of changedFolders) {
const label = labelPrefix + folder;
labelsToAdd.push(label);
}

const currentLabels = new Set(
github.context.payload.pull_request.labels.map((label: any) => label.name)
);

for (const labelToRemove of labelsToRemove) {
if (!labelsToAdd.includes(labelToRemove)) {
await octokit.rest.issues.removeLabel({
...repo,
issue_number: prNumber,
name: labelToRemove,
});
}
}

let newLabels: string[] = [];
for (const labelToAdd of labelsToAdd) {
if (!currentLabels.has(labelToAdd)) {
newLabels.push(labelToAdd);
}
}

if (newLabels.length > 0) {
await octokit.rest.issues.addLabels({
...repo,
issue_number: prNumber,
labels: newLabels,
});
}
const labelPrefix = "product:";
const labelsToRemove: string[] = [];
const labelsToAdd: string[] = [];

for (const label of github.context.payload.pull_request.labels) {
if (label.name.startsWith(labelPrefix)) {
labelsToRemove.push(label.name);
}
}

for (const folder of changedFolders) {
const label = labelPrefix + folder;
labelsToAdd.push(label);
}

const currentLabels = new Set(
github.context.payload.pull_request.labels.map((label: any) => label.name),
);

for (const labelToRemove of labelsToRemove) {
if (!labelsToAdd.includes(labelToRemove)) {
await octokit.rest.issues.removeLabel({
...repo,
issue_number: prNumber,
name: labelToRemove,
});
}
}

let newLabels: string[] = [];
for (const labelToAdd of labelsToAdd) {
if (!currentLabels.has(labelToAdd)) {
newLabels.push(labelToAdd);
}
}

if (newLabels.length > 0) {
await octokit.rest.issues.addLabels({
...repo,
issue_number: prNumber,
labels: newLabels,
});
}
}

run();

111 changes: 56 additions & 55 deletions .github/actions/label-size/index.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,68 @@
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as core from "@actions/core";
import * as github from "@actions/github";

async function run(): Promise<void> {
try {
const ctx = github.context;
const token = core.getInput('GITHUB_TOKEN', { required: true });
const octokit = github.getOctokit(token);
const pr = ctx.payload.pull_request;
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
...ctx.repo,
pull_number: pr.number,
per_page: 100
});
try {
const ctx = github.context;
const token = core.getInput("GITHUB_TOKEN", { required: true });
const octokit = github.getOctokit(token);
const pr = ctx.payload.pull_request;
const files = await octokit.paginate(octokit.rest.pulls.listFiles, {
...ctx.repo,
pull_number: pr.number,
per_page: 100,
});

const changes = files.reduce((total, file) => total + file.changes, 0);
const changes = files.reduce((total, file) => total + file.changes, 0);

let label: string | undefined;
let label: string | undefined;

switch (true) {
case changes <= 10:
label = 'size/xs';
break;
case changes <= 100:
label = 'size/s';
break;
case changes <= 500:
label = 'size/m';
break;
case changes <= 1000:
label = 'size/l';
break;
default:
label = 'size/xl';
break;
}
switch (true) {
case changes <= 10:
label = "size/xs";
break;
case changes <= 100:
label = "size/s";
break;
case changes <= 500:
label = "size/m";
break;
case changes <= 1000:
label = "size/l";
break;
default:
label = "size/xl";
break;
}

const currentLabels = pr.labels.map((label) => label.name);
const sizeLabels = ['size/xs', 'size/s', 'size/m', 'size/l', 'size/xl'];
const currentLabels = pr.labels.map((label) => label.name);
const sizeLabels = ["size/xs", "size/s", "size/m", "size/l", "size/xl"];

// Remove previous size-related labels that are different from the current label
const labelsToRemove = currentLabels.filter(
(currentLabel) => sizeLabels.includes(currentLabel) && currentLabel !== label
);
// Remove previous size-related labels that are different from the current label
const labelsToRemove = currentLabels.filter(
(currentLabel) =>
sizeLabels.includes(currentLabel) && currentLabel !== label,
);

for (const labelToRemove of labelsToRemove) {
await octokit.rest.issues.removeLabel({
...ctx.repo,
issue_number: pr.number,
name: labelToRemove
});
}
for (const labelToRemove of labelsToRemove) {
await octokit.rest.issues.removeLabel({
...ctx.repo,
issue_number: pr.number,
name: labelToRemove,
});
}

// Add the current label
if (!currentLabels.includes(label)) {
await octokit.rest.issues.addLabels({
...ctx.repo,
issue_number: pr.number,
labels: [label]
});
}
} catch (error) {
core.setFailed(error.message);
}
// Add the current label
if (!currentLabels.includes(label)) {
await octokit.rest.issues.addLabels({
...ctx.repo,
issue_number: pr.number,
labels: [label],
});
}
} catch (error) {
core.setFailed(error.message);
}
}

run();
2 changes: 1 addition & 1 deletion src/content/docs/src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference path="../.astro/types.d.ts" />
2 changes: 1 addition & 1 deletion src/env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
/// <reference path="../.astro/types.d.ts" />
/// <reference path="../.astro/types.d.ts" />
10 changes: 6 additions & 4 deletions src/pages/pages/platform/build-configuration.json.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { getEntry } from "astro:content";

export async function GET() {
const entries = await getEntry("pages-framework-presets", "index");
const entries = await getEntry("pages-framework-presets", "index");

const sorted = Object.fromEntries(Object.entries(entries.data.build_configs).sort())
const sorted = Object.fromEntries(
Object.entries(entries.data.build_configs).sort(),
);

return Response.json(sorted);
}
return Response.json(sorted);
}
2 changes: 1 addition & 1 deletion src/util/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export async function entryToString(entry: CollectionEntry<"docs">) {
params: { slug: entry.slug },
});

return html;
return html;
}
2 changes: 1 addition & 1 deletion src/util/description.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CollectionEntry } from "astro:content";
import { parse } from "node-html-parser";
import { entryToString } from "./container"
import { entryToString } from "./container";
/*
1. If there is a `description` property in the frontmatter, return that.
2. If there is a `<p>...</p>` element in the HTML, return that.
Expand Down
9 changes: 5 additions & 4 deletions src/util/sidebar.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export function sortBySidebarOrder(a: any, b: any): number {
const collator = new Intl.Collator("en");
const collator = new Intl.Collator("en");

if (a.data.sidebar.order !== b.data.sidebar.order) return a.data.sidebar.order - b.data.sidebar.order;
if (a.data.sidebar.order !== b.data.sidebar.order)
return a.data.sidebar.order - b.data.sidebar.order;

return collator.compare(a.data.title, b.data.title);
}
return collator.compare(a.data.title, b.data.title);
}

0 comments on commit 3dd03fd

Please sign in to comment.