summaryrefslogtreecommitdiffstats
path: root/bin/reproducible_html_notes.sh
blob: ee7c16e672fba0ca64b9b0878a35647d5d5d4d15 (plain)
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
#!/bin/bash

# Copyright 2014 Holger Levsen <holger@layer-acht.org>
# released under the GPLv=2

. /srv/jenkins/bin/common-functions.sh
common_init "$@"

# common code defining db access
. /srv/jenkins/bin/reproducible_common.sh

set +x
init_html

#
# this files are from the git repo cloned by the job-cfg
# on changes, this job is triggered
#
PACKAGES_YML=$PWD/packages.yml
ISSUES_YML=$PWD/issues.yml

#
# declare some variables
#
declare -A NOTES_VERSION
declare -A NOTES_ISSUES
declare -A NOTES_BUGS
declare -A NOTES_COMMENTS
declare -A ISSUES_DESCRIPTION
declare -A ISSUES_URL

#
# declare some functions only used for dealing with notes
#
show_multi_values() {
	TMPFILE=$(mktemp)
	echo "$@" > $TMPFILE
	while IFS= read -r p ; do
		if [ "$p" = "-" ] || [ "$p" = "" ] ; then
			continue
		elif [ "${p:0:2}" = "- " ] ; then
			p="${p:2}"
		fi
		echo "    $PROPERTY = $p"
	done < $TMPFILE
	unset IFS
	rm $TMPFILE
}

tag_property_loop() {
	BEFORE=$1
	shift
	AFTER=$1
	shift
	TMPFILE=$(mktemp)
	echo "$@" > $TMPFILE
	while IFS= read -r p ; do
		if [ "$p" = "-" ] || [ "$p" = "" ] ; then
			continue
		elif [ "${p:0:2}" = "- " ] ; then
			p="${p:2}"
		fi
		write_page "$BEFORE"
		if $BUG ; then
			# turn bugs into links
			p="<a href=\"https://bugs.debian.org/$p\">#$p</a>"
		else
			# turn URLs into links
			p="$(echo $p |sed  -e 's|http[s:]*//[^ ]*|<a href=\"\0\">\0</a>|g')"
		fi
		write_page "$p"
		write_page "$AFTER"
	done < $TMPFILE
	unset IFS
	rm $TMPFILE
}

issues_loop() {
	TTMPFILE=$(mktemp)
	echo "$@" > $TTMPFILE
	while IFS= read -r p ; do
		if [ "${p:0:2}" = "- " ] ; then
			p="${p:2}"
		fi
		write_page "<table class=\"body\"><tr><td>Identifier:</td><td><a href=\"$JENKINS_URL/userContent/issues/${p}_issue.html\" target=\"_parent\">$p</a></tr>"
		if [ "${ISSUES_URL[$p]}" != "" ] ; then
			write_page "<tr><td>URL</td><td><a href=\"${ISSUES_URL[$p]}\" target=\"_blank\">${ISSUES_URL[$p]}</a></td></tr>"
		fi
		if [ "${ISSUES_DESCRIPTION[$p]}" != "" ] ; then
			write_page "<tr><td>Description</td><td>"
			tag_property_loop "" "<br />" "${ISSUES_DESCRIPTION[$p]}"
			write_page "</td></tr>"
		fi
		write_page "</table>"
	done < $TTMPFILE
	unset IFS
	rm $TTMPFILE
}

write_meta_note() {
	write_page "<p>Notes are stored in <a href=\"https://anonscm.debian.org/cgit/reproducible/notes.git\">notes.git</a>.</p>"
}

create_pkg_note() {
	BUG=false
	rm -f $PAGE
	# write_page_header() is not used as it contains the <h2> tag...
	write_page "<!DOCTYPE html><html><head>"
	write_page "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />"
	write_page "<link href=\"$JENKINS_URL/userContent/static/style.css\" type=\"text/css\" rel=\"stylesheet\" />"
	write_page "<title>Notes for $1</title></head>"
	write_page "<body></header>"
	write_page "<table class=\"body\">"

	write_page "<tr><td>Version annotated:</td><td colspan=\"2\">${NOTES_VERSION[$1]}</td></tr>"

	if [ "${NOTES_ISSUES[$1]}" != "" ] ; then
		write_page "<tr><td colspan=\"2\">Identified issues:</td><td>"
		issues_loop "${NOTES_ISSUES[$1]}"
		write_page "</td></tr>"
	fi

	BUG=true
	if [ "${NOTES_BUGS[$1]}" != "" ] ; then
		write_page "<tr><td>Bugs noted:</td>"
		write_page "<td colspan=\"2\">"
		tag_property_loop "" "<br />" "${NOTES_BUGS[$1]}"
		write_page "</tr>"
	fi
	BUG=false

	if [ "${NOTES_COMMENTS[$1]}" != "" ] ; then
		write_page "<tr><td>Comments:</td>"
		write_page "<td colspan=\"2\">"
		tag_property_loop "" "<br />" "${NOTES_COMMENTS[$1]}"
		write_page "</tr>"
	fi
	write_page "<tr><td colspan=\"3\">&nbsp;</td></tr>"
	write_page "<tr><td colspan=\"3\" style=\"text-align:right; font-size:0.9em;\">"
	write_meta_note
	write_page "</td></tr></table>"
	write_page_footer
}

create_issue() {
	BUG=false
	write_page_header "" "Notes about issue '$1'"
	write_page "<table class=\"body\">"

	write_page "<tr><td>Identifier:</td><td colspan=\"2\">$1</td></tr>"

	if [ "${ISSUES_URL[$1]}" != "" ] ; then
		write_page "<tr><td>URL:</td><td colspan=\"2\"><a href=\"${ISSUES_URL[$1]}\">${ISSUES_URL[$1]}</a></td></tr>"
	fi
	if [ "${ISSUES_DESCRIPTION[$1]}" != "" ] ; then
		write_page "<tr><td>Description:</td>"
		write_page "<td colspan=\"2\">"
		tag_property_loop "" "<br />" "${ISSUES_DESCRIPTION[$1]}"
		write_page "</td></tr>"
	fi

	write_page "<tr><td colspan=\"2\">Packages known to be affected by this issue:</td><td>"
	for PKG in $PACKAGES_WITH_NOTES ; do
		if [ "${NOTES_ISSUES[$PKG]}" != "" ] ; then
			TTMPFILE=$(mktemp)
			echo "${NOTES_ISSUES[$PKG]}" > $TTMPFILE
			while IFS= read -r p ; do
				if [ "${p:0:2}" = "- " ] ; then
					p="${p:2}"
				fi
			if [ "$p" = "$1" ] ; then
				write_page " ${LINKTARGET[$PKG]} "
			fi
			done < $TTMPFILE
			unset IFS
			rm $TTMPFILE
		fi
	done
	write_page "</td></tr>"
	write_page "<tr><td colspan=\"3\">&nbsp;</td></tr>"
	write_page "<tr><td colspan=\"3\" style=\"text-align:right; font-size:0.9em;\">"
	write_meta_note
	write_page "</td></tr></table>"
	write_page_meta_sign
	write_page_footer
}

write_issues() {
	touch $ISSUES_PATH/stamp
	for ISSUE in ${ISSUES} ; do
		PAGE=$ISSUES_PATH/${ISSUE}_issue.html
		create_issue $ISSUE
	done
	cd $ISSUES_PATH
	for FILE in *.html ; do
		# if issue is older than stamp file...
		if [ $FILE -ot stamp ] ; then
			rm $FILE
		fi
	done
	rm stamp
	cd - > /dev/null
}

parse_issues() {
	ISSUES=$(cat ${ISSUES_YML} | /srv/jenkins/bin/shyaml keys)
	for ISSUE in ${ISSUES} ; do
		echo " Issue = ${ISSUE}"
		for PROPERTY in url description ; do
			VALUE="$(cat ${ISSUES_YML} | /srv/jenkins/bin/shyaml get-value ${ISSUE}.${PROPERTY} )"
			if [ "$VALUE" != "" ] ; then
				case $PROPERTY in
					url)		ISSUES_URL[${ISSUE}]=$VALUE
							echo "    $PROPERTY = $VALUE"
							;;
					description)	ISSUES_DESCRIPTION[${ISSUE}]=$VALUE
							show_multi_values "$VALUE"
							;;
				esac
			fi
		done
	done
}

write_notes() {
	touch $NOTES_PATH/stamp
	for PKG in $PACKAGES_WITH_NOTES ; do
		PAGE=$NOTES_PATH/${PKG}_note.html
		create_pkg_note $PKG
	done
	cd $NOTES_PATH
	for FILE in *.html ; do
		PKG_FILE=/var/lib/jenkins/userContent/rb-pkg/${FILE:0:-10}.html
		# if note was removed...
		if [ $FILE -ot stamp ] ; then
			# cleanup old notes
			rm $FILE
			# force re-creation of package file if there was a note
			rm -f ${PKG_FILE}
		else
			# ... else re-recreate ${PKG_FILE} if it does not contain a link to the note
			grep _note.html ${PKG_FILE} > /dev/null 2>&1 || rm -f ${PKG_FILE}
		fi
	done
	rm stamp
	cd - > /dev/null
}

parse_notes() {
	PACKAGES_WITH_NOTES=$(cat ${PACKAGES_YML} | /srv/jenkins/bin/shyaml keys)
	for PKG in $PACKAGES_WITH_NOTES ; do
		echo " Package = ${PKG}"
		for PROPERTY in version issues bugs comments ; do
			VALUE="$(cat ${PACKAGES_YML} | /srv/jenkins/bin/shyaml get-value ${PKG}.${PROPERTY} )"
			if [ "$VALUE" != "" ] ; then
				case $PROPERTY in
					version)	NOTES_VERSION[${PKG}]=$VALUE
							echo "    $PROPERTY = $VALUE"
							;;
					issues)		NOTES_ISSUES[${PKG}]=$VALUE
							show_multi_values "$VALUE"
							;;
					bugs)		NOTES_BUGS[${PKG}]=$VALUE
							show_multi_values "$VALUE"
							;;
					comments)	NOTES_COMMENTS[${PKG}]=$VALUE
							show_multi_values "$VALUE"
							;;
				esac
			fi
		done
	done
}

validate_yaml() {
	VALID_YAML=true
	set +e
	cat $1 | /srv/jenkins/bin/shyaml keys > /dev/null 2>&1 || VALID_YAML=false
	cat $1 | /srv/jenkins/bin/shyaml get-values > /dev/null 2>&1 || VALID_YAML=false
	set -e
	echo "$1 is valid yaml: $VALID_YAML"
}

#
# end note parsing functions...
#

#
# actually validate & parse the notes and then write pages for all notes and issues
#
validate_yaml ${ISSUES_YML}
validate_yaml ${PACKAGES_YML}
if $VALID_YAML ; then
	echo "$(date) - processing notes and issues"
	parse_issues
	parse_notes
	echo "$(date) - processing packages with notes"
	force_package_targets ${PACKAGES_WITH_NOTES}
	write_issues
	write_notes
	process_packages ${PACKAGES_WITH_NOTES}
else
	echo "Warning: ${ISSUES_YML} or ${PACKAGES_YML} contains invalid yaml, please fix."
fi

#
# write packages with notes page
#
VIEW=notes
PAGE=index_${VIEW}.html
echo "$(date) - starting to write $PAGE page."
write_page_header $VIEW "Overview of ${SPOKENTARGET[$VIEW]}"
if $VALID_YAML ; then
	write_page "<p>Packages which have notes: <code>"
	PACKAGES_WITH_NOTES=$(echo $PACKAGES_WITH_NOTES | sed -s "s# #\n#g" | sort | xargs echo)
	link_packages $PACKAGES_WITH_NOTES
	write_page "</code></p>"
else
	write_page "<p style=\"font-size:1.5em; color: red;\">Broken .yaml files in notes.git could not be parsed, please investigate and fix!</p>"
fi
write_meta_note
write_page_meta_sign
write_page_footer
publish_page

#
# write page with all issues
#
VIEW=issues
PAGE=index_${VIEW}.html
echo "$(date) - starting to write $PAGE page."
write_page_header $VIEW "Overview of ${SPOKENTARGET[$VIEW]}"
if $VALID_YAML ; then
	write_page "<table class=\"body\">"
	ISSUES=$(echo ${ISSUES} | sed -s "s# #\n#g" | sort | xargs echo)
	for ISSUE in ${ISSUES} ; do
		write_page "<tr><td><a href=\"$JENKINS_URL/userContent/issues/${ISSUE}_issue.html\">${ISSUE}</a></td></tr>"
	done
	write_page "</table>"
else
	write_page "<p style=\"font-size:1.5em; color: red;\">Broken .yaml files in notes.git could not be parsed, please investigate and fix!</p>"
fi
write_meta_note
write_page_footer
publish_page