feat(core): add output_condition option (#620) [skip ci]

This commit is contained in:
Simon Lecoq
2021-10-26 00:11:06 -04:00
committed by GitHub
parent 9cb1ca667a
commit 7fffb151c5
4 changed files with 61 additions and 1 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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

View File

@@ -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: