Skip to content

Commit

Permalink
gh-actions/jq::gfm: Add tables/utils fun (#1194)
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Northey <ryan@synca.io>
  • Loading branch information
phlax authored Dec 4, 2023
1 parent 54eb712 commit 78f91f9
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 45 deletions.
2 changes: 1 addition & 1 deletion gh-actions/jq/dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion gh-actions/jq/jq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const run = async (): Promise<void> => {
let filterFunArg = ''

const modPath = path.join(__dirname, '../modules')
mangledFilter = `import "bash" as bash; import "gfm" as gfm; import "str" as str; import "validate" as validate; ${mangledFilter}`
mangledFilter = `import "bash" as bash; import "gfm" as gfm; import "str" as str; import "utils" as utils; import "validate" as validate; ${mangledFilter}`
filterFunArg = `-L ${modPath}`

if (filterFun) {
Expand Down
150 changes: 107 additions & 43 deletions gh-actions/jq/modules/gfm.jq
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
import "str" as str;
import "utils" as utils;

def action(name):
if name == "failure" then
":x: \(.)"
else
":heavy_check_mark: \(.)"
end
;

def blockquote:
split("\n")
| map("> " + .)
| join("\n")
;

def collapse:
if (.open // false) then
Expand All @@ -20,12 +35,34 @@ def collapse:
"
;

def action(name):
if name == "failure" then
":x: \(.)"
else
":heavy_check_mark: \(.)"
end
def event_title:
.
| .link as $link
| .sha as $sha
| .repo as $repo
| .title as $title
| .["target-branch"] as $targetBranch
| .pr as $pr
| {}
| if $title != "" then
.link = $title
elif $link != "" then
if $pr != "" then
.link = "pr/\($pr)"
else
.link = "postsubmit"
end
| .link |= "\(.)/\($targetBranch)@\($sha[:7])"
else
if $pr != "" then
.link = "pr/[\($pr)](https://github.com/\($repo)/pull/\($pr))"
else
.link = "postsubmit"
end
| "[\($sha[:7])](https://github.com/\($repo)/commit/\($sha))" as $shaLink
| .link |= "\(.)/\($targetBranch)@\($shaLink)"
end
| .link
;

def fence(name):
Expand All @@ -36,70 +73,97 @@ def fence(name):
"
;

def blockquote:
split("\n")
| map("> " + .)
| join("\n")
;

def table_headers:
. as $headers
| ("| " + (. | join(" | ")) + " |")
+ "\n"
+ ("| " + "--- | " * ($headers | length))
;

def table_cell_sanitize:
.cell as $cell
| .row as $row
| $cell
| if type == "null" then
""
elif (type == "boolean" or type == "number") then
"`\(.)`"
elif (type == "string" and test("\n")) then
(split("\n")[0] + "..." )
else . end
| tojson
| gsub("\\\\\\("; "\\\\\\(")
| gsub("\\$"; "<span>$</span>")
| .[1:-1]
;

def table(filter; ifempty; mutate; sanitize):
. as $table
| ($table.headers | table_headers) as $headers
| ($table.headers // [] | table_headers) as $headers
| $table.data
| filter
| (filter // .)
| if (. | length) == 0 then
ifempty
ifempty // .
else
.
end
| to_entries
| map([.key, .value] as $row
| map(. as $cell
| {table: $table, row: $row, cell: $cell}
| mutate as $cell
| (mutate // .cell) as $cell
| {table: $table, row: $row, cell: $cell}
| sanitize))
| (sanitize // table_cell_sanitize)))
| map(join("|"))
| join("\n") as $rows
| "\($headers)
\($rows)
"
;

def event_title:
.
| .link as $link
| .sha as $sha
| .repo as $repo
| .title as $title
| .["target-branch"] as $targetBranch
| .pr as $pr
| {}
| if $title != "" then
.link = $title
elif $link != "" then
if $pr != "" then
.link = "pr/\($pr)"
def tables(mutate):
to_entries
| map(
.key as $k
| .value as $v
| {data: $v.data,
collapse: $v.collapse,
title: $v.title,
"collapse-open": $v["collapse-open"],
"table-title": $v["table-title"],
headers: ($v.headers // ["Key", "Value"])}
| table(null; null; mutate // .cell; null) as $t
| ($v.heading // "") as $heading
| $v["table-title"] as $summary
| $v.title as $title
| if $v.collapse then
$title as $_title
| $summary as $title
| "
#### \($title)
\($t)
"
| {title: $_title, content: ., open: $v["collapse-open"] }
| collapse
else
.link = "postsubmit"
"
#### \($title)
\($t)
"
end
| .link |= "\(.)/\($targetBranch)@\($sha[:7])"
else
if $pr != "" then
.link = "pr/[\($pr)](https://github.com/\($repo)/pull/\($pr))"
else
.link = "postsubmit"
end
| "[\($sha[:7])](https://github.com/\($repo)/commit/\($sha))" as $shaLink
| .link |= "\(.)/\($targetBranch)@\($shaLink)"
end
| .link
| . as $content
| if $heading != "" then
"
## \($heading)
\($content)
"
else . end)
| join("\n")
;

def tables:
tables(null)
;

0 comments on commit 78f91f9

Please sign in to comment.