summaryrefslogtreecommitdiff
path: root/debbugs/operations.scm
blob: c4cd80f5e7147afa37def7f216d32103056768ec (about) (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
;;; Guile-Debbugs --- Guile bindings for Debbugs
;;; Copyright © 2017, 2018 Ricardo Wurmus <rekado@elephly.net>
;;; Copyright © 2018 Arun Isaac <arunisaac@systemreboot.net>
;;;
;;; This file is part of Guile-Debbugs.
;;;
;;; Guile-Debbugs is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU General Public License as
;;; published by the Free Software Foundation; either version 3 of the
;;; License, or (at your option) any later version.
;;;
;;; Guile-Debbugs 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
;;; General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with Guile-Debbugs.  If not, see <http://www.gnu.org/licenses/>.

(define-module (debbugs operations)
  #:use-module (debbugs soap)
  #:use-module (debbugs bug)
  #:use-module (email email)
  #:use-module (sxml xpath)
  #:use-module (sxml match)
  #:use-module (srfi srfi-1)
  #:use-module (ice-9 match)
  #:use-module (ice-9 optargs))

(define (soap-email->email email-item)
  "Convert an SXML expression representing an email item from a SOAP
response to an <email> object."
  (define (drop-lines str k)
    (if (zero? k)
        str
        (drop-lines (substring str (1+ (string-index str #\newline)))
                    (1- k))))

  (let ((email-properties (map soap->scheme (cdr email-item))))
    (let* ((headers (parse-email-headers
                     (drop-lines (assoc-ref email-properties 'header) 2)))
           (content-type (assoc-ref headers 'content-type))
           (body (assoc-ref email-properties 'body)))
      (case (assoc-ref content-type 'type)
        ((multipart)
         (let ((boundary (assoc-ref content-type 'boundary)))
           (make-email
            headers
            ;; Sometimes the debbugs SOAP API provides only the first
            ;; MIME entity of a multipart message. This is a bug, and
            ;; the following works around it until it can be fixed in
            ;; the debbugs SOAP service.
            (if (string-contains body (string-append "--" boundary))
                (parse-email-body headers body)
                (list (make-mime-entity
                       `(content-type (type . text)
                                      (subtype . plain))
                       body))))))
        (else (make-email headers body))))))

(define-public (newest-bugs amount)
  "Return a list of bug numbers corresponding to the newest AMOUNT
bugs."
  (soap-request
   `(ns1:newest_bugs
     (@ (xmlns:ns1 . "urn:Debbugs/SOAP")
        (soapenc:encodingStyle . "http://schemas.xmlsoap.org/soap/encoding/"))
     (amount (@ (xsi:type . "xsd:int")) ,amount))
   (lambda (response-body)
     (map string->number ((sxpath '(// urn:Debbugs/SOAP:item *text*)) response-body)))))

(define-public (get-status bug-ids)
  "Return <bug> records containing the details for the bugs identified
by BUG-IDS, a list of bug numbers."
  (soap-request
   `(ns1:get_status
     (@ (xmlns:ns1 . "urn:Debbugs/SOAP")
        (soapenc:encodingStyle . "http://schemas.xmlsoap.org/soap/encoding/"))
     (ns1:bugs
      (@ (xsi:type "soapenc:Array")
         (soapenc:arrayType ,(string-append "xsd:int[" (number->string (length bug-ids)) "]")))
      ,(map (lambda (bug-id)
              `(ns1:bugs (@ (xsi:type "xsd:int")) ,bug-id))
            bug-ids)))
   (lambda (response-body)
     (let ((bugs ((sxpath '(// urn:Debbugs/SOAP:get_statusResponse
                               urn:Debbugs/SOAP:s-gensym3
                               urn:Debbugs/SOAP:item)) response-body)))
       (map soap-bug->bug bugs)))))

(define-public (get-bugs args)
  "Returns bug numbers for bugs that match the conditions given by
ARGS, an alist of key-value pairs.  Possible keys: package, submitter,
maint, src, severity, status (which can be 'done', 'forwarded',
'open'), tag, owner, bugs, affects, archive (which can be 'both', or a
Boolean value)."
  (soap-request
   `(ns1:get_bugs
     (@ (xmlns:ns1 . "urn:Debbugs/SOAP")
        (soapenc:encodingStyle . "http://schemas.xmlsoap.org/soap/encoding/"))
     (ns1:query
      (@ (xsi:type "soapenc:Array")
         (soapenc:arrayType ,(string-append "xsd:anyType[" (number->string (* 2 (length args))) "]")))
      ,@(map (match-lambda
               ((key . value)
                `((ns1:query (@ (xsi:type "xsd:string")) ,key)
                  (ns1:query (@ (xsi:type "xsd:string")) ,value))))
             args)))
   (lambda (response-body)
     (map string->number ((sxpath '(// urn:Debbugs/SOAP:item *text*)) response-body)))))

(define-public (get-bug-log bug-id)
  "Return emails associated with the bug identified by BUG-ID."
  (soap-request
   `(ns1:get_bug_log
     (@ (xmlns:ns1 . "urn:Debbugs/SOAP")
        (soapenc:encodingStyle . "http://schemas.xmlsoap.org/soap/encoding/"))
     (ns1:bugnumber
      (@ (xsi:type "xsd:int")) ,bug-id))
   (lambda (response-body)
     (let ((emails ((sxpath '(// urn:Debbugs/SOAP:get_bug_logResponse
                                 http://schemas.xmlsoap.org/soap/encoding/:Array
                                 urn:Debbugs/SOAP:item)) response-body)))
       (map soap-email->email emails)))))

(define-public (get-usertag email)
  "Return an association list of tag names to lists of bug numbers for
all bugs that have been tagged by EMAIL."
  (soap-request
   `(ns1:get_usertag
     (@ (xmlns:ns1 . "urn:Debbugs/SOAP")
        (soapenc:encodingStyle . "http://schemas.xmlsoap.org/soap/encoding/"))
     (ns1:user
      (@ (xsi:type "xsd:string")) ,email))
   (lambda (response-body)
     (let ((response ((sxpath '(// urn:Debbugs/SOAP:get_usertagResponse *)) response-body)))
       ;; The problem here is that for some usertags (e.g. usertags
       ;; for "hertzog@debian.org") a map is returned (so I could use
       ;; soap->scheme), but in other cases (e.g. usertags for
       ;; "aj@azure.humbug.org.au") each tag is an array with bug-num
       ;; items.
       (sxml-match (car response)
        ((urn:Debbugs/SOAP:s-gensym3 (@ (http://www.w3.org/1999/XMLSchema-instance:type "apachens:Map")) ,items ...)
         (map car (soap->scheme (car response) #t)))
        ((urn:Debbugs/SOAP:s-gensym3 ,tags ...)
         (map (lambda (tag)
                ;; For consistency make sure that the keys are strings.
                (let ((pair (soap->scheme tag)))
                  (if (symbol? (car pair))
                      (cons (symbol->string (car pair))
                            (cdr pair))
                      pair)))
              tags)))))))

(define %allowed-attributes
  '(@title severity submitter @author subject status @cdate package tags date))

(define %number-operators
  '((=  . NUMEQ)
    (/= . NUMNE)
    (>  . NUMGT)
    (>= . NUMGE)
    (<  . NUMLT)
    (<= . NUMLE)
    (>< . NUMBT)))

;; We use case insensitive searches for all string operators except
;; for string-regex.
(define %string-operators
  '((string=            . ISTREQ)
    (string-not-equal   . ISTRNE)
    (string-contains    . ISTRINC)
    (string-prefix      . ISTRBW)
    (string-suffix      . ISTREW)
    (string-and         . ISTRAND)
    (string-or          . ISTROR)
    (string-or-equal    . ISTROREQ)
    (string-regex       . STRRX)))

(define (operator op)
  "Translate readable search operators to attribute search conditions
supported by the HyperEstraier search engine, which is used by
debbugs.gnu.org."
  (or (assoc-ref %string-operators op)
      (assoc-ref %number-operators op)))

;; This is not implemented in the Debian deployment of Debbugs.
(define*-public (search-est phrase
                            #:key (skip 0) (max 0) (attributes '()))
  "Return the result of a full text search according to PHRASE.  When
SKIP is provided the given number of hits will be skipped; this is
useful for paged results.  At most MAX results are returned when MAX
is greater than zero.  The number of returned results is always
limited by the absolute maximum returned by the Debbugs server.

ATTRIBUTES is a list of lists containing the attribute, the value, and
an operator."
  (soap-request
   `(ns1:search_est
     (@ (xmlns:ns1 . "urn:Debbugs/SOAP")
        (xmlns:types . "urn:Debbugs/SOAP/TYPES")
        (soapenc:encodingStyle . "http://schemas.xmlsoap.org/soap/encoding/"))
     (ns1:query
      (@ (xsi:type "soapenc:Array")
         (soapenc:arrayType "types:ArrayOfAnyType[]"))
      ;; The primary query
      (ns1:query
       (@ (xsi:type "soapenc:Array")
          (soapenc:arrayType "xsd:anyType[6]"))
       ((ns1:query (@ (xsi:type "xsd:string")) "phrase")
        (ns1:query (@ (xsi:type "xsd:string")) ,phrase)
        (ns1:query (@ (xsi:type "xsd:string")) "max")
        (ns1:query (@ (xsi:type "xsd:string")) ,(number->string max))
        (ns1:query (@ (xsi:type "xsd:string")) "skip")
        (ns1:query (@ (xsi:type "xsd:string")) ,(number->string skip))))
      ;; Attribute filters.
      ,@(map (match-lambda
               ((attr op . values)
                `((ns1:query
                   (@ (xsi:type "soapenc:Array")
                      (soapenc:arrayType "xsd:anyType[4]"))
                   ((ns1:query (@ (xsi:type "xsd:string"))
                               ,(symbol->string attr))
                    (ns1:query (@ (xsi:type "xsd:string"))
                               ,(match values
                                  (((? string? s)) s)
                                  (((? number? n)) (number->string n))
                                  (((? number? a) (? number? b))
                                   (string-append (number->string a)
                                                  " "
                                                  (number->string b)))))
                    (ns1:query (@ (xsi:type "xsd:string")) "operator")
                    (ns1:query (@ (xsi:type "xsd:string"))
                               ,(operator op)))))))
             attributes)))
   (lambda (response-body)
     (let ((items ((sxpath '(// urn:Debbugs/SOAP:search_estResponse
                                http://schemas.xmlsoap.org/soap/encoding/:Array
                                urn:Debbugs/SOAP:item)) response-body)))
       ;; Flatten to list of alists
       (map (lambda (item)
              (match (soap->scheme item)
                (('item (pair) ...) pair)))
            items)))))