Skip to content

Instantly share code, notes, and snippets.

@ajvargo
Created March 28, 2019 18:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajvargo/40af070e12300877fa53658176a03c28 to your computer and use it in GitHub Desktop.
Save ajvargo/40af070e12300877fa53658176a03c28 to your computer and use it in GitHub Desktop.
Extension to allow pulling trello cards into org
;;; the functions of interest are
;;; org-trello-insert-todo
;;; takes the card - like 'JgW7pEMn'
;;; org-trello-insert-todo-from-url
;;; takes a full trello url and pulls the card out
;; For key & token, go to https://trello.com/app-key The key should be
;; front and center. For the token, follow the 'Token' link in the
;; first paragraph and then 'Allow'
(defvar org-trello-key "....")
(defvar org-trello-token "....")
(defun org-trello-fetch-api-data (url fields)
(json-read-from-string
(with-current-buffer (url-retrieve-synchronously (concat
url
"?key=" org-trello-key
"&token=" org-trello-token
"&fields=" fields))
(beginning-of-buffer)
(delete-region (point-min) (- (search-forward "{" nil t) 1))
(buffer-string))))
(defun org-trello-fetch-card-details (card-id)
(org-trello-fetch-api-data (concat "https://trello.com/c/" card-id ".json")
"name,shortUrl,desc,labels,idChecklists"))
(defun org-trello-fetch-checklist-details (checklist-id)
(org-trello-fetch-api-data (concat "https://api.trello.com/1/checklists/" checklist-id)
"name,checkItems&checkItem_fields=state,name"))
(defun org-trello-get-attr (attr)
(or (alist-get attr org-trello-card-details)
(error "No value for attr `%s' found." attr)))
(defun org-trello-card-name ()
(org-trello-get-attr 'name))
(defun org-trello-card-url ()
(org-trello-get-attr 'shortUrl))
(defun org-trello-card-description ()
(org-trello-get-attr 'desc))
(defun org-trello-insert-tags ()
(org-set-tags
(delq nil (mapcar (lambda (x) (if (eq (car x) 'name)
(replace-regexp-in-string " " "_" (cdr x))
nil))
(mapcan (lambda(x) x) (org-trello-get-attr 'labels))))))
(defun org-trello-insert-header-line ()
(insert "TODO "
(org-trello-card-name) " | "
(org-make-link-string (org-trello-card-url) "Trello"))
(org-trello-insert-tags)
(org-align-tags t))
(defun org-trello-insert-body ()
(insert (org-trello-card-description))
(fill-paragraph))
(defun org-trello-insert-checkist-item (item)
(insert "- ["
(if (string-equal "complete" (alist-get 'state item))
"X" " ")
"] "
(alist-get 'name item)
"\n"))
(defun org-trello-insert-checklist (checklist-id)
(let ((org-trello-checklist-data (org-trello-fetch-checklist-details checklist-id)))
(insert "\n")
(org-insert-heading)
(if (eql (org-outline-level) org-trello-level)
(org-demote-subtree))
(insert (alist-get 'name org-trello-checklist-data) " [/]")
(org-return-indent)
(seq-do #'org-trello-insert-checkist-item (alist-get 'checkItems org-trello-checklist-data))))
(defun org-trello-insert-checklists ()
(seq-do #'org-trello-insert-checklist (org-trello-get-attr 'idChecklists)))
(defun org-trello--insert-todo (card-id)
(let ((org-trello-card-details (org-trello-fetch-card-details card-id)))
(org-trello-insert-header-line)
(setf org-trello-level (org-outline-level))
(end-of-line)
(insert "\n\n")
(org-trello-insert-body)
(insert "\n\n")
(org-trello-insert-checklists)))
(defun org-trello-insert-todo (card-id)
(interactive "MCard id: ")
(org-trello--insert-todo card-id))
(defun org-trello-insert-todo-from-url (trello-card-url)
(interactive "MCard URL: ")
(org-trello--insert-todo (replace-regexp-in-string "http.*/c/\\(.+?\\)/.*" "\\1" trello-card-url)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment