ahungry / org-jira

Bring Jira and OrgMode together
684 stars 132 forks source link

Get issues without creating new buffers #71

Closed JuanPabloMF closed 6 years ago

JuanPabloMF commented 6 years ago

Hi,

I am trying to work inside only one org file, and doing a few wrappers over org-jira-get-issues in order to call jql from a heading properties (that way I can handle a "board" for each heading).

I tried eliminating the calls to project-key buffers in org-jira-get-issues but I am still getting side effects and creation of new buffers. Is there any better way to handle this besides killing the new buffers from inside my wrapper function?

#+BEGIN_SRC emacs-lisp
(defun org-jira-org-get-issues (issues)
  "Get list of ISSUES into an org buffer.

Default is get unfinished issues assigned to you, but you can
customiz
                      e jql with a prefix argument.
See`org-jira-get-issue-list'"
  ;; If the user doesn't provide a default, async call to build an issue list
  ;; from the JQL style query
  (interactive
   (org-jira-get-issue-list org-jira-get-issue-list-callback))
  (mapc (lambda (issue)
            (let* ((proj-key (org-jira-get-issue-project issue))
                   (issue-id (org-jira-get-issue-key issue))
                   (issue-summary (org-jira-get-issue-summary issue))
                   (issue-headline issue-summary))

           (save-excursion
                    (org-jira-mode t)
                    (widen)
                    (outline-show-all)
                    (goto-char (point-min))
                    (setq p (org-find-entry-with-id issue-id))
                    ;;
                    (save-restriction
              ;;check if line already exists
                      (if (and p (>= p (point-min))
                               (<= p (point-max)))
                          (progn
                            (goto-char p)
                            (forward-thing 'whitespace)
                            (kill-line))
                        (goto-char (point-max))
                      ;;insert issue with status
                      (unless (looking-at "^")
                          (insert "\n"))
                        (insert "* "))
                      (let ((status (org-jira-get-issue-val 'status issue)))
                        (org-jira-insert (concat (cond (org-jira-use-status-as-todo
                                                        (upcase (replace-regexp-in-string " " "-" status)))
                                                       ((member status org-jira-done-states) "DONE")
                                                       ("TODO")) " "
                                                       issue-headline)))
                      ;;add issue ID
                      (save-excursion
                        (unless (search-forward "\n" (point-max) 1)
                          (insert "\n")))
                      (org-narrow-to-subtree)
                      (org-change-tag-in-region
                       (point-min)
                       (save-excursion
                         (forward-line 1)
                         (point))
                       (replace-regexp-in-string "-" "_" issue-id)
                       nil)
                      (mapc (lambda (entry)
                              (let ((val (org-jira-get-issue-val entry issue)))
                                (when (or (and val (not (string= val "")))
                                          (eq entry 'assignee)) ;; Always show assignee
                                  (org-jira-entry-put (point) (symbol-name entry) val))))
                            '(assignee reporter type priority resolution status components created updated))

                      (org-jira-entry-put (point) "ID" (org-jira-get-issue-key issue))
                      (org-jira-entry-put (point) "CUSTOM_ID" (org-jira-get-issue-key issue))

              ;; Insert the duedate as a deadline if it exists
                      (when org-jira-deadline-duedate-sync-p
                        (let ((duedate (org-jira-get-issue-val 'duedate issue)))
                          (when (> (length duedate) 0)
                            (org-deadline nil duedate))))

                      (mapc (lambda (heading-entry)
                              (ensure-on-issue-id
                               issue-id
                               (let* ((entry-heading (concat (symbol-name heading-entry) (format ": [[%s][%s]]" (concat jiralib-url "/browse/" issue-id) issue-id))))
                                 (setq p (org-find-exact-headline-in-buffer entry-heading))
                                 (if (and p (>= p (point-min))
                                          (<= p (point-max)))
                                     (progn
                                       (goto-char p)
                                       (org-narrow-to-subtree)
                                       (goto-char (point-min))
                                       (forward-line 1)
                                       (delete-region (point) (point-max)))
                                   (if (org-goto-first-child)
                                       (org-insert-heading)
                                     (goto-char (point-max))
                                     (org-insert-subheading t))
                                   (org-jira-insert entry-heading "\n"))

                                 (org-jira-insert (replace-regexp-in-string "^" "  " (org-jira-get-issue-val heading-entry issue))))))
                            '(description))
                      (org-jira-update-comments-for-current-issue)
                      ;; FIXME: Re-enable when attachments are not erroring.
                      ;;(org-jira-update-attachments-for-current-issue)

                      ;; only sync worklog clocks when the user sets it to be so.
                      (when org-jira-worklog-sync-p
                        (org-jira-update-worklogs-for-current-issue))

                      ))))
          issues)
      )
#+END_SRC

#+RESULTS:
: org-jira-org-get-issues

* Personales
  :PROPERTIES:
  :jql:      project in (NAV, IPS, CV, DS, DOP, PMS, PRED, PM, PROD) and issuetype in (Desarrollo, Ops, Bug, "I+D") and status in ("To Do","In Progress", Test) ORDER BY priority
  :END:

#+RESULTS:
: org-jira-org-get-issues

#+BEGIN_SRC emacs-lisp
(setq jql (cdr (nth 1 (org-entry-properties))))
(setq current-prefix-arg '(4))
(org-jira-org-get-issues (jiralib-do-jql-search jql))
#+END_SRC
ahungry commented 6 years ago

I have a new branch that changes how tasks are pulled in (under a main top-level heading, one per file) to help with org-sparse-trees functionality.

You might have an easier time accomplishing your goal using that branch, as you don't have to select a full buffer's contents any longer, but can simply refile a heading from one buffer into another.

JuanPabloMF commented 6 years ago

Thanks! I will look into that branch.

In the meantime I found that org-jira-update-worklogs-for-current-issue was creating the new buffers aside from what org-jira-get-issues was doing so I modified it and now the function is only working in my main buffer.