-
Notifications
You must be signed in to change notification settings - Fork 0
/
ww.sh
executable file
·91 lines (81 loc) · 3.79 KB
/
ww.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env bash
# SPDX-FileCopyrightText: 2023-2024 Paul Colby <[email protected]>
# SPDX-License-Identifier: GPL-3.0-or-later
set -o errexit -o noclobber -o nounset -o pipefail
shopt -s inherit_errexit
: "${AXIS_FORMAT:=%X}" # Mermaid's default is %Y-%m-%d, but %X makes more sense (as a default) for Workflow runs.
: "${DISPLAY_MODE:=compact}"
: "${MIN_STEP_DURATION:=1}"
function showUsage {
cat <<--
Usage: ${BASH_SOURCE[0]} <run_url> > mermaid.txt
or: ${BASH_SOURCE[0]} <owner> <repo> <run_id> [<attempt_number>] > mermaid.txt
-
}
# Parse the CLI's positional arguments.
if [[ $# -eq 1 ]]; then
read -r owner repo runId attemptNumber < <(
sed -Ene 's|.*/([^/]*)/([^/]*)/actions/runs/([0-9]+)(/attempts/([0-9]+))?$|\1 \2 \3 \5|p' <<< "$1" || :
) || { echo "Invalid workflow run URL: $1" >&2; showUsage; exit 2; }
[[ -n "${attemptNumber}" ]] || unset attemptNumber
elif [[ $# -ge 3 && $# -le 4 ]]; then
owner=$1; repo=$2; runId=$3
[[ $# -lt 4 ]] || attemptNumber=$4
else
showUsage; exit 1
fi
readonly API_PATH="/repos/${owner}/${repo}/actions/runs/${runId}${attemptNumber:+/attempts/${attemptNumber}}"
# Fetch the workflow run data.
[[ -v TEST_RUN_FILE ]] && workflowRun=$(cat "${TEST_RUN_FILE}") || workflowRun=$(gh api "${API_PATH}")
#jq . <<< "$workflowRun" >| "./test/data/$owner-$repo-$runId${attemptNumber:+-${attemptNumber}}.json"
# Generate Mermaid Gantt chart header.
jq -er \
--arg axisFormat "${AXIS_FORMAT}" \
--arg displayMode "${DISPLAY_MODE}" \
--arg unsafeChars "${PRE_MERMAID_10_8:+;#}" \
"$(cat <<-"-" || :
def safeTitle(s): if ($unsafeChars|length) > 0 then s|gsub("["+$unsafeChars+"]";"") else s end;
"---\ndisplayMode: " + $displayMode + "\n---\ngantt\n" +
" title " + safeTitle(.name) + " (run " + (.id|tostring) + ", attempt " + (.run_attempt|tostring) + ")\n" +
" dateFormat YYYY-MM-DDTHH:MM:SS.SSSZ\n axisFormat " + $axisFormat + "\n %% " + .html_url
-
)" <<< "${workflowRun}"
# Fetch the worflow run jobs data.
[[ -v TEST_JOBS_FILE ]] && workflowRunJobs=$(cat "${TEST_JOBS_FILE}") || workflowRunJobs=$(gh api "${API_PATH}/jobs" --paginate)
#jq . <<< "$workflowRunJobs" >| "./test/data/$owner-$repo-$runId${attemptNumber:+-${attemptNumber}}-jobs.json"
# Add some summary metadata.
jq -er "$(cat <<-"-" || :
def roundto(n): (n|exp10)*.|round/(n|exp10);
def duration:
if .>86400 then ./86400|roundto(1)|tostring+" days"
elif .>36000 then ./3600|round|tostring+" hrs"
elif .>7200 then ./3600|roundto(1)|tostring+" hrs"
elif .>600 then ./60|round|tostring+" mins"
elif .>120 then ./60|roundto(1)|tostring+" mins"
else .|roundto(1)|tostring+" secs"
end;
def format: .|duration + if .>120 then " ("+(.|tostring)+" secs)" else "" end;
def isodiff(d1;d2): (d2|fromdate)-(d1|fromdate);
{
elapsed: isodiff([.jobs[].started_at]|min;[.jobs[].started_at]|max),
total: [.jobs[]|isodiff(.started_at;.completed_at)]|add
}
|" %% duration: "+(.elapsed|format)+" elapsed, "+(.total|format)+" total."
-
)" <<< "${workflowRunJobs}"
# Generate Mermaid Gantt chart sections.
jq -er --argjson minStepDuration "${MIN_STEP_DURATION}" --arg unsafeChars "${PRE_MERMAID_10_8:+;#}" "$(cat <<-"-" || :
def isodate(d): d|strptime("%FT%T.000%z")|mktime;
def isodiff(d1;d2): isodate(d2)-isodate(d1);
def safeSectionName(s): if ($unsafeChars|length) > 0 then s|gsub("[:"+$unsafeChars+"]";"") else s end;
def safeTaskName(s): s|gsub("[:"+$unsafeChars+"]";"");
.jobs[]|"\n section " + safeSectionName(.name) + "\n" + ([
.steps[]|select(.completed_at)|
(.+{duration:isodiff(.started_at;.completed_at)})|
select(.duration>=$minStepDuration or .conclusion=="failed")|
" " + safeTaskName(.name) + " :" +
if .conclusion != "success" then "crit, " else "" end +
.started_at + ", " + (.duration|tostring) + "s"
]|join("\n"))
-
)" <<< "${workflowRunJobs}"