aboutsummaryrefslogtreecommitdiff
path: root/src/infrastructure/scripts/report.sh
blob: 0b63ffcc28eea08fa61792a50febb5f5bb3a5828 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  report -o DIRECTORY [-K] [-S STEP]
		  report -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -o DIRECTORY    the directory where to place the generated files
		  -K              keep intermediary files
		  -S STEP         which substep of the report to perform (default: top)
		  -h, --help      show this message


		Gather data from Git Notes, and generate an HTML report on CI runs.

		Two refs with notes are expected:
		1. refs/notes/ci-data: contains metadata abount the CI runs,
		   with timestamps, filenames and exit status;
		2. refs/notes/ci-logs: contains the content of the log.

		When reconstructing the CI run, the $FILENAME present in
		the refs/notes/ci-data ref names the file, and its content comes
		from refs/notes/ci-logs.

		On a CI run that generated the numbers from 1 to 10, for a file named
		'my-ci-run-2020-01-01-deadbeef.log' that exited successfully, the
		expected output on the target directory "public" is:

		  $ tree public/
		  public/
		    index.html
		    data/
		      my-ci-run-2020-01-01-deadbeef.log
		      ...
		    logs/
		      my-ci-run-2020-01-01-deadbeef.log
		      ...

		  $ cat public/data/my-ci-run-2020-01-01-deadbeef.log
		  0 deadbeef my-ci-run-2020-01-01-deadbeef.log

		  $ cat public/logs/my-ci-run-2020-01-01-deadbeef.log
		  1
		  2
		  3
		  4
		  5
		  6
		  7
		  8
		  9
		  10

		The generated 'index.html' is a webpage with the list of all known
		CI runs, their status, a link to the commit and a link to the
		log file.

		To enable fetching these refs by default, do so in the git config:

		  $ git config --add remote.origin.fetch '+refs/notes/*:refs/notes/*'


		Examples:

		  Generate the report on the 'www' directory:

		    $ report -o www
	EOF
}


for flag in "$@"; do
	case "$flag" in
		--)
			break
			;;
		--help)
			usage
			help
			exit
			;;
		*)
			;;
	esac
done

KEEP_FILES=false
STEP=top
while getopts 'o:KS:h' flag; do
	case "$flag" in
		o)
			OUTDIR="$OPTARG"
			;;
		K)
			KEEP_FILES=true
			;;
		S)
			STEP="$OPTARG"
			;;
		h)
			usage
			help
			exit
			;;
		*)
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

if [ -z "${OUTDIR:-}" ]; then
	printf 'Missing -o OUTDIR.\n\n' >&2
	usage >&2
	exit 2
fi


if [ -r src/infrastructure/config/conf.env ]; then
	CONF=src/infrastructure/config/conf.env
else
	CONF=/etc/conf.env
fi

# shellcheck source=/dev/null
. "$CONF"


escape_html() {
	sed \
		-e 's|&|\&amp;|g'  \
		-e 's|<|\&lt;|g'   \
		-e 's|>|\&gt;|g'   \
		-e 's|"|\&quot;|g' \
		-e "s|'|\&#39;|g"
}



emit_stage0_make() {
	git notes list | awk -v OUT="$OUTDIR" '{
		printf "all: %s/stage0/%s\n\n", OUT, $2
		printf "%s/stage0/%s:\n", OUT, $2
		printf "\tgit notes --ref=refs/notes/ci-data show %s > $@\n\n", $2
	}'
	printf 'all:\n'
	printf '\tprintf "%%s\\n" "$?" | tr " " "\\n"\n'
}

emit_stage1_make() {
	awk -F/ -vCMD="'$0'" -vOUT="$OUTDIR" '{
		printf "all: %s/stage1/%s.mk\n\n", OUT, $(NF)
		printf "%s/stage1/%s.mk:\n", OUT, $(NF)
		printf "\t%s -o \"%s\" -S stage1-exec < %s/stage0/%s > $@\n", CMD, OUT, OUT, $(NF)
	}'
	printf 'all:\n'
	printf '\tcat -- "%s"/stage1/*.mk\n' "$OUTDIR"
}

stage1_exec() {
	awk -vOUT="$OUTDIR" -vCMD="'$0'" '
	{ d[$1] = $2 }
	END {
		escaped = d["filename"]
		gsub(/:/, "\\:", escaped)

		gsub(/"/, "", d["duration"])

		printf "all: %s/data/%s\n\n", OUT, escaped
		printf "%s/data/%s:\n", OUT, escaped
		printf "\tln -- %s/stage0/%s $@\n\n\n", OUT, d["sha"]

		printf "all: %s/logs/%s\n\n", OUT, escaped
		printf "%s/logs/%s:\n", OUT, escaped
		printf "\tgit notes --ref=refs/notes/ci-logs show %s > $@\n\n\n", d["sha"]

		printf "all: %s/msgs/%s\n\n", OUT, escaped
		printf "%s/msgs/%s:\n", OUT, escaped
		printf "\tif ! git show %s 1>/dev/null 2>&1; then \\\n", d["sha"]
		printf "\t\tgit fetch origin %s; \\\n", d["sha"]
		printf "\tfi\n"
		printf "\tgit log -1 --format=%%B %s > $@\n\n\n", d["sha"]

		printf "all: %s/html/%s\n\n", OUT, escaped
		printf "%s/html/%s: %s/msgs/%s\n", OUT, escaped, OUT, escaped
		printf "\t%s -o \"%s\" -S html-item \"%s\" \"%s\" \"%s\" \"%s\" \"%s/msgs/%s\" \"%s\" > $@\n\n\n",
			CMD, OUT, d["status"], d["sha"], d["filename"], d["duration"], OUT, d["filename"], CGIT_URL
	}
	'
}

emit_html_item() {
	STATUS="$1"
	SHA="$2"
	FILENAME="$3"
	DURATION="$4"
	MESSAGE_F="$5"
	CGIT_URL="$6"

	PASS='&#x2705;'  # ✅
	WARN='&#x1F40C;' # 🐌
	FAIL='&#x274C;'  # ❌
	if [ "$STATUS" = 0 ]; then
		if [ "$DURATION" -le 60 ]; then
			STATUS_MARKER="$PASS"
		else
			STATUS_MARKER="$WARN"
		fi
	else
		STATUS_MARKER="$FAIL"
	fi
	cat <<-EOF
		        <li id="$FILENAME">
		          <a href="#$FILENAME"><pre>#</pre></a>
		          $STATUS_MARKER - <pre>${DURATION}s</pre>
		          <pre>(<a href="${CGIT_URL}${SHA}">commit</a>)</pre>
		          <a href="logs/$FILENAME"><pre>$FILENAME</pre></a>
		          <br />
		          <code><pre>$(escape_html < "$MESSAGE_F")</pre></code>
		        </li>
	EOF
}

emit_html_index() {
	cat <<-EOF
		<!DOCTYPE html>
		<html lang="en">
		  <head>
		    <meta charset="UTF-8" />
		    <meta name="viewport"    content="width=device-width, initial-scale=1" />
		    <meta name="description" content="CI logs for $NAME" />
		    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
		    <title>$NAME - CI logs</title>
		    <style>
		      body {
		        max-width: 800px;
		        margin: 0 auto;
		      }

		      code {
		        display: block;
		        margin: 1em 0em 3em 3em;
		        overflow: auto;
		      }

		      pre {
		        display: inline;
		      }

		      ol {
		        list-style-type: disc;
		      }

		      pre, code {
		        background-color: #ddd;
		      }

		      @media(prefers-color-scheme: dark) {
		        :root {
		          color: white;
		          background-color: black;
		        }

		        a {
		          color: hsl(211, 100%, 60%);
		        }

		        a:visited {
		          color: hsl(242, 100%, 80%);
		        }

		        pre, code {
		          background-color: #222;
		        }
		      }
		    </style>
		  </head>
		  <body>
		    <main>
		      <h1>
		        CI logs for
		        <a href="$HOMEPAGE">$NAME</a>
		      </h1>
		      <ol>
	EOF

	echo "$OUTDIR"/html/* |
		tr ' ' '\n' |
		LANG=C.UTF-8 sort -r |
		xargs cat --

	cat <<-'EOF'
		      </ol>
		    </main>
		  </body>
		</html>
	EOF
}



case "$STEP" in
	top)
		for d in stages stage0 stage1 data logs html msgs; do
			mkdir -p -- "$OUTDIR/$d"
		done

		"$0" -o "$OUTDIR" -S stage0-make |
			tee -- "$OUTDIR"/stages/stage0.mk |
			make -sf- |
			"$0" -o "$OUTDIR" -S stage1-make |
			tee -- "$OUTDIR"/stages/stage1.mk |
			make -sf- |
			tee -- "$OUTDIR"/stages/stage2.mk |
			make -sf-

		"$0" -o "$OUTDIR" -S html

		if [ "$KEEP_FILES" = false ]; then
			for d in stages stage0 stage1 html msgs; do
				rm -rf "${OUTDIR:?}/$d"
			done
		fi
		;;
	stage0-make)
		emit_stage0_make
		;;
	stage1-make)
		emit_stage1_make
		;;
	stage1-exec)
		stage1_exec
		;;
	html-item)
		emit_html_item "$@"
		;;
	html)
		emit_html_index > "$OUTDIR"/index.html
		;;
	*)
		printf 'Unsupported STEP type: "%s"\n\n' "$STEP" >&2
		usage >&2
		exit 2
		;;
esac