aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2021-09-12 13:53:15 -0400
committerKevin J Hoerr <kjhoerr@protonmail.com>2021-09-12 13:53:15 -0400
commit317b2590bf125ba3ceb1dba14d91d296f115846b (patch)
tree4482555558323325066c78c8b258bfe07f949c72 /src
parent4e14261c50311096a8a1d0a29689708f4320a9a9 (diff)
downloadao-coverage-317b2590bf125ba3ceb1dba14d91d296f115846b.tar.gz
ao-coverage-317b2590bf125ba3ceb1dba14d91d296f115846b.tar.bz2
ao-coverage-317b2590bf125ba3ceb1dba14d91d296f115846b.zip
Refactor upload processing
Diffstat (limited to 'src')
-rw-r--r--src/index.ts4
-rw-r--r--src/metadata.ts10
-rw-r--r--src/routes.test.ts11
-rw-r--r--src/routes.ts100
4 files changed, 72 insertions, 53 deletions
diff --git a/src/index.ts b/src/index.ts
index aa2f617..a488538 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -21,7 +21,9 @@ const ENV_CONFIG: EnvConfig = {
token: process.env.TOKEN ?? "",
uploadLimit: Number(process.env.UPLOAD_LIMIT ?? 4194304),
publicDir: path.join(__dirname, "..", "public"),
- hostDir: configOrError("HOST_DIR")
+ hostDir: configOrError("HOST_DIR"),
+ stage1: Number(process.env.STAGE_1 ?? 95),
+ stage2: Number(process.env.STAGE_2 ?? 80)
};
const logger = winston.createLogger(loggerConfig("ROOT"));
diff --git a/src/metadata.ts b/src/metadata.ts
index 0877031..b7dbf19 100644
--- a/src/metadata.ts
+++ b/src/metadata.ts
@@ -3,6 +3,7 @@ import winston from "winston";
import loggerConfig from "./util/logger";
import { BranchNotFoundError } from "./errors";
+import { GradientStyle } from "./formats";
interface Branch {
head: string;
@@ -32,6 +33,8 @@ export interface EnvConfig {
uploadLimit: number;
hostDir: string;
publicDir: string;
+ stage1: number;
+ stage2: number;
}
class Metadata {
@@ -59,6 +62,13 @@ class Metadata {
return this.config.publicDir;
}
+ getGradientStyle(): GradientStyle {
+ return {
+ stage1: this.config.stage1,
+ stage2: this.config.stage2
+ };
+ }
+
async getHeadCommit(
organization: string,
repository: string,
diff --git a/src/routes.test.ts b/src/routes.test.ts
index bda427b..e1b8f74 100644
--- a/src/routes.test.ts
+++ b/src/routes.test.ts
@@ -23,8 +23,6 @@ test("HOST_DIR must be readable and writable", () => {
).not.toThrowError();
});
-process.env.UPLOAD_LIMIT = "40000";
-
import { configOrError, persistTemplate } from "./util/config";
import routes from "./routes";
import Metadata, { EnvConfig } from "./metadata";
@@ -41,6 +39,7 @@ type MetadataMockType = {
getUploadLimit: jest.Mock;
getHostDir: jest.Mock;
getPublicDir: jest.Mock;
+ getGradientStyle: jest.Mock;
updateBranch: jest.Mock;
createRepository: jest.Mock;
};
@@ -50,7 +49,9 @@ const config = {
// should be just larger than the example report used
uploadLimit: Number(40000),
hostDir: configOrError("HOST_DIR"),
- publicDir: path.join(__dirname, "..", "public")
+ publicDir: path.join(__dirname, "..", "public"),
+ stage1: 95,
+ stage2: 80
};
const mock = (
headCommit: jest.Mock = jest.fn(
@@ -64,6 +65,10 @@ const mock = (
getUploadLimit: jest.fn(() => config.uploadLimit),
getHostDir: jest.fn(() => config.hostDir),
getPublicDir: jest.fn(() => config.publicDir),
+ getGradientStyle: jest.fn(() => ({
+ stage1: config.stage1,
+ stage2: config.stage2
+ })),
getHeadCommit: headCommit,
updateBranch: updateBranch,
createRepository: jest.fn()
diff --git a/src/routes.ts b/src/routes.ts
index 1601c8e..be4380a 100644
--- a/src/routes.ts
+++ b/src/routes.ts
@@ -5,7 +5,7 @@ import winston from "winston";
import path from "path";
import fs from "fs";
-import formats, { GradientStyle } from "./formats";
+import formats from "./formats";
import Metadata, { HeadIdentity } from "./metadata";
import loggerConfig from "./util/logger";
import { Messages } from "./errors";
@@ -51,16 +51,23 @@ export default (metadata: Metadata): Router => {
return res.status(406).send(Messages.InvalidFormat);
}
- let contents = "";
const limit = metadata.getUploadLimit();
+ if (Number(req.headers["content-length"] ?? 0) > limit) {
+ return res.status(413).send(Messages.FileTooLarge);
+ }
+
+ let contents = "";
req.on("data", raw => {
- if (contents.length + raw.length > limit) {
- res.status(413).send(Messages.FileTooLarge);
- } else {
+ if (contents.length <= limit) {
contents += raw;
}
});
- req.on("end", () => {
+ req.on("end", async () => {
+ // Ignore large requests
+ if (contents.length > limit) {
+ return res.status(413).send(Messages.FileTooLarge);
+ }
+
let coverage: number;
const doc = new JSDOM(contents).window.document;
const formatter = formats.getFormat(format);
@@ -80,50 +87,45 @@ export default (metadata: Metadata): Router => {
commit
);
- fs.promises
- .mkdir(reportPath, { recursive: true })
- .then(
- () =>
- //TODO @Metadata stage values should come from metadata
- new Promise<GradientStyle>(solv => solv({ stage1: 95, stage2: 80 }))
- )
- .then(
- style =>
- new Promise<string>(solv =>
- solv(
- badgen({
- label: "coverage",
- status: Math.floor(coverage).toString() + "%",
- color: formatter.matchColor(coverage, style)
- })
- )
- )
- )
- .then(badge =>
- fs.promises.writeFile(path.join(reportPath, "badge.svg"), badge)
- )
- .then(() =>
- fs.promises.writeFile(path.join(reportPath, "index.html"), contents)
- )
- .then(() =>
- metadata.updateBranch({
- organization: org,
- repository: repo,
- branch,
- head: commit
- })
- )
- .then(result =>
- result
- ? res.status(200).send()
- : res.status(500).send(Messages.UnknownError)
- )
- .catch(err => {
- logger.error(
- err ?? "Unknown error occurred while processing POST request"
- );
- return res.status(500).send(Messages.UnknownError);
+ try {
+ // Create report directory if not exists
+ await fs.promises.mkdir(reportPath, { recursive: true });
+
+ // Create report badge
+ const style = metadata.getGradientStyle();
+ const badge = badgen({
+ label: "coverage",
+ status: Math.floor(coverage).toString() + "%",
+ color: formatter.matchColor(coverage, style)
});
+
+ // Write report and badge to directory
+ await fs.promises.writeFile(path.join(reportPath, "badge.svg"), badge);
+ await fs.promises.writeFile(
+ path.join(reportPath, "index.html"),
+ contents
+ );
+
+ // Update (or create) given branch with commit info
+ const branchUpdate = await metadata.updateBranch({
+ organization: org,
+ repository: repo,
+ branch,
+ head: commit
+ });
+
+ if (branchUpdate) {
+ return res.status(200).send();
+ } else {
+ logger.error("Branch update failed");
+ return res.status(500).send(Messages.UnknownError);
+ }
+ } catch (err) {
+ logger.error(
+ err ?? "Unknown error occurred while processing POST request"
+ );
+ return res.status(500).send(Messages.UnknownError);
+ }
});
});