aboutsummaryrefslogtreecommitdiff
path: root/guix-data-service/web/repository/controller.scm
blob: 9837867516d50c4aa155fd3f62192a2d3696bc5f (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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
;;; Guix Data Service -- Information about Guix over time
;;; Copyright © 2019 Christopher Baines <mail@cbaines.net>
;;;
;;; This program is free software: you can redistribute it and/or
;;; modify it under the terms of the GNU Affero General Public License
;;; as published by the Free Software Foundation, either version 3 of
;;; the License, or (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;; Affero General Public License for more details.
;;;
;;; You should have received a copy of the GNU Affero General Public
;;; License along with this program.  If not, see
;;; <http://www.gnu.org/licenses/>.

(define-module (guix-data-service web repository controller)
  #:use-module (ice-9 match)
  #:use-module (web uri)
  #:use-module (web request)
  #:use-module (guix-data-service web render)
  #:use-module (guix-data-service web query-parameters)
  #:use-module (guix-data-service web util)
  #:use-module (guix-data-service model utils)
  #:use-module (guix-data-service model build-server)
  #:use-module (guix-data-service model derivation)
  #:use-module (guix-data-service model package)
  #:use-module (guix-data-service model git-branch)
  #:use-module (guix-data-service model git-repository)
  #:use-module (guix-data-service web view html)
  #:use-module (guix-data-service web revision controller)
  #:use-module (guix-data-service web repository html)
  #:export (repository-controller))

(define (repository-controller request
                               method-and-path-components
                               mime-types
                               body
                               conn)
  (define path
    (uri-path (request-uri request)))

  (match method-and-path-components
    (('GET "repositories")
     (let ((git-repositories (all-git-repositories conn)))
       (render-html
        #:sxml
        (view-git-repositories git-repositories))))
    (('GET "repository" id)
     (match (select-git-repository conn id)
       ((label url cgit-url-base)
        (render-html
         #:sxml
         (view-git-repository
          (string->number id)
          label url cgit-url-base
          (all-branches-with-most-recent-commit conn
                                                (string->number id)))))
       (#f
        (render-html
         #:sxml (general-not-found
                 "Repository not found"
                 "")
         #:code 404))))
    (('GET "repository" repository-id "branch" branch-name)
     (let ((parsed-query-parameters
            (parse-query-parameters
             request
             `((after_date     ,parse-datetime)
               (before_date    ,parse-datetime)
               (limit_results  ,parse-result-limit #:default 100)))))
       (let ((revisions
              (most-recent-commits-for-branch
               conn
               (string->number repository-id)
               branch-name
               #:limit (assq-ref parsed-query-parameters 'limit_results)
               #:after-date (assq-ref parsed-query-parameters
                                      'after_date)
               #:before-date (assq-ref parsed-query-parameters
                                       'before_date))))
         (case (most-appropriate-mime-type
                '(application/json text/html)
                mime-types)
           ((application/json)
            (render-json
             `((revisions
                . ,(list->vector
                    (map (match-lambda
                           ((date commit-hash _ _)
                            `((date . ,date)
                              (commit-hash . ,commit-hash))))
                         revisions))))))
           (else
            (render-html
             #:sxml (if (any-invalid-query-parameters? parsed-query-parameters)
                        (view-branch repository-id
                                     branch-name parsed-query-parameters '())
                        (view-branch
                         repository-id
                         branch-name
                         parsed-query-parameters
                         revisions))))))))
    (('GET "repository" repository-id "branch" branch-name "package" package-name)
     (let ((package-versions
            (package-versions-for-branch conn
                                         (string->number repository-id)
                                         branch-name
                                         package-name)))
       (case (most-appropriate-mime-type
              '(application/json text/html)
              mime-types)
         ((application/json)
          (render-json
           `((versions . ,(list->vector
                           (map (match-lambda
                                  ((package-version first-guix-revision-commit
                                                    first-datetime
                                                    last-guix-revision-commit
                                                    last-datetime)
                                   `((version . ,package-version)
                                     (first_revision
                                      . ((commit . ,first-guix-revision-commit)
                                         (datetime . ,first-datetime)))
                                     (last_revision
                                      . ((commit . ,last-guix-revision-commit)
                                         (datetime . ,last-datetime))))))
                                package-versions))))))
         (else
          (render-html
           #:sxml (view-branch-package
                   repository-id
                   branch-name
                   package-name
                   package-versions))))))
    (('GET "repository" repository-id "branch" branch-name "package" package-name "derivation-history")
     (render-branch-package-derivation-history request
                                               mime-types
                                               conn
                                               repository-id
                                               branch-name
                                               package-name))
    (('GET "repository" repository-id "branch" branch-name
           "package" package-name "output-history")
     (render-branch-package-output-history request
                                           mime-types
                                           conn
                                           repository-id
                                           branch-name
                                           package-name))
    (('GET "repository" repository-id "branch" branch-name "latest-processed-revision")
     (let ((commit-hash
            (latest-processed-commit-for-branch conn repository-id branch-name)))
       (if commit-hash
           (render-view-revision mime-types
                                 conn
                                 commit-hash
                                 #:path-base path
                                 #:header-text
                                 `("Latest processed revision for branch "
                                   (samp ,branch-name)))
           (render-unknown-revision mime-types
                                    conn
                                    commit-hash))))
    (('GET "repository" repository-id "branch" branch-name "latest-processed-revision" "packages")
     (let ((commit-hash
            (latest-processed-commit-for-branch conn repository-id branch-name)))
       (if commit-hash
           (let ((parsed-query-parameters
                  (guard-against-mutually-exclusive-query-parameters
                   (parse-query-parameters
                    request
                    `((after_name     ,identity)
                      (field          ,identity #:multi-value
                                      #:default ("version" "synopsis"))
                      (search_query   ,identity)
                      (limit_results  ,parse-result-limit
                                      #:no-default-when (all_results)
                                      #:default 100)
                      (all_results    ,parse-checkbox-value)))
                   ;; You can't specify a search query, but then also limit the
                   ;; results by filtering for after a particular package name
                   '((after_name search_query)
                     (limit_results all_results)))))

             (render-revision-packages mime-types
                                       conn
                                       commit-hash
                                       parsed-query-parameters
                                       #:path-base path
                                       #:header-text
                                       `("Latest processed revision for branch "
                                         (samp ,branch-name))
                                       #:header-link
                                       (string-append
                                        "/repository/" repository-id
                                        "/branch/" branch-name
                                        "/latest-processed-revision")))
           (render-unknown-revision mime-types
                                    conn
                                    commit-hash))))
    (('GET "repository" repository-id "branch" branch-name "latest-processed-revision"
           "lint-warnings")
     (let ((commit-hash
            (latest-processed-commit-for-branch conn repository-id branch-name)))
       (if commit-hash
           (let ((parsed-query-parameters
                  (parse-query-parameters
                   request
                   `((package_query  ,identity)
                     (linter         ,identity #:multi-value)
                     (message_query  ,identity)
                     (field          ,identity #:multi-value
                                     #:default ("linter"
                                                "message"
                                                "location"))))))

             (render-revision-lint-warnings mime-types
                                            conn
                                            commit-hash
                                            parsed-query-parameters
                                            #:path-base path
                                            #:header-text
                                            `("Latest processed revision for branch "
                                              (samp ,branch-name))
                                            #:header-link
                                            (string-append
                                             "/repository/" repository-id
                                             "/branch/" branch-name
                                             "/latest-processed-revision")))
           (render-unknown-revision mime-types
                                    conn
                                    commit-hash))))
    (('GET "repository" repository-id "branch" branch-name "latest-processed-revision" "package" name version)
     (let ((commit-hash
            (latest-processed-commit-for-branch conn repository-id branch-name)))
       (if commit-hash
           (render-revision-package-version mime-types
                                            conn
                                            commit-hash
                                            name
                                            version
                                            #:header-text
                                            `("Latest processed revision for branch "
                                              (samp ,branch-name))
                                            #:header-link
                                            (string-append
                                             "/repository/" repository-id
                                             "/branch/" branch-name
                                             "/latest-processed-revision")
                                            #:version-history-link
                                            (string-append
                                             "/repository/" repository-id
                                             "/branch/" branch-name
                                             "/package/" name))
           (render-unknown-revision mime-types
                                    conn
                                    commit-hash))))
    (_ #f)))

(define (parse-build-system conn)
  (let ((systems
         (valid-systems conn)))
    (lambda (s)
      (if (member s systems)
          s
          (make-invalid-query-parameter
           s "unknown system")))))

(define (render-branch-package-derivation-history request
                                                  mime-types
                                                  conn
                                                  repository-id
                                                  branch-name
                                                  package-name)
  (let ((parsed-query-parameters
         (parse-query-parameters
          request
          `((system  ,(parse-build-system conn)
                     #:default "x86_64-linux")
            (target  ,parse-target
                     #:default "")))))
    (let* ((system
            (assq-ref parsed-query-parameters 'system))
           (target
            (assq-ref parsed-query-parameters 'target))
           (package-derivations
            (package-derivations-for-branch conn
                                            (string->number repository-id)
                                            branch-name
                                            system
                                            target
                                            package-name))
           (build-server-urls
           (group-to-alist
            (match-lambda
              ((id url lookup-all-derivations)
               (cons id url)))
            (select-build-servers conn))))
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          `((derivations . ,(list->vector
                             (map (match-lambda
                                    ((package-version derivation-file-name
                                                      first-guix-revision-commit
                                                      first-datetime
                                                      last-guix-revision-commit
                                                      last-datetime)
                                     `((version . ,package-version)
                                       (derivation . ,derivation-file-name)
                                       (first_revision
                                        . ((commit . ,first-guix-revision-commit)
                                           (datetime . ,first-datetime)))
                                       (last_revision
                                        . ((commit . ,last-guix-revision-commit)
                                           (datetime . ,last-datetime))))))
                                  package-derivations))))))
        (else
         (render-html
          #:sxml (view-branch-package-derivations
                  parsed-query-parameters
                  repository-id
                  branch-name
                  package-name
                  (valid-systems conn)
                  (valid-targets->options
                   (valid-targets conn))
                  build-server-urls
                  package-derivations)))))))

(define (render-branch-package-output-history request
                                              mime-types
                                              conn
                                              repository-id
                                              branch-name
                                              package-name)
  (let ((parsed-query-parameters
         (parse-query-parameters
          request
          `((output  ,identity
                     #:default "out")
            (system  ,(parse-build-system conn)
                     #:default "x86_64-linux")
            (target  ,parse-target
                     #:default "")))))
    (let* ((system
            (assq-ref parsed-query-parameters 'system))
           (target
            (assq-ref parsed-query-parameters 'target))
           (output-name
            (assq-ref parsed-query-parameters 'output))
           (package-outputs
            (package-outputs-for-branch conn
                                        (string->number repository-id)
                                        branch-name
                                        system
                                        target
                                        package-name
                                        output-name))
           (build-server-urls
            (group-to-alist
             (match-lambda
               ((id url lookup-all-derivations)
                (cons id url)))
             (select-build-servers conn))))
      (case (most-appropriate-mime-type
             '(application/json text/html)
             mime-types)
        ((application/json)
         (render-json
          `((derivations . ,(list->vector
                             (map (match-lambda
                                    ((package-version derivation-file-name
                                                      first-guix-revision-commit
                                                      first-datetime
                                                      last-guix-revision-commit
                                                      last-datetime)
                                     `((version . ,package-version)
                                       (derivation . ,derivation-file-name)
                                       (first_revision
                                        . ((commit . ,first-guix-revision-commit)
                                           (datetime . ,first-datetime)))
                                       (last_revision
                                        . ((commit . ,last-guix-revision-commit)
                                           (datetime . ,last-datetime))))))
                                  package-outputs))))))
        (else
         (render-html
          #:sxml (view-branch-package-outputs
                  parsed-query-parameters
                  repository-id
                  branch-name
                  package-name
                  output-name
                  (valid-systems conn)
                  (valid-targets->options
                   (valid-targets conn))
                  build-server-urls
                  package-outputs)))))))