-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebui.nu
386 lines (342 loc) Β· 19.7 KB
/
webui.nu
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
use ./nulib/nagoya/fgq.nu
use ./nulib/nutils/html.nu
use ./nulib/webserver format_http
use ./nulib/webserver http_redirect
use ./nulib/webserver start_webserver
const JSON = "application/json;charset=UTF-8"
const HTML = "text/html;charset=UTF-8"
const HTML_HEAD = '
<!DOCTYPE HTML><html><head>
<style>
html {background-color:wheat;}
pre {background-color:lightgray;border-radius:15px;padding:15px;white-space:pre-wrap;}
span.label {padding:5px;border-radius:5px;margin-left:2px;}
table.issues td {border-top:1px solid black;padding:7px;}
div.reactions>span {background-color:gray;border-radius:2px;padding:2px;margin-left:2px;color:white;}
div.comment {border:1px solid black;border-radius:15px;padding:15px;margin:5px;}
issue_title {word-wrap:anywhere;}
</style>
<title>GitHub-repo-backuper viewer</title>
</head><body>
'
const HTML_TAIL = '</body></html>'
def main [
--port: int = 8080
--with-download-ui
--download-que-check-rate: duration = 15min
] {
let download_q = (if $with_download_ui { fgq create } else { null })
if $with_download_ui {
0 | tee {||
while ($download_q | path exists) {
let next = (fgq pop $download_q)
if $next != null {
for attempt in 1..5 {
try {
^python3 ./github-repo-backuper.py ...$next
break
}
print $'Download failed.. retrying in 1min.. [attempt ($attempt) / 5]'
sleep 1min
}
} else { sleep $download_que_check_rate }
}
} | null
}
start_webserver $port {|req|
if $req.path == "/" {return (http_redirect "/index.html")}
if ($with_download_ui) and ($req.path == '/github/_init_download') {return (generate_init_download $req $download_q)}
if $req.path == "/github" {return (generate_landingpage $with_download_ui)}
if $req.path =~ "^/github/[^/]+$" {return (generate_userpage $req)}
if $req.path =~ "^/github/[^/]+/[^/]+$" {return (generate_repopage $req)}
if $req.path =~ "^/github/[^/]+/[^/]+/issues$" {return (generate_issue_list $req)}
if $req.path =~ '^/github/[^/]+/[^/]+/issues/\d+$' {return (generate_issue_details $req)}
if $req.path =~ "^/github/[^/]+/[^/]+/releases$" {return (generate_release_list_page $req)}
# this does currently not work, since `nc` (webui.nu) restarts to slow and thus causes git to fail with a `Failed to connect to SERVER`
if $req.path =~ '^/github/[^/]+/[^/]+/clone/.*$' {return (git_clone $req)}
format_http 404 $HTML ([$HTML_HEAD '<h1>Page not found</h1><a href="/github">Go Home</a>' $HTML_TAIL] | str join '')
}
if $with_download_ui {
fgq delete $download_q
}
}
def generate_init_download [request, download_q]: nothing -> string {
let valid = (
("repo_owner" in $request.params)
and ($request.params.repo_owner =~ '^[^ \t]+$')
and ("repo_name" in $request.params)
and ($request.params.repo_name =~ '^[^ \t]+$')
)
if $valid {
fgq push $download_q ([
$request.params.repo_owner
$request.params.repo_name
(if $request.params.compress? == "true" { "--gzip" })
(if $request.params.wiki? == "true" { "--include-wiki" })
(if $request.params.releases? == "true" { "--include-releases" })
(if $request.params.detailed_prs? == "true" { "--detailed-prs" })
(if $request.params.lfs? == "true" { "--include-lfs" })
(if $request.params.prune? == "true" { "--prune" })
] | where $it != null)
}
format_http (if "repo_owner" in $request.params and not $valid { 400 } else { 200 }) $HTML ([
$HTML_HEAD
'<a href="/github">Back</a>'
(if "repo_owner" in $request.params { if $valid { '<p>Added repo to que.</p>' } else { '<p>Your last start-requst was invalid.</p>' } } else { '' })
'<h1>Initialize new Download</h1>'
'<form>'
'<label for="repo_owner">Repo Owner:</label><br><input type="text" id="repo_owner" name="repo_owner"><br>'
'<label for="repo_name">Repo Name:</label><br><input type="text" id="repo_name" name="repo_name"><br>'
'<input type="checkbox" id="compress" name="compress" value="true" checked><label for="compress">Compress</label><br>'
'<input type="checkbox" id="wiki" name="wiki" value="true" checked><label for="wiki">Include Wiki</label><br>'
'<input type="checkbox" id="releases" name="releases" value="true" checked><label for="releases">Include Releases</label><br>'
'<input type="checkbox" id="prune" name="prune" value="true"><label for="prune">Prune</label><br>'
'<input type="checkbox" id="detailed_prs" name="detailed_prs" value="true" checked><label for="detailed_prs">Detailed PRs</label><br>'
'<input type="checkbox" id="lfs" name="lfs" value="true"><label for="lfs">Include LFS</label><br>'
'<input type="submit" value="Submit">'
'</form>'
$HTML_TAIL
] | str join '')
}
def generate_landingpage [download_ui_enabled: bool]: nothing -> string {
if (not ($'github' | path exists)) {return (format_http 404 $HTML "Nothing found. no users, no repos, nothing.")}
format_http 200 $HTML ([
$HTML_HEAD
"<h1>GitHub repo backuper results</h1>"
(if $download_ui_enabled { '<a href="/github/_init_download">Initialize a new download</a>' } else { '' })
"<h2>Users and Orgs</h2><ul>"
((ls github/).name | sort | each {|i| $'<li><a href="/(html escape $i)">(html escape $i)</a></li>'} | str join '')
"</ul>"
$HTML_TAIL
] | str join "")
}
def generate_userpage [req]: nothing -> string {
let user = ($req.path | parse "/github/{user}").0.user
if $user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if (not ($'github/($user)' | path exists)) {return (format_http 404 $HTML "User not found")}
format_http 200 $HTML ([
$HTML_HEAD
$'<h1>/<a href="/github">github</a>/(html escape $user)</h1>'
'<h2>Repos</h2><ul>'
((ls $"github/($user)").name | sort | each {|i| $'<li><a href="/(html escape $i)">(html escape $i)</a></li>'} | str join '')
'</ul>'
$HTML_TAIL
] | str join '')
}
def generate_repopage [req]: nothing -> string {
let rp = ($req.path | parse "/github/{user}/{repo}").0
if $rp.user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if $rp.repo in ["..", "."] {return (format_http 300 $HTML "Invalid repo name")} # "/" is already filter by mapper
if (not ($'github/($rp.user)/($rp.repo)/git' | path exists)) {return (format_http 404 $HTML "Repo not found")}
cd $'github/($rp.user)/($rp.repo)/git'
let files = (^git ls-tree --name-only -r HEAD | lines) # " <-- quote fixes treesitter for line below (dont ask me)
let readme_name = ($files | where ($it | str starts-with 'README')).0?
format_http 200 $HTML ([
$HTML_HEAD
$'<h1>/<a href="/github">github</a>/<a href="/github/(html escape $rp.user)">(html escape $rp.user)</a>/(html escape $rp.repo)</h1>'
(if ("../issues" | path exists) {$'<a href="(html escape $req.path)/issues">Issues</a> '})
(if ("../releases" | path exists) {$'<a href="(html escape $req.path)/releases">Releases</a> '})
(if $readme_name != null {$'<h2>(html escape $readme_name)</h2><details><summary>toggle</summary><pre>(html escape (^git show -q $"HEAD:($readme_name)"))</pre></details>'} else {""})
$'<h2>Files</h2><details><summary>toggle</summary><ul>'
($files | each {|i| $'<li>(html escape $i)</li>'} | str join '') # TODO: replace with line below once file-viewer (incl images?) is implemented
#($files | each {|i| $'<li><a href="(html escape $req.path)/files/(html escape $i)">(html escape $i)</a></li>'} | str join '')
'</ul></details>'
# TODO: releae list
$HTML_TAIL
] | str join '')
}
def generate_issue_list [req]: nothing -> string {
let rp = ($req.path | parse "/github/{user}/{repo}/issues").0
if $rp.user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if $rp.repo in ["..", "."] {return (format_http 300 $HTML "Invalid repo name")} # "/" is already filter by mapper
if (not ($'github/($rp.user)/($rp.repo)/issues' | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"dir not found: github/($rp.user)/($rp.repo)/issues")}
cd $'github/($rp.user)/($rp.repo)/issues'
format_http 200 $HTML ([
$HTML_HEAD
$'<h1>/<a href="/github">github</a>/<a href="/github/(html escape $rp.user)">(html escape $rp.user)</a>/<a href="/github/(html escape $rp.user)/(html escape $rp.repo)">(html escape $rp.repo)</a>/issues</h1>'
'<table class="issues"><tr><th>Id</th><th></th><th></th><th>Name</th><th>Author</th><th></th></tr>'
((ls).name | sort --natural | each {|filename|
let data = (if ($filename | str ends-with ".json.gz") {^gunzip -kc $filename | from json} else if ($filename | str ends-with ".json") {open $filename} else {null})
let issue_id = ($filename | split row "." | get 0)
if $data == null {""} else {[
$'<tr><td><a href="(html escape $req.path)/(html escape $issue_id)">(html escape $issue_id)</a></td>'
$'<td>(if $data.is_pull_request? == true {"π₯"} else {"π"})</td>'
$'<td>(if $data.state? == "closed" {"π£"} else {"π’"})</td>'
$'<td class="issue_title">(html escape ($data.title? | default "<no title>"))'
($data.labels? | default [] | each {|label|
if ($label | describe) == "string" {
$'<span class="label" style="background-color:#00ff00">(html escape $label)</span>'
} else {
let fc = (if (isDarkColor ($label.color? | default '0a0')) {"fff"} else {"000"})
$'<span class="label" style="background-color:#(html escape ($label.color? | default '0a0')); color:#($fc)">(html escape ($label.name? | default ""))</span>'
}
} | str join '')
'</td>'
$'<td>(html escape ($data.user? | default "<no author>"))</td>'
$'<td>π¬($data.comments? | default [] | length)</td></tr>'
] | str join ''}
} | str join "")
'</table>'
$HTML_TAIL
] | str join "")
}
def generate_issue_details [req]: nothing -> string {
let rp = ($req.path | parse "/github/{user}/{repo}/issues/{issue}").0
if $rp.user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if $rp.repo in ["..", "."] {return (format_http 300 $HTML "Invalid repo name")} # "/" is already filter by mapper
if $rp.issue !~ '^\d+$' {return (format_http 300 $HTML "Invalid issue id (not a number)")}
let issue_file = (if ($'github/($rp.user)/($rp.repo)/issues/($rp.issue).json' | path exists) {$'github/($rp.user)/($rp.repo)/issues/($rp.issue).json'} else {$'github/($rp.user)/($rp.repo)/issues/($rp.issue).json.gz'})
if (not ($issue_file | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"Unknown issue")}
let data = (if ($issue_file | str ends-with ".json.gz") {^gunzip -kc $issue_file | from json} else if ($issue_file | str ends-with ".json") {open $issue_file} else {null})
format_http 200 $HTML ([
$HTML_HEAD
$'<h1>/<a href="/github">github</a>/<a href="/github/(html escape $rp.user)">(html escape $rp.user)</a>/<a href="/github/(html escape $rp.user)/(html escape $rp.repo)">(html escape $rp.repo)</a>/<a href="/github/(html escape $rp.user)/(html escape $rp.repo)/issues">issues</a>/($rp.issue)</h1>'
$'<h2>(html escape ($data.title? | default "<no name>"))</h2>'
($data.labels? | default [] | each {|label|
if ($label | describe) == "string" {
$'<span class="label" style="background-color:#00ff00">(html escape $label)</span>'
} else {
let fc = (if (isDarkColor ($label.color? | default '0a0')) {"fff"} else {"000"})
$'<span class="label" style="background-color:#(html escape ($label.color? | default '0a0')); color:#($fc)">(html escape ($label.name? | default ""))</span>'
}
} | str join '')
$'<p>Author: (format_username $data.user? $data.author_association?)</p>'
(if $data.draft? == true {"<p>This is a draft</p>"} else {""})
(if $data.locked? == true {"<p>This is locked</p>"} else {""})
(if $data.state? != null {$"<p>State: (html escape $data.state)</p>"})
(if ($data.assignees? | default []) != [] {$'<p>Assignees: (html escape ($data.assignees? | default [] | get login | str join ", "))</p>'} else {""})
# MR data
# TODO: requested reviewers
(if $data.merge_commit_sha? != null {$'<p>Merge Commit SHA: (html escape $data.merge_commit_sha)</p>'} else {''})
(if $data.head? != null {$'<p>FROM <code>(html escape ($data.head.gh_repo? | default "<unknown repo>")):(html escape ($data.head.ref? | default "<unknown ref>"))</code> TO <code>(html escape ($data.base? | default "<unknown target branch>"))</code></p>'})
(if $data.merged? == true {$'<p>Merged at (html escape ($data.merged_at? | default "<unknown>")) by (html escape ($data.merged_by? | default "<unknown>"))</p>'})
$'<pre>(html escape ($data.body? | default "<no body>"))</pre>'
$'created: (html escape ($data.created_at? | default "<unknown>")) updated: (html escape ($data.updated_at? | default "<unknown>"))'
(format_reactions $data.reactions?)
'<div class="issue-comments">'
($data.comments? | default [] | each {|comment|
[
'<div class="comment">'
$'<b>(format_username $comment.user? $comment.author_association?)</b>'
$'<pre>(html escape ($comment.body? | default "<no body>"))</pre>'
$'created: (html escape ($comment.created_at? | default "<unknown>")) updated: (html escape ($comment.updated_at? | default "<unknown>"))'
(format_reactions $comment.reactions?)
'</div>'
] | str join ''
} | str join '')
'</div>'
$HTML_TAIL
] | str join "")
}
def generate_release_list_page [req]: nothing -> string {
let rp = ($req.path | parse "/github/{user}/{repo}/releases").0
if $rp.user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if $rp.repo in ["..", "."] {return (format_http 300 $HTML "Invalid repo name")} # "/" is already filter by mapper
if (not ($"github/($rp.user)/($rp.repo)/releases" | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"Repo has no releases or does not exist")}
cd $'github/($rp.user)/($rp.repo)/releases'
format_http 200 $HTML ([
$HTML_HEAD
$'<h1>/<a href="/github">github</a>/<a href="/github/(html escape $rp.user)">(html escape $rp.user)</a>/<a href="/github/(html escape $rp.user)/(html escape $rp.repo)">(html escape $rp.repo)</a>/releases</h1>'
'<ul>'
((ls --short-names).name | each {|release_id|
let details_file = (if ($'($release_id)/release.json' | path exists) {$'($release_id)/release.json'} else {$'($release_id)/release.json.gz'})
let data = (if ($details_file | str ends-with ".json.gz") {^gunzip -kc $details_file | from json} else if ($details_file | str ends-with ".json") {open $details_file} else {null})
[
$'<li><a href="(html escape $req.path)/($release_id)">'
(if $data.is_draft {html escape '<DRAFT>'} else {''})
(if $data.is_prerelease {html escape '<PRERELEASE>'} else {''})
(html escape $data.name)
'</a></li>'
] | str join ''
} | str join '')
'</ul>'
$HTML_TAIL
] | str join '')
}
def git_clone [req]: nothing -> string {
let rp = ($req.path | parse "/github/{user}/{repo}/clone/{path}").0
if $rp.user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if $rp.repo in ["..", "."] {return (format_http 300 $HTML "Invalid repo name")} # "/" is already filter by mapper
if '..' in $rp.path {return (format_http 300 $HTML "<html><h1>Security error</h1><p>Repo path contains <code>..</code></p></html>")}
if (not ($"github/($rp.user)/($rp.repo)/git" | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"Repo has no git-clone or does not exist")}
if (not ($"github/($rp.user)/($rp.repo)/git/($rp.path)" | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"Repo has no git-clone or does not exist")}
if $rp.path == "info/refs" { do {
cd $"github/($rp.user)/($rp.repo)/git"
^git update-server-info # make web-cloneable / update info
} }
format_http 200 'application/octet-stream' (open --raw $'github/($rp.user)/($rp.repo)/git/($rp.path)')
}
def generate_release_details_page [req]: nothing -> string {
let rp = ($req.path | parse "/github/{user}/{repo}/releases/{id}").0
if $rp.user !~ "^[a-zA-Z0-9_-]+$" {return (format_http 300 $HTML "Invalid user name")}
if $rp.repo in ["..", "."] {return (format_http 300 $HTML "Invalid repo name")} # "/" is already filter by mapper
if $rp.id !~ '^\d+$' {return (format_http 300 $HTML "Invalid release id (not a number)")}
if (not ($"github/($rp.user)/($rp.repo)/releases" | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"Repo has no releases or does not exist")}
let release_file = (if ($'github/($rp.user)/($rp.repo)/releases/($rp.id).json' | path exists) {$'github/($rp.user)/($rp.repo)/releases/($rp.id)/release.json'} else {$'github/($rp.user)/($rp.repo)/releases/($rp.id)/release.json.gz'})
if (not ($release_file | path exists)) {return (format_http 404 "text/plain;charset=utf-8" $"Unknown release")}
let data = (if ($release_file | str ends-with ".json.gz") {^gunzip -kc $release_file | from json} else if ($release_file | str ends-with ".json") {open $release_file} else {null})
format_http 200 $HTML ([
$HTML_HEAD
$'<h1>/<a href="/github">github</a>/<a href="/github/(html escape $rp.user)">(html escape $rp.user)</a>/<a href="/github/(html escape $rp.user)/(html escape $rp.repo)">(html escape $rp.repo)</a>/<a href="(html escape $rp.user)/(html escape $rp.repo)/releases">releases</a>/(html escape $rp.id)</h1>'
'<h2>'
(if $data.is_draft {html escape '<DRAFT>'} else {''})
(if $data.is_prerelease {html escape '<PRERELEASE>'} else {''})
(html escape $data.name)
'</h2>'
(if ("created_at" in $data) {$' created at (html escape $data.created_at)'} else {''})
(if ("published_at" in $data) {$' published at (html escape $data.published_at)'} else {''})
(if ("body" in $data) {$'<pre>(html escape $data.body)</pre>'} else {''})
(if ("reactions" in $data) {format_reactions $data.reactions} else {''})
(if ("assets" in $data) {[
'<h3>Assets</h3><table>'
($data.assets | each {|asset|
$'<tr><td>πΎ($asset.download_count)</td><td>(html escape $asset.name)</td></tr>'
} | str join '')
'</table>'
] | str join ''} else {})
$HTML_TAIL
] | str join '')
}
const REACTION_NAME_TO_EMOTE = {
# https://docs.github.com/en/rest/reactions/reactions
"+1": "π"
"-1": "π"
"laugh": "π"
"confused": "π"
"heart": "β€οΈ"
"hooray": "π"
"rocket": "π"
"eyes": "π"
}
def format_reactions [reactions] {
[
'<div class="reactions">'
($reactions
| default {}
| transpose k v
| update k {|i| $REACTION_NAME_TO_EMOTE | get $i.k? | default $i.k? | default "?"}
| each {|i| $'<span>(html escape $i.k) ($i.v)</span>'}
| str join ''
)
'</div>'
] | str join ''
}
def format_username [name, association]: nothing -> string {
if $association in [null, "NONE"] {return (html escape ($name | default "<no name>"))}
$'(html escape ($name | default "<no name>")) [(html escape $association)]'
}
def isDarkColor [color: string] {
# non hex -> idk -> false
if $color !~ "[0-9a-f]{3,8}" {return false;}
# remove alpha
let color = (if ($color | str length) == 4 {$color | str substring 0..2} else {$color})
let color = (if ($color | str length) == 8 {$color | str substring 0..5} else {$color})
# 3 -> 6
let color = (if ($color | str length) == 3 {let a = ($color | split chars); $"($a.0)($a.0)($a.1)($a.1)($a.2)($a.2)"} else {$color})
let color = $"($color)0000" # better than indexOutOfRange error
let r = ($color | str substring 0..1 | into int --radix 16)
let g = ($color | str substring 2..3 | into int --radix 16)
let b = ($color | str substring 4..5 | into int --radix 16)
let luminance = (($r * 0.299 + $g * 0.587 + $b * 0.114) / 256)
return ($luminance < 0.5)
}