Skip to content

Commit

Permalink
Add json output support
Browse files Browse the repository at this point in the history
  • Loading branch information
lowlighter committed Mar 11, 2021
1 parent 86a6fa9 commit a2215a0
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 6 deletions.
2 changes: 1 addition & 1 deletion source/app/action/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@
info.break()
info.group({metadata, name:"core", inputs:config})
info("Plugin errors", die ? "(exit with error)" : "(displayed in generated image)")
const convert = ["jpeg", "png"].includes(config["config.output"]) ? config["config.output"] : null
const convert = ["jpeg", "png", "json"].includes(config["config.output"]) ? config["config.output"] : null
Object.assign(q, config)

//Base content
Expand Down
6 changes: 6 additions & 0 deletions source/app/metrics/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@
console.warn(util.inspect(errors, {depth:Infinity, maxStringLength:256}))
}

//JSON output
if (convert === "json") {
console.debug(`metrics/compute/${login} > json output`)
return {rendered:data, mime:"application/json"}
}

//Rendering
console.debug(`metrics/compute/${login} > render`)
let rendered = await ejs.render(image, {...data, s:imports.s, f:imports.format, style, fonts}, {views, async:true})
Expand Down
2 changes: 1 addition & 1 deletion source/app/web/instance.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@
graphql, rest, plugins, conf,
die:q["plugins.errors.fatal"] ?? false,
verify:q.verify ?? false,
convert:["jpeg", "png"].includes(q["config.output"]) ? q["config.output"] : null,
convert:["jpeg", "png", "json"].includes(q["config.output"]) ? q["config.output"] : null,
}, {Plugins, Templates})
//Cache
if ((!debug)&&(cached))
Expand Down
11 changes: 7 additions & 4 deletions source/plugins/core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Specify a single value to apply it to both height and with, and two values to us
It is possible to configure output behaviour using `output_action` option, which can be set to:
- `none`, where output will be generated in `/rendered/${filename}` without being pushed
- You can then manually post-process it
- `commit` (default), where output will directly be committed and pushed to `committer_branch`
- `commit` (default), where output will directly be committed and pushed to `committer_branch`
- `pull-request`, where output will be committed to a new branch with current run id waiting for to be merged in `committer_branch`
- By appending either `-merge`, `-squash` or `-rebase`, pull request will be automatically merged with given method
- This method is useful to combine all editions of a single run with multiples metrics steps into a single commit on targetted branch
Expand All @@ -116,7 +116,7 @@ It is possible to configure output behaviour using `output_action` option, which
#### ℹ️ Examples workflows

```yaml
# The following will:
# The following will:
# - open a pull request with "my-metrics-0.svg" as first commit
# - append "my-metrics-1.svg" as second commit
# - merge pull request (as second step is set to "pull-request-merge")
Expand Down Expand Up @@ -149,12 +149,15 @@ It is possible to mitigate this issue using `retries` and `retries_delay` option
retries_delay: 300
```

### 💱 Convert output to PNG/JPEG
### 💱 Convert output to PNG/JPEG or JSON

It is possible to convert output from SVG to PNG or JPEG images by using `config_output` option.
It is possible to convert output from SVG to PNG or JPEG images and even to JSON by using `config_output` option.

Note that `png` does not support animations while `jpeg` does not support both animations and transparency.

Using `json` output can be useful if you want to retrieve all data computed by metrics without rendering it.
It could then be processed for other usages.

#### ℹ️ Examples workflows

```yaml
Expand Down
1 change: 1 addition & 0 deletions source/plugins/core/metadata.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ inputs:
- svg
- png # Does not support animations
- jpeg # Does not support animations and transparency
- json # Outputs a JSON file instead of an image

# Number of retries in case rendering fail
retries:
Expand Down
6 changes: 6 additions & 0 deletions source/plugins/core/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
token: MOCKED_TOKEN
config_output: png

- name: Image output (json)
uses: lowlighter/metrics@latest
with:
token: MOCKED_TOKEN
config_output: json

- name: Use twemoji
uses: lowlighter/metrics@latest
with:
Expand Down

0 comments on commit a2215a0

Please sign in to comment.