aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin J Hoerr <kjhoerr@protonmail.com>2019-12-09 18:11:26 -0500
committerKevin J Hoerr <kjhoerr@protonmail.com>2019-12-09 18:11:26 -0500
commit85d646880e1e3fef1d9547b72c2d0107e3465d90 (patch)
treeaaa1b2e2347daf1fa277baf33a6b0bedf35c009d
parent05204ed4699f983887db85416dba9a387b1c235a (diff)
downloadao-coverage-85d646880e1e3fef1d9547b72c2d0107e3465d90.tar.gz
ao-coverage-85d646880e1e3fef1d9547b72c2d0107e3465d90.tar.bz2
ao-coverage-85d646880e1e3fef1d9547b72c2d0107e3465d90.zip
Add template process to serve bash file
Unfortunately, a static file can't really be used to serve the connection - the host address is needed to link back to the server. Only a light library is needed to handle the template file though, and if more are needed in the future it should be a non-trivial task to add them to the process. By default I'm configuring this to work with drone.io, since that is the CI that I personally will be using (most likely). It should be non-trivial to configure other CI to handle the script as well.
-rw-r--r--.env.sample1
-rw-r--r--package-lock.json52
-rw-r--r--package.json3
-rw-r--r--public/bash.template34
-rw-r--r--src/index.ts3
-rw-r--r--src/routes.ts8
-rw-r--r--src/templates.ts30
7 files changed, 126 insertions, 5 deletions
diff --git a/.env.sample b/.env.sample
index 05eed7a..7784eac 100644
--- a/.env.sample
+++ b/.env.sample
@@ -1,5 +1,6 @@
PORT=3000
BIND_ADDRESS=localhost
+TARGET_URL=http://localhost:3000
TOKEN=
HOST_DIR=/dist
UPLOAD_LIMIT=4194304
diff --git a/package-lock.json b/package-lock.json
index 2c51bae..05f3fdc 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -516,6 +516,12 @@
"delayed-stream": "~1.0.0"
}
},
+ "commander": {
+ "version": "2.20.3",
+ "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
+ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==",
+ "optional": true
+ },
"concat-map": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
@@ -1178,6 +1184,17 @@
"type-fest": "^0.8.1"
}
},
+ "handlebars": {
+ "version": "4.5.3",
+ "resolved": "https://registry.npmjs.org/handlebars/-/handlebars-4.5.3.tgz",
+ "integrity": "sha512-3yPecJoJHK/4c6aZhSvxOyG4vJKDshV36VHp0iVCDVh7o9w2vwi3NSnL2MMPj3YdduqaBcu7cGbggJQM0br9xA==",
+ "requires": {
+ "neo-async": "^2.6.0",
+ "optimist": "^0.6.1",
+ "source-map": "^0.6.1",
+ "uglify-js": "^3.1.4"
+ }
+ },
"har-schema": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/har-schema/-/har-schema-2.0.0.tgz",
@@ -1605,8 +1622,7 @@
"minimist": {
"version": "0.0.8",
"resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz",
- "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=",
- "dev": true
+ "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0="
},
"mkdirp": {
"version": "0.5.1",
@@ -1650,6 +1666,11 @@
"resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz",
"integrity": "sha512-hZXc7K2e+PgeI1eDBe/10Ard4ekbfrrqG8Ep+8Jmf4JID2bNg7NvCPOZN+kfF574pFQI7mum2AUqDidoKqcTOw=="
},
+ "neo-async": {
+ "version": "2.6.1",
+ "resolved": "https://registry.npmjs.org/neo-async/-/neo-async-2.6.1.tgz",
+ "integrity": "sha512-iyam8fBuCUpWeKPGpaNMetEocMt364qkCsfL9JuhjXX6dRnguRVOfk2GZaDpPjcOKiiXCPINZC1GczQ7iTq3Zw=="
+ },
"nice-try": {
"version": "1.0.5",
"resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",
@@ -1703,6 +1724,15 @@
"mimic-fn": "^2.1.0"
}
},
+ "optimist": {
+ "version": "0.6.1",
+ "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz",
+ "integrity": "sha1-2j6nRob6IaGaERwybpDrFaAZZoY=",
+ "requires": {
+ "minimist": "~0.0.1",
+ "wordwrap": "~0.0.2"
+ }
+ },
"optionator": {
"version": "0.8.3",
"resolved": "https://registry.npmjs.org/optionator/-/optionator-0.8.3.tgz",
@@ -2133,8 +2163,7 @@
"source-map": {
"version": "0.6.1",
"resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz",
- "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g==",
- "optional": true
+ "integrity": "sha512-UjgapumWlbMhkBgzT7Ykc5YXUT46F0iKu8SGXq0bcwP5dz/h0Plj6enJqjz1Zbq2l5WaqYnrVbwWOWMyF3F47g=="
},
"sparse-bitfield": {
"version": "3.0.3",
@@ -2452,6 +2481,16 @@
"resolved": "https://registry.npmjs.org/typescript/-/typescript-3.7.3.tgz",
"integrity": "sha512-Mcr/Qk7hXqFBXMN7p7Lusj1ktCBydylfQM/FZCk5glCNQJrCUKPkMHdo9R0MTFWsC/4kPFvDS0fDPvukfCkFsw=="
},
+ "uglify-js": {
+ "version": "3.7.2",
+ "resolved": "https://registry.npmjs.org/uglify-js/-/uglify-js-3.7.2.tgz",
+ "integrity": "sha512-uhRwZcANNWVLrxLfNFEdltoPNhECUR3lc+UdJoG9CBpMcSnKyWA94tc3eAujB1GcMY5Uwq8ZMp4qWpxWYDQmaA==",
+ "optional": true,
+ "requires": {
+ "commander": "~2.20.3",
+ "source-map": "~0.6.1"
+ }
+ },
"unpipe": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz",
@@ -2610,6 +2649,11 @@
"resolved": "https://registry.npmjs.org/word-wrap/-/word-wrap-1.2.3.tgz",
"integrity": "sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ=="
},
+ "wordwrap": {
+ "version": "0.0.3",
+ "resolved": "https://registry.npmjs.org/wordwrap/-/wordwrap-0.0.3.tgz",
+ "integrity": "sha1-o9XabNXAvAAI03I0u68b7WMFkQc="
+ },
"wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
diff --git a/package.json b/package.json
index 85b4079..1db7730 100644
--- a/package.json
+++ b/package.json
@@ -9,7 +9,7 @@
"main": "build/index.js",
"scripts": {
"start:dev": "tsc-watch --onSuccess \"node ./build/index.js\"",
- "format": "prettier --write src/**/*.ts",
+ "format": "prettier --write 'src/**/*.ts'",
"lint": "eslint 'src/**/*.ts'",
"lint:fix": "eslint --fix 'src/**/*.ts'",
"tsc": "tsc"
@@ -24,6 +24,7 @@
"dotenv": "8.2.0",
"express": "4.17.1",
"express-winston": "^4.0.1",
+ "handlebars": "^4.5.3",
"jsdom": "^15.2.1",
"mongodb": "^3.3.5",
"typescript": "^3.7.3",
diff --git a/public/bash.template b/public/bash.template
new file mode 100644
index 0000000..c7f95bc
--- /dev/null
+++ b/public/bash.template
@@ -0,0 +1,34 @@
+#!/usr/bin/env bash
+
+# ISC License
+# The full version of this license can be found here:
+# https://git.submelon.dev/kjhoerr/ao-coverage/raw/branch/trunk/LICENSE
+
+url="{{TARGET_URL}}"
+commit=""
+branch=""
+repo=""
+format="tarpaulin"
+report=""
+token="$COV_TOKEN"
+
+if [[ "$CI" -eq "drone" ]]; then
+ commit="$DRONE_COMMIT"
+ branch="$DRONE_BRANCH"
+ repo="$DRONE_REPO"
+else
+ commit="$VCS_COMMIT"
+ branch="$VCS_BRANCH"
+ repo="$VCS_REPO"
+fi
+
+if [[ "$format" -eq "tarpaulin" ]]; then
+ report="${REPORT_FILE:-tarpaulin-report.html}"
+fi
+
+if [[ ! -f "$report" ]]; then
+ echo "Report file could not be found."
+ exit 1
+fi
+
+curl -X POST --data-binary "@$report" -H 'Content-Type: text/html' "$url/v1/$repo/$branch/$commit.html?token=$token&format=tarpaulin" \ No newline at end of file
diff --git a/src/index.ts b/src/index.ts
index aab1da6..4165d2e 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -30,6 +30,9 @@ if (!path.isAbsolute(HOST_DIR)) {
process.exit(1);
}
+// prepare template files
+require("./templates");
+
new MongoClient(MONGO_URI, { useUnifiedTopology: true }).connect(
(err, mongo) => {
if (err !== null) {
diff --git a/src/routes.ts b/src/routes.ts
index becf4da..d429659 100644
--- a/src/routes.ts
+++ b/src/routes.ts
@@ -20,6 +20,14 @@ const logger = winston.createLogger(loggerConfig("HTTP"));
export default (metadata: Metadata): Router => {
const router = Router();
+ // serve script for posting coverage report
+ router.use(
+ "/bash",
+ express.static(path.join(HOST_DIR, "bash"), {
+ setHeaders: res => res.contentType("text/plain")
+ })
+ );
+
// Upload HTML file
router.post("/v1/:org/:repo/:branch/:commit.html", (req, res) => {
const { org, repo, branch, commit } = req.params;
diff --git a/src/templates.ts b/src/templates.ts
new file mode 100644
index 0000000..2ffd0dd
--- /dev/null
+++ b/src/templates.ts
@@ -0,0 +1,30 @@
+import winston from "winston";
+import handlebars from "handlebars";
+import path from "path";
+import fs from "fs";
+
+import loggerConfig from "./util/logger";
+import { configOrError } from "./util/config";
+
+const logger = winston.createLogger(loggerConfig("TEMPLATE"));
+
+const HOST_DIR = configOrError("HOST_DIR");
+const TARGET_URL = process.env.TARGET_URL || "http://localhost:3000";
+
+fs.promises
+ .readFile(path.join(__dirname, "..", "public", "bash.template"), "utf-8")
+ .then(buffer => {
+ const translate = handlebars.compile(buffer);
+
+ return {
+ name: "bash",
+ data: translate({ TARGET_URL })
+ };
+ })
+ .then(file =>
+ fs.promises
+ .writeFile(path.join(HOST_DIR, file.name), file.data)
+ .then(() => file)
+ )
+ .then(file => logger.debug("Generated '%s' from template file", file.name))
+ .catch(err => logger.error("Error while generating template file: %s", err));