diff options
| author | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-11-23 12:02:45 -0500 |
|---|---|---|
| committer | Kevin J Hoerr <kjhoerr@protonmail.com> | 2019-11-23 12:02:45 -0500 |
| commit | 62a6c312a19824a284d95d6f81d3eb5da86dbf5a (patch) | |
| tree | ee398cb28006ed3d988b93bd9bf0a7c64953fcaf | |
| parent | a4fdedfcac02cb53608d9d3b9e96de0526140fd7 (diff) | |
| download | ao-coverage-62a6c312a19824a284d95d6f81d3eb5da86dbf5a.tar.gz ao-coverage-62a6c312a19824a284d95d6f81d3eb5da86dbf5a.tar.bz2 ao-coverage-62a6c312a19824a284d95d6f81d3eb5da86dbf5a.zip | |
Generate report badge with color gradient
| -rw-r--r-- | src/formats.ts | 17 | ||||
| -rw-r--r-- | src/index.ts | 34 |
2 files changed, 37 insertions, 14 deletions
diff --git a/src/formats.ts b/src/formats.ts index b5a24bf..db9a188 100644 --- a/src/formats.ts +++ b/src/formats.ts @@ -1,5 +1,7 @@ interface Format { - parse_coverage: (file: Document) => number; + // 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, } interface FormatList { @@ -12,12 +14,23 @@ interface FormatObj { get_format: (format: string) => Format, } +// color is a gradient from green (>=stage_1) -> yellow (stage_2) -> red. Stage values should come from metadata. +const default_color_matches = (coverage: number, stage_1: number, stage_2: number) => { + const gradient = coverage >= stage_1 ? 15 : + (coverage >= stage_2 ? + (Math.floor(coverage) - stage_2) * 16 + 15 : + 240 + Math.floor(coverage / (stage_2 / 15))); + return gradient.toString(16) + "0"; +}; + const FormatsObj: FormatObj = { formats: { tarpaulin: { parse_coverage: (file: Document) => { + //TODO parse coverage from file (example?) return 0.0; - } + }, + match_color: default_color_matches, }, }, diff --git a/src/index.ts b/src/index.ts index 5e54c84..ca65cb9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import dotenv from 'dotenv'; import express from 'express'; -import badgen from 'badgen'; +import {badgen} from 'badgen'; import path from 'path'; import fs from 'fs'; @@ -27,6 +27,7 @@ app.post('/v1/:org/:repo/:branch/:commit.html', (req, res) => { console.info("POST request to /v1/%s/%s/%s/%s.html", org, repo, branch, commit); const {token, format} = req.query; + //TODO @Metadata token should come from metadata if (token != TOKEN) { return res.status(401).send(); } @@ -36,27 +37,35 @@ app.post('/v1/:org/:repo/:branch/:commit.html', (req, res) => { return res.status(406).send(); } - return res.status(501).send(); - //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 coverage = formats.get_format(reporter).parse_coverage(doc); - //TODO create badge for coverage % + const doc = new DOMParser().parseFromString(contents, "text/html"); + const formatter = formats.get_format(reporter); + const coverage = formatter.parse_coverage(doc); + + 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), + }); //TODO store coverage % badge at %HOST_DIR%/%org%/%repo%/%commit%/badge.svg - //TODO store uploaded file at %HOST_DIR%/%org%/%repo%/%commit%/index.html + //TODO store report file at %HOST_DIR%/%org%/%repo%/%commit%/index.html - //TODO set branch alias for coverage % / uploaded file + //TODO @Metadata set branch alias for badge / report file + + return res.status(501).send(); }); app.get('/v1/:org/:repo/:branch.svg', (req, res) => { const {org, repo, branch} = req.params; console.info("GET request to /v1/%s/%s/%s.svg", org, repo, branch); - //TODO link the badge via metadata + //TODO @Metadata get the commit @@ via metadata + //TODO send the badge file return res.status(501).send(); }); @@ -64,7 +73,8 @@ app.get('/v1/:org/:repo/:branch.html', (req, res) => { const {org, repo, branch} = req.params; console.info("GET request to /v1/%s/%s/%s.html", org, repo, branch); - //TODO link the file via metadata + //TODO @Metadata get the commit @@ via metadata + //TODO send the report file return res.status(501).send(); }); @@ -73,7 +83,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 link the badge + //TODO send the badge file return res.status(501).send(); }); @@ -82,7 +92,7 @@ app.get('/v1/:org/:repo/:branch/:commit.html', (req, res) => { const {org, repo, branch, commit} = req.params; console.info("GET request to /v1/%s/%s/%s/%s.html", org, repo, branch, commit); - //TODO link the file + //TODO send the report file return res.status(501).send(); }); |
