From f40e0a995d35c93d7d2706e28e39133b0b8be6b0 Mon Sep 17 00:00:00 2001 From: Simon Lecoq <22963968+lowlighter@users.noreply.github.com> Date: Wed, 23 Nov 2022 00:16:30 +0100 Subject: [PATCH] fix(app/action): mishandled `config_output` (#1293) --- source/app/action/index.mjs | 64 ++++++++++++++++++-------------- source/plugins/core/metadata.yml | 2 + 2 files changed, 39 insertions(+), 27 deletions(-) diff --git a/source/app/action/index.mjs b/source/app/action/index.mjs index 53261d12..ea302ccf 100644 --- a/source/app/action/index.mjs +++ b/source/app/action/index.mjs @@ -318,6 +318,9 @@ function quit(reason) { console.debug(error) } info("Previous render sha", committer.sha ?? "(none)") + //Compatibility check + if ((_action === "gist")&&(["png", "jpeg", "markdown-pdf"].includes(_output))) + throw new Error(`"config_output: ${_output}" is not supported with "config_action: ${_action}"`) } else if (dryrun) { info("Dry-run", true) @@ -398,18 +401,21 @@ function quit(reason) { //Render metrics info.break() info.section("Rendering") - let rendered = await retry(async () => { - const {rendered, errors} = await metrics({login: user, q}, {graphql, rest, plugins, conf, die, verify, convert}, {Plugins, Templates}) + let {rendered, mime} = await retry(async () => { + const {rendered, mime, errors} = await metrics({login: user, q}, {graphql, rest, plugins, conf, die, verify, convert}, {Plugins, Templates}) if (errors.length) { console.warn(`::group::${errors.length} error(s) occurred`) console.warn(util.inspect(errors, {depth: Infinity, maxStringLength: 256})) console.warn("::endgroup::") } - return rendered + return {rendered, mime} }, {retries, delay: retries_delay}) if (!rendered) throw new Error("Could not render metrics") info("Status", "complete") + info("MIME type", mime) + const buffer = typeof rendered === "object" ? rendered instanceof Buffer ? rendered : Buffer.from(JSON.stringify(rendered)) : Buffer.from(`${rendered}`) + //Debug print if (dprint) { @@ -421,35 +427,39 @@ function quit(reason) { //Output condition info.break() info.section("Saving") - info("Output condition", _output_condition) - if ((_output_condition === "data-changed") && ((committer.commit) || (committer.pr))) { - const {svg} = await import("../metrics/utils.mjs") - let data = "" - await retry(async () => { - 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 - } - }, {retries: retries_output_action, delay: retries_delay_output_action}) - 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 + if (_output === "svg") { + info("Output condition", _output_condition) + if ((_output_condition === "data-changed") && ((committer.commit) || (committer.pr))) { + const {svg} = await import("../metrics/utils.mjs") + let data = "" + await retry(async () => { + 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 + } + }, {retries: retries_output_action, delay: retries_delay_output_action}) + 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 + } } + else + info("Output condition", `Not applicable for ${_output}`) //Save output to renders output folder if (dryrun) info("Actions to perform", "(none)") else { await fs.mkdir(paths.dirname(paths.join("/renders", filename)), {recursive: true}) - await fs.writeFile(paths.join("/renders", filename), Buffer.from(typeof rendered === "object" ? JSON.stringify(rendered) : `${rendered}`)) + await fs.writeFile(paths.join("/renders", filename), buffer) info(`Save to /metrics_renders/${filename}`, "ok") info("Output action", _action) } @@ -517,7 +527,7 @@ function quit(reason) { //Upload to gist (this is done as user since committer_token may not have gist rights) if (committer.gist) { await retry(async () => { - await rest.gists.update({gist_id: committer.gist, files: {[filename]: {content: rendered}}}) + await rest.gists.update({gist_id: committer.gist, files: {[filename]: {content: buffer.toString()}}}) info(`Upload to gist ${committer.gist}`, "ok") committer.commit = false }, {retries: retries_output_action, delay: retries_delay_output_action}) @@ -530,7 +540,7 @@ function quit(reason) { ...github.context.repo, path: filename, message: committer.message, - content: Buffer.from(typeof rendered === "object" ? JSON.stringify(rendered) : `${rendered}`).toString("base64"), + content: buffer.toString("base64"), branch: committer.pr ? committer.head : committer.branch, ...(committer.sha ? {sha: committer.sha} : {}), }) diff --git a/source/plugins/core/metadata.yml b/source/plugins/core/metadata.yml index c48b30e4..14581873 100644 --- a/source/plugins/core/metadata.yml +++ b/source/plugins/core/metadata.yml @@ -106,6 +106,8 @@ inputs: - `gist`: push output to `committer_gist` > 💡 When using `pull-request`, you will need to set the last job with a `pull-request-*` action instead, else it won't be merged + + > ⚠️ As GitHub gists API does not support binary files upload, `gist` does not support [`config_output`](/source/plugins/core/README.md#config_output) set to either `png`, `jpeg` or `markdown-pdf` type: string default: commit values: