Skip to content

Commit

Permalink
Perform a little refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andresz1 committed Mar 15, 2020
1 parent 3dbb623 commit ce58bfe
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 136 deletions.
11 changes: 11 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[*]
indent_style = space
end_of_line = lf
indent_size = 2
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false
32 changes: 32 additions & 0 deletions src/Git.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { exec } from "@actions/exec";

class Git {
async execSizeLimit(
branch?: string
): Promise<{ status: number; output: string }> {
let output = "";

if (branch) {
await exec(`git checkout -f ${branch}`);
}

await exec("npm install");
await exec("npm run build");
const status = await exec("npx size-limit --json", [], {
windowsVerbatimArguments: true,
ignoreReturnCode: true,
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
}
});

return {
status,
output
};
}
}

export default Git;
101 changes: 101 additions & 0 deletions src/SizeLimit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// @ts-ignore
import bytes from "bytes";

interface IResult {
name: string;
size: number;
running: number;
loading: number;
total: number;
}

class SizeLimit {
parseResults(output: string): { [name: string]: IResult } {
const results = JSON.parse(output);

return results.reduce(
(current: { [name: string]: IResult }, result: any) => {
const total = result.running + result.loading;

return {
...current,
[result.name]: {
name: result.name,
size: +result.size,
running: +result.running,
loading: +result.loading,
total
}
};
},
{}
);
}

formatBytes(size: number): string {
return bytes.format(size, { unitSeparator: " " });
}

formatTime(seconds: number): string {
if (seconds >= 1) {
return `${Math.ceil(seconds * 10) / 10} s`;
}

return `${Math.ceil(seconds * 1000)} ms`;
}

formatChange(base: number = 0, current: number = 0): string {
if (current === 0) {
return "-100%";
}

const value = ((current - base) / current) * 100;
const formatted =
(Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;

if (value > 0) {
return `+${formatted}% 🔺`;
}

if (value === 0) {
return `${formatted}%`;
}

return `${formatted}% 🔽`;
}

formatLine(value: string, change: string) {
return `${value} (${change})`;
}

formatResult(name: string, base: IResult, current: IResult): Array<string> {
return [
name,
this.formatLine(
this.formatBytes(current.size),
this.formatChange(base.size, current.size)
),
this.formatLine(
this.formatTime(current.loading),
this.formatChange(base.loading, current.loading)
),
this.formatLine(
this.formatTime(current.running),
this.formatChange(base.running, current.running)
),
this.formatTime(current.total)
];
}

formatResults(
base: { [name: string]: IResult },
current: { [name: string]: IResult }
): Array<Array<string>> {
const names = [...new Set([...Object.keys(base), ...Object.keys(current)])];

return names.map((name: string) =>
this.formatResult(name, base[name], current[name])
);
}
}
export default SizeLimit;
164 changes: 28 additions & 136 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,157 +1,49 @@
import { getInput, setFailed } from "@actions/core";
import { context, GitHub } from "@actions/github";
import { exec } from "@actions/exec";
// @ts-ignore
import table from "markdown-table";
// @ts-ignore
import bytes from "bytes";

interface IResult {
name: string;
size: number;
running: number;
loading: number;
}

interface IResults {
[name: string]: IResult;
}

const formatBytes = (size: number): string => {
return bytes.format(size, { unitSeparator: " " });
};

const formatTime = (seconds: number): string => {
if (seconds >= 1) {
return `${Math.ceil(seconds * 10) / 10} s`;
}

return `${Math.ceil(seconds * 1000)} ms`;
};

const parseResults = (str: string): IResults => {
const results = JSON.parse(str);

return results.reduce((current: IResults, result: any) => {
return {
...current,
[result.name]: {
name: result.name,
size: +result.size,
running: +result.running,
loading: +result.loading
}
};
}, {});
};

const getResults = async (
branch?: string
): Promise<{ status: number; results: IResults }> => {
let output = "";

if (branch) {
await exec(`git checkout -f ${branch}`);
}

await exec(`npm install`);
await exec(`npm run build`);
const status = await exec(`npx size-limit --json`, [], {
windowsVerbatimArguments: true,
ignoreReturnCode: true,
listeners: {
stdout: (data: Buffer) => {
output += data.toString();
}
}
});

return {
status,
results: parseResults(output)
};
};
import Git from "./Git";
import SizeLimit from "./SizeLimit";

const formatChange = (base: number = 0, current: number = 0) => {
if (current === 0) {
return "-100%";
}

const value = ((current - base) / current) * 100;
const formatted = (Math.sign(value) * Math.ceil(Math.abs(value) * 100)) / 100;

if (value > 0) {
return `+${formatted}% 🔺`;
}

if (value === 0) {
return `${formatted}%`;
}

return `${formatted}% 🔽`;
};

const getTable = (baseResults: IResults, currentResults: IResults): string => {
const keys = [
...new Set([...Object.keys(baseResults), ...Object.keys(currentResults)])
];

const values = keys.map((key: string) => {
const base = baseResults[key];
const current = currentResults[key];
const total = current.loading + current.running;

return [
key,
`${formatBytes(current.size)} (${formatChange(base.size, current.size)})`,
`${formatTime(current.loading)} (${formatChange(
base.loading,
current.loading
)})`,
`${formatTime(current.running)} (${formatChange(
base.running,
current.running
)})`,
formatTime(total)
];
});

return table([
[
"Path",
"Size",
"Loading time (3g)",
"Running time (snapdragon)",
"Total time"
],
...values
]);
};
const TABLE_HEADER = [
"Path",
"Size",
"Loading time (3g)",
"Running time (snapdragon)",
"Total time"
];

async function run() {
try {
const token = getInput("github_token");

if (context.payload.pull_request === null) {
setFailed("No pull request found.");
return;
return setFailed("No pull request found.");
}

const { status, results: current } = await getResults();
const { results: base } = await getResults(process.env.GITHUB_BASE_REF);
const token = getInput("github_token");
const octokit = new GitHub(token);
const git = new Git();
const limit = new SizeLimit();

const { status, output } = await git.execSizeLimit();
const { output: baseOutput } = await git.execSizeLimit(
process.env.GITHUB_BASE_REF
);
const base = limit.parseResults(baseOutput);
const current = limit.parseResults(output);

const number = context.payload.pull_request.number;
const octokit = new GitHub(token);
const event = status > 0 ? "REQUEST_CHANGES" : "COMMENT";
const body = [
"## [size-limit](https://github.com/ai/size-limit) report",
table([TABLE_HEADER, ...limit.formatResults(base, current)])
].join("\r\n");

octokit.pulls.createReview({
...context.repo,
// eslint-disable-next-line camelcase
pull_number: number,
event: status > 0 ? "REQUEST_CHANGES" : "COMMENT",
body: [
"## [size-limit](https://github.com/ai/size-limit) report",
getTable(base, current)
].join("\r\n")
event,
body
});
} catch (error) {
setFailed(error.message);
Expand Down

0 comments on commit ce58bfe

Please sign in to comment.