Skip to content

Commit c16e786

Browse files
committed
feat(jiralib): add +jira-commit-auto-insert-ticket-id
1 parent 7e178be commit c16e786

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

modules/me-services.el

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
:autoload +jira-get-ticket
1616
:init
1717
(defvar-local +jira-open-status '("open" "to do" "in progress"))
18+
(defvar-local +jira-commit-auto-insert-ticket-id-predicate nil)
1819
:config
1920
(defun +jira--ticket-annotation-fn (ticket)
2021
(let ((item (assoc ticket minibuffer-completion-table)))
@@ -58,6 +59,31 @@ The link style depends on the current major mode."
5859
(format "[%s%s](%s)" id (if with-summary (concat ": " summary) "") link))
5960
(t link)))))
6061

62+
;; INFO: Here is an example of a predicate function, it inserts a Jira ID
63+
;; only if the project's remote contains the username "abougouffa"
64+
;; (setq +jira-commit-auto-insert-ticket-id-predicate
65+
;; (lambda ()
66+
;; (when-let* ((urls (mapcar (lambda (remote) (string-trim-right (shell-command-to-string (format "git config --get remote.%s.url" remote)))) (magit-list-remotes))))
67+
;; (cl-some (apply-partially #'string-match-p (rx (or "github.com/abougouffa" "gitlab.com/abougouffa"))) urls))))
68+
69+
(defun +jira-commit-auto-insert-ticket-id ()
70+
"Insert a ticket ID at the beginning of the commit if the first line is empty.
71+
72+
This function is meant to be hooked to `git-commit-mode-hook' for
73+
projects to uses the convention of commit messages like:
74+
75+
\"JIRA-TICKET-ID: Commit message\"."
76+
(when (and (+first-line-empty-p) ; Do not auto insert if the commit message is not empty (ex. amend)
77+
+jira-commit-auto-insert-ticket-id-predicate
78+
(funcall +jira-commit-auto-insert-ticket-id-predicate))
79+
(goto-char (point-min))
80+
(insert "\n")
81+
(goto-char (point-min))
82+
(let ((pt (point)))
83+
(+jira-insert-ticket-id nil)
84+
(when (/= pt (point))
85+
(insert ": ")))))
86+
6187
(defun +jiralib-auto-login ()
6288
"Auto login to Jira using credentials from `auth-source'."
6389
(interactive)

0 commit comments

Comments
 (0)