diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-11-29 21:37:31 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-11-29 21:37:31 -0500 |
| commit | a375b0b575ec205608abb7a460fe2156a3ea945f (patch) | |
| tree | 52f4f6f7c57cc79bdd7681277eb93fdc211b2a7c /src | |
| parent | 1158e558606034b681f4663d62677e798cf5ec23 (diff) | |
| download | ao-coverage-a375b0b575ec205608abb7a460fe2156a3ea945f.tar.gz ao-coverage-a375b0b575ec205608abb7a460fe2156a3ea945f.tar.bz2 ao-coverage-a375b0b575ec205608abb7a460fe2156a3ea945f.zip | |
Implement file handling for reports
Diffstat (limited to 'src')
| -rw-r--r-- | src/formats.ts | 2 | ||||
| -rw-r--r-- | src/index.ts | 87 |
2 files changed, 55 insertions, 34 deletions
diff --git a/src/formats.ts b/src/formats.ts index 5d389b4..59a966e 100644 --- a/src/formats.ts +++ b/src/formats.ts @@ -1,4 +1,4 @@ -interface Format { +export interface Format { // returns the coverage value as %: Number(90.0), Number(100.0), Number(89.5) parse_coverage: (file: Document) => number; match_color: (coverage: number, stage_1: number, stage_2: number) => string; diff --git a/src/index.ts b/src/index.ts index d592405..3c5ba36 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,22 +1,29 @@ import dotenv from "dotenv"; import express from "express"; +import { JSDOM } from "jsdom"; import { badgen } from "badgen"; import path from "path"; import fs from "fs"; -import formats from "./formats"; +import formats, { Format } from "./formats"; import metadata from "./metadata"; // Start-up configuration dotenv.config(); -const PORT = process.env.PORT ? Number(process.env.PORT) : 3000; +const PORT = Number(process.env.PORT || 3000); const TOKEN = process.env.TOKEN || ""; +const UPLOAD_LIMIT = Number(process.env.UPLOAD_LIMIT || 4194304); const HOST_DIR = process.env.HOST_DIR || (() => { throw Error("HOST_DIR must be defined"); })(); +fs.accessSync(HOST_DIR, fs.constants.R_OK | fs.constants.W_OK); +if (!path.isAbsolute(HOST_DIR)) { + throw Error("HOST_DIR must be an absolute path"); +} + const app: express.Application = express(); // serve script for posting coverage report @@ -44,32 +51,44 @@ app.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => { return res.status(406).send("Report format unknown"); } - //TODO acquire file, verify file size/content type (HTML) - const contents = ""; - //req.on('data', (raw) => {}); - //req.on('end', () => {}); - - const doc = new DOMParser().parseFromString(contents, "text/html"); - const formatter = formats.get_format(reporter); - let coverage: number; - try { - coverage = formatter.parse_coverage(doc); - } catch { - return res.status(400).send("Invalid report document"); - } - - const badge = badgen({ - label: "coverage", - status: Math.floor(coverage).toString() + "%", - //TODO @Metadata stage values should come from metadata - color: formatter.match_color(coverage, 95, 80) + var contents = ""; + req.on("data", raw => { + if (contents.length + raw.length > UPLOAD_LIMIT) { + res.status(413).send("Uploaded file is too large"); + } else { + contents += raw; + } + }); + req.on("end", () => { + let formatter: Format, coverage: number; + try { + const doc = new JSDOM(contents).window.document; + formatter = formats.get_format(reporter); + coverage = formatter.parse_coverage(doc); + } catch { + return res.status(400).send("Invalid report document"); + } + + const badge = badgen({ + label: "coverage", + status: Math.floor(coverage).toString() + "%", + //TODO @Metadata stage values should come from metadata + color: formatter.match_color(coverage, 95, 80) + }); + + const report_path = path.join(HOST_DIR, org, repo, branch, commit); + + fs.promises + .mkdir(report_path, { recursive: true }) + .then(() => + fs.promises.writeFile(path.join(report_path, "badge.svg"), badge) + ) + .then(() => + fs.promises.writeFile(path.join(report_path, "index.html"), contents) + ) + //TODO @Metadata set branch alias for badge / report file + .then(() => res.status(200).send()); }); - //TODO store coverage % badge at %HOST_DIR%/%org%/%repo%/%commit%/badge.svg - //TODO store report file at %HOST_DIR%/%org%/%repo%/%commit%/index.html - - //TODO @Metadata set branch alias for badge / report file - - return res.status(501).send(); }); app.get("/v1/:org/:repo/:branch.svg", (req, res) => { @@ -77,7 +96,9 @@ app.get("/v1/:org/:repo/:branch.svg", (req, res) => { console.info("GET request to /v1/%s/%s/%s.svg", org, repo, branch); //TODO @Metadata get the commit @@ via metadata - //TODO send the badge file + const commit = ""; + + res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "badge.svg")); return res.status(501).send(); }); @@ -86,7 +107,9 @@ app.get("/v1/:org/:repo/:branch.html", (req, res) => { console.info("GET request to /v1/%s/%s/%s.html", org, repo, branch); //TODO @Metadata get the commit @@ via metadata - //TODO send the report file + const commit = ""; + + res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "index.html")); return res.status(501).send(); }); @@ -95,8 +118,7 @@ app.get("/v1/:org/:repo/:branch/:commit.svg", (req, res) => { const { org, repo, branch, commit } = req.params; console.info("GET request to /v1/%s/%s/%s/%s.svg", org, repo, branch, commit); - //TODO send the badge file - return res.status(501).send(); + res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "badge.svg")); }); // provide hard link for commit @@ -110,8 +132,7 @@ app.get("/v1/:org/:repo/:branch/:commit.html", (req, res) => { commit ); - //TODO send the report file - return res.status(501).send(); + res.sendFile(path.join(HOST_DIR, org, repo, branch, commit, "index.html")); }); app.listen(PORT, () => { |
