forked from arjun024/actions-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.sh
executable file
·92 lines (76 loc) · 2.19 KB
/
generate.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
92
#!/usr/bin/env bash
set -euo pipefail
inputs=()
output=
output_file=out.md
usage() {
cat <<EOF
usage: ${0##*/} [-h] -o OUTFILE [-i FILE]...
-h display this help
-i input file path
-o output markdown file path
EOF
exit 1
}
urlencode() {
for (( i = 0; i < "${#1}"; i++ )); do
local c="${1:i:1}"
case $c in
[a-zA-Z0-9.~_-]) printf '%s' "$c" ;;
*) printf '%%%02X' "'$c" ;;
esac
done
}
isurl() { [[ "$1" =~ https?://* ]]; }
writeout() { output="$output""$1"; }
parse_repo() {
project="$1"
repo="https://github.com/${project}"
writeout "| $repo |"
while read -r name; do
encoded_name="$(urlencode "${name}")"
writeout " ["
writeout "![${name}](${repo}/workflows/${encoded_name}/badge.svg)"
writeout "]"
writeout "(${repo}/actions?query=workflow:\"${encoded_name}\")"
done < <(curl -sL "https://api.github.com/repos/${1}/actions/workflows" | jq -r '.workflows[].name')
writeout " [![GitHub PR](https://img.shields.io/github/issues/${1}.svg)](https://GitHub.com/${1}/issues)"
writeout " [![GitHub PR](https://img.shields.io/github/issues-pr/${1}.svg)](https://GitHub.com/${1}/pulls)"
writeout " |\n"
echo " Generated markdown for $1"
}
[ "$#" -lt 4 ] && usage
command -v jq > /dev/null || { echo "Need jq"; exit 1; }
OPTIND=1
while getopts ":ho:i:" opt; do
case $opt in
i)
inputs+=("$OPTARG")
;;
o)
output_file="$OPTARG"
;;
*)
usage
;;
esac
done
[[ "$output_file" != *.md ]] && output_file="$output_file".md
tmpd="$(mktemp -d -t dashboardXXXX)"
writeout "| Repo | Actions |\n"
writeout "| --- | --- |\n"
for i in "${inputs[@]}"; do
echo Generating markdown for "${i##*/}"...
count=0
while read -r line; do
[[ "$line" = \#* ]] && continue
[ -z "$line" ] && continue
parse_repo "$line"
count=$((count+1))
done < <(if isurl "$i"; then curl -sL "$i"; else cat "$i"; fi)
[ $count -eq 0 ] && { echo "Failed to read $i"; exit 1; }
writeout "---\n\n"
done
echo -e "$output" > "$output_file"
echo Wrote to "$output_file"
rm -rf "$tmpd"