File: magithub.el

package info (click to toggle)
magithub 0.1.7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 1,920 kB
  • sloc: lisp: 3,684; makefile: 82
file content (294 lines) | stat: -rw-r--r-- 11,698 bytes parent folder | download | duplicates (2)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
;;; magithub.el --- Magit interfaces for GitHub  -*- lexical-binding: t; -*-

;; Copyright (C) 2016-2018  Sean Allred

;; Author: Sean Allred <code@seanallred.com>
;; Keywords: git, tools, vc
;; Homepage: https://github.com/vermiculus/magithub
;; Package-Requires: ((emacs "25") (magit "2.12") (s "1.12.0") (ghub+ "0.3") (git-commit "2.12") (markdown-mode "2.3"))
;; Package-Version: 0.1.7

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Magithub is a Magit-based interface to GitHub.
;;
;; Integrated into Magit workflows, Magithub lets you interact with
;; your GitHub repositories and manage your work/play from emacs:
;;
;;  - push brand-new local repositories up to GitHub
;;  - create forks of existing repositories
;;  - submit pull requests upstream
;;  - view and create issues
;;  - view, create, and edit comments
;;  - view status checks (e.g., Travis CI)
;;  - manage labels and assignees
;;  - view/visit notifications
;;  - write personal notes on issues for reference later
;;  - and probably more...
;;
;; Press `H' in the status buffer to get started -- happy hacking!

;;; Code:

(require 'magit)
(require 'magit-process)
(require 'cl-lib)
(require 's)
(require 'dash)
(require 'ghub+)

(require 'magithub-core)
(require 'magithub-issue)
(require 'magithub-ci)
(require 'magithub-issue-post)
(require 'magithub-issue-tricks)
(require 'magithub-orgs)
(require 'magithub-dash)

;;;###autoload (autoload 'magithub-dispatch-popup "magithub" nil t)
(magit-define-popup magithub-dispatch-popup
  "Popup console for dispatching other Magithub popups."
  'magithub-commands
  :variables '((?C "Settings..." magithub-settings-popup))
  :actions '("Actions"
             (?d "Dashboard" magithub-dashboard)
             (?H "Browse on GitHub" magithub-browse)
             (?c "Create on GitHub" magithub-create)
             (?f "Fork this repo" magithub-fork)
             (?i "Submit an issue" magithub-issue-new)
             (?p "Submit a pull request" magithub-pull-request-new)
             "Meta"
             (?& "Request a feature or report a bug" magithub--meta-new-issue)
             (?h "Ask for help on Gitter" magithub--meta-help)))

;;;###autoload
(eval-after-load 'magit
  '(progn
     (magit-define-popup-action 'magit-dispatch-popup
       ?H "Magithub" #'magithub-dispatch-popup ?!)
     (define-key magit-status-mode-map
       "H" #'magithub-dispatch-popup)))

(defun magithub-browse ()
  "Open the repository in your browser."
  (interactive)
  (unless (magithub-github-repository-p)
    (user-error "Not a GitHub repository"))
  (magithub-repo-visit (magithub-repo)))

(defvar magithub-after-create-messages
  '("Don't be shy!"
    "Don't let your dreams be dreams!")
  "One of these messages will be displayed after you create a
GitHub repository.")

(defun magithub-create (repo &optional org)
  "Create REPO on GitHub.

If ORG is non-nil, it is an organization object under which to
create the new repository.  You must be a member of this
organization."
  (interactive (if (or (not (magit-toplevel)) (magithub-github-repository-p))
                   (list nil nil)
                 (let* ((ghub-username (ghubp-username)) ;performance
                        (account (magithub--read-user-or-org))
                        (priv (magithub-confirm-no-error 'create-repo-as-private))
                        (reponame (magithub--read-repo-name account))
                        (desc (read-string "Description (optional): ")))
                   (list
                    `((name . ,reponame)
                      (private . ,priv)
                      (description . ,desc))
                    (unless (string= ghub-username account)
                      `((login . ,account)))))))
  (when (magithub-github-repository-p)
    (error "Already in a GitHub repository"))
  (if (not (magit-toplevel))
      (when (magithub-confirm-no-error 'init-repo-after-create)
        (magit-init default-directory)
        (call-interactively #'magithub-create))
    (with-temp-message "Creating repository on GitHub..."
      (setq repo
            (magithub-request
             (if org
                 (ghubp-post-orgs-org-repos org repo)
               (ghubp-post-user-repos repo)))))
    (magithub--random-message "Creating repository on GitHub...done!")
    (magit-status-internal default-directory)
    (magit-remote-add "origin" (magithub-repo--clone-url repo))
    (magit-refresh)
    (when (magit-rev-verify "HEAD")
      (magit-push-popup))))

(defun magithub--read-user-or-org ()
  "Prompt for an account with completion.

Candidates will include the current user and all organizations,
public and private, of which they're a part.  If there is only
one candidate (i.e., no organizations), the single candidate will
be returned without prompting the user."
  (let ((user (ghubp-username))
        (orgs (ghubp-get-in-all '(login)
                (magithub-orgs-list)))
        candidates)
    (setq candidates orgs)
    (when user (push user candidates))
    (cl-case (length candidates)
      (0 (user-error "No accounts found"))
      (1 (car candidates))
      (t (completing-read "Account: " candidates nil t)))))

(defun magithub--read-repo-name (for-user)
  (let* ((prompt (format "Repository name: %s/" for-user))
         (dirnam (file-name-nondirectory (substring default-directory 0 -1)))
         (valid-regexp (rx bos (+ (any alnum "." "-" "_")) eos))
         ret)
    ;; This is not very clever, but it gets the job done.  I'd like to
    ;; either have instant feedback on what's valid or not allow users
    ;; to enter invalid names at all.  Could code from Ivy be used?
    (while (not (s-matches-p valid-regexp
			     (setq ret (read-string prompt nil nil dirnam))))
      (message "invalid name")
      (sit-for 1))
    ret))

(defun magithub--random-message (&optional prefix)
  (let ((msg (nth (random (length magithub-after-create-messages))
                  magithub-after-create-messages)))
    (if prefix (format "%s  %s" prefix msg) msg)))

(defun magithub-fork ()
  "Fork 'origin' on GitHub."
  (interactive)
  (unless (magithub-github-repository-p)
    (user-error "Not a GitHub repository"))
  (magithub-confirm 'fork)
  (let* ((repo (magithub-repo))
         (fork (with-temp-message "Forking repository on GitHub..."
                 (magithub-request
                  (ghubp-post-repos-owner-repo-forks repo)))))
    (when (magithub-confirm-no-error 'fork-create-spinoff)
      (call-interactively #'magit-branch-spinoff))
    (magithub--random-message
     (let-alist repo (format "%s/%s forked!" .owner.login .name)))
    (let-alist fork
      (when (magithub-confirm-no-error 'fork-add-me-as-remote .owner.login)
        (magit-remote-add .owner.login (magithub-repo--clone-url fork))
        (magit-set .owner.login "branch" (magit-get-current-branch) "pushRemote")))
    (let-alist repo
      (when (magithub-confirm-no-error 'fork-set-upstream-to-me .owner.login)
        (call-interactively #'magit-set-branch*merge/remote)))))

(defvar magithub-clone-history nil
  "History for `magithub-clone' prompt.")

(defun magithub-clone--get-repo ()
  "Prompt for a user and a repository.
Returns a sparse repository object."
  (let ((user (ghubp-username))
        (repo-regexp  (rx bos (group (+ (not (any " "))))
                          "/" (group (+ (not (any " ")))) eos))
        repo)
    (while (not (and repo (string-match repo-regexp repo)))
      (setq repo (read-from-minibuffer
                  (concat
                   "Clone GitHub repository "
                   (if repo "(format is \"user/repo\"; C-g to quit)" "(user/repo)")
                   ": ")
                  (when user (concat user "/"))
                  nil nil 'magithub-clone-history)))
    `((owner (login . ,(match-string 1 repo)))
      (name . ,(match-string 2 repo)))))

(defcustom magithub-clone-default-directory nil
  "Default directory to clone to when using `magithub-clone'.
When nil, the current directory at invocation is used."
  :type 'directory
  :group 'magithub)

(defun magithub-clone (repo dir)
  "Clone REPO.
Banned inside existing GitHub repositories if
`magithub-clone-default-directory' is nil.

See also `magithub-preferred-remote-method'."
  (interactive (let* ((repo (magithub-clone--get-repo))
                      (repo (or (magithub-request
                                 (ghubp-get-repos-owner-repo repo))
                                (let-alist repo
                                  (user-error "Repository %s/%s does not exist"
                                              .owner.login .name))))
                      (name (alist-get 'name repo))
                      (dirname (read-directory-name
                                "Destination: "
                                magithub-clone-default-directory
                                name nil name)))
                 (list repo dirname)))
  ;; Argument validation
  (unless (called-interactively-p 'any)
    (unless (setq repo (magithub-request
                        (ghubp-get-repos-owner-repo repo)))
      (let-alist repo
        (user-error "Repository %s/%s does not exist"
                    .owner.login .name))))
  (let ((parent (file-name-directory dir)))
    (unless (file-exists-p parent)
      (when (magithub-confirm 'clone-create-directory parent)
        (mkdir parent t))))
  (unless (file-writable-p dir)
    (user-error "%s is not writable" dir))

  (let-alist repo
    (when (magithub-confirm-no-error 'clone .full_name dir)
      (let (set-upstream set-proxy)
        (setq set-upstream
              (and .fork (magithub-confirm-no-error
			  'clone-fork-set-upstream-to-parent
                          .parent.full_name))
              set-proxy
              (and set-upstream (magithub-confirm-no-error
				 'clone-fork-set-proxy-to-upstream)))
        (condition-case _
            (let ((default-directory dir)
                  (magit-clone-set-remote.pushDefault t))
              (mkdir dir t)
              (magit-clone (magithub-repo--clone-url repo) dir)
              (while (process-live-p magit-this-process)
                (magit-process-buffer)
                (message "Waiting for clone to finish...")
                (sit-for 1))
              (when set-upstream
                (let ((upstream "upstream"))
                  (when set-proxy (magit-set upstream "magithub.proxy"))
                  (magit-remote-add upstream (magithub-repo--clone-url .parent))
                  (magit-set-branch*merge/remote (magit-get-current-branch)
						 upstream)))))))))

(defun magithub-clone--finished (user repo dir)
  "After finishing the clone, allow the user to jump to their new repo."
  (when (magithub-confirm-no-error 'clone-open-magit-status user repo dir)
    (magit-status-internal (s-chop-suffix "/" dir))))

(defun magithub-visit-thing ()
  (interactive)
  (user-error
   (with-temp-buffer
     (use-local-map magithub-map)
     (substitute-command-keys
      "Deprecated; use `\\[magithub-browse-thing]' instead"))))

(provide 'magithub)
;;; magithub.el ends here