From 289046ca036a282e973d19d8c70ddad3dee79131 Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Wed, 21 Oct 2020 22:28:13 -0700 Subject: [PATCH 1/7] make reporting of github status opt-in and configurable #9 --- lib/config.js | 4 ++++ lib/report.js | 15 ++++++++++++--- test/config.test.js | 19 ++++++++++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/lib/config.js b/lib/config.js index 8390d9a..a643d32 100644 --- a/lib/config.js +++ b/lib/config.js @@ -25,6 +25,10 @@ const DEFAULT_CONFIG = { fail: "50%", warn: "10%", ok: "-10%" + }, + report: { + githubComment: true, + githubStatus: false } }; diff --git a/lib/report.js b/lib/report.js index 114b610..ecaf42b 100644 --- a/lib/report.js +++ b/lib/report.js @@ -17,6 +17,7 @@ const debug = require("debug")("sizewatcher"); const render = require("./render"); const github = require("./github"); +const config = require("./config").get(); const STATUS_TEXTS = { cheers: "decreasing, perfect!", @@ -118,6 +119,10 @@ async function report(deltas) { console.log(); console.log(render.asText(deltas)); + if (!config.report.githubComment && !config.report.githubStatus) { + return; + } + const repo = await getGithubRepo(); if (!repo) { @@ -132,10 +137,14 @@ async function report(deltas) { return; } - const commentUrl = await reportAsGithubComment(repo, deltas); + let commentUrl; + if (config.report.githubComment) { + commentUrl = await reportAsGithubComment(repo, deltas); + } - // TODO: make status configurable (enable on/off) - await reportAsGithubStatus(repo, deltas, commentUrl); + if (config.report.githubStatus) { + await reportAsGithubStatus(repo, deltas, commentUrl); + } } module.exports = report; \ No newline at end of file diff --git a/test/config.test.js b/test/config.test.js index b790038..6846cf2 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -30,9 +30,11 @@ describe("config", function() { warn: "10%", ok: "-10%" }); + assert.ok(cfg.report.githubComment); + assert.ok(!cfg.report.githubStatus); }); - it("loads config file if exists", function() { + it("loads config file with percentage limits", function() { mockFs({ ".sizewatcher.yml": ` @@ -67,4 +69,19 @@ limits: ok: 50 }); }); + + it("loads config file with reportStatus", function() { + mockFs({ + ".sizewatcher.yml": +` +report: + githubComment: false + githubStatus: true +` + }); + const cfg = config.reload(); + assert.strictEqual(typeof cfg, "object"); + assert.strictEqual(cfg.report.githubComment, false); + assert.strictEqual(cfg.report.githubStatus, true); + }); }); \ No newline at end of file From 31b78f655f7be4f4cfc4ab6885795e1a230d666d Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Wed, 21 Oct 2020 22:34:02 -0700 Subject: [PATCH 2/7] add test .sizewatcher.yml --- .sizewatcher.yml | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 .sizewatcher.yml diff --git a/.sizewatcher.yml b/.sizewatcher.yml new file mode 100644 index 0000000..6db7d5c --- /dev/null +++ b/.sizewatcher.yml @@ -0,0 +1,7 @@ +# FOR TESTING ONLY + +report: + githubStatus: true + +limits: + fail: 0% From 238556ac88c853dd5523cc36d8e30a6377214457 Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Wed, 21 Oct 2020 22:36:30 -0700 Subject: [PATCH 3/7] config.test.js: mock fs in default case as well (in case .sizewatcher.yml is in git repo) --- test/config.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/config.test.js b/test/config.test.js index 6846cf2..0a8f1a2 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -23,6 +23,7 @@ describe("config", function() { }); it("loads default config if no config file exists", function() { + mockFs(); const cfg = config.reload(); assert.strictEqual(typeof cfg, "object"); assert.deepStrictEqual(cfg.limits, { From ba765257601b83837328647d1c4c23e59ccc99de Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Wed, 21 Oct 2020 22:47:44 -0700 Subject: [PATCH 4/7] print configuration in notes --- lib/config.js | 4 ++++ lib/render.js | 3 +++ test/config.test.js | 14 ++++++++++++++ 3 files changed, 21 insertions(+) diff --git a/lib/config.js b/lib/config.js index a643d32..461b766 100644 --- a/lib/config.js +++ b/lib/config.js @@ -60,5 +60,9 @@ module.exports = { reload() { config = null; return this.get(); + }, + + asYaml() { + return yaml.safeDump(this.get()); } }; \ No newline at end of file diff --git a/lib/render.js b/lib/render.js index 0e77fd1..710a7c6 100644 --- a/lib/render.js +++ b/lib/render.js @@ -14,6 +14,7 @@ // render.js - formats comparison result text +const config = require("./config"); const pb = require('pretty-bytes'); const ICONS = { @@ -74,6 +75,8 @@ function renderAsMarkdown(deltas) { markdown += `- PR branch: \`${deltas.after.branch}\` @ ${deltas.after.sha}\n`; markdown += `- Base branch: \`${deltas.before.branch}\`\n`; markdown += `- Sizewatcher v${require('../package.json').version}\n`; + markdown += `- Configuration`; + markdown += `
\n${config.asYaml()}
`; markdown += `\n\n`; return markdown; diff --git a/test/config.test.js b/test/config.test.js index 0a8f1a2..e7a5a58 100644 --- a/test/config.test.js +++ b/test/config.test.js @@ -85,4 +85,18 @@ report: assert.strictEqual(cfg.report.githubComment, false); assert.strictEqual(cfg.report.githubStatus, true); }); + + it("returns default config in asYaml() if no config file exists", function() { + mockFs(); + config.reload(); + const yaml = config.asYaml(); + assert.strictEqual(yaml, `limits: + fail: 50% + warn: 10% + ok: '-10%' +report: + githubComment: true + githubStatus: false +`); + }); }); \ No newline at end of file From 5e713297cc0185ae3f2fe3e4a8efe53d22cac971 Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Wed, 21 Oct 2020 22:49:40 -0700 Subject: [PATCH 5/7] fix newline --- lib/render.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/render.js b/lib/render.js index 710a7c6..9c1a82f 100644 --- a/lib/render.js +++ b/lib/render.js @@ -75,8 +75,8 @@ function renderAsMarkdown(deltas) { markdown += `- PR branch: \`${deltas.after.branch}\` @ ${deltas.after.sha}\n`; markdown += `- Base branch: \`${deltas.before.branch}\`\n`; markdown += `- Sizewatcher v${require('../package.json').version}\n`; - markdown += `- Configuration`; - markdown += `
\n${config.asYaml()}
`; + markdown += `- Configuration\n`; + markdown += `
\n${config.asYaml()}
`; markdown += `\n\n`; return markdown; From e764a67bdf6e226b78d0d27758407a4e82f4852e Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Wed, 21 Oct 2020 23:00:00 -0700 Subject: [PATCH 6/7] Update .sizewatcher.yml --- .sizewatcher.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.sizewatcher.yml b/.sizewatcher.yml index 6db7d5c..933e443 100644 --- a/.sizewatcher.yml +++ b/.sizewatcher.yml @@ -4,4 +4,4 @@ report: githubStatus: true limits: - fail: 0% + fail: 400000 From f60eb5bec564b408fd7c316eabdb9e1bb58e9137 Mon Sep 17 00:00:00 2001 From: Alexander Klimetschek Date: Sat, 24 Oct 2020 00:58:58 -0700 Subject: [PATCH 7/7] Update .sizewatcher.yml --- .sizewatcher.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.sizewatcher.yml b/.sizewatcher.yml index 933e443..fb83281 100644 --- a/.sizewatcher.yml +++ b/.sizewatcher.yml @@ -4,4 +4,4 @@ report: githubStatus: true limits: - fail: 400000 + fail: 400KB