From 7fffb151c57d5a1feebac8f09ac56afcb6d84e34 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Tue, 26 Oct 2021 00:11:06 -0400 Subject: [PATCH] feat(core): add output_condition option (#620) [skip ci] --- source/app/action/index.mjs | 25 ++++++++++++++++++++++++- source/app/metrics/utils.mjs | 24 ++++++++++++++++++++++++ source/plugins/core/README.md | 4 ++++ source/plugins/core/metadata.yml | 9 +++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) diff --git a/source/app/action/index.mjs b/source/app/action/index.mjs index 1dd59940..aeaca919 100644 --- a/source/app/action/index.mjs +++ b/source/app/action/index.mjs @@ -93,6 +93,7 @@ async function wait(seconds) { retries, "retries.delay":retries_delay, "output.action":_action, + "output.condition":_output_condition, delay, ...config } = metadata.plugins.core.inputs.action({core}) @@ -321,9 +322,31 @@ async function wait(seconds) { throw error ?? new Error("Could not render metrics") info("Status", "complete") - //Save output to renders output folder + //Output condition info.break() info.section("Saving") + info("Output condition", _output_condition) + if ((_output_condition === "skip-if-only-metadata-changed")&&((committer.commit) || (committer.pr))) { + const {svg} = await import("../metrics/utils.mjs") + let data = "" + try { + data = `${Buffer.from((await committer.rest.repos.getContent({...github.context.repo, ref:`heads/${committer.head}`, path:filename})).data.content, "base64")}` + } + catch (error) { + if (error.response.status !== 404) + throw error + } + const previous = await svg.hash(data) + info("Previous hash", previous) + const current = await svg.hash(rendered) + info("Current hash", current) + const changed = (previous !== current) + info("Content changed", changed) + if (!changed) + committer.commit = false + } + + //Save output to renders output folder if (dryrun) info("Actions to perform", "(none)") else { diff --git a/source/app/metrics/utils.mjs b/source/app/metrics/utils.mjs index c5c7f873..86969818 100644 --- a/source/app/metrics/utils.mjs +++ b/source/app/metrics/utils.mjs @@ -24,6 +24,7 @@ import fetch from "node-fetch" import readline from "readline" import emoji from "emoji-name-map" import minimatch from "minimatch" +import crypto from "crypto" prism_lang() //Exports @@ -413,6 +414,29 @@ export const svg = { console.debug("metrics/svg/resize > rendering complete") return {resized, mime} }, + /**Hash a SVG (removing its metadata first)*/ + async hash(rendered) { + //Handle empty case + if (!rendered) + return null + //Instantiate browser if needed + if (!svg.resize.browser) { + svg.resize.browser = await puppeteer.launch() + console.debug(`metrics/svg/hash > started ${await svg.resize.browser.version()}`) + } + //Compute hash + const page = await svg.resize.browser.newPage() + await page.setContent(rendered, {waitUntil:["load", "domcontentloaded", "networkidle2"]}) + const data = await page.evaluate(async () => { + document.querySelector("footer")?.remove() + return document.querySelector("svg").outerHTML + }) + const hash = crypto.createHash("md5").update(data).digest("hex") + //Result + await page.close() + console.debug(`metrics/svg/hash > MD5=${hash}`) + return hash + }, /**Render twemojis */ async twemojis(rendered, {custom = true} = {}) { //Load emojis diff --git a/source/plugins/core/README.md b/source/plugins/core/README.md index b8b189c4..540875e3 100644 --- a/source/plugins/core/README.md +++ b/source/plugins/core/README.md @@ -203,6 +203,10 @@ It is possible to configure output behaviour using `output_action` option, which - `gist`, where output will be stored an already existing gist - To use this feature, a `gists` scope must be granted to your `token` and `committer_gist` identifier must be provided +It also possible to alter output condition using `output_action` option, which can be set to: +- `always`, to always push changes (provided that git sha changed) +- `skip-if-only-metadata-changed`, to skip changes if only metadata contained in footer changed + #### ℹ️ Examples workflows ```yaml diff --git a/source/plugins/core/metadata.yml b/source/plugins/core/metadata.yml index 33ce4854..c896d716 100644 --- a/source/plugins/core/metadata.yml +++ b/source/plugins/core/metadata.yml @@ -94,6 +94,15 @@ inputs: - pull-request-rebase # Same as "pull-request" and additionaly rebase and merge pull request - gist # Save output to "committer_gist" + # Output condition + output_condition: + description: Output condition + type: string + default: always + values: + - always # Always push changes + - skip-if-only-metadata-changed # Skip changes if only metadata contained in footer changed + # Optimize SVG image to reduce its filesize # Some templates may not support this option optimize: