about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/magit-20180913.1247/magit-collab.el
blob: 7c4b1a267104739c957a4600ad060140dc2d95e9 (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
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
;;; magit-collab.el --- collaboration tools       -*- lexical-binding: t -*-

;; Copyright (C) 2010-2018  The Magit Project Contributors
;;
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors.  If not, see http://magit.vc/authors.

;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>

;; Magit 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, or (at your option)
;; any later version.
;;
;; Magit 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 Magit.  If not, see http://www.gnu.org/licenses.

;;; Commentary:

;; This library implements various collaboration tools.  These tools
;; are only early incarnation -- implementing collaboration tools is
;; a top priority for future development.

;; Currently these tools (including `magit-branch-pull-request', which
;; is defined elsewhere) only support Github, but support for other
;; Git forges as well as mailing list based collaboration is in
;; planning.

;;; Code:

(require 'magit)
(require 'ghub)

;;; Variables

(defvar magit-github-token-scopes '(repo)
  "The Github API scopes needed by Magit.

`repo' is the only required scope.  Without this scope none of
Magit's features that use the API work.  Instead of this scope
you could use `public_repo' if you are only interested in public
repositories.

`repo' Grants read/write access to code, commit statuses,
  invitations, collaborators, adding team memberships, and
  deployment statuses for public and private repositories
  and organizations.

`public_repo' Grants read/write access to code, commit statuses,
  collaborators, and deployment statuses for public repositories
  and organizations. Also required for starring public
  repositories.")

;;; Commands

;;;###autoload
(defun magit-browse-pull-request (pr)
  "Visit pull-request PR using `browse-url'.

Currently this only supports Github, but that restriction will
be lifted eventually to support other Git forges."
  (interactive (list (magit-read-pull-request "Visit pull request")))
  (browse-url (format "https://github.com/%s/pull/%s"
                      (--> pr
                           (cdr (assq 'base it))
                           (cdr (assq 'repo it))
                           (cdr (assq 'full_name it)))
                      (cdr (assq 'number pr)))))

;;; Utilities

(defun magit-read-pull-request (prompt)
  "Read a pull request from the user, prompting with PROMPT.
Return the Git forge's API response.  Currently this function
only supports Github, but that will change eventually."
  (let* ((origin (magit-upstream-repository))
         (id     (magit--forge-id origin))
         (fmtfun (lambda (pull-request)
                   (format "%s  %s"
                           (cdr (assq 'number pull-request))
                           (cdr (assq 'title  pull-request)))))
         (prs    (ghub-get (format "/repos/%s/pulls" id) nil :auth 'magit))
         (choice (magit-completing-read
                  prompt (mapcar fmtfun prs) nil nil nil nil
                  (let ((default (thing-at-point 'github-pull-request)))
                    (and default (funcall fmtfun default)))))
         (number (and (string-match "\\([0-9]+\\)" choice)
                      (string-to-number (match-string 1 choice)))))
    (and number
         ;; Don't reuse the pr from the list, it lacks some information
         ;; that is only returned when requesting a single pr.  #3371
         (ghub-get (format "/repos/%s/pulls/%s" id number)
                   nil :auth 'magit))))

(defun magit-upstream-repository ()
  "Return the remote name of the upstream repository.

If the Git variable `magit.upstream' is set, then return its
value.  Otherwise return \"origin\".  If the remote does not
exist, then raise an error."
  (let ((remote (or (magit-get "magit.upstream") "origin")))
    (unless (magit-remote-p remote)
      (error "No remote named `%s' exists (consider setting `magit.upstream')"
             remote))
    (unless (magit--github-remote-p remote)
      (error "Currently only Github is supported"))
    remote))

(defun magit--forge-id (remote)
  (let ((url (magit-get "remote" remote "url")))
    (and (string-match "\\([^:/]+/[^/]+?\\)\\(?:\\.git\\)?\\'" url)
         (match-string 1 url))))

(defconst magit--github-url-regexp "\
\\`\\(?:git://\\|git@\\|ssh://git@\\|https://\\)\
\\(.*?\\)[/:]\
\\(\\([^:/]+\\)/\\([^/]+?\\)\\)\
\\(?:\\.git\\)?\\'")

(defun magit--github-url-p (url)
  (save-match-data
    (and url
         (string-match magit--github-url-regexp url)
         (let ((host (match-string 1 url)))
           ;; Match values like "github.com-as-someone", which are
           ;; translated to just "github.com" according to settings
           ;; in "~/.ssh/config".  Theoretically this could result
           ;; in false-positives, but that's rather unlikely.  #3392
           (and (or (string-match-p (regexp-quote "github.com") host)
                    (string-match-p (regexp-quote (ghub--host)) host))
                host)))))

(defun magit--github-remote-p (remote)
  (or (--when-let (magit-git-string "remote" "get-url" "--push" remote)
        (magit--github-url-p it))
      (--when-let (magit-git-string "remote" "get-url" "--all" remote)
        (magit--github-url-p it))))

(defun magit--github-url-equal (r1 r2)
  (or (equal r1 r2)
      (save-match-data
        (let ((n1 (and (string-match magit--github-url-regexp r1)
                       (match-string 2 r1)))
              (n2 (and (string-match magit--github-url-regexp r2)
                       (match-string 2 r2))))
          (and n1 n2 (equal n1 n2))))))

(defun magit--pullreq-from-upstream-p (pr)
  (let-alist pr
    (equal .head.repo.full_name
           .base.repo.full_name)))

(defun magit--pullreq-branch (pr &optional assert-new)
  (let-alist pr
    (let ((branch .head.ref))
      (when (and (not (magit--pullreq-from-upstream-p pr))
                 (or (not .maintainer_can_modify)
                     (magit-branch-p branch)))
        (setq branch (format "pr-%s" .number)))
      (when (and assert-new (magit-branch-p branch))
        (user-error "Branch `%s' already exists" branch))
      branch)))

(provide 'magit-collab)
;;; magit-collab.el ends here