Skip to content

Instantly share code, notes, and snippets.

@Apteryks
Last active May 29, 2016 21:54
Show Gist options
  • Save Apteryks/943c63dd39ad9e75843afb73c0410e21 to your computer and use it in GitHub Desktop.
Save Apteryks/943c63dd39ad9e75843afb73c0410e21 to your computer and use it in GitHub Desktop.
Preview of buganizer-to-rst.el
(require 'cl-lib)
(require 'rst)
(require 'csv) ; Depends on Ulf Jasper's csv.el module.
(defun parse-csv-file (file)
"Parse a csv file using the `csv-parse-buffer' method of `csv.el'."
(interactive (list (read-file-name "CSV file: ")))
(with-temp-buffer
(insert-file-contents file)
(csv-parse-buffer t (current-buffer))))
(defun validate-data (parsed-csv-data)
"Verify that the CSV parsed data contains the required columns."
(let ((required-columns '("TYPE" "TITLE" "STATUS" "ISSUE_ID"))
(first-row (car parsed-csv-data)))
(dolist (column-header required-columns)
(if (not (assoc-default column-header first-row))
(error "%s" (concat "Column missing in csv file: "
column-header))
))))
(defun buganizer-to-rst (file)
"Generate an ReST formatted changelog from a Buganizer CSV exported file."
(interactive (list (read-file-name "CSV file: ")))
(let ((parsed-csv-data (parse-csv-file file))
prefix issue-type issue-title issue-id issue-status
generated-content)
(validate-data parsed-csv-data)
;; Format entries and build a string with them.
(with-temp-buffer
(rst-mode) ; set buffer major mode
(dolist (row parsed-csv-data)
;; Retrieve variables.
(setq issue-status (assoc-default "STATUS" row))
(setq issue-type (assoc-default "TYPE" row))
(setq issue-title (assoc-default "TITLE" row))
(setq issue-id (assoc-default "ISSUE_ID" row))
(if (equal issue-status "FIXED")
(progn
(cond ; Programmatically define a type prefix for the entry.
((string-equal "BUG" issue-type)
(setq prefix "Bugfix: "))
((string-equal "INTERNAL_CLEANUP" issue-type)
(setq prefix "Cleanup: "))
(t
(setq prefix nil))) ; default prefix value.
;; Form and insert ReST list item.
(insert
(concat prefix issue-title ". (Issue #" issue-id ")\n")))
(warn (concat "Skipping issue #" issue-id
" because its status is not \"FIXED\""))))
;; Bulletized newly inserted paragraphs.
;; The third argument is the command prefix argument which must be set to
;; non-nil since we want to process each line.
(rst-bullet-list-region (point-min) (point-max) t)
(fill-region (point-min) (point-max))
(setq generated-content (buffer-string)))
(insert generated-content)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment