-
Notifications
You must be signed in to change notification settings - Fork 130
/
org-jira.el
2629 lines (2311 loc) · 105 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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; org-jira.el --- Syncing between Jira and Org-mode. -*- lexical-binding: t -*-
;; Copyright (C) 2016-2022 Matthew Carter <m@ahungry.com>
;; Copyright (C) 2011 Bao Haojun
;;
;; Authors:
;; Matthew Carter <m@ahungry.com>
;; Bao Haojun <baohaojun@gmail.com>
;;
;; Maintainer: Matthew Carter <m@ahungry.com>
;; URL: https://github.com/ahungry/org-jira
;; Version: 4.3.3
;; Keywords: ahungry jira org bug tracker
;; Package-Requires: ((emacs "24.5") (cl-lib "0.5") (request "0.2.0") (dash "2.14.1"))
;; 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/> or 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.
;;; News:
;;;; Changes in 4.4.2
;; - Fix for org-insert-subheading behavior change in org 9.7+ in render-issues
;;;; Changes in 4.4.1
;; - Fix tag (4.3.3 was out of order - we had a 4.4.0 on repo)
;; - Fix for some crazy scoping issue in the org-jira-get-issue-val-from-org function
;;;; Changes in 4.3.3:
;; - Address issue with assignee property being removed when Unassigned
;;;; Changes in 4.3.2:
;; - Fixes issues with org-jira-add-comment and org-jira-update-comment
;;;; Changes in 4.3.1:
;; - Fix to make custom-jql results sync worklogs properly.
;;;; Changes in 4.3.0:
;; - Allow org-jira-set-issue-reporter call to dynamically set this value.
;;;; Changes in 4.1.0:
;; - Allow custom-jql to be specified and render in special files (see: README.md).
;;;; Changes in 4.0.0:
;; - Introduce SDK type for handling records vs random alist structures.
;;;; Changes since 3.1.0:
;; - Fix how we were ruining the kill-ring with kill calls.
;;;; Changes since 3.0.0:
;; - Add new org-jira-add-comment call (C-c c c)
;;;; Changes since 2.8.0:
;; - New version 3.0.0 deprecates old filing mechanism and files
;; all of the changes under the top level ticket header.
;; - If you want other top level headers in the same file, this should
;; work now, as long as they come after the main project one.
;;;; Changes since 2.7.0:
;; - Clean up multi-buffer handling, disable attachments call until
;; - refresh is compatible with it.
;;;; Changes since 2.6.3:
;; - Insert worklog import filter in the existing org-jira-update-worklogs-for-current-issue function
;; - Sync up org-clocks and worklogs! Set org-jira-worklog-sync-p to nil to avoid.
;;;; Changes since 2.6.1:
;; - Fix bug with getting all issues when worklog is an error trigger.
;;;; Changes since 2.5.4:
;; - Added new org-jira-refresh-issues-in-buffer call and binding
;;;; Changes since 2.5.3:
;; - Re-introduce the commit that introduced a break into Emacs 25.1.1 list/array push
;; The commit caused updates/comment updates to fail when a blank list of components
;; was present - it will now handle both cases (full list, empty list).
;;;; Changes since 2.5.2:
;; - Revert a commit that introduced a break into Emacs 25.1.1 list/array push
;; The commit caused updates/comment updates to fail
;;;; Changes since 2.5.1:
;; - Only set duedate if a DEADLINE is present in the tags and predicate is t
;;;; Changes since 2.5.0:
;; - Allow overriding the org property names with new defcustom
;;;; Changes since 2.4.0:
;; - Fix many deprecation/warning issues
;; - Fix error with allow-other-keys not being wrapped in cl-function
;;;; Changes since 2.3.0:
;; - Integration with org deadline and Jira due date fields
;;;; Changes since 2.2.0:
;; - Selecting issue type based on project key for creating issues
;;;; Changes since 2.1.0:
;; - Allow changing to unassigned user
;; - Add new shortcut for quick assignment
;;;; Changes since 2.0.0:
;; - Change statusCategory to status value
;; - Clean out some redundant code
;; - Add ELPA tags in keywords
;;;; Changes since 1.0.1:
;; - Converted many calls to async
;; - Removed minor annoyances (position resets etc.)
;;; Code:
(eval-when-compile (require 'cl))
(require 'org)
(require 'org-clock)
(require 'cl-lib)
(require 'url)
(require 'ls-lisp)
(require 'dash)
(require 'jiralib)
(require 'org-jira-sdk)
(defconst org-jira-version "4.4.2"
"Current version of org-jira.el.")
(defgroup org-jira nil
"Customization group for org-jira."
:tag "Org JIRA"
:group 'org)
(defcustom org-jira-working-dir "~/.org-jira"
"Folder under which to store org-jira working files."
:group 'org-jira
:type 'directory)
(defcustom org-jira-project-filename-alist nil
"Alist translating project keys to filenames.
Each element has a structure:
(PROJECT-KEY . NEW-FILE-NAME)
where both are strings. NEW-FILE-NAME is relative to
`org-jira-working-dir'."
:group 'org-jira
:type '(alist :key-type (string :tag "Project key")
:value-type (string :tag "File name for this project")))
(defcustom org-jira-default-jql
"assignee = currentUser() and resolution = unresolved ORDER BY
priority DESC, created ASC"
"Default jql for querying your Jira tickets."
:group 'org-jira
:type 'string)
(defcustom org-jira-ignore-comment-user-list
'("admin")
"Jira usernames that should have comments ignored."
:group 'org-jira
:type '(repeat (string :tag "Jira username:")))
(defcustom org-jira-reverse-comment-order nil
"If non-nil, order comments from most recent to least recent."
:group 'org-jira
:type 'boolean)
(defcustom org-jira-done-states
'("Closed" "Resolved" "Done")
"Jira states that should be considered as DONE for `org-mode'."
:group 'org-jira
:type '(repeat (string :tag "Jira state name:")))
(defcustom org-jira-users
'(("Full Name" . "account-id"))
"A list of displayName and key pairs."
:group 'org-jira
:type 'list)
(defcustom org-jira-progress-issue-flow
'(("To Do" . "In Progress")
("In Progress" . "Done"))
"Quickly define a common issue flow."
:group 'org-jira
:type 'list)
(defcustom org-jira-property-overrides (list)
"An assoc list of property tag overrides.
The KEY . VAL pairs should both be strings.
For instance, to change the :assignee: property in the org :PROPERTIES:
block to :WorkedBy:, you can set this as such:
(setq org-jira-property-overrides (list (cons \"assignee\" \"WorkedBy\")))
or simply:
(add-to-list (quote org-jira-property-overrides)
(cons (\"assignee\" \"WorkedBy\")))
This will work for most any of the properties, with the
exception of ones with unique functionality, such as:
- deadline
- summary
- description"
:group 'org-jira
:type 'list)
(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 override 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)
(defcustom org-jira-deadline-duedate-sync-p t
"Keep org deadline and jira duedate fields synced.
You may wish to set this to nil if you track org deadlines in
your buffer that you do not want to send back to your Jira
instance."
:group 'org-jira
:type 'boolean)
(defcustom org-jira-worklog-sync-p t
"Keep org clocks and jira worklog fields synced.
You may wish to set this to nil if you track org clocks in
your buffer that you do not want to send back to your Jira
instance."
:group 'org-jira
:type 'boolean)
(defcustom org-jira-download-dir "~/Downloads"
"Name of the default jira download library."
:group 'org-jira
:type 'string)
(defcustom org-jira-download-ask-override t
"Ask before overriding tile."
:group 'org-jira
:type 'boolean)
(defcustom org-jira-jira-status-to-org-keyword-alist nil
"Custom alist of jira status stored in car and `org-mode' keyword stored in cdr."
:group 'org-jira
:type '(alist :key-type string :value-type string))
(defcustom org-jira-priority-to-org-priority-omit-default-priority nil
"Whether to omit insertion of priority when it matches the default.
When set to t, will omit the insertion of the matched value from
`org-jira-priority-to-org-priority-alist' when it matches the `org-default-priority'."
:group 'org-jira
:type 'boolean)
(defcustom org-jira-priority-to-org-priority-alist nil
"Alist mapping jira priority keywords to `org-mode' priority cookies.
A sample value might be
(list (cons \"High\" ?A)
(cons \"Medium\" ?B)
(cons \"Low\" ?C)).
See `org-default-priority' for more info."
:group 'org-jira
:type '(alist :key-type string :value-type character))
(defcustom org-jira-boards-default-limit 50
"Default limit for number of issues retrieved from agile boards."
:group 'org-jira
:type 'integer)
;; FIXME: Issue with using this - issues are grouped under a headline incorrectly.
(defcustom org-jira-custom-jqls
'(
(:jql " assignee = currentUser() and createdDate < '2019-01-01' order by created DESC "
:limit 100
:filename "last-years-work")
(:jql " assignee = currentUser() and createdDate >= '2019-01-01' order by created DESC "
:limit 100
:filename "this-years-work")
)
"A list of plists with :jql and :filename keys to run arbitrary user JQL."
:group 'org-jira
:type '(alist :value-type plist))
(defcustom org-jira-download-comments t
"Set to nil if you don't want to update comments during issue rendering."
:group 'org-jira
:type 'boolean)
(defcustom org-jira-update-issue-details-include-reporter t
"For Jira Cloud API we will get an error if `reporter' is sent with an update request."
:group 'org-jira
:type 'string)
(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)
(defvar org-jira-mode-hook nil
"Hook to run upon entry into mode.")
(defvar org-jira-issue-id-history '()
"Prompt history for issue id.")
(defvar org-jira-fixversion-id-history '()
"Prompt history for fixversion id.")
(defvar org-jira-verbosity 'debug)
(defun org-jira-log (s) (when (eq 'debug org-jira-verbosity) (message "%s" s)))
(defmacro ensure-on-issue (&rest body)
"Make sure we are on an issue heading, before executing BODY."
(declare (debug t)
(indent 0))
`(save-excursion
(save-restriction
(widen)
(unless (looking-at "^\\*\\* ")
(search-backward-regexp "^\\*\\* " nil t)) ; go to top heading
(let ((org-jira-id (org-jira-id)))
(unless (and org-jira-id (string-match (jiralib-get-issue-regexp) (downcase org-jira-id)))
(error "Not on an issue region!")))
,@body)))
(defmacro org-jira-with-callback (&rest body)
"Simpler way to write the data BODY callbacks."
(declare (debug t)
(indent 0))
`(lambda (&rest request-response)
(declare (ignore cb-data))
(let ((cb-data (cl-getf request-response :data)))
,@body)))
(defmacro org-jira-freeze-ui (&rest body)
"Freeze the UI layout for the user as much as possible."
(declare (debug t)
(indent 0))
`(save-excursion
(save-restriction
(widen)
(org-save-outline-visibility t
(outline-show-all)
,@body))))
(defvar org-jira-proj-key-override nil
"String. An override for the proj-key. Set to nil to restore old behavior.")
;; We want some hooking system to override default-jql + this.
(defun org-jira--get-proj-key (issue-id)
"Get the proper proj-key. Typically derived from ISSUE-ID."
(if org-jira-proj-key-override org-jira-proj-key-override
(replace-regexp-in-string "-.*" "" issue-id)))
(defun org-jira--get-proj-key-from-issue (Issue)
"Get the proper proj-key from an ISSUE instance."
(oref Issue filename))
;; TODO: Merge these 3 ensure macros (or, scrap all but ones that work on Issue)
(defmacro ensure-on-issue-id (issue-id &rest body)
"Just do some work on ISSUE-ID, execute BODY."
(declare (debug t)
(indent 1))
(let ((issue-id-var (make-symbol "issue-id")))
`(let* ((,issue-id-var ,issue-id)
(proj-key (org-jira--get-proj-key ,issue-id-var))
(project-file (org-jira--get-project-file-name proj-key))
(project-buffer (or (find-buffer-visiting project-file)
(find-file project-file))))
(with-current-buffer project-buffer
(org-jira-freeze-ui
(let ((p (org-find-entry-with-id ,issue-id-var)))
(unless p (error "Issue %s not found!" ,issue-id-var))
(goto-char p)
(org-narrow-to-subtree)
,@body))))))
(defmacro ensure-on-issue-id-with-filename (issue-id filename &rest body)
"Just do some work on ISSUE-ID, execute BODY."
(declare (debug t)
(indent 1))
(let ((issue-id-var (make-symbol "issue-id"))
(filename-var (make-symbol "filename")))
`(let* ((,issue-id-var ,issue-id)
(,filename-var ,filename)
(proj-key ,filename-var)
(project-file (org-jira--get-project-file-name proj-key))
(project-buffer (or (find-buffer-visiting project-file)
(find-file project-file))))
(with-current-buffer project-buffer
(org-jira-freeze-ui
(let ((p (org-find-entry-with-id ,issue-id-var)))
(unless p (error "Issue %s not found!" ,issue-id-var))
(goto-char p)
(org-narrow-to-subtree)
,@body))))))
(defmacro ensure-on-issue-Issue (Issue &rest body)
"Just do some work on ISSUE, execute BODY."
(declare (debug t)
(indent 1))
(let ((Issue-var (make-symbol "Issue")))
`(let ((,Issue-var ,Issue))
(with-slots (issue-id) ,Issue-var
(let* ((proj-key (org-jira--get-proj-key-from-issue ,Issue-var))
(project-file (org-jira--get-project-file-name proj-key))
(project-buffer (or (find-buffer-visiting project-file)
(find-file project-file))))
(with-current-buffer project-buffer
(org-jira-freeze-ui
(let ((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, before executing BODY."
(declare (debug t)
(indent 0))
`(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, before executing BODY."
(declare (debug t)
(indent 0))
`(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)))
(defmacro ensure-on-worklog (&rest body)
"Make sure we are on a worklog heading, before executing BODY."
(declare (debug t)
(indent 0))
`(save-excursion
(org-back-to-heading)
(forward-thing 'whitespace)
(unless (looking-at "Worklog:")
(error "Not on a worklog region!"))
(save-restriction
(org-narrow-to-subtree)
,@body)))
(defun org-jira--ensure-working-dir ()
"Ensure that the org-jira-working-dir exists"
(unless (file-exists-p org-jira-working-dir)
(error (format "org-jira directory does not exist! Run (make-directory \"%s\")" org-jira-working-dir))
)
org-jira-working-dir
)
(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 bg") 'org-jira-get-boards)
(define-key org-jira-map (kbd "C-c iv") 'org-jira-get-issues-by-board)
(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 ij") 'org-jira-get-issues-from-custom-jql)
(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 il") 'org-jira-update-issue-labels)
(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 in") 'org-jira-progress-issue-next)
(define-key org-jira-map (kbd "C-c ia") 'org-jira-assign-issue)
;(define-key org-jira-map (kbd "C-c isr") 'org-jira-set-issue-reporter)
(define-key org-jira-map (kbd "C-c ir") 'org-jira-refresh-issue)
(define-key org-jira-map (kbd "C-c iR") 'org-jira-refresh-issues-in-buffer)
(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 cc") 'org-jira-add-comment)
(define-key org-jira-map (kbd "C-c cu") 'org-jira-update-comment)
(define-key org-jira-map (kbd "C-c wu") 'org-jira-update-worklogs-from-org-clocks)
(define-key org-jira-map (kbd "C-c tj") 'org-jira-todo-to-jira)
(define-key org-jira-map (kbd "C-c if") 'org-jira-get-issues-by-fixversion)
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
(progn
(set (make-local-variable 'org-element-use-cache) nil)
(run-mode-hooks 'org-jira-mode-hook))
(progn
(kill-local-variable 'org-element-use-cache))))
(defun org-jira-maybe-activate-mode ()
"Having hooks can be an expensive operation, and they are invoked each
time the mode is started - we should only ever re-activate the mode if
it isn't already on."
(unless (bound-and-true-p org-jira-mode) (org-jira-mode t)))
(defun org-jira-get-project-name (proj)
(org-jira-find-value proj 'key))
(defun org-jira-find-value (l &rest keys)
(let* (key exists)
(while (and keys (listp l))
(setq key (car keys))
(setq exists nil)
(mapc (lambda (item)
(when (equal key (car item))
(setq exists t)))
(if (and (listp l)
(listp (car l)))
l
nil))
(setq keys (cdr keys))
(if exists
(setq l (cdr (assoc key l)))
(setq l (or (cdr (assoc key l)) l))))
l))
(defun org-jira--get-project-file-name (project-key)
"Translate PROJECT-KEY into filename."
(-if-let (translation (cdr (assoc project-key org-jira-project-filename-alist)))
(expand-file-name translation (org-jira--ensure-working-dir))
(expand-file-name (concat project-key ".org") (org-jira--ensure-working-dir))))
(defun org-jira-get-project-lead (proj)
(org-jira-find-value proj 'lead 'name))
;; This is mapped to accountId and not username, so we need nil not blank string.
(defun org-jira-get-assignable-users (project-key)
"Get the list of assignable users for PROJECT-KEY, adding user set jira-users first."
(append
'(("Unassigned" . nil))
org-jira-users
(mapcar (lambda (user)
(cons (org-jira-decode (cdr (assoc 'displayName user)))
(org-jira-decode (cdr (assoc 'accountId user)))))
(jiralib-get-users project-key))))
(defun org-jira-get-reporter-candidates (project-key)
"Get the list of assignable users for PROJECT-KEY, adding user set jira-users first."
(append
org-jira-users
(mapcar (lambda (user)
(cons (org-jira-decode (cdr (assoc 'displayName user)))
(org-jira-decode (cdr (assoc 'accountId user)))))
(jiralib-get-users project-key))))
(defun org-jira-entry-put (pom property value)
"Similar to org-jira-entry-put, but with an optional alist of overrides.
At point-or-marker POM, set PROPERTY to VALUE.
Look at customizing org-jira-property-overrides if you want
to change the property names this sets."
(unless (stringp property)
(setq property (symbol-name property)))
(let ((property (or (assoc-default property org-jira-property-overrides)
property)))
(org-entry-put pom property (org-jira-decode value))))
(defun org-jira-kill-line ()
"Kill the line, without 'kill-line' side-effects of altering kill ring."
(interactive)
(delete-region (point) (line-end-position)))
;; Appropriated from org.el
(defun org-jira-org-kill-line (&optional _arg)
"Kill line, to tags or end of line."
(cond
((or (not org-special-ctrl-k)
(bolp)
(not (org-at-heading-p)))
(when (and (get-char-property (min (point-max) (point-at-eol)) 'invisible)
org-ctrl-k-protect-subtree
(or (eq org-ctrl-k-protect-subtree 'error)
(not (y-or-n-p "Kill hidden subtree along with headline? "))))
(user-error "C-k aborted as it would kill a hidden subtree"))
(call-interactively
(if (bound-and-true-p visual-line-mode) 'kill-visual-line 'org-jira-kill-line)))
((looking-at ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)[ \t]*$")
(delete-region (point) (match-beginning 1))
(org-set-tags nil t))
(t (delete-region (point) (point-at-eol)))))
;;;###autoload
(defun org-jira-get-projects ()
"Get list of projects."
(interactive)
(let ((projects-file (expand-file-name "projects-list.org" (org-jira--ensure-working-dir))))
(or (find-buffer-visiting projects-file)
(find-file projects-file))
(org-jira-maybe-activate-mode)
(save-excursion
(let* ((oj-projs (jiralib-get-projects)))
(mapc (lambda (proj)
(let* ((proj-key (org-jira-find-value proj 'key))
(proj-headline (format "Project: [[file:%s.org][%s]]" proj-key proj-key)))
(save-restriction
(widen)
(goto-char (point-min))
(outline-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 "* ")
(org-jira-insert proj-headline)
(org-narrow-to-subtree))
(org-jira-entry-put (point) "name" (org-jira-get-project-name proj))
(org-jira-entry-put (point) "key" (org-jira-find-value proj 'key))
(org-jira-entry-put (point) "lead" (org-jira-get-project-lead proj))
(org-jira-entry-put (point) "ID" (org-jira-find-value proj 'id))
(org-jira-entry-put (point) "url" (format "%s/browse/%s" (replace-regexp-in-string "/*$" "" jiralib-url) (org-jira-find-value proj 'key))))))
oj-projs)))))
(defun org-jira-get-issue-components (issue)
"Return the components the ISSUE belongs to."
(mapconcat
(lambda (comp)
(org-jira-find-value comp 'name))
(org-jira-find-value issue 'fields 'components) ", "))
(defun org-jira-get-issue-labels (issue)
"Return the labels the ISSUE belongs to."
(org-jira-find-value issue 'fields 'labels))
(defun org-jira-decode (data)
"Decode text DATA.
It must receive a coercion to string, as not every time will it
be populated."
(decode-coding-string
(cl-coerce data 'string) jiralib-coding-system))
(defun org-jira-insert (&rest args)
"Set coding to text provide by `ARGS' when insert in buffer."
(insert (org-jira-decode (apply #'concat args))))
(defun org-jira-transform-time-format (jira-time-str)
"Convert JIRA-TIME-STR to format \"%Y-%m-%d %T\".
Example: \"2012-01-09T08:59:15.000Z\" becomes \"2012-01-09
16:59:15\", with the current 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--fix-encode-time-args (arg)
"Fix ARG for 3 nil values at the head."
(cl-loop
for n from 0 to 2 by 1 do
(when (not (nth n arg))
(setcar (nthcdr n arg) 0)))
arg)
(defun org-jira-time-format-to-jira (org-time-str)
"Convert ORG-TIME-STR back to jira time format."
(condition-case ()
(format-time-string
"%Y-%m-%dT%T.000Z"
(apply 'encode-time
(org-jira--fix-encode-time-args (parse-time-string org-time-str)))
t)
(error org-time-str)))
(defun org-jira-get-comment-val (key comment)
"Return the value associated with KEY of COMMENT."
(org-jira-get-issue-val key comment))
(defun org-jira-time-stamp-to-org-clock (time-stamp)
"Convert TIME-STAMP into org-clock format."
(format-time-string "%Y-%m-%d %a %H:%M" time-stamp))
(defun org-jira-date-to-org-clock (date)
"Convert DATE into a time stamp and then into org-clock format.
Expects a date in format such as: 2017-02-26T00:08:00.000-0500."
(org-jira-time-stamp-to-org-clock (date-to-time date)))
(defun org-jira-worklogs-to-org-clocks (worklogs)
"Get a list of WORKLOGS and convert to org-clocks."
(mapcar
(lambda (worklog)
(let ((wl-start (cdr (assoc 'started worklog)))
(wl-time (cdr (assoc 'timeSpentSeconds worklog)))
(wl-end))
(setq wl-start (org-jira-date-to-org-clock wl-start))
(setq wl-end (org-jira-time-stamp-to-org-clock (time-add (date-to-time wl-start) wl-time)))
(list
wl-start
wl-end
(cdr (assoc 'comment worklog))
(cdr (assoc 'id worklog))
)
))
worklogs)
)
(defun org-jira-format-clock (clock-entry)
"Format a CLOCK-ENTRY given the (list start end).
This format is typically generated from org-jira-worklogs-to-org-clocks call."
(format "CLOCK: [%s]--[%s]" (car clock-entry) (cadr clock-entry)))
(defun org-jira-insert-clock (clock-entry)
"Insert a CLOCK-ENTRY given the (list start end).
This format is typically generated from org-jira-worklogs-to-org-clocks call."
(insert (org-jira-format-clock clock-entry))
(org-beginning-of-line)
(org-ctrl-c-ctrl-c) ;; @TODO Maybe not call directly? does it matter? - used to resync the clock estimate
(org-end-of-line)
(insert "\n")
(insert (format " :id: %s\n" (cadddr clock-entry)))
(when (caddr clock-entry) (insert (replace-regexp-in-string "^\\*" "-" (format " %s\n" (org-jira-decode (caddr clock-entry)))))) ;; No comment is nil, so don't print it
)
(defun org-jira-logbook-reset (issue-id filename &optional clocks)
"Find logbook for ISSUE-ID in FILENAME, delete it.
Re-create it with CLOCKS. This is used for worklogs."
(interactive)
(let ((existing-logbook-p nil))
;; See if the LOGBOOK already exists or not.
(ensure-on-issue-id-with-filename issue-id filename
(let ((drawer-name (or (org-clock-drawer-name) "LOGBOOK")))
(when (search-forward (format ":%s:" drawer-name) nil 1 1)
(setq existing-logbook-p t))))
(ensure-on-issue-id-with-filename issue-id filename
(let ((drawer-name (or (org-clock-drawer-name) "LOGBOOK")))
(if existing-logbook-p
(progn ;; If we had a logbook, drop it and re-create in a bit.
(search-forward (format ":%s:" drawer-name) nil 1 1)
(org-beginning-of-line)
(delete-region (point) (search-forward ":END:" nil 1 1))
)
(progn ;; Otherwise, create a new one at the end of properties list
(search-forward ":END:" nil 1 1)
(forward-line)))
(org-insert-drawer nil (format "%s" drawer-name)) ;; Doc says non-nil, but this requires nil
(mapc #'org-jira-insert-clock clocks)
;; Clean up leftover newlines (we left 2 behind)
(dotimes (n 2)
(search-forward-regexp "^$" nil 1 1)
(delete-region (point) (min (point-max) (1+ (point)))))))))
(defun org-jira-get-worklog-val (key WORKLOG)
"Return the value associated with KEY of WORKLOG."
(org-jira-get-comment-val key WORKLOG))
(defun org-jira-get-issue-val (key issue)
"Return the value associated with key KEY of issue ISSUE."
(let ((tmp (or (org-jira-find-value issue 'fields key 'key) ""))) ;; For project, we need a key, not the name...
(unless (stringp tmp)
(setq tmp (org-jira-find-value issue key)))
(unless (stringp tmp)
(setq tmp (org-jira-find-value issue 'fields key 'displayName)))
(unless (stringp tmp)
(setq tmp ""))
(cond ((eq key 'components)
(org-jira-get-issue-components issue))
((eq key 'labels)
(org-jira-get-issue-labels issue))
((member key '(created updated startDate))
(org-jira-transform-time-format tmp))
((eq key 'status)
(if jiralib-use-restapi
(org-jira-find-value issue 'fields 'status 'name)
(org-jira-find-value (jiralib-get-statuses) tmp)))
((eq key 'resolution)
(if jiralib-use-restapi
tmp
(if (string= tmp "")
""
(org-jira-find-value (jiralib-get-resolutions) tmp))))
((eq key 'type)
(if jiralib-use-restapi
(org-jira-find-value issue 'fields 'issuetype 'name)
(org-jira-find-value (jiralib-get-issue-types) tmp)))
((eq key 'priority)
(if jiralib-use-restapi
(org-jira-find-value issue 'fields 'priority 'name)
(org-jira-find-value (jiralib-get-priorities) tmp)))
((eq key 'description)
(org-trim tmp))
(t
tmp))))
(defvar org-jira-jql-history nil)
;;;###autoload
(defun org-jira-get-issue-list (&optional callback)
"Get list of issues, using jql (jira query language), invoke CALLBACK after.
Default is unresolved issues assigned to current login user; with
a prefix argument you are given the chance to enter your own
jql."
(org-jira-log (format "I was called, was it with a callback? %s" (if callback "yes" "no")))
(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 nil callback))))
(defun org-jira-get-issue-by-id (id)
"Get an issue by its ID."
(push id org-jira-issue-id-history)
(let ((jql (format "id = %s" id)))
(jiralib-do-jql-search jql)))
(defun org-jira-get-issue-by-fixversion (fixversion-id)
"Get an issue by its FIXVERSION-ID."
(push fixversion-id org-jira-fixversion-id-history)
(let ((jql (format "fixVersion = \"%s\"" fixversion-id)))
(jiralib-do-jql-search jql)))
;;;###autoload
(defun org-jira-get-summary ()
"Get issue summary from point and place next to issue id from jira"
(interactive)
(let ((jira-id (thing-at-point 'symbol)))
(unless jira-id (error "ORG_JIRA_ERROR: JIRA-ID missing in org-jira-get-summary!"))
(forward-symbol 1)
(insert (format " - %s"
(cdr (assoc 'summary (assoc 'fields (car (org-jira-get-issue-by-id jira-id)))))))))
;;;###autoload
(defun org-jira-get-summary-url ()
"Get issue summary from point and place next to issue id from jira, and make issue id a link"
(interactive)
(let ((jira-id (thing-at-point 'symbol)))
(insert (format "[[%s][%s]] - %s"
(concatenate 'string jiralib-url "browse/" jira-id) jira-id
(cdr (assoc 'summary (car (org-jira-get-issue-by-id jira-id))))))))
;;;###autoload
(defun org-jira-get-issues-headonly (issues)
"Get list of ISSUES, head only.
The default behavior is to return issues assigned to you and unresolved.
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--ensure-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-key issue))
(issue-summary (org-jira-get-issue-summary issue)))
(org-jira-insert (format "- [jira:%s] %s\n" issue-id issue-summary))))
issues))
(switch-to-buffer issues-headonly-buffer)))
;;;###autoload
(defun org-jira-get-issue (id)
"Get a JIRA issue, allowing you to enter the issue-id first."
(interactive (list (read-string "Issue ID: " "" 'org-jira-issue-id-history)))
(org-jira-get-issues (org-jira-get-issue-by-id id))
(let ((issue-pos (org-find-entry-with-id id)))
(when issue-pos
(goto-char issue-pos)
(recenter 0))))
;;;###autoload
(defun org-jira-get-issues-by-fixversion (fixversion)
"Get list of issues by FIXVERSION."
(interactive (list (read-string "Fixversion ID: " ""
'org-jira-fixversion-id-history)))
(org-jira-get-issues (org-jira-get-issue-by-fixversion fixversion)))
;;;###autoload
(defun org-jira-get-issue-project (issue)
(org-jira-find-value issue 'fields 'project 'key))
(defun org-jira-get-issue-key (issue)
(org-jira-find-value issue 'key))