Commit 9a5b7f2b authored by to1ne's avatar to1ne

Initial implementation of syncing todos

Todos sync now.
parent f404a478
Pipeline #54137709 passed with stage
in 32 seconds
......@@ -5,9 +5,9 @@
;; Author: Toon Claes <toon@iotcl.com>
;; Maintainer: Toon Claes <toon@iotcl.com>
;; Created: 13 Feb 2017
;; Modified: 26 May 2018
;; Version: 0.1
;; Package-Version: 20180526.1029
;; Modified: 28 Mar 2019
;; Version: 0.2
;; Package-Version: 20190328.2137
;; Package-Requires: ((async "1.9") (request "0.3") (org "9.0"))
;; Keywords: org-mode gitlab todos issues merge-requests
;; URL: https://gitlab.com/to1ne/org-gitlab
......@@ -119,17 +119,17 @@ saved in customize."
:parser (lambda ()
(let ((json-array-type 'list))
(json-read))))))
;; TODO error handling --> widen narrowing
;; TODO error handling --> widen narrowing (using =condition-case= ?)
(request-response-data response)))
(defun org-gitlab--get-issues (params)
"Fetch all Issues from api using PARAMS."
(org-gitlab--get-request "/issues" params))
(defun org-gitlab--get-issuables (class params)
"Fetch all issuables of type CLASS from api using PARAMS."
(org-gitlab--get-request class params))
(defun org-gitlab--get-todos (params)
"Fetch all Todos from api using PARAMS."
(org-gitlab--get-request "todos" params))
(defun org-gitlab--params-at-point ()
"Get the query parameters for the heading at point."
(let ((author (org-entry-get (point) "author"))
......@@ -165,9 +165,9 @@ saved in customize."
(setq attr (cdr attr)))
(org-set-property property attr))))
(defun org-gitlab--format-iid (issuable)
(defun org-gitlab--format-iid (issuable &optional key)
"It formats the iid from the ISSUABLE `web_url'."
(let* ((web-url (cdr (assoc 'web_url issuable)))
(let* ((web-url (cdr (assoc (or key 'web_url) issuable)))
(web-filename (substring (url-filename (url-generic-parse-url web-url)) 1))
(iid (replace-regexp-in-string "/merge_requests/" "!"
(replace-regexp-in-string "/issues/" "#" web-filename))))
......@@ -181,9 +181,9 @@ to filter."
(save-excursion
(unless params
(setq params (org-gitlab--params-at-point)))
(let* ((issuables (org-gitlab--get-issuables class params))
(heading-pos (point))
pos)
(let ((issuables (org-gitlab--get-issuables class params))
(heading-pos (point))
pos)
(dolist (issuable issuables)
(let ((attr (number-to-string (cdr (assoc 'id issuable)))))
(if (setq pos (org-find-property "id" attr))
......@@ -199,6 +199,46 @@ to filter."
(org-set-property "iid" (org-gitlab--format-iid issuable))
(org-gitlab--try-set-property "assignee" issuable))))))
(defun org-gitlab--titlize-todo (todo)
"Generate title for TODO."
(let* ((action (cdr (assoc 'action_name todo)))
(iid (org-gitlab--format-iid todo 'target_url))
(author (assoc 'author todo)))
(if author
(concat "@" (cdr (assoc 'username author))
(cond ((equal "assigned" action) " assigned ")
((equal "mentioned" action) " mentioned you on ")
((equal "marked" action) " added a todo for ")
((equal "approval_required" action) " asked your approval for ")
((equal "directly_addressed" action) " directly addressed you on "))
iid)
(concat (cond ((equal "build_failed" action) "The build failed for ")
((equal "unmergeable" action) "Merge is not possible on "))
iid))))
(defun org-gitlab--sync-todos (&optional params)
"Synchronize your Todos from GitLab server to sub headings.
Optionally specify PARAMS to filter."
(save-excursion
(unless params
(setq params (org-gitlab--params-at-point)))
(let ((todos (org-gitlab--get-todos params))
(heading-pos (point))
pos)
(dolist (todo todos)
(let ((attr (number-to-string (cdr (assoc 'id todo)))))
(if (setq pos (org-find-property "id" attr))
(goto-char pos)
(goto-char heading-pos)
(org-insert-heading-respect-content)
(org-do-demote)
(org-set-property "org-gitlab" "todo")
(org-set-property "id" attr))
(org-edit-headline (org-gitlab--titlize-todo todo))
(org-set-property "action" (cdr (assoc 'action_name todo)))
;; TODO more
)))))
(defun org-gitlab--untodo-tree ()
"Remove TODO from all headings in tree."
(org-map-entries (lambda ()
......@@ -219,8 +259,11 @@ contain one of the following properties: `author' or `assignee'."
(org-gitlab--untodo-tree)
(let ((params (org-gitlab--params-at-point))
(class (org-entry-get (point) "org-gitlab")))
(if (or (equal "issues" class) (equal "merge-requests" class))
(org-gitlab--sync-issuables class params)))
(cond ((or (equal "issues" class)
(equal "merge-requests" class))
(org-gitlab--sync-issuables class params))
((equal "todos" class)
(org-gitlab--sync-todos params))))
(widen)))
;;;###autoload
......@@ -229,6 +272,8 @@ contain one of the following properties: `author' or `assignee'."
(interactive)
(save-excursion
(let (org-gitlab-heading)
;; TODO this isn't very efficient, since it shouldn't sync
;; children of headings it synced already
(while (setq org-gitlab-heading (org-find-property "org-gitlab"))
(goto-char org-gitlab-heading)
(org-gitlab-sync-at-point)
......@@ -254,6 +299,7 @@ It inserts:
:assignee: To limit everything assigned to the user specified.
:state: To limit to the specified state."
(interactive)
;; TODO implement me
)
;;; Minor mode
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment