Skip to content

Commit f40e0a9

Browse files
authored
fix(app/action): mishandled config_output (lowlighter#1293)
1 parent f791122 commit f40e0a9

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

source/app/action/index.mjs

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,9 @@ function quit(reason) {
318318
console.debug(error)
319319
}
320320
info("Previous render sha", committer.sha ?? "(none)")
321+
//Compatibility check
322+
if ((_action === "gist")&&(["png", "jpeg", "markdown-pdf"].includes(_output)))
323+
throw new Error(`"config_output: ${_output}" is not supported with "config_action: ${_action}"`)
321324
}
322325
else if (dryrun) {
323326
info("Dry-run", true)
@@ -398,18 +401,21 @@ function quit(reason) {
398401
//Render metrics
399402
info.break()
400403
info.section("Rendering")
401-
let rendered = await retry(async () => {
402-
const {rendered, errors} = await metrics({login: user, q}, {graphql, rest, plugins, conf, die, verify, convert}, {Plugins, Templates})
404+
let {rendered, mime} = await retry(async () => {
405+
const {rendered, mime, errors} = await metrics({login: user, q}, {graphql, rest, plugins, conf, die, verify, convert}, {Plugins, Templates})
403406
if (errors.length) {
404407
console.warn(`::group::${errors.length} error(s) occurred`)
405408
console.warn(util.inspect(errors, {depth: Infinity, maxStringLength: 256}))
406409
console.warn("::endgroup::")
407410
}
408-
return rendered
411+
return {rendered, mime}
409412
}, {retries, delay: retries_delay})
410413
if (!rendered)
411414
throw new Error("Could not render metrics")
412415
info("Status", "complete")
416+
info("MIME type", mime)
417+
const buffer = typeof rendered === "object" ? rendered instanceof Buffer ? rendered : Buffer.from(JSON.stringify(rendered)) : Buffer.from(`${rendered}`)
418+
413419

414420
//Debug print
415421
if (dprint) {
@@ -421,35 +427,39 @@ function quit(reason) {
421427
//Output condition
422428
info.break()
423429
info.section("Saving")
424-
info("Output condition", _output_condition)
425-
if ((_output_condition === "data-changed") && ((committer.commit) || (committer.pr))) {
426-
const {svg} = await import("../metrics/utils.mjs")
427-
let data = ""
428-
await retry(async () => {
429-
try {
430-
data = `${Buffer.from((await committer.rest.repos.getContent({...github.context.repo, ref: `heads/${committer.head}`, path: filename})).data.content, "base64")}`
431-
}
432-
catch (error) {
433-
if (error.response.status !== 404)
434-
throw error
435-
}
436-
}, {retries: retries_output_action, delay: retries_delay_output_action})
437-
const previous = await svg.hash(data)
438-
info("Previous hash", previous)
439-
const current = await svg.hash(rendered)
440-
info("Current hash", current)
441-
const changed = (previous !== current)
442-
info("Content changed", changed)
443-
if (!changed)
444-
committer.commit = false
430+
if (_output === "svg") {
431+
info("Output condition", _output_condition)
432+
if ((_output_condition === "data-changed") && ((committer.commit) || (committer.pr))) {
433+
const {svg} = await import("../metrics/utils.mjs")
434+
let data = ""
435+
await retry(async () => {
436+
try {
437+
data = `${Buffer.from((await committer.rest.repos.getContent({...github.context.repo, ref: `heads/${committer.head}`, path: filename})).data.content, "base64")}`
438+
}
439+
catch (error) {
440+
if (error.response.status !== 404)
441+
throw error
442+
}
443+
}, {retries: retries_output_action, delay: retries_delay_output_action})
444+
const previous = await svg.hash(data)
445+
info("Previous hash", previous)
446+
const current = await svg.hash(rendered)
447+
info("Current hash", current)
448+
const changed = (previous !== current)
449+
info("Content changed", changed)
450+
if (!changed)
451+
committer.commit = false
452+
}
445453
}
454+
else
455+
info("Output condition", `Not applicable for ${_output}`)
446456

447457
//Save output to renders output folder
448458
if (dryrun)
449459
info("Actions to perform", "(none)")
450460
else {
451461
await fs.mkdir(paths.dirname(paths.join("/renders", filename)), {recursive: true})
452-
await fs.writeFile(paths.join("/renders", filename), Buffer.from(typeof rendered === "object" ? JSON.stringify(rendered) : `${rendered}`))
462+
await fs.writeFile(paths.join("/renders", filename), buffer)
453463
info(`Save to /metrics_renders/${filename}`, "ok")
454464
info("Output action", _action)
455465
}
@@ -517,7 +527,7 @@ function quit(reason) {
517527
//Upload to gist (this is done as user since committer_token may not have gist rights)
518528
if (committer.gist) {
519529
await retry(async () => {
520-
await rest.gists.update({gist_id: committer.gist, files: {[filename]: {content: rendered}}})
530+
await rest.gists.update({gist_id: committer.gist, files: {[filename]: {content: buffer.toString()}}})
521531
info(`Upload to gist ${committer.gist}`, "ok")
522532
committer.commit = false
523533
}, {retries: retries_output_action, delay: retries_delay_output_action})
@@ -530,7 +540,7 @@ function quit(reason) {
530540
...github.context.repo,
531541
path: filename,
532542
message: committer.message,
533-
content: Buffer.from(typeof rendered === "object" ? JSON.stringify(rendered) : `${rendered}`).toString("base64"),
543+
content: buffer.toString("base64"),
534544
branch: committer.pr ? committer.head : committer.branch,
535545
...(committer.sha ? {sha: committer.sha} : {}),
536546
})

source/plugins/core/metadata.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ inputs:
106106
- `gist`: push output to `committer_gist`
107107
108108
> 💡 When using `pull-request`, you will need to set the last job with a `pull-request-*` action instead, else it won't be merged
109+
110+
> ⚠️ 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`
109111
type: string
110112
default: commit
111113
values:

0 commit comments

Comments
 (0)