-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rkt
228 lines (208 loc) · 8.76 KB
/
main.rkt
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
#lang at-exp racket
(provide
(contract-out
[launch-run! (string?
string?
string?
string?
. -> .
(either/c actions-run?))]
[cancel-run! (actions-run? . -> . boolean?)]
[get-run-log! ({actions-run?}
{#:section (or/c string? #f)}
. ->* .
(either/c string?))]
[get-all-runs! (string? string? . -> . (either/c (listof actions-run?)))]
[get-run-by-url! (string? . -> . (either/c actions-run?))]
;; undocumented, bc there's no point until `get-all-runs!` is fixed
[get-runs-by-url! (string?
string?
(listof string?)
. -> . (either/c (hash/c string? (either/c actions-run?))))])
(struct-out actions-run))
(require "github-api.rkt"
simple-option/either
gregor
file/unzip)
(struct actions-run (id
url
html-url
commit
status
conclusion
creation-time
log-url
cancel-url)
#:prefab)
;; todo: this doesn't deal with pagination
(define (get-all-runs! repo-owner repo-name)
(either-let*
([run-info (github-request! (~a "repos/" repo-owner "/" repo-name "/actions/runs"))]
[runs (hash-ref/either run-info
'workflow_runs
@~a{Failed to get runs: @run-info})])
(map json->actions-run runs)))
(define (get-runs-by-url! repo-owner repo-name urls)
;; todo: Alternative version that more efficiently uses api calls, but
;; requires `get-all-runs!` to deal with pagination
#;(either-let*
([all-runs (get-all-runs! repo-owner repo-name)]
[runs-by-url (for/hash ([run (in-list all-runs)])
(values (actions-run-url run) run))])
(for/hash ([url (in-list urls)])
(values url
(hash-ref/option runs-by-url url @~a{No run found with url @url}))))
(for/hash ([url (in-list urls)])
(values url (get-run-by-url! url))))
(define (get-run-by-url! url)
(either-let*
([run-json (github-request! url)
#:extra-failure-message (~a url " : ")])
(json->actions-run run-json)))
(define (failed-to action code headers in)
(failure
@~a{
Failed to @action, response code @code
Message: @(try-read-body-string code headers in)
}))
(define (json->actions-run run-info-json)
(match run-info-json
[(hash-table ['id id]
['url url]
['html_url html-url]
['head_sha commit]
['status status]
['conclusion conclusion]
['created_at creation-time]
['logs_url log-url]
['cancel_url cancel-url]
_ ...)
(actions-run id
url
html-url
commit
status
conclusion
(iso8601->moment creation-time)
log-url
cancel-url)]
[else (failure "Unexpected run info shape in api response")]))
(define (cancel-run! a-run)
(github-request! (actions-run-cancel-url a-run)
#:method POST
#:read-response (λ (code headers in) (equal? code 202))))
(define run-retrieval-polling-period-seconds 5)
(define run-retrieval-polling-timeout-seconds (* 1 60))
(define (launch-run! repo-owner repo-name workflow-id ref)
(define (actions-run-newer? run1 run2)
(moment>? (actions-run-creation-time run1)
(actions-run-creation-time run2)))
(define (newest-job-in jobs)
(match (sort jobs actions-run-newer?)
[(cons newest _) newest]
['() #f]))
(define (wait/poll-for-new-job! original-jobs)
(either-let*
([original-latest-job
(let loop ([retry-count 0])
(match (newest-job-in original-jobs)
[(? actions-run? a-job) a-job]
[else
#:when (< (* retry-count run-retrieval-polling-period-seconds)
run-retrieval-polling-timeout-seconds)
(sleep run-retrieval-polling-period-seconds)
(loop (add1 retry-count))]
[else (failure @~a{
Couldn't find launched job after polling for @;
@|run-retrieval-polling-timeout-seconds|s
})]))]
[the-run
(let loop ([retry-count 0])
(match (get-all-runs! repo-owner repo-name)
[(and (not (? failure?))
(app newest-job-in latest-job))
#:when (actions-run-newer? latest-job original-latest-job)
latest-job]
[else
#:when (< (* retry-count run-retrieval-polling-period-seconds)
run-retrieval-polling-timeout-seconds)
(sleep run-retrieval-polling-period-seconds)
(loop (add1 retry-count))]
[else (failure @~a{
Couldn't find launched job after polling for @;
@|run-retrieval-polling-timeout-seconds|s
})]))])
the-run))
(either-let*
([jobs-before-launch (get-all-runs! repo-owner repo-name)]
[_ (github-request! (~a "repos/" repo-owner "/" repo-name
"/actions/workflows/" workflow-id "/dispatches")
#:method POST
#:data (string->bytes/utf-8 @~a{{"ref": "@ref"}})
#:read-response (match-lambda**
[{204 _ _} 'ok]
[{other headers in}
(failed-to "launch run"
other
headers
in)]))]
[the-run (wait/poll-for-new-job! jobs-before-launch)])
the-run))
(define wget (find-executable-path "wget"))
(define (get-run-log! a-run #:section [section-name #f])
(define (url->log-contents log-download-url)
(with-handlers ([exn:fail? (λ (e) (failure @~a{Error raised while getting log: @(exn-message e)}))])
(match-define (list stdout stdin _ stderr ctl)
(process* wget
"-4" ;; wget sometimes hangs on IPv6 addresses, so force IPv4
"-O"
"-"
log-download-url))
(close-output-port stdin)
(define log-sections (box empty))
(unzip
stdout
(match-lambda**
[{(regexp #rx"^.*/([0-9]+)_(.+).txt$" (list _
section-number-bytes
section-name-bytes))
#f
contents-port}
#:when (or (not section-name)
(string-ci=? (bytes->string/utf-8 section-name-bytes) section-name))
;; Sections aren't necessarily unpacked in order, so hang on to the section number
;; to order them later
(define section-number (string->number (bytes->string/utf-8 section-number-bytes)))
(set-box! log-sections
(cons (list section-number (port->string contents-port))
(unbox log-sections)))]
[{_ _ _} (void)]))
(ctl 'kill)
(close-input-port stdout)
(close-input-port stderr)
(match (unbox log-sections)
['() (failure "Failed to get log using download url")]
[sections
(string-join (map second (sort sections < #:key first))
"\n")])))
(either-let*
([log-download-url (github-request!
(actions-run-log-url a-run)
#:read-response (match-lambda**
[{302 (regexp #rx"(?mi:^location: (.+?)$)" (list _ url)) _}
url]
[{302 headers in}
(failed-to @~a{
parse log download url in headers:
@headers
}
302
headers
in)]
[{other headers in}
(failed-to "get log download url"
other
headers
in)]))]
[contents (url->log-contents log-download-url)])
contents))