Created
December 20, 2016 08:53
-
-
Save DamienCassou/d5d05c2501271ac4b55281b4c7aef4fc to your computer and use it in GitHub Desktop.
magit branch name from jira
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defvar ftgp-jira-summary-url | |
"https://foretagsplatsen.atlassian.net/rest/api/2/issue/%s?fields=summary" | |
"Format string to get the summary of a ticket.") | |
(defcustom ftgp-atlassian-pass-entry "ftgp/foretagsplatsen.atlassian.net" | |
"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))) | |
(base64-encode-string | |
(concat username ":" password)) | |
(error "Couldn't get username and password from password-store entry '%s'" | |
pass-entry)))) | |
(eval-when-compile | |
(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)) | |
(url-request-extra-headers | |
`(("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) | |
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))) | |
(concat | |
ticketid | |
"_" | |
(replace-regexp-in-string | |
"[^a-zA-Z0-9-]+" | |
"_" | |
(s-trim summary))))) | |
(with-eval-after-load "magit" | |
(require 's) | |
(defun magit-create-branch-from-jira-issue () | |
(interactive) | |
(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