Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #172, point to beginning for parsing headline #179

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions org-gcal.el
Original file line number Diff line number Diff line change
Expand Up @@ -1458,25 +1458,35 @@ For valid values of EXISTING-MODE see
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-heading-regexp nil t)
(condition-case nil
(goto-char (cdr (org-gcal--timestamp-successor)))
(error (error "Org-gcal error: Couldn't parse %s"
(buffer-file-name))))
(let ((elem (org-element-headline-parser (point-max) t))
(tobj (cadr (org-element-timestamp-parser))))
(when (>
(time-to-seconds (time-subtract (current-time) (days-to-time org-gcal-up-days)))
(time-to-seconds (encode-time 0 (if (plist-get tobj :minute-end)
(plist-get tobj :minute-end) 0)
(if (plist-get tobj :hour-end)
(plist-get tobj :hour-end) 24)
(plist-get tobj :day-end)
(plist-get tobj :month-end)
(plist-get tobj :year-end))))
(org-gcal--notify "Archived event." (org-element-property :title elem))
(let ((kill-ring kill-ring)
(select-enable-clipboard nil))
(org-archive-subtree)))))
(let ((properties (org-entry-properties)))
; Check if headline is managed by `org-gcal', and hasn't been archived
; yet. Only in that case, potentially archive.
(when (and (assoc "ORG-GCAL-MANAGED" properties)
(not (assoc "ARCHIVE_TIME" properties)))

; Go to beginning of line to parse the headline
(beginning-of-line)
(let ((elem (org-element-headline-parser (point-max) t)))

; Go to next timestamp to parse it
(condition-case nil
(goto-char (cdr (org-gcal--timestamp-successor)))
(error (error "Org-gcal error: Couldn't parse %s"
(buffer-file-name))))
(let ((tobj (cadr (org-element-timestamp-parser))))
(when (>
(time-to-seconds (time-subtract (current-time) (days-to-time org-gcal-up-days)))
(time-to-seconds (encode-time 0 (if (plist-get tobj :minute-end)
(plist-get tobj :minute-end) 0)
(if (plist-get tobj :hour-end)
(plist-get tobj :hour-end) 24)
(plist-get tobj :day-end)
(plist-get tobj :month-end)
(plist-get tobj :year-end))))
(org-gcal--notify "Archived event." (org-element-property :title elem))
(let ((kill-ring kill-ring)
(select-enable-clipboard nil))
(org-archive-subtree))))))))
(save-buffer)))

(defun org-gcal--save-sexp (data file)
Expand Down
50 changes: 50 additions & 0 deletions test/org-gcal-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
(require 'org-gcal)
(require 'cl-lib)
(require 'el-mock)
(let* ((org-el-source (file-truename (locate-library "org.el")))
(org-root-dir (f-dirname (f-dirname org-el-source))))
(require 'org-test (concat org-root-dir "/testing/org-test.el")))

(defconst org-gcal-test-calendar-id "foo@foobar.com")

Expand Down Expand Up @@ -1214,6 +1217,53 @@ Second paragraph
;; "2021-03-04T03:30:00+0800"))


(ert-deftest org-gcal-test--headline-archive-old-event ()
"Check that `org-gcal--archive-old-event' parses headlines correctly.
Regression test for https://github.com/kidd/org-gcal.el/issues/172 .

Also tests that the `org-gcal--archive-old-event' function does
not loop over and over, archiving the same entry because it is
under another heading in the same file."
(let ((org-archive-location "::* Archived") ; Make the archive this same buffer
(test-time "2022-01-30 Sun 01:23")
(buf "\
* Event Title
:PROPERTIES:
:org-gcal-managed: something
:END:
<2021-01-01 Fri 12:34-14:35>
"))
(org-test-at-time (format "<%s>" test-time)
(org-test-with-temp-text-in-file
buf
(let* ((buf-category (org-get-category (point-max) 'force-refresh))
(target-buf (format "\
* Archived

** Event Title
:PROPERTIES:
:org-gcal-managed: something
:ARCHIVE_TIME: %s
:ARCHIVE_FILE: %s
:ARCHIVE_CATEGORY: %s
:END:
<2021-01-01 Fri 12:34-14:35>
" test-time file buf-category))) ; The variable `file' is the current file name
; under the macro `org-test-with-temp-text-in-file'
;
; The old error: try to parse a headline when not starting at the beginning
(should-error
(progn (goto-char 2)
(org-element-headline-parser (point-max) t))
:type 'error)

(org-gcal--archive-old-event)
(should
(equal
target-buf
(buffer-substring-no-properties (point-min) (point-max)))))))))

(ert 'org-gcal-test--headline-archive-old-event)

;;; TODO: Figure out mocking for POST/PATCH followed by GET
;;; - ‘mock‘ might work for this - the argument list must be specified up
Expand Down