Skip to content

Instantly share code, notes, and snippets.

Created December 20, 2016 08:53
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save DamienCassou/d5d05c2501271ac4b55281b4c7aef4fc to your computer and use it in GitHub Desktop.
magit branch name from jira
(defvar ftgp-jira-summary-url
"Format string to get the summary of a ticket.")
(defcustom ftgp-atlassian-pass-entry "ftgp/"
"Password-store entry where to find FTGP Atlassian credentials.
Must contain both the password and username such that
`auth-pass-get' will find them."
:group 'ftgp
:type 'string)
(defun ftgp-jira-authentication ()
"Return an encoded string with jira username and password."
(let ((pass-entry ftgp-atlassian-pass-entry))
(require 'auth-password-store)
(if-let ((username (auth-pass-get "username" pass-entry))
(password (auth-pass-get 'secret pass-entry)))
(concat username ":" password))
(error "Couldn't get username and password from password-store entry '%s'"
(defvar url-http-end-of-headers))
(defun ftgp-jira-ticket-summary (ticketid)
"Retrieve summary of TICKETID in jira."
(require 'json)
(require 'url)
(require 'url-http)
(require 'url-vars)
(require 'subr-x)
(let* ((url (format ftgp-jira-summary-url ticketid))
`(("Content-Type" . "application/json")
("Authorization" .
,(concat "Basic " (ftgp-jira-authentication))))))
(with-current-buffer (url-retrieve-synchronously url)
(goto-char url-http-end-of-headers)
(if-let ((json-object-type 'hash-table)
(json-key-type 'symbol)
(result (json-read))
(fields (map-elt result 'fields)))
(map-elt fields 'summary)))))
(defun ftgp-ask-for-ticketid ()
"Ask the user for a jira ticketid."
(let ((ticketid (read-from-minibuffer
"Ticket id (e.g., FTGP-2016 or just 2016): ")))
(if (string-match "-" ticketid)
(concat "FTGP-" ticketid))))
(defun ftgp-branch-name-from-jira (&optional ticketid)
"Return a branch name from jira's TICKETID.
If TICKETID is not provided, ask for it. The branch name is
derived from the ticket's summary."
(let* ((ticketid (or ticketid (ftgp-ask-for-ticketid)))
(summary (ftgp-jira-ticket-summary ticketid)))
(s-trim summary)))))
(with-eval-after-load "magit"
(require 's)
(defun magit-create-branch-from-jira-issue ()
(let ((branch-name (ftgp-branch-name-from-jira))
(start-point (magit-read-branch-or-commit "Create branch starting at")))
(when (yes-or-no-p (format "The new branch will be named '%s', ok ? " branch-name))
(magit-branch branch-name start-point)
(magit-checkout branch-name))))
(magit-define-popup-action 'magit-branch-popup
?j "Create branch from jira" 'magit-create-branch-from-jira-issue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment