Commits

Hugh Giddens  committed cb946f3 Merge

Fix searching against JIRA 4.4 instances.

  • Participants
  • Parent commits 8ab4e07, 223fcf6

Comments (0)

Files changed (1)

       (format "summary ~ \"%s\" OR description ~ \"%s\" OR comment ~ \"%s\""
               quote-escaped quote-escaped quote-escaped))))
 
+(defun jira-issue-needs-enrichment-p (issue)
+  (null (assoc 'fields issue)))
+
+(defun jira-enrich-issue (issue instance outstanding callback callback-args)
+  ;; We should be using the self link.
+  (jira-get instance
+            (jira-issue-endpoint (cdr (assoc 'key issue)))
+            (lambda (issue-details issue outstanding callback callback-args)
+              (unwind-protect
+                  (let* ((required-fields '(status priority assignee summary))
+                         (field-list (loop for field in (cdr (assoc 'fields issue-details))
+                                           when (memq (car field) required-fields)
+                                           collect field)))
+                    (setf (nthcdr (length issue) issue) `((fields . ,field-list))))
+                (when (zerop (decf (car outstanding)))
+                  (apply callback callback-args))))
+            (list issue outstanding callback callback-args)))
+
+(defun jira-enrich-issues (issue-list instance callback &optional callback-args)
+  ;; 4.4 search results are just '((key . "key-1") (self
+  ;; . "https://url/to/key-1")); Here we enrich them so they match the
+  ;; cool search results we get from 5. There doesn't seem to be a way
+  ;; to coerce 4.4 to do this for us with the expand parameter.
+  (let ((outstanding (list (length issue-list))))
+    (if (not (find-if 'jira-issue-needs-enrichment-p issue-list))
+        (apply callback callback-args)
+      (dolist (issue issue-list)
+        (jira-enrich-issue issue instance outstanding callback callback-args)))))
+
 ;;; XXX We seem to be getting back much richer documents here than the documentation says we should be
 (defun jira-search-issues (instance text)
   "Displays a list of issues matching TEXT on INSTANCE.
               (jira-search-endpoint)
               `((jql . ,(or (jira-jql-text-search text) ""))
                 (maxResults . 20))
-              (lambda (search-results text max-results target-buffer)
-                (with-jira-buffer target-buffer
-                  (insert (propertize "Issue search\n" 'face 'jira-title-face) "\n")
-                  (unless (zerop (length text))
-                    (insert (propertize "Query text\n" 'face 'jira-heading-face) text "\n\n"))
-                  (jira-display-issues (cdr (assoc 'issues search-results)))))
-              (list text 20 (current-buffer))))
+              (lambda (search-results instance text target-buffer)
+                (let ((issue-list (cdr (assoc 'issues search-results))))
+                  (jira-enrich-issues issue-list
+                                      instance
+                                      (lambda (issue-list text target-buffer)
+                                        (with-jira-buffer target-buffer
+                                          (insert (propertize "Issue search\n" 'face 'jira-title-face) "\n")
+                                          (unless (zerop (length text))
+                                            (insert (propertize "Query text\n" 'face 'jira-heading-face) text "\n\n"))
+                                          (jira-display-issues issue-list)))
+                                      (list issue-list text target-buffer))))
+              (list instance text (current-buffer))))
   (jira-revert-buffer))
 
 (defun jira-search-project-issues (instance project text)
                               (format "project = \"%s\"" project)
                             (format "project = \"%s\" AND (%s)" project text-query))))
                 (maxResults . 20))
-              (lambda (search-results project text max-results target-buffer)
-                (with-jira-buffer target-buffer
-                  (insert (propertize (concat (upcase project) " project issue search\n")
-                                      'face 'jira-title-face) "\n")
-                  (unless (zerop (length text))
-                    (insert (propertize "Query text\n" 'face 'jira-heading-face) text "\n\n"))
-                  (jira-display-issues (cdr (assoc 'issues search-results)))))
-              (list project text 20 (current-buffer))))
+              (lambda (search-results project instance text target-buffer)
+                (let ((issue-list (cdr (assoc 'issues search-results))))
+                  (jira-enrich-issues issue-list
+                                      instance
+                                      (lambda (issue-list project text target-buffer)
+                                        (with-jira-buffer target-buffer
+                                          (insert (propertize (concat (upcase project) " project issue search\n")
+                                                              'face 'jira-title-face) "\n")
+                                          (unless (zerop (length text))
+                                            (insert (propertize "Query text\n" 'face 'jira-heading-face) text "\n\n"))
+                                          (jira-display-issues issue-list)))
+                                      (list issue-list project text target-buffer))))
+              (list project instance text (current-buffer))))
   (jira-revert-buffer))
 
+(defun jira-issue-summary (issue)
+  (let* ((fields (cdr (assoc 'fields issue)))
+         (summary (cdr (assoc 'summary fields))))
+    (etypecase summary
+      (string summary)
+      (list (cdr (assoc 'value summary))))))
+
+(defun jira-issue-priority (issue)
+  (let* ((fields (cdr (assoc 'fields issue)))
+         (priority (cdr (assoc 'priority fields))))
+    (if (equal (cdr (assoc 'name priority)) "priority")
+        (cdr (assoc 'value priority))
+      priority)))
+
+(defun jira-issue-status (issue)
+  (let* ((fields (cdr (assoc 'fields issue)))
+         (status (cdr (assoc 'status fields))))
+    (if (equal (cdr (assoc 'name status)) "status")
+        (cdr (assoc 'value status))
+      status)))
+
+(defun jira-issue-assignee (issue)
+  (let* ((fields (cdr (assoc 'fields issue)))
+         (assignee (cdr (assoc 'assignee fields))))
+    (if (equal (cdr (assoc 'name assignee)) "assignee")
+        (cdr (assoc 'value assignee))
+        assignee)))
+
 ;; TODO: look at tabulated-list-mode for this stuff in info node
 ;; (elisp)Basic Major Modes
 (defun jira-display-issues (issues)
   (let ((row-count 0)
         icon-requests)
     (dolist (issue issues)
-      (let* ((fields (cdr (assoc 'fields issue)))
-             (status (cdr (assoc 'status fields)))
-             (priority (cdr (assoc 'priority fields)))
-             (assignee (cdr (assoc 'assignee fields)))
+      (let* ((key (cdr (assoc 'key issue)))
+             (status (jira-issue-status issue))
+             (priority (jira-issue-priority issue))
+             (assignee (jira-issue-assignee issue))
+             (summary (jira-issue-summary issue))
              (row-start (point)))
         (insert (format "%-16s" " "))
         (beginning-of-line)
         (make-text-button (point)
                           (progn
-                            (insert (cdr (assoc 'key issue)))
+                            (insert key)
                             (point))
                           'action (lambda (button)
                                     (jira-show-issue
                                      jira-instance
                                      (button-get button 'jira-issue-key)))
                           'follow-link 'mouse-face
-                          'jira-issue-key (cdr (assoc 'key issue)))
+                          'jira-issue-key key)
         (beginning-of-line)
         (forward-char 16)
         (insert (format "%-15s" (if (null assignee)
                                              ""
                                            (cdr (assoc 'name priority))))))
         (put-text-property (- (point) 12) (- (point) 10) 'invisible t)
-        (insert "\n" (cdr (assoc 'summary fields)) "\n")
+        (insert "\n" summary "\n")
         (when (and jira-zebra-stripe-rows (oddp row-count))
           (overlay-put (make-overlay row-start (point)) 'face 'jira-zebra-stripe-face)))
       (incf row-count))
   (unless (eq major-mode 'jira-mode)
     (jira-mode))
   (incf jira-request-level)
-  (labels ((got-filter (filter filter-id target-buffer)
-             (with-current-buffer target-buffer
-               (jira-post jira-instance
-                               (jira-search-endpoint)
-                               `((jql . ,(format "filter = %s" filter-id)))
-                               'got-search-results
-                               (list filter filter-id target-buffer))))
-           (got-search-results (search-results filter filter-id target-buffer)
-             (let ((issues (cdr (assoc 'issues search-results))))
-               (with-jira-buffer target-buffer
-                (insert (propertize (format "Filter: %s (%s)\n"
-                                            (cdr (assoc 'name filter))
-                                            filter-id)
-                                    'face 'jira-title-face)
-                        "\n")
-                (when (cdr (assoc 'description filter))
-                  (insert (propertize "Description\n" 'face 'jira-heading-face)
-                          (cdr (assoc 'description filter)) "\n\n"))
-                (jira-display-issues issues)))))
+  (labels ((got-filter (filter filter-id instance target-buffer)
+             (jira-post instance
+                        (jira-search-endpoint)
+                        `((jql . ,(format "filter = %s" filter-id)))
+                        'got-search-results
+                        (list filter filter-id instance target-buffer)))
+           (got-search-results (search-results filter filter-id instance target-buffer)
+             (let ((issue-list (cdr (assoc 'issues search-results))))
+               (jira-enrich-issues issue-list
+                                   instance
+                                   'got-enriched-results
+                                   (list issue-list filter filter-id target-buffer))))
+           (got-enriched-results (issue-list filter filter-id target-buffer)
+             (with-jira-buffer target-buffer
+               (insert (propertize (format "Filter: %s (%s)\n"
+                                           (cdr (assoc 'name filter))
+                                           filter-id)
+                                   'face 'jira-title-face)
+                       "\n")
+               (when (cdr (assoc 'description filter))
+                 (insert (propertize "Description\n" 'face 'jira-heading-face)
+                         (cdr (assoc 'description filter)) "\n\n"))
+               (jira-display-issues issues-list))))
+    ;; TODO: Setting jira-instance should be done as part of the end of the
+    ;;       chain of requests rather than their beginning.
     (setf jira-instance instance
           jira-revert-buffer
           (list 'jira-get
                 instance
                 (jira-filter-endpoint filter-id)
                 'got-filter
-                (list filter-id (current-buffer))))
+                (list filter-id instance (current-buffer))))
     (jira-revert-buffer)))
 
 (defun jira-format-date (date)
                 (let* ((fields (cdr (assoc 'fields issue)))
                        (project (cdr (assoc 'project fields)))
                        (issue-type (cdr (assoc 'issuetype fields)))
-                       (status (cdr (assoc 'status fields)))
+                       (status (jira-issue-status issue))
                        (resolution (cdr (assoc 'resolution fields)))
-                       (priority (cdr (assoc 'priority fields)))
-                       (assignee (cdr (assoc 'assignee fields)))
+                       (priority (jira-issue-priority issue))
+                       (assignee (jira-issue-assignee issue))
                        (reporter (cdr (assoc 'reporter fields)))
                        (watches (cdr (assoc 'watches fields)))
                        (comments (cdr (assoc 'comments (cdr (assoc 'comment fields)))))
                   (with-jira-buffer target-buffer
                     (insert (propertize (format "%s: %s\n"
                                                 (cdr (assoc 'key issue))
-                                                (cdr (assoc 'summary fields)))
+                                                (jira-issue-summary issue))
                                         'face 'jira-title-face)
                             "\n")
                     (let* ((fields (list (list "Type"