forked from baohaojun/org-jira
-
Notifications
You must be signed in to change notification settings - Fork 0
/
org-jira.el
889 lines (780 loc) · 35.2 KB
/
org-jira.el
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
;;; org-jira.el --- Syncing between Jira and Org-mode.
;; Author: Bao Haojun <[email protected]>
;; Maintainer: Bao Haojun <[email protected]>
;; Version: 0.1
;; Homepage: https://github.com/baohaojun/org-jira
;; This file is not part of GNU Emacs.
;; This program 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.
;; 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;; This program 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 2
;; 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 General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program; if not, write to the Free Software
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
;; 02110-1301, USA.
;;; Commentary:
;;
;; This provides an extension to org-mode for syncing issues with JIRA
;; issue servers.
;;
;;; Code:
(require 'org)
(require 'jiralib)
(defgroup org-jira nil
"Customisation group for org-jira."
:tag "Org JIRA"
:group 'org)
(defvar org-jira-working-dir "~/.org-jira"
"Folder under which to store org-jira working files")
(defvar org-jira-default-jql
"assignee = currentUser() and
((reporter = currentUser() and status != closed) or resolution = unresolved)
ORDER BY priority DESC, created ASC"
"Default jql for querying your Jira tickets")
(defvar jira-users (list (cons "Full Name" "username"))
"Jira has not api for discovering all users, so we should provide it somewhere else")
(defcustom org-jira-serv-alist nil
"Association list to set information for each jira server.
Each element of the alist is a jira server name. The CAR of each
element is a string, uniquely identifying the server. The CDR
of each element is a well-formed property list with an even
number of elements, alternating keys and values, specifying
parameters for the server.
(:property value :property value ... )
When a property is given a value in org-jira-serv-alist, its
setting overrides the value of the corresponding user
variable (if any) during syncing.
Most properties are optional, but some should always be set:
:url soap url of the jira server.
:username username to be used.
:host hostname of the jira server (TODO: compute it from ~url~).
All the other properties are optional. They over-ride the global variables.
:password password to be used, will be prompted if missing.
"
:group 'org-jira
:type '(alist :value-type plist))
(defcustom org-jira-use-status-as-todo nil
"Use the JIRA status as the TODO tag value."
:group 'org-jira)
(defvar org-jira-serv nil
"Parameters of the currently selected blog.")
(defvar org-jira-serv-name nil
"Name of the blog, to pick from `org-jira-serv-alist'")
(defvar org-jira-projects-list nil
"List of jira projects.")
(defvar org-jira-current-project nil
"currently selected (i.e., active project).")
(defvar org-jira-issues-list nil
"List of jira issues under the current project.")
(defvar org-jira-server-rpc-url nil
"Jira server soap URL")
(defvar org-jira-server-userid nil
"Jira server user id")
(defvar org-jira-proj-id nil
"Jira project ID")
(defvar org-jira-logged-in nil
"Flag whether user is logged-in or not")
(defvar org-jira-buffer-name "*org-jira-%s*"
"Name of the jira buffer")
(defvar org-jira-buffer-kill-prompt t
"Ask before killing buffer")
(make-variable-buffer-local 'org-jira-buffer-kill-prompt)
(defconst org-jira-version "0.1"
"Current version of org-jira.el")
(defvar org-jira-mode-hook nil
"Hook to run upon entry into mode.")
(defvar org-jira-issue-id-history '()
"Prompt history for issue id")
(defmacro ensure-on-issue (&rest body)
"Make sure we are on an issue heading"
`(save-excursion
(while (org-up-heading-safe)) ; goto the top heading
(let ((org-jira-id (org-jira-id)))
(unless (and org-jira-id (string-match (jiralib-get-issue-regexp) org-jira-id))
(error "Not on a issue region!")))
,@body))
(defmacro ensure-on-issue-id (issue-id &rest body)
"Make sure we are on an issue heading with id ISSUE-ID"
(declare (indent 1))
`(save-excursion
(save-restriction
(widen)
(show-all)
(goto-char (point-min))
(let (p)
(setq p (org-find-entry-with-id ,issue-id))
(unless p
(error "issue %s not found!" ,issue-id))
(goto-char p)
(org-narrow-to-subtree)
,@body))))
(defmacro ensure-on-todo (&rest body)
"Make sure we are on an todo heading"
`(save-excursion
(save-restriction
(let ((continue t)
(on-todo nil))
(while continue
(when (org-get-todo-state)
(setq continue nil on-todo t))
(unless (and continue (org-up-heading-safe))
(setq continue nil)))
(if (not on-todo)
(error "TODO not found")
(org-narrow-to-subtree)
,@body)))))
(defmacro ensure-on-comment (&rest body)
"Make sure we are on a comment heading"
`(save-excursion
(org-back-to-heading)
(forward-thing 'whitespace)
(unless (looking-at "Comment:")
(error "Not on a comment region!"))
(save-restriction
(org-narrow-to-subtree)
,@body)))
(defun hash (dlist dhash)
"Convert a list to hash and return that hash."
(progn (new-list-to-hash dlist dhash)
dhash))
(defun new-list-to-hash (dlist dhash)
"Given a nested list, convert it into a hash-table."
(mapc (lambda (x)
(puthash (car x)
(if (and (equal 'cons (type-of (cdr x)))
(not (equal 'string (type-of (car (cdr x))))))
(hash (car (cdr x)) (make-hash-table :test 'equal))
(cdr x))
dhash))
dlist))
(defun org-jira-kill-buffer-hook ()
"Prompt before killing buffer."
(if (and org-jira-buffer-kill-prompt
(not (buffer-file-name)))
(if (y-or-n-p "Save Jira?")
(progn
(save-buffer)
(org-jira-save-details (org-jira-parse-entry) nil
(y-or-n-p "Published?"))))))
(defvar org-jira-entry-mode-map
(let ((org-jira-map (make-sparse-keymap)))
(define-key org-jira-map (kbd "C-c pg") 'org-jira-get-projects)
(define-key org-jira-map (kbd "C-c ib") 'org-jira-browse-issue)
(define-key org-jira-map (kbd "C-c ig") 'org-jira-get-issues)
(define-key org-jira-map (kbd "C-c ih") 'org-jira-get-issues-headonly)
(define-key org-jira-map (kbd "C-c if") 'org-jira-get-issues-from-filter-headonly)
(define-key org-jira-map (kbd "C-c iF") 'org-jira-get-issues-from-filter)
(define-key org-jira-map (kbd "C-c iu") 'org-jira-update-issue)
(define-key org-jira-map (kbd "C-c iw") 'org-jira-progress-issue)
(define-key org-jira-map (kbd "C-c ir") 'org-jira-refresh-issue)
(define-key org-jira-map (kbd "C-c ic") 'org-jira-create-issue)
(define-key org-jira-map (kbd "C-c ik") 'org-jira-copy-current-issue-key)
(define-key org-jira-map (kbd "C-c sc") 'org-jira-create-subtask)
(define-key org-jira-map (kbd "C-c sg") 'org-jira-get-subtasks)
(define-key org-jira-map (kbd "C-c cu") 'org-jira-update-comment)
(define-key org-jira-map (kbd "C-c tj") 'org-jira-todo-to-jira)
org-jira-map))
;;;###autoload
(define-minor-mode org-jira-mode
"Toggle org-jira mode.
With no argument, the mode is toggled on/off.
Non-nil argument turns mode on.
Nil argument turns mode off.
Commands:
\\{org-jira-entry-mode-map}
Entry to this mode calls the value of `org-jira-mode-hook'."
:init-value nil
:lighter " jira"
:group 'org-jira
:keymap org-jira-entry-mode-map
(if org-jira-mode
(run-mode-hooks 'org-jira-mode-hook)))
;;;###autoload
(defun org-jira-get-projects ()
"Get list of projects."
(interactive)
(let ((projects-file (expand-file-name "projects-list.org" org-jira-working-dir)))
(or (find-buffer-visiting projects-file)
(find-file projects-file))
(org-jira-mode t)
(save-excursion
(let* ((oj-projs (jiralib-get-projects)))
(mapc (lambda (proj)
(let* ((proj-key (cdr (assoc 'key proj)))
(proj-headline (format "Project: [[file:%s.org][%s]]" proj-key proj-key)))
(save-restriction
(widen)
(goto-char (point-min))
(show-all)
(setq p (org-find-exact-headline-in-buffer proj-headline))
(if (and p (>= p (point-min))
(<= p (point-max)))
(progn
(goto-char p)
(org-narrow-to-subtree)
(end-of-line))
(goto-char (point-max))
(unless (looking-at "^")
(insert "\n"))
(insert "* ")
(insert proj-headline)
(org-narrow-to-subtree))
(org-entry-put (point) "name" (cdr (assoc 'name proj)))
(org-entry-put (point) "key" (cdr (assoc 'key proj)))
(org-entry-put (point) "lead" (cdr (assoc 'lead proj)))
(org-entry-put (point) "ID" (cdr (assoc 'id proj)))
(org-entry-put (point) "url" (cdr (assoc 'url proj))))))
oj-projs)))))
(defun org-jira-get-issue-components (issue)
"Return the components the issue belongs to."
(mapconcat (lambda (comp)
(cdr (assoc 'name comp)))
(cdr (assoc 'components issue)) ", "))
(defun org-jira-transform-time-format (jira-time-str)
"Convert \"2012-01-09T08:59:15.000Z\" to \"2012-01-09 16:59:15\", with my timezone being +0800"
(condition-case ()
(format-time-string "%Y-%m-%d %T"
(apply
'encode-time
(parse-time-string (replace-regexp-in-string "T\\|\\.000" " " jira-time-str))))
(error jira-time-str)))
(defun org-jira-get-comment-val (key comment)
"Return the value associated with KEY of COMMENT"
(let ((tmp (or (cdr (assoc key comment)) "")))
(cond ((or (eq key 'created) (eq key 'updated))
(org-jira-transform-time-format tmp))
(t
tmp))))
(defun org-jira-get-issue-val (key issue)
"Return the value associated with key KEY of issue ISSUE."
(let ((tmp (or (cdr (assoc key issue)) "")))
(cond ((eq key 'components)
(org-jira-get-issue-components issue))
((or (eq key 'created) (eq key 'updated))
(org-jira-transform-time-format tmp))
((eq key 'status)
(cdr (assoc tmp (jiralib-get-statuses))))
((eq key 'resolution)
(cdr (assoc tmp (jiralib-get-resolutions))))
((eq key 'type)
(cdr (assoc tmp (jiralib-get-issue-types))))
((eq key 'priority)
(cdr (assoc tmp (jiralib-get-prioritys))))
((eq key 'description)
(org-jira-strip-string tmp))
(t
tmp))))
(defvar org-jira-jql-history nil)
(defun org-jira-get-issue-list ()
"Get list of issues, using jql (jira query language). Default
is unresolved issues assigned to current login user; with a
prefix argument you are given the chance to enter your own jql."
(let ((jql org-jira-default-jql))
(when current-prefix-arg
(setq jql (read-string "Jql: "
(if org-jira-jql-history
(car org-jira-jql-history)
"assignee = currentUser() and resolution = unresolved")
'org-jira-jql-history
"assignee = currentUser() and resolution = unresolved")))
(list (jiralib-do-jql-search jql))))
(defun org-jira-get-issue-by-id (id)
"Get an issue by it's id"
(interactive (list (read-string "Issue ID: " "IMINAN-" 'org-jira-issue-id-history)))
(push id org-jira-issue-id-history)
(let ((jql (format "id = %s" id)))
(jiralib-do-jql-search jql)))
;;;###autoload
(defun org-jira-get-issues-headonly (issues)
"Get list of issues assigned to you and unresolved, head
only. With a prefix argument, allow you to customize the jql. See `org-jira-get-issue-list'"
(interactive
(org-jira-get-issue-list))
(let* ((issues-file (expand-file-name "issues-headonly.org" org-jira-working-dir))
(issues-headonly-buffer (or (find-buffer-visiting issues-file)
(find-file issues-file))))
(with-current-buffer issues-headonly-buffer
(widen)
(delete-region (point-min) (point-max))
(mapc (lambda (issue)
(let ((issue-id (org-jira-get-issue-val 'key issue))
(issue-summary (org-jira-get-issue-val 'summary issue)))
(insert (format "- [jira:%s] %s\n" issue-id issue-summary))))
issues))
(switch-to-buffer issues-headonly-buffer)))
(defun org-jira-get-issue ()
(interactive)
(org-jira-get-issues (call-interactively 'org-jira-get-issue-by-id)))
;;;###autoload
(defun org-jira-get-issues (issues)
"Get list of issues. Default is get unfinished issues assigned
to you, but you can customize jql with a prefix argument. See
`org-jira-get-issue-list'"
(interactive
(org-jira-get-issue-list))
(let (project-buffer)
(mapc (lambda (issue)
(let* ((proj-key (cdr (assoc 'project issue)))
(issue-id (cdr (assoc 'key issue)))
(issue-summary (cdr (assoc 'summary issue)))
(issue-headline issue-summary))
(let ((project-file (expand-file-name (concat proj-key ".org") org-jira-working-dir)))
(setq project-buffer (or (find-buffer-visiting project-file)
(find-file project-file)))
(with-current-buffer project-buffer
(org-jira-mode t)
(widen)
(show-all)
(goto-char (point-min))
(setq p (org-find-entry-with-id issue-id))
(save-restriction
(if (and p (>= p (point-min))
(<= p (point-max)))
(progn
(goto-char p)
(forward-thing 'whitespace)
(kill-line))
(goto-char (point-max))
(unless (looking-at "^")
(insert "\n"))
(insert "* "))
(let ((status (org-jira-get-issue-val 'status issue)))
(insert (concat (cond (org-jira-use-status-as-todo
(upcase (replace-regexp-in-string " " "-" status)))
((member status '("Closed" "Resolved")) "DONE")
("TODO")) " "
issue-headline)))
(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 (and val (not (string= val "")))
(org-entry-put (point) (symbol-name entry) val))))
'(assignee reporter type priority resolution status components created updated))
(org-entry-put (point) "ID" (cdr (assoc 'key issue)))
(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))
(insert entry-heading "\n"))
(insert (replace-regexp-in-string "^" " " (org-jira-get-issue-val heading-entry issue))))))
'(description))
(org-jira-update-comments-for-current-issue)
)))))
issues)
(switch-to-buffer project-buffer)))
;;;###autoload
(defun org-jira-update-comment ()
"update a comment for the current issue"
(interactive)
(let* ((issue-id (org-jira-get-from-org 'issue 'key))
(comment-id (org-jira-get-from-org 'comment 'id))
(comment (replace-regexp-in-string "^ " "" (org-jira-get-comment-body comment-id))))
(if comment-id
(jiralib-edit-comment comment-id comment)
(jiralib-add-comment issue-id comment)
(org-jira-delete-current-comment)
(org-jira-update-comments-for-current-issue))))
(defun org-jira-delete-current-comment ()
"delete the current comment"
(ensure-on-comment
(delete-region (point-min) (point-max))))
;;;###autoload
(defun org-jira-copy-current-issue-key ()
"Copy the current issue's key into clipboard"
(interactive)
(let ((issue-id (org-jira-get-from-org 'issue 'key)))
(with-temp-buffer
(insert issue-id)
(kill-region (point-min) (point-max)))))
(defun org-jira-update-comments-for-current-issue ()
(let* ((issue-id (org-jira-get-from-org 'issue 'key))
(comments (jiralib-get-comments issue-id)))
(mapc (lambda (comment)
(ensure-on-issue-id issue-id
(let* ((comment-id (cdr (assoc 'id comment)))
(comment-author (or (car (rassoc
(cdr (assoc 'author comment))
jira-users))
(cdr (assoc 'author comment))))
(comment-headline (format "Comment: %s" comment-author)))
(setq p (org-find-entry-with-id comment-id))
(if (and p (>= p (point-min))
(<= p (point-max)))
(progn
(goto-char p)
(org-narrow-to-subtree)
(delete-region (point-min) (point-max))))
(goto-char (point-max))
(unless (looking-at "^")
(insert "\n"))
(insert "** ")
(insert comment-headline "\n")
(org-narrow-to-subtree)
(org-entry-put (point) "ID" comment-id)
(let ((created (org-jira-get-comment-val 'created comment))
(updated (org-jira-get-comment-val 'updated comment)))
(org-entry-put (point) "created" created)
(unless (string= created updated)
(org-entry-put (point) "updated" updated)))
(goto-char (point-max))
(insert (replace-regexp-in-string "^" " " (or (cdr (assoc 'body comment)) ""))))))
(mapcan (lambda (comment) (if (string= (cdr (assoc 'author comment))
"admin")
nil
(list comment)))
comments))))
;;;###autoload
(defun org-jira-update-issue ()
"update an issue"
(interactive)
(let ((issue-id (org-jira-parse-issue-id)))
(if issue-id
(org-jira-update-issue-details issue-id)
(error "not on an issue"))))
;;;###autoload
(defun org-jira-todo-to-jira ()
"convert an ordinary todo item to a jira ticket"
(interactive)
(ensure-on-todo
(when (org-jira-parse-issue-id)
(error "Already on jira ticket"))
(save-excursion (org-jira-create-issue
(org-jira-read-project)
(org-jira-read-issue-type)
(org-get-heading t t)
(org-get-entry)))
(delete-region (point-min) (point-max))))
;;;###autoload
(defun org-jira-get-subtasks ()
"get subtasks for the current issue"
(interactive)
(ensure-on-issue
(org-jira-get-issues-headonly (jiralib-do-jql-search (format "parent = %s" (org-jira-parse-issue-id))))))
(defvar org-jira-project-read-history nil)
(defvar org-jira-priority-read-history nil)
(defvar org-jira-type-read-history nil)
(defun org-jira-read-project ()
"Read project name"
(completing-read
"Project: "
(jiralib-make-list (jiralib-get-projects) 'key)
nil
t
(car org-jira-project-read-history)
'org-jira-project-read-history))
(defun org-jira-read-priority ()
"Read priority name"
(completing-read
"Priority: "
(mapcar 'cdr (jiralib-get-prioritys))
nil
t
(car org-jira-priority-read-history)
'org-jira-priority-read-history))
(defun org-jira-read-issue-type ()
"Read issue type name"
(completing-read
"Type: "
(mapcar 'cdr (jiralib-get-issue-types))
nil
t
(car org-jira-type-read-history)
'org-jira-type-read-history))
(defun org-jira-read-subtask-type ()
"Read issue type name"
(completing-read
"Type: "
(mapcar 'cdr (jiralib-get-subtask-types))
nil
t
(car org-jira-type-read-history)
'org-jira-type-read-history))
(defun org-jira-get-issue-struct (project type summary description)
"helper function for the struct used in creating issues and subtasks"
(if (or (equal project "")
(equal type "")
(equal summary ""))
(error "Must provide all information!"))
(let* ((project-components (jiralib-get-components project))
(user (completing-read "Assignee: " (mapcar 'car jira-users)))
(priority (car (rassoc (org-jira-read-priority) (jiralib-get-prioritys))))
(ticket-struct (list (cons 'project project)
(cons 'type (car (rassoc type (if (and (boundp 'parent-id) parent-id)
(jiralib-get-subtask-types)
(jiralib-get-issue-types)))))
(cons 'summary (format "%s%s" summary
(if (and (boundp 'parent-id) parent-id)
(format " (subtask of [jira:%s])" parent-id)
"")))
(cons 'description description)
(cons 'priority priority)
(cons 'assignee (cdr (assoc user jira-users))))))
ticket-struct))
;;;###autoload
(defun org-jira-create-issue (project type summary description)
"create an issue"
(interactive (list (org-jira-read-project)
(org-jira-read-issue-type)
(read-string "Summary: ")
(read-string "Description: ")))
(if (or (equal project "")
(equal type "")
(equal summary ""))
(error "Must provide all information!"))
(let* ((parent-id nil)
(ticket-struct (org-jira-get-issue-struct project type summary description)))
(org-jira-get-issues (list (jiralib-create-issue ticket-struct)))))
;;;###autoload
(defun org-jira-create-subtask (project type summary description)
"create an subtask issue"
(interactive (ensure-on-issue (list (org-jira-read-project)
(org-jira-read-subtask-type)
(read-string "Summary: ")
(read-string "Description: "))))
(if (or (equal project "")
(equal type "")
(equal summary ""))
(error "Must provide all information!"))
(let* ((parent-id (org-jira-parse-issue-id))
(ticket-struct (org-jira-get-issue-struct project type summary description)))
(org-jira-get-issues (list (jiralib-create-subtask ticket-struct parent-id)))))
(defun org-jira-strip-string (str)
"remove the beginning and ending white space for a string"
(replace-regexp-in-string "\\`\n+\\|\n+\\'" "" str))
(defun org-jira-get-issue-val-from-org (key)
(ensure-on-issue
(cond ((eq key 'description)
(org-goto-first-child)
(forward-thing 'whitespace)
(if (looking-at "description: ")
(org-jira-strip-string (org-get-entry))
(error "Can not find description field for this issue")))
((eq key 'summary)
(ensure-on-issue
(org-get-heading t t)))
(t
(when (symbolp key)
(setq key (symbol-name key)))
(when (string= key "key")
(setq key "ID"))
(or (org-entry-get (point) key)
"")))))
(defvar org-jira-actions-history nil)
(defun org-jira-read-action (actions)
"Read issue workflow progress actions."
(let ((action (completing-read
"Action: "
(mapcar 'cdr actions)
nil
t
(car org-jira-actions-history)
'org-jira-actions-history)))
(car (rassoc action actions))))
(defvar org-jira-fields-history nil)
(defun org-jira-read-field (fields)
"Read (custom) fields for workflow progress"
(let ((field-desc (completing-read
"More fields to set: "
(cons "Thanks, no more fields are *required*." (mapcar 'cdr fields))
nil
t
nil
'org-jira-fields-history))
field-name)
(setq field-name (car (rassoc field-desc fields)))
(if field-name
(intern field-name)
field-name)))
(defvar org-jira-resolution-history nil)
(defun org-jira-read-resolution ()
"Read issue workflow progress resolution."
(let ((resolution (completing-read
"Resolution: "
(mapcar 'cdr (jiralib-get-resolutions))
nil
t
(car org-jira-resolution-history)
'org-jira-resolution-history)))
(car (rassoc resolution (jiralib-get-resolutions)))))
;;;###autoload
(defun org-jira-refresh-issue ()
"Refresh issue from jira to org"
(interactive)
(ensure-on-issue
(let* ((issue-id (org-jira-id)))
(org-jira-get-issues (list (jiralib-get-issue issue-id))))))
(defvar org-jira-fields-values-history nil)
;;;###autoload
(defun org-jira-progress-issue ()
"Progress issue workflow"
(interactive)
(ensure-on-issue
(let* ((issue-id (org-jira-id))
(actions (jiralib-get-available-actions issue-id))
(action (org-jira-read-action actions))
(fields (jiralib-get-fields-for-action issue-id action))
(field-key)
(custom-fields-collector nil)
(custom-fields (progn
;delete those elements in fields, which has
;already been set in custom-fields-collector
(while fields
(setq fields (delete-if (lambda (strstr)
(member-if (lambda (symstr)
(string= (car strstr) (symbol-name (car symstr))))
custom-fields-collector))
fields))
(setq field-key (org-jira-read-field fields))
(if (not field-key)
(setq fields nil)
(setq custom-fields-collector
(cons
(cons field-key
(if (eq field-key 'resolution)
(org-jira-read-resolution)
(completing-read
(format "Please enter %s's value: "
(cdr (assoc (symbol-name field-key) fields)))
org-jira-fields-values-history
nil
nil
nil
'org-jira-fields-values-history)))
custom-fields-collector))))
custom-fields-collector))
(issue (jiralib-progress-workflow-action issue-id action custom-fields)))
(org-jira-get-issues (list issue)))))
(defun org-jira-update-issue-details (issue-id)
(ensure-on-issue-id
issue-id
(let* ((org-issue-components (org-jira-get-issue-val-from-org 'components))
(org-issue-description (replace-regexp-in-string "^ " "" (org-jira-get-issue-val-from-org 'description)))
(org-issue-resolution (org-jira-get-issue-val-from-org 'resolution))
(org-issue-priority (org-jira-get-issue-val-from-org 'priority))
(org-issue-type (org-jira-get-issue-val-from-org 'type))
(org-issue-assignee (org-jira-get-issue-val-from-org 'assignee))
(org-issue-status (org-jira-get-issue-val-from-org 'status))
(issue (jiralib-get-issue issue-id))
(project (org-jira-get-issue-val 'project issue))
(project-components (jiralib-get-components project)))
(jiralib-update-issue issue-id ;(jiralib-update-issue "FB-1" '((components . ["10001" "10000"])))
(list (cons
'components
(apply 'vector
(mapcan
(lambda (item)
(let ((comp-id (car (rassoc item project-components))))
(if comp-id
(list comp-id)
nil)))
(split-string org-issue-components ",\\s *"))))
(cons 'priority (car (rassoc org-issue-priority (jiralib-get-prioritys))))
(cons 'description org-issue-description)
(cons 'assignee org-issue-assignee)
(cons 'summary (org-jira-get-issue-val-from-org 'summary))))
(org-jira-get-issues (list (jiralib-get-issue issue-id))))))
(defun org-jira-parse-issue-id ()
"get issue id from org text"
(save-excursion
(let ((continue t)
issue-id)
(while continue
(when (string-match (jiralib-get-issue-regexp)
(or (setq issue-id (org-entry-get (point) "ID"))
""))
(setq continue nil))
(unless (and continue (org-up-heading-safe))
(setq continue nil)))
issue-id)))
(defun org-jira-get-from-org (type entry)
"get org ENTRY for heading of TYPE.
TYPE can be 'issue, or 'comment.
ENTRY will vary with regard to the TYPE, if it is a symbol, it will be converted to string"
(when (symbolp entry)
(setq entry (symbol-name entry)))
(if (eq type 'issue)
(org-jira-get-issue-val-from-org entry)
(if (eq type 'comment)
(org-jira-get-comment-val-from-org entry)
(error "unknown type %s" type))))
(defun org-jira-get-comment-val-from-org (entry)
"get the jira issue field value for ENTRY of current comment item"
(ensure-on-comment
(when (symbolp entry)
(setq entry (symbol-name entry)))
(when (string= entry "id")
(setq entry "ID"))
(org-entry-get (point) entry)))
(defun org-jira-get-comment-body (&optional comment-id)
(ensure-on-comment
(goto-char (point-min))
(org-entry-put (point) "ID" comment-id)
(search-forward ":END:")
(forward-line)
(org-jira-strip-string (buffer-substring-no-properties (point) (point-max)))))
(defun org-jira-id ()
"get the ID entry for the current HEADING."
(org-entry-get (point) "ID"))
;;;###autoload
(defun org-jira-browse-issue ()
"Open the current issue in external browser."
(interactive)
(ensure-on-issue
(browse-url (concat jiralib-url "/browse/" (org-jira-id)))))
;;;###autoload
(defun org-jira-get-issues-from-filter (filter)
"Get issues from filter which are jql created and saved on the
server side. Provide this command in case some users are not able
to use client side jql (maybe because of Jira server version?)."
(interactive
(list (completing-read "Filter: " (mapcar 'cdr (jiralib-get-saved-filters)))))
(org-jira-get-issues (jiralib-get-issues-from-filter (car (rassoc filter (jiralib-get-saved-filters))))))
;;;###autoload
(defun org-jira-get-issues-from-filter-headonly (filter)
"Get issues *head only* from saved filter. See `org-jira-get-issues-from-filter'"
(interactive
(list (completing-read "Filter: " (mapcar 'cdr (jiralib-get-saved-filters)))))
(org-jira-get-issues-headonly (jiralib-get-issues-from-filter (car (rassoc filter (jiralib-get-saved-filters))))))
(org-add-link-type "jira" 'org-jira-open)
(defun org-jira-open (path)
"Open a Jira Link from PATH."
(org-jira-get-issues (list (jiralib-get-issue path))))
(provide 'org-jira)
;;; org-jira.el ends here