Personal emacs config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

866 lines
35 KiB

  1. ;;; ghub.el --- minuscule client libraries for Git forge APIs -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2016-2020 Jonas Bernoulli
  3. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  4. ;; Homepage: https://github.com/magit/ghub
  5. ;; Keywords: tools
  6. ;; This file is not part of GNU Emacs.
  7. ;; This file is free software; you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation; either version 3, or (at your option)
  10. ;; any later version.
  11. ;; This file is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; For a copy of the GPL see https://www.gnu.org/licenses/gpl.txt.
  16. ;;; Commentary:
  17. ;; Ghub provides basic support for using the APIs of various Git forges
  18. ;; from Emacs packages. Originally it only supported the Github REST
  19. ;; API, but now it also supports the Github GraphQL API as well as the
  20. ;; REST APIs of Gitlab, Gitea, Gogs and Bitbucket.
  21. ;; Ghub abstracts access to API resources using only a handful of basic
  22. ;; functions such as `ghub-get'. These are convenience wrappers around
  23. ;; `ghub-request'. Additional forge-specific wrappers like `glab-put',
  24. ;; `gtea-put', `gogs-post' and `buck-delete' are also available. Ghub
  25. ;; does not provide any resource-specific functions, with the exception
  26. ;; of `FORGE-repository-id'.
  27. ;; When accessing Github, then Ghub handles the creation and storage of
  28. ;; access tokens using a setup wizard to make it easier for users to get
  29. ;; started. The tokens for other forges have to be created manually.
  30. ;; Ghub is intentionally limited to only provide these two essential
  31. ;; features — basic request functions and guided setup — to avoid being
  32. ;; too opinionated, which would hinder wide adoption. It is assumed that
  33. ;; wide adoption would make life easier for users and maintainers alike,
  34. ;; because then all packages that talk to forge APIs could be configured
  35. ;; the same way.
  36. ;; Please consult the manual (info "ghub") for more information.
  37. ;;; Code:
  38. (require 'auth-source)
  39. (require 'cl-lib)
  40. (require 'gnutls)
  41. (require 'json)
  42. (require 'let-alist)
  43. (require 'url)
  44. (require 'url-auth)
  45. (require 'url-http)
  46. (eval-when-compile
  47. (require 'subr-x))
  48. (defvar url-callback-arguments)
  49. (defvar url-http-end-of-headers)
  50. (defvar url-http-extra-headers)
  51. (defvar url-http-response-status)
  52. ;;; Settings
  53. (defconst ghub-default-host "api.github.com"
  54. "The default host that is used if `ghub.host' is not set.")
  55. (defvar ghub-github-token-scopes '(repo)
  56. "The Github API scopes that your private tools need.
  57. You have to manually create or update the token at
  58. https://github.com/settings/tokens. This variable
  59. only serves as documentation.")
  60. (defvar ghub-insecure-hosts nil
  61. "List of hosts that use http instead of https.")
  62. ;;; Request
  63. ;;;; Object
  64. (cl-defstruct (ghub--req
  65. (:constructor ghub--make-req)
  66. (:copier nil))
  67. (url nil :read-only nil)
  68. (forge nil :read-only t)
  69. (silent nil :read-only t)
  70. (method nil :read-only t)
  71. (headers nil :read-only t)
  72. (handler nil :read-only t)
  73. (unpaginate nil :read-only nil)
  74. (noerror nil :read-only t)
  75. (reader nil :read-only t)
  76. (callback nil :read-only t)
  77. (errorback nil :read-only t)
  78. (value nil :read-only nil)
  79. (extra nil :read-only nil))
  80. (defalias 'ghub-req-extra 'ghub--req-extra)
  81. ;;;; API
  82. (define-error 'ghub-error "Ghub/Url Error" 'error)
  83. (define-error 'ghub-http-error "HTTP Error" 'ghub-error)
  84. (defvar ghub-response-headers nil
  85. "The headers returned in response to the last request.
  86. `ghub-request' returns the response body and stores the
  87. response headers in this variable.")
  88. (cl-defun ghub-head (resource &optional params
  89. &key query payload headers
  90. silent unpaginate noerror reader
  91. username auth host
  92. callback errorback extra)
  93. "Make a `HEAD' request for RESOURCE, with optional query PARAMS.
  94. Like calling `ghub-request' (which see) with \"HEAD\" as METHOD."
  95. (ghub-request "HEAD" resource params
  96. :query query :payload payload :headers headers
  97. :silent silent :unpaginate unpaginate
  98. :noerror noerror :reader reader
  99. :username username :auth auth :host host
  100. :callback callback :errorback errorback :extra extra))
  101. (cl-defun ghub-get (resource &optional params
  102. &key query payload headers
  103. silent unpaginate noerror reader
  104. username auth host
  105. callback errorback extra)
  106. "Make a `GET' request for RESOURCE, with optional query PARAMS.
  107. Like calling `ghub-request' (which see) with \"GET\" as METHOD."
  108. (ghub-request "GET" resource params
  109. :query query :payload payload :headers headers
  110. :silent silent :unpaginate unpaginate
  111. :noerror noerror :reader reader
  112. :username username :auth auth :host host
  113. :callback callback :errorback errorback :extra extra))
  114. (cl-defun ghub-put (resource &optional params
  115. &key query payload headers
  116. silent unpaginate noerror reader
  117. username auth host
  118. callback errorback extra)
  119. "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
  120. Like calling `ghub-request' (which see) with \"PUT\" as METHOD."
  121. (ghub-request "PUT" resource params
  122. :query query :payload payload :headers headers
  123. :silent silent :unpaginate unpaginate
  124. :noerror noerror :reader reader
  125. :username username :auth auth :host host
  126. :callback callback :errorback errorback :extra extra))
  127. (cl-defun ghub-post (resource &optional params
  128. &key query payload headers
  129. silent unpaginate noerror reader
  130. username auth host
  131. callback errorback extra)
  132. "Make a `POST' request for RESOURCE, with optional payload PARAMS.
  133. Like calling `ghub-request' (which see) with \"POST\" as METHOD."
  134. (ghub-request "POST" resource params
  135. :query query :payload payload :headers headers
  136. :silent silent :unpaginate unpaginate
  137. :noerror noerror :reader reader
  138. :username username :auth auth :host host
  139. :callback callback :errorback errorback :extra extra))
  140. (cl-defun ghub-patch (resource &optional params
  141. &key query payload headers
  142. silent unpaginate noerror reader
  143. username auth host
  144. callback errorback extra)
  145. "Make a `PATCH' request for RESOURCE, with optional payload PARAMS.
  146. Like calling `ghub-request' (which see) with \"PATCH\" as METHOD."
  147. (ghub-request "PATCH" resource params
  148. :query query :payload payload :headers headers
  149. :silent silent :unpaginate unpaginate
  150. :noerror noerror :reader reader
  151. :username username :auth auth :host host
  152. :callback callback :errorback errorback :extra extra))
  153. (cl-defun ghub-delete (resource &optional params
  154. &key query payload headers
  155. silent unpaginate noerror reader
  156. username auth host
  157. callback errorback extra)
  158. "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
  159. Like calling `ghub-request' (which see) with \"DELETE\" as METHOD."
  160. (ghub-request "DELETE" resource params
  161. :query query :payload payload :headers headers
  162. :silent silent :unpaginate unpaginate
  163. :noerror noerror :reader reader
  164. :username username :auth auth :host host
  165. :callback callback :errorback errorback :extra extra))
  166. (cl-defun ghub-request (method resource &optional params
  167. &key query payload headers
  168. silent unpaginate noerror reader
  169. username auth host forge
  170. callback errorback value extra)
  171. "Make a request for RESOURCE and return the response body.
  172. Also place the response headers in `ghub-response-headers'.
  173. METHOD is the HTTP method, given as a string.
  174. RESOURCE is the resource to access, given as a string beginning
  175. with a slash.
  176. PARAMS, QUERY, PAYLOAD and HEADERS are alists used to specify
  177. data. The Github API documentation is vague on how data has
  178. to be transmitted and for a particular resource usually just
  179. talks about \"parameters\". Generally speaking when the METHOD
  180. is \"HEAD\" or \"GET\", then they have to be transmitted as a
  181. query, otherwise as a payload.
  182. Use PARAMS to automatically transmit like QUERY or PAYLOAD would
  183. depending on METHOD.
  184. Use QUERY to explicitly transmit data as a query.
  185. Use PAYLOAD to explicitly transmit data as a payload.
  186. Instead of an alist, PAYLOAD may also be a string, in which
  187. case it gets encoded as UTF-8 but is otherwise transmitted as-is.
  188. Use HEADERS for those rare resources that require that the data
  189. is transmitted as headers instead of as a query or payload.
  190. When that is the case, then the API documentation usually
  191. mentions it explicitly.
  192. If SILENT is non-nil, then don't message progress reports and
  193. the like.
  194. If UNPAGINATE is t, then make as many requests as necessary to
  195. get all values. If UNPAGINATE is a natural number, then get
  196. at most that many pages. For any other non-nil value raise
  197. an error.
  198. If NOERROR is non-nil, then do not raise an error if the request
  199. fails and return nil instead. If NOERROR is `return', then
  200. return the error payload instead of nil.
  201. If READER is non-nil, then it is used to read and return from the
  202. response buffer. The default is `ghub--read-json-payload'.
  203. For the very few resources that do not return JSON, you might
  204. want to use `ghub--decode-payload'.
  205. If USERNAME is non-nil, then make a request on behalf of that
  206. user. It is better to specify the user using the Git variable
  207. `github.user' for \"api.github.com\", or `github.HOST.user' if
  208. connecting to a Github Enterprise instance.
  209. Each package that uses `ghub' should use its own token. If AUTH
  210. is nil, then the generic `ghub' token is used instead. This
  211. is only acceptable for personal utilities. A packages that
  212. is distributed to other users should always use this argument
  213. to identify itself, using a symbol matching its name.
  214. Package authors who find this inconvenient should write a
  215. wrapper around this function and possibly for the
  216. method-specific functions as well.
  217. Some symbols have a special meaning. `none' means to make an
  218. unauthorized request. `basic' means to make a password based
  219. request. If the value is a string, then it is assumed to be
  220. a valid token. `basic' and an explicit token string are only
  221. intended for internal and debugging uses.
  222. If HOST is non-nil, then connect to that Github instance. This
  223. defaults to \"api.github.com\". When a repository is connected
  224. to a Github Enterprise instance, then it is better to specify
  225. that using the Git variable `github.host' instead of using this
  226. argument.
  227. If FORGE is `gitlab', then connect to Gitlab.com or, depending
  228. on HOST, to another Gitlab instance. This is only intended for
  229. internal use. Instead of using this argument you should use
  230. function `glab-request' and other `glab-*' functions.
  231. If CALLBACK and/or ERRORBACK is non-nil, then make one or more
  232. asynchronous requests and call CALLBACK or ERRORBACK when
  233. finished. If no error occurred, then call CALLBACK, unless
  234. that is nil.
  235. If an error occurred, then call ERRORBACK, or if that is nil,
  236. then CALLBACK. ERRORBACK can also be t, in which case an error
  237. is signaled instead. NOERROR is ignored for all asynchronous
  238. requests.
  239. Both callbacks are called with four arguments.
  240. 1. For CALLBACK, the combined value of the retrieved pages.
  241. For ERRORBACK, the error that occurred when retrieving the
  242. last page.
  243. 2. The headers of the last page as an alist.
  244. 3. Status information provided by `url-retrieve'. Its `:error'
  245. property holds the same information as ERRORBACK's first
  246. argument.
  247. 4. A `ghub--req' struct, which can be passed to `ghub-continue'
  248. (which see) to retrieve the next page, if any."
  249. (cl-assert (or (booleanp unpaginate) (natnump unpaginate)))
  250. (unless (string-prefix-p "/" resource)
  251. (setq resource (concat "/" resource)))
  252. (unless host
  253. (setq host (ghub--host forge)))
  254. (unless (or username (stringp auth) (eq auth 'none))
  255. (setq username (ghub--username host forge)))
  256. (cond ((not params))
  257. ((member method '("GET" "HEAD"))
  258. (when query
  259. (error "PARAMS and QUERY are mutually exclusive for METHOD %S"
  260. method))
  261. (setq query params))
  262. (t
  263. (when payload
  264. (error "PARAMS and PAYLOAD are mutually exclusive for METHOD %S"
  265. method))
  266. (setq payload params)))
  267. (when (or callback errorback)
  268. (setq noerror t))
  269. (ghub--retrieve
  270. (ghub--encode-payload payload)
  271. (ghub--make-req
  272. :url (url-generic-parse-url
  273. (concat (if (member host ghub-insecure-hosts) "http://" "https://")
  274. (if (and (equal resource "/graphql")
  275. (string-suffix-p "/v3" host))
  276. (substring host 0 -3)
  277. host)
  278. resource
  279. (and query (concat "?" (ghub--url-encode-params query)))))
  280. :forge forge
  281. :silent silent
  282. ;; Encode in case caller used (symbol-name 'GET). #35
  283. :method (encode-coding-string method 'utf-8)
  284. :headers (ghub--headers headers host auth username forge)
  285. :handler 'ghub--handle-response
  286. :unpaginate unpaginate
  287. :noerror noerror
  288. :reader reader
  289. :callback callback
  290. :errorback errorback
  291. :value value
  292. :extra extra)))
  293. (defun ghub-continue (req)
  294. "If there is a next page, then retrieve that.
  295. This function is only intended to be called from callbacks. If
  296. there is a next page, then retrieve that and return the buffer
  297. that the result will be loaded into, or t if the process has
  298. already completed. If there is no next page, then return nil.
  299. Callbacks are called with four arguments (see `ghub-request').
  300. The forth argument is a `ghub--req' struct, intended to be passed
  301. to this function. A callback may use the struct's `extra' slot
  302. to pass additional information to the callback that will be
  303. called after the next request has finished. Use the function
  304. `ghub-req-extra' to get and set the value of this slot."
  305. (and (assq 'next (ghub-response-link-relations req))
  306. (or (ghub--retrieve nil req) t)))
  307. (cl-defun ghub-wait (resource &optional duration
  308. &key username auth host forge)
  309. "Busy-wait up to DURATION seconds for RESOURCE to become available.
  310. DURATION specifies how many seconds to wait at most. It defaults
  311. to 64 seconds. The first attempt is made immediately, the second
  312. after two seconds, and each subsequent attempt is made after
  313. waiting as long again as we already waited between all preceding
  314. attempts combined.
  315. See `ghub-request' for information about the other arguments."
  316. (unless duration
  317. (setq duration 64))
  318. (with-local-quit
  319. (let ((total 0))
  320. (while (not (ghub-request "GET" resource nil
  321. :noerror t
  322. :username username
  323. :auth auth
  324. :host host
  325. :forge forge))
  326. (message "Waited (%3ss of %ss) for %s..." total duration resource)
  327. (if (= total duration)
  328. (error "%s is taking too long to create %s"
  329. (if forge (capitalize (symbol-name forge)) "Github")
  330. resource)
  331. (if (> total 0)
  332. (let ((wait (min total (- duration total))))
  333. (sit-for wait)
  334. (cl-incf total wait))
  335. (sit-for (setq total 2))))))))
  336. (defun ghub-response-link-relations (req &optional headers payload)
  337. "Return an alist of link relations in HEADERS.
  338. If optional HEADERS is nil, then return those that were
  339. previously stored in the variable `ghub-response-headers'.
  340. When accessing a Bitbucket instance then the link relations
  341. are in PAYLOAD instead of HEADERS, making their API merely
  342. RESTish and forcing this function to append those relations
  343. to the value of `ghub-response-headers', for later use when
  344. this function is called with nil for PAYLOAD."
  345. (if (eq (ghub--req-forge req) 'bitbucket)
  346. (if payload
  347. (let* ((page (cl-mapcan (lambda (key)
  348. (when-let ((elt (assq key payload)))
  349. (list elt)))
  350. '(size page pagelen next previous)))
  351. (headers (cons (cons 'link-alist page) headers)))
  352. (if (and req (or (ghub--req-callback req)
  353. (ghub--req-errorback req)))
  354. (setq-local ghub-response-headers headers)
  355. (setq-default ghub-response-headers headers))
  356. page)
  357. (cdr (assq 'link-alist ghub-response-headers)))
  358. (when-let ((rels (cdr (assoc "Link" (or headers ghub-response-headers)))))
  359. (mapcar (lambda (elt)
  360. (pcase-let ((`(,url ,rel) (split-string elt "; ")))
  361. (cons (intern (substring rel 5 -1))
  362. (substring url 1 -1))))
  363. (split-string rels ", ")))))
  364. (cl-defun ghub-repository-id (owner name &key username auth host forge noerror)
  365. "Return the id of the specified repository.
  366. Signal an error if the id cannot be determined."
  367. (let ((fn (cl-case forge
  368. ((nil ghub github) 'ghub--repository-id)
  369. (gitlab 'glab-repository-id)
  370. (gitea 'gtea-repository-id)
  371. (gogs 'gogs-repository-id)
  372. (bitbucket 'buck-repository-id)
  373. (t (intern (format "%s-repository-id" forge))))))
  374. (unless (fboundp fn)
  375. (error "ghub-repository-id: Forge type/abbreviation `%s' is unknown"
  376. forge))
  377. (or (funcall fn owner name :username username :auth auth :host host)
  378. (and (not noerror)
  379. (error "Repository %S does not exist on %S.\n%s%S?"
  380. (concat owner "/" name)
  381. (or host (ghub--host forge))
  382. "Maybe it was renamed and you have to update "
  383. "remote.<remote>.url")))))
  384. ;;;; Internal
  385. (defvar ghub-use-workaround-for-emacs-bug
  386. (and
  387. ;; Note: For build sans gnutls, `libgnutls-version' is -1.
  388. (>= libgnutls-version 30603)
  389. (or (version<= emacs-version "26.2")
  390. (eq system-type 'darwin))
  391. 'force)
  392. "Whether to use a kludge that hopefully works around an Emacs bug.
  393. In Emacs versions before 26.3 there is a bug that often but not
  394. always causes network connections to fail when using TLS1.3. It
  395. appears that even when using Emacs 26.3 the bug still exists but
  396. only on macOS.
  397. The workaround works by binding `gnutls-algorithm-priority' to
  398. \"NORMAL:-VERS-TLS1.3\" in `ghub--retrieve' around the call to
  399. `url-retrieve' or `url-retrieve-synchronously'. If you would
  400. like to use the same kludge for other uses of these functions,
  401. then you have to set this variable globally to the mentioned
  402. value.
  403. This variable controls whether the `ghub' package should use the
  404. kludge.
  405. - If nil, then never use the kludge.
  406. - If `force' then always use the kludge no matter what.
  407. - For any other non-nil value use the kludge, if and only if we
  408. believe that doing so is the correct thing to do.
  409. The default value of this variable is either nil or `forge'. It
  410. is `forge' if using libgnutls >=3.6.3 (the version introducing
  411. TLS1.3); AND also using Emacs < 26.3 and/or macOS (any version).
  412. If the value is any other non-nil value, then `ghub--retrieve'
  413. used the same logic as describe in the previous paragraph, but
  414. every time it is called. (This complication is mostly a historic
  415. accident, which we don't want to change because doing so would
  416. break this kludge for some users who have been relying on it for
  417. a while already.)
  418. For more information see https://github.com/magit/ghub/issues/81
  419. and https://debbugs.gnu.org/cgi/bugreport.cgi?bug=34341.")
  420. (cl-defun ghub--retrieve (payload req)
  421. (let ((url-request-extra-headers
  422. (let ((headers (ghub--req-headers req)))
  423. (if (functionp headers) (funcall headers) headers)))
  424. (url-request-method (ghub--req-method req))
  425. (url-request-data payload)
  426. (url-show-status nil)
  427. (url (ghub--req-url req))
  428. (handler (ghub--req-handler req))
  429. (silent (ghub--req-silent req))
  430. (gnutls-algorithm-priority
  431. (if (and ghub-use-workaround-for-emacs-bug
  432. (or (eq ghub-use-workaround-for-emacs-bug 'force)
  433. (and (not gnutls-algorithm-priority)
  434. (>= libgnutls-version 30603)
  435. (or (version<= emacs-version "26.2")
  436. (eq system-type 'darwin))
  437. (memq (ghub--req-forge req) '(github nil)))))
  438. "NORMAL:-VERS-TLS1.3"
  439. gnutls-algorithm-priority)))
  440. (if (or (ghub--req-callback req)
  441. (ghub--req-errorback req))
  442. (url-retrieve url handler (list req) silent)
  443. (with-current-buffer
  444. (url-retrieve-synchronously url silent)
  445. (funcall handler (car url-callback-arguments) req)))))
  446. (defun ghub--handle-response (status req)
  447. (let ((buffer (current-buffer)))
  448. (unwind-protect
  449. (progn
  450. (set-buffer-multibyte t)
  451. (let* ((unpaginate (ghub--req-unpaginate req))
  452. (headers (ghub--handle-response-headers status req))
  453. (payload (ghub--handle-response-payload req))
  454. (payload (ghub--handle-response-error status payload req))
  455. (value (ghub--handle-response-value payload req))
  456. (prev (ghub--req-url req))
  457. (next (cdr (assq 'next (ghub-response-link-relations
  458. req headers payload)))))
  459. (when (numberp unpaginate)
  460. (cl-decf unpaginate))
  461. (setf (ghub--req-url req)
  462. (url-generic-parse-url next))
  463. (setf (ghub--req-unpaginate req) unpaginate)
  464. (or (and next
  465. unpaginate
  466. (or (eq unpaginate t)
  467. (> unpaginate 0))
  468. (ghub-continue req))
  469. (let ((callback (ghub--req-callback req))
  470. (errorback (ghub--req-errorback req))
  471. (err (plist-get status :error)))
  472. (cond ((and err errorback)
  473. (setf (ghub--req-url req) prev)
  474. (funcall (if (eq errorback t)
  475. 'ghub--errorback
  476. errorback)
  477. err headers status req))
  478. (callback
  479. (funcall callback value headers status req))
  480. (t value))))))
  481. (when (buffer-live-p buffer)
  482. (kill-buffer buffer)))))
  483. (defun ghub--handle-response-headers (status req)
  484. (goto-char (point-min))
  485. (forward-line 1)
  486. (let (headers)
  487. (when (memq url-http-end-of-headers '(nil 0))
  488. (setq url-debug t)
  489. (let ((print-escape-newlines nil))
  490. (error "BUG: missing headers
  491. See https://github.com/magit/ghub/issues/81.
  492. url: %s
  493. headers: %S
  494. status: %S
  495. buffer: %S"
  496. (url-recreate-url (ghub--req-url req))
  497. url-http-end-of-headers
  498. status
  499. (current-buffer))))
  500. (while (re-search-forward "^\\([^:]*\\): \\(.+\\)"
  501. url-http-end-of-headers t)
  502. (push (cons (match-string 1)
  503. (match-string 2))
  504. headers))
  505. (setq headers (nreverse headers))
  506. (goto-char (1+ url-http-end-of-headers))
  507. (if (and req (or (ghub--req-callback req)
  508. (ghub--req-errorback req)))
  509. (setq-local ghub-response-headers headers)
  510. (setq-default ghub-response-headers headers))
  511. headers))
  512. (defun ghub--handle-response-error (status payload req)
  513. (let ((noerror (ghub--req-noerror req))
  514. (err (plist-get status :error)))
  515. (if err
  516. (if noerror
  517. (if (eq noerror 'return)
  518. payload
  519. (setcdr (last err) (list payload))
  520. nil)
  521. (ghub--signal-error err payload req))
  522. payload)))
  523. (defun ghub--signal-error (err &optional payload req)
  524. (pcase-let ((`(,symb . ,data) err))
  525. (if (eq symb 'error)
  526. (if (eq (car-safe data) 'http)
  527. (signal 'ghub-http-error
  528. (let ((code (car (cdr-safe data))))
  529. (list code
  530. (nth 2 (assq code url-http-codes))
  531. (and req (url-filename (ghub--req-url req)))
  532. payload)))
  533. (signal 'ghub-error data))
  534. (signal symb data))))
  535. (defun ghub--errorback (err _headers _status req)
  536. (ghub--signal-error err (nth 3 err) req))
  537. (defun ghub--handle-response-value (payload req)
  538. (setf (ghub--req-value req)
  539. (nconc (ghub--req-value req)
  540. (if-let ((nested (and (eq (ghub--req-forge req) 'bitbucket)
  541. (assq 'values payload))))
  542. (cdr nested)
  543. payload))))
  544. (defun ghub--handle-response-payload (req)
  545. (funcall (or (ghub--req-reader req)
  546. 'ghub--read-json-payload)
  547. url-http-response-status))
  548. (defun ghub--read-json-payload (_status)
  549. (let ((raw (ghub--decode-payload)))
  550. (and raw
  551. (condition-case nil
  552. (let ((json-object-type 'alist)
  553. (json-array-type 'list)
  554. (json-key-type 'symbol)
  555. (json-false nil)
  556. (json-null nil))
  557. (json-read-from-string raw))
  558. (json-readtable-error
  559. `((message
  560. . ,(if (looking-at "<!DOCTYPE html>")
  561. (if (re-search-forward
  562. "<p>\\(?:<strong>\\)?\\([^<]+\\)" nil t)
  563. (match-string 1)
  564. "error description missing")
  565. (string-trim (buffer-substring (point) (point-max)))))
  566. (documentation_url
  567. . "https://github.com/magit/ghub/wiki/Github-Errors")))))))
  568. (defun ghub--decode-payload (&optional _status)
  569. (and (not (eobp))
  570. (decode-coding-string
  571. (buffer-substring-no-properties (point) (point-max))
  572. 'utf-8)))
  573. (defun ghub--encode-payload (payload)
  574. (and payload
  575. (progn
  576. (unless (stringp payload)
  577. ;; Unfortunately `json-encode-list' may modify the input.
  578. ;; See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=40693.
  579. ;; and https://github.com/magit/forge/issues/267
  580. (setq payload (json-encode-list (copy-tree payload))))
  581. (encode-coding-string payload 'utf-8))))
  582. (defun ghub--url-encode-params (params)
  583. (mapconcat (lambda (param)
  584. (pcase-let ((`(,key . ,val) param))
  585. (concat (url-hexify-string (symbol-name key)) "="
  586. (if (integerp val)
  587. (number-to-string val)
  588. (url-hexify-string val)))))
  589. params "&"))
  590. ;;; Authentication
  591. ;;;; API
  592. ;;;###autoload
  593. (defun ghub-clear-caches ()
  594. "Clear all caches that might negatively affect Ghub.
  595. If a library that is used by Ghub caches incorrect information
  596. such as a mistyped password, then that can prevent Ghub from
  597. asking the user for the correct information again.
  598. Set `url-http-real-basic-auth-storage' to nil
  599. and call `auth-source-forget+'."
  600. (interactive)
  601. (setq url-http-real-basic-auth-storage nil)
  602. (auth-source-forget+))
  603. ;;;; Internal
  604. (defun ghub--headers (headers host auth username forge)
  605. (push (cons "Content-Type" "application/json") headers)
  606. (if (eq auth 'none)
  607. headers
  608. (unless (or username (stringp auth))
  609. (setq username (ghub--username host forge)))
  610. (lambda ()
  611. (if (eq auth 'basic)
  612. (cons (cons "Authorization" (ghub--basic-auth host username))
  613. headers)
  614. (cons (ghub--auth host auth username forge) headers)))))
  615. (defun ghub--auth (host auth &optional username forge)
  616. (unless username
  617. (setq username (ghub--username host forge)))
  618. (if (eq auth 'basic)
  619. (cl-ecase forge
  620. ((nil gitea gogs bitbucket)
  621. (cons "Authorization" (ghub--basic-auth host username)))
  622. ((github gitlab)
  623. (error "%s does not support basic authentication"
  624. (capitalize (symbol-name forge)))))
  625. (cons (cl-ecase forge
  626. ((nil github gitea gogs bitbucket)
  627. "Authorization")
  628. (gitlab
  629. "Private-Token"))
  630. (if (eq forge 'bitbucket)
  631. ;; For some undocumented reason Bitbucket supports
  632. ;; values of the form "token <token>" only for GET
  633. ;; requests. For PUT requests we have to use basic
  634. ;; authentication. Note that the secret is a token
  635. ;; (aka "app password"), not the actual password.
  636. ;; The documentation fails to mention this little
  637. ;; detail. See #97.
  638. (concat "Basic "
  639. (base64-encode-string
  640. (concat username ":"
  641. (ghub--token host username auth nil forge))
  642. t))
  643. (concat
  644. (and (not (eq forge 'gitlab)) "token ")
  645. (encode-coding-string
  646. (cl-typecase auth
  647. (string auth)
  648. (null (ghub--token host username 'ghub nil forge))
  649. (symbol (ghub--token host username auth nil forge))
  650. (t (signal 'wrong-type-argument
  651. `((or stringp symbolp) ,auth))))
  652. 'utf-8))))))
  653. (defun ghub--basic-auth (host username)
  654. (let ((url (url-generic-parse-url
  655. (if (member host ghub-insecure-hosts) "http://" "https://"))))
  656. (setf (url-user url) username)
  657. (url-basic-auth url t)))
  658. (defun ghub--token (host username package &optional nocreate forge)
  659. (let* ((user (ghub--ident username package))
  660. (token
  661. (or (car (ghub--auth-source-get (list :secret)
  662. :host host :user user))
  663. (progn
  664. ;; Auth-Source caches the information that there is no
  665. ;; value, but in our case that is a situation that needs
  666. ;; fixing so we want to keep trying by invalidating that
  667. ;; information.
  668. ;; The (:max 1) is needed and has to be placed at the
  669. ;; end for Emacs releases before 26.1. #24 #64 #72
  670. (auth-source-forget (list :host host :user user :max 1))
  671. (and (not nocreate)
  672. (error "\
  673. Required %s token (%S for %S) does not exist.
  674. See https://magit.vc/manual/ghub/Getting-Started.html
  675. or (info \"(ghub)Getting Started\") for instructions.
  676. \(The setup wizard no longer exists.)"
  677. (capitalize (symbol-name (or forge 'github)))
  678. user host))))))
  679. (if (functionp token) (funcall token) token)))
  680. (cl-defmethod ghub--host (&optional forge)
  681. (cl-ecase forge
  682. ((nil github)
  683. (or (ignore-errors (car (process-lines "git" "config" "github.host")))
  684. ghub-default-host))
  685. (gitlab
  686. (or (ignore-errors (car (process-lines "git" "config" "gitlab.host")))
  687. (bound-and-true-p glab-default-host)))
  688. (gitea
  689. (or (ignore-errors (car (process-lines "git" "config" "gitea.host")))
  690. (bound-and-true-p gtea-default-host)))
  691. (gogs
  692. (or (ignore-errors (car (process-lines "git" "config" "gogs.host")))
  693. (bound-and-true-p gogs-default-host)))
  694. (bitbucket
  695. (or (ignore-errors (car (process-lines "git" "config" "bitbucket.host")))
  696. (bound-and-true-p buck-default-host)))))
  697. (cl-defmethod ghub--username (host &optional forge)
  698. (let ((var
  699. (cl-ecase forge
  700. ((nil github)
  701. (if (equal host ghub-default-host)
  702. "github.user"
  703. (format "github.%s.user" host)))
  704. (gitlab
  705. (if (equal host "gitlab.com/api/v4")
  706. "gitlab.user"
  707. (format "gitlab.%s.user" host)))
  708. (bitbucket
  709. (if (equal host "api.bitbucket.org/2.0")
  710. "bitbucket.user"
  711. (format "bitbucket.%s.user" host)))
  712. (gitea
  713. (when (zerop (call-process "git" nil nil nil "config" "gitea.host"))
  714. (error "gitea.host is set but always ignored"))
  715. (format "gitea.%s.user" host))
  716. (gogs
  717. (when (zerop (call-process "git" nil nil nil "config" "gogs.host"))
  718. (error "gogs.host is set but always ignored"))
  719. (format "gogs.%s.user" host)))))
  720. (condition-case nil
  721. (car (process-lines "git" "config" var))
  722. (error
  723. (let ((user (read-string
  724. (format "Git variable `%s' is unset. Set to: " var))))
  725. (if (equal user "")
  726. (user-error "The empty string is not a valid username")
  727. (call-process
  728. "git" nil nil nil "config"
  729. (and (eq (read-char-choice
  730. (format
  731. "Set %s=%s [g]lobally (recommended) or [l]ocally? "
  732. var user)
  733. (list ?g ?l))
  734. ?g)
  735. "--global")
  736. var user)
  737. user))))))
  738. (defun ghub--ident (username package)
  739. (format "%s^%s" username package))
  740. (defun ghub--auth-source-get (keys &rest spec)
  741. (declare (indent 1))
  742. (let ((plist (car (apply #'auth-source-search
  743. (append spec (list :max 1))))))
  744. (mapcar (lambda (k)
  745. (plist-get plist k))
  746. keys)))
  747. (when (version< emacs-version "26.2")
  748. ;; Fixed by Emacs commit 60ff8101449eea3a5ca4961299501efd83d011bd.
  749. (advice-add 'auth-source-netrc-parse-next-interesting :around
  750. 'auth-source-netrc-parse-next-interesting@save-match-data)
  751. (defun auth-source-netrc-parse-next-interesting@save-match-data (fn)
  752. "Save match-data for the benefit of caller `auth-source-netrc-parse-one'.
  753. Without wrapping this function in `save-match-data' the caller
  754. won't see the secret from a line that is followed by a commented
  755. line."
  756. (save-match-data (funcall fn))))
  757. (advice-add 'url-http-handle-authentication :around
  758. 'url-http-handle-authentication@unauthorized-bugfix)
  759. (defun url-http-handle-authentication@unauthorized-bugfix (fn proxy)
  760. "If authorization failed then don't try again but fail properly.
  761. For Emacs 27.1 prevent a useful `http' error from being replaced
  762. by a generic one that omits all useful information. For earlier
  763. releases prevent a new request from being made, which would
  764. either result in an infinite loop or (e.g. in the case of `ghub')
  765. the user being asked for their name."
  766. (if (assoc "Authorization" url-http-extra-headers)
  767. t ; Return "success", here also known as "successfully failed".
  768. (funcall fn proxy)))
  769. ;;; _
  770. (provide 'ghub)
  771. (require 'ghub-graphql)
  772. ;;; ghub.el ends here