summary refs log tree commit diff
path: root/emacs/elisp/my-gerrit.el
blob: 4a9dc27aeccb8093c97480454592e38a4848f6dc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
;;; my-gerrit.el --- magit integration for Gerrit -*- lexical-binding: t -*-
;; Author: Franck Cuny <franck@fcuny.net>

;;; Commentary:

;; interact with Gerrit from magit

;;; Code:

(require 'magit)
(require 's)

(defgroup my/gerrit nil
  "Customization options for Gerrit integration with magit."
  :group 'magit)

(defcustom my/gerrit-remote "origin"
  "Name of the git remote for Gerrit."
  :type '(string)
  :group 'my/gerrit)

(defun my/gerrit-ref (target-branch &optional flags)
  "Target ref to push change based on TARGET-BRANCH, with optional FLAGS."
  (let ((flag-suffix (if flags (format "%%%s" (s-join "," flags))
                       "")))
    (format "HEAD:refs/for/%s%s" target-branch flag-suffix)))

(transient-define-suffix my/magit-gerrit-push-for-review ()
  "Push to Gerrit for review."
  (interactive)
  (magit-push-refspecs my/gerrit-remote (my/gerrit-ref (magit-main-branch)) nil))

(transient-append-suffix 'magit-push "m"
  '("R" "push to Gerrit for review" my/magit-gerrit-push-for-review))

;; By appending `%wip' to the ref, we mark the change as `work in progress'.
;; https://gerrit-review.googlesource.com/Documentation/intro-user.html#wip
(transient-define-suffix my/magit-gerrit-push-for-review-wip ()
  "Push to Gerrit as work in progress."
  (interactive)
  (magit-push-refspecs my/gerrit-remote (my/gerrit-ref (magit-main-branch) '("wip")) nil))

(transient-append-suffix 'magit-push "R"
  '("W" "push to Gerrit for review (WIP)" my/magit-gerrit-push-for-review-wip))

;; By appending `%ready' to the ref, we mark the change as ready for
;; review.
;; https://gerrit-review.googlesource.com/Documentation/intro-user.html#wip
(transient-define-suffix my/magit-gerrit-push-for-review-ready ()
  "Push to Gerrit as ready."
  (interactive)
  (magit-push-refspecs my/gerrit-remote (my/gerrit-ref (magit-main-branch) '("ready")) nil))

(transient-append-suffix 'magit-push "W"
  '("r" "push to Gerrit for review (ready)" my/magit-gerrit-push-for-review-ready))

;; For this to work, permissions need to be setup, as per
;; https://gerrit-review.googlesource.com/Documentation/user-upload.html#auto_merge
(transient-define-suffix my/magit-gerrit-submit ()
  (interactive)
  (magit-push-refspecs my/gerrit-remote (my/gerrit-ref (magit-main-branch) '("submit")) nil))

(transient-append-suffix 'magit-push "r"
  '("P" "push to Gerrit to submit change" my/magit-gerrit-submit))

(transient-define-suffix my/magit-gerrit-shipit ()
  "Ship it."
  (interactive)
  (magit-push-refspecs my/gerrit-remote (my/gerrit-ref (magit-main-branch)'("l=Code-Review+2" "publish-comments")) nil))

(transient-append-suffix 'magit-push "P"
  '("S" "Approve the change in Gerrit" my/magit-gerrit-shipit))

(provide 'my-gerrit)

;;; my-gerrit.el ends here