-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathejira-hourmarking.el
269 lines (221 loc) · 9.75 KB
/
ejira-hourmarking.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
;;; ejira-hourmarking.el --- Syncing org-clock entries to JIRA.
;; Copyright (C) 2017 Henrik Nyman
;; Author: Henrik Nyman <henrikjohannesnyman@gmail.com>
;; URL: https://github.com/nyyManni/ejira
;; Keywords: calendar, data, org, jira
;; Version: 1.0
;; Package-Requires: ((org "8.3") (ox-jira) (language-detection) (s "1.0"))
;; This file is NOT part of GNU Emacs.
;; This file 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, or (at your option)
;; any later version.
;; This file 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 GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Based on org-clock-csv.el by Aaron Jacobs
;; Parses timestamps from org files and generates a report buffer.
;; Report buffer can be modified and then posted to JIRA Tempo.
;;; Code:
(require 'org)
(require 's)
(require 'jiralib2)
(require 'ejira-core)
(defvar ejira-hourmarking-step 15
"Step size for adjusting clocked entries in clock buffer.")
(defvar ejira-hourmarking-round-by 15
"Amount of minutes to round clocks to.")
(defun ejira-hourmarking--find-headlines (element)
"Get a list of headline ancestors of ELEMENT from closest parent to the farthest."
(let ((ph (org-element-lineage element '(headline))))
(if ph
(cons ph (ejira-hourmarking--find-headlines ph)))))
(defun ejira-hourmarking--get-entries (date-str)
"Get entries matching DATE-STR."
(sort
(let ((ejira-hourmarking--date-filter date-str))
(cl-loop
for file in (org-agenda-files) append
(with-current-buffer (or (find-buffer-visiting file)
(find-file file))
(save-mark-and-excursion
(save-restriction
(widen)
(org-element-map (org-element-parse-buffer) 'clock
#'ejira-hourmarking--parse-clock-data nil nil))))))
;; Sort entries based on start time.
(lambda (a b)
(time-less-p
(plist-get a ':start)
(plist-get b ':start)))))
(defun ejira-hourmarking--parse-org-clock-stamp (s)
"Parse org timestamp S to to elisp time objects."
(mapcar #'date-to-time (split-string s "--")))
(defun ejira-hourmarking--parse-clock-data (element)
"Parse clock data of ELEMENT. Return plist."
(when (and (equal (org-element-type element) 'clock)
;; Only ingest closed, inactive clock elements.
(equal (org-element-property :status element) 'closed)
(equal (org-element-property
:type (org-element-property :value element))
'inactive-range))
(let* ((timestamp (org-element-property :value element))
(headlines (ejira-hourmarking--find-headlines element))
(headlines-values (mapcar (lambda (h) (org-element-property :raw-value h)) headlines))
(task-headline (car headlines))
(task (car headlines-values))
(subtask-p (unless (org-element-property :ID (car headlines)) t))
(task-key (ejira-hourmarking--get-id headlines))
(times (ejira-hourmarking--parse-org-clock-stamp
(org-element-property :raw-value timestamp)))
(start (car times))
(end (cadr times))
(duration (time-subtract end start)))
(when (equal (format-time-string "%Y-%m-%d" start)
;; Dynamically bound.
ejira-hourmarking--date-filter)
(list :start start
:start-r start ;; TODO: Maybe round this as well
:end end
:duration (ejira-hourmarking-round duration 1)
:duration-r (ejira-hourmarking-round duration ejira-hourmarking-round-by)
:key task-key
:title task
:subtask-p subtask-p)))))
(defun ejira-hourmarking-round (time round-by)
"Round TIME to nearest ROUND-BY."
;; Account for stupid time zone issues.
(let* ((offset (/ (car (current-time-zone)) 3600))
(hours (- (string-to-number (format-time-string "%H" time)) offset))
(minutes (string-to-number (format-time-string "%M" time)))
(total (+ (* 60 hours) minutes))
(rounded (round total round-by))
(hours-r (/ rounded (/ 60 round-by)))
(minutes-r (* round-by (% rounded (/ 60 round-by)))))
(+ (* 3600 hours-r) (* 60 minutes-r))))
(defun ejira--format-h-m (seconds)
"Format number of SECONDS into JIRA format %Hm %Mm."
(if (>= seconds 3600)
;; More than hour
(format "% 1dh% 3dm" (/ seconds 3600) (/ (% seconds 3600) 60))
;; Less than hour
(format " % 3dm" (/ (% seconds 3600) 60))))
(defun ejira-hourmarking-format-row (entry)
"Pretty-print ENTRY."
`(,(format-time-string "%H:%M" (plist-get entry :start))
,(ejira--format-h-m (plist-get entry :duration))
,(ejira--format-h-m (plist-get entry :duration-r))
,(format "%-15s" (plist-get entry :key))
,(plist-get entry :title)))
(defun ejira-hourmarking--get-id (headlines)
"Return the ID of the given headline.
If headline does not have an id, use it's parents id in HEADLINES."
(condition-case nil
(or (org-element-property :ID (car headlines))
(org-element-property :ID (cadr headlines)))
(error nil)))
(defun ejira-hourmarking--redraw (rows)
"Redraw buffer with content from ROWS."
(unless (equal (buffer-name) "*ejira-hourlog*")
(error "Not in a hourlog buffer"))
(let ((inhibit-read-only t))
(goto-char 0)
(erase-buffer)
;; Print header
(if (> (length rows) 0)
(insert (format-time-string " JIRA Hourlog %A, %h %e\n\n"
(plist-get (nth 0 rows) :start)))
(insert "\n\n"))
(let ((sum (apply #'+ (mapcar (lambda (e) (plist-get e :duration)) rows)))
(sum-r (apply #'+ (mapcar (lambda (e) (plist-get e :duration-r)) rows))))
(mapc (lambda (r)
(insert (concat " " (s-join " | " r) "\n")))
(mapcar #'ejira-hourmarking-format-row ejira-hourlog-entries))
(insert (format "\nTOTAL: %dh %dm" (/ sum-r 3600) (/ (% sum-r 3600) 60)))
(insert (format "\nCLOCKED: %dh %dm\n" (/ sum 3600) (/ (% sum 3600) 60))))))
;;;###autoload
(defun ejira-hourmarking-get-hourlog (date-str)
"Open hourlog from today into an ejira-hourlog -buffer.
Limit entries to DATE-STR."
(interactive
(last (split-string
(completing-read
"Date (YYYY-MM-DD): "
(mapcar (lambda (d)
(let ((time (time-subtract (current-time)
(days-to-time d))))
(format "%-15s %s"
(format-time-string "%A" time)
(format-time-string "%Y-%m-%d" time))))
(number-sequence 0 14))) " " t)))
(let* ((buffer (get-buffer-create "*ejira-hourlog*"))
(entries (ejira-hourmarking--get-entries date-str)))
(with-current-buffer buffer
(let ((mode 'ejira-hourlog-mode))
(funcall mode))
(make-local-variable 'ejira-hourlog-entries)
(setq-local ejira-hourlog-entries entries)
(ejira-hourmarking--redraw ejira-hourlog-entries)
(goto-char (point-min))
(pop-to-buffer (current-buffer)))))
(defvar ejira-hourlog-mode-map
(let ((map (make-sparse-keymap))) map))
(defun ejira-hourlog--adjust-current-row (amount)
"Adjust the rounded duration of current row by AMOUNT minutes."
(let ((index (- (line-number-at-pos) 3))
(p (point)))
(when (or (>= index (length ejira-hourlog-entries))
(< index 0))
(user-error "Outside hourlog"))
(plist-put (nth index ejira-hourlog-entries) :duration-r
(max
0
(+ (plist-get (nth index ejira-hourlog-entries) :duration-r)
(* 60 amount))))
(ejira-hourmarking--redraw ejira-hourlog-entries)
(goto-char p)))
(defun ejira-hourlog-increase ()
"Increase the duration of item under point by `ejira-hourmarking-step'."
(interactive)
(ejira-hourlog--adjust-current-row ejira-hourmarking-step))
(defun ejira-hourlog-decrease ()
"Decrease the duration of item under point by `ejira-hourmarking-step'."
(interactive)
(ejira-hourlog--adjust-current-row (- ejira-hourmarking-step)))
(defun ejira-hourlog-commit ()
"Confirm hourlog and push it to Tempo."
(interactive)
(dolist (entry ejira-hourlog-entries)
(let ((key (plist-get entry :key))
(start (format-time-string "%Y-%m-%dT%H:%M:%S.000%z" (plist-get entry :start)))
(duration (plist-get entry :duration-r))
(comment (plist-get entry :title)))
(when (> duration 0)
(jiralib2-add-worklog key start duration comment))))
(ejira-hourlog-quit)
(message "Successfully updated worklog"))
(defun ejira-hourlog-quit ()
"Quit ejira-hourlog and close window."
(interactive)
(kill-this-buffer)
(delete-window))
;;;###autoload
(define-derived-mode ejira-hourlog-mode special-mode "JIRA Hourlog"
(local-set-key (kbd "C-k") 'ejira-hourlog-increase)
(local-set-key (kbd "C-j") 'ejira-hourlog-decrease)
(local-set-key (kbd "C-c C-c") 'ejira-hourlog-commit)
(local-set-key (kbd "q") 'ejira-hourlog-quit))
(defun ejira-hourmarking--hourlog-mode-hook ()
"Set up font-locks for hourlog-mode."
(font-lock-add-keywords nil
'(("^ JIRA Hourlog .*$" . font-lock-warning-face)
("^.*| *0m *|.*$" . font-lock-comment-face)
(" | " . font-lock-comment-face)
)))
(add-hook 'ejira-hourlog-mode-hook #'ejira-hourmarking--hourlog-mode-hook)
(provide 'ejira-hourmarking)
;;; ejira-hourmarking.el ends here