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.

1212 lines
51 KiB

  1. This is ghub.info, produced by makeinfo version 6.7 from ghub.texi.
  2. Copyright (C) 2017-2021 Jonas Bernoulli <jonas@bernoul.li>
  3. You can redistribute this document and/or modify it under the terms
  4. of the GNU General Public License as published by the Free Software
  5. Foundation, either version 3 of the License, or (at your option)
  6. any later version.
  7. This document is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. General Public License for more details.
  11. INFO-DIR-SECTION Emacs
  12. START-INFO-DIR-ENTRY
  13. * Ghub: (ghub). Minuscule client library for the Github API.
  14. END-INFO-DIR-ENTRY
  15. 
  16. File: ghub.info, Node: Top, Next: Introduction, Up: (dir)
  17. Ghub User and Developer Manual
  18. ******************************
  19. Ghub is an Emacs library that is used by various packages to access the
  20. APIs of various instances of various Git forge implementations.
  21. This manual is for Ghub version 3.5.2.
  22. Copyright (C) 2017-2021 Jonas Bernoulli <jonas@bernoul.li>
  23. You can redistribute this document and/or modify it under the terms
  24. of the GNU General Public License as published by the Free Software
  25. Foundation, either version 3 of the License, or (at your option)
  26. any later version.
  27. This document is distributed in the hope that it will be useful,
  28. but WITHOUT ANY WARRANTY; without even the implied warranty of
  29. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  30. General Public License for more details.
  31. * Menu:
  32. * Introduction::
  33. * Getting Started::
  34. * API::
  35. * Notes::
  36. * Function Index::
  37. * Variable Index::
  38. — The Detailed Node Listing —
  39. Getting Started
  40. * Basic Concepts, Arguments and Variables: Basic Concepts Arguments and Variables.
  41. * Setting the Username::
  42. * Creating and Storing a Token::
  43. * Github Configuration Variables::
  44. Creating and Storing a Token
  45. * Creating a Token::
  46. * Storing a Token::
  47. API
  48. * Their APIs::
  49. * Making REST Requests::
  50. * Making GraphQL Requests::
  51. * Github Convenience Wrappers::
  52. * Non-Github Convenience Wrappers::
  53. Notes
  54. * Using Ghub in Personal Scripts::
  55. * Using Ghub in Your Own Package::
  56. * Forge Limitations and Notes::
  57. 
  58. File: ghub.info, Node: Introduction, Next: Getting Started, Prev: Top, Up: Top
  59. 1 Introduction
  60. **************
  61. Ghub is an Emacs library that is used by various packages to access the
  62. APIs of various instances of various Git forge implementations.
  63. A forge is a web-based collaborative software platform for developing
  64. and distributing computer applications. Examples include Github and
  65. Gitlab.
  66. 
  67. File: ghub.info, Node: Getting Started, Next: API, Prev: Introduction, Up: Top
  68. 2 Getting Started
  69. *****************
  70. This manual guides you through the steps that are necessary to use the
  71. Forge package and/or to make a request using just Ghub itself, such as
  72. this:
  73. (ghub-request "GET" "/user")
  74. To be able to do that, Ghub needs to know who you want to talk to,
  75. who you are, and how you are going to prove the latter to the former.
  76. Additionally Ghub wants to know on behalf of which Emacs package it
  77. is making a request. So the question of "who is making the request" has
  78. to be rephrased as "which human (or bot) is using what (Emacs) package
  79. to make the request". If for example, the human known as "tarsius" is
  80. using the ‘forge’ package, then that is represented in some places using
  81. the string "tarsius^forge".
  82. This package used to attempt to get the answers to these questions
  83. using a setup wizard. Unfortunately that had to be removed because (a)
  84. it only ever supported Github, (b) Github is about to remove support for
  85. that on their end, (c) it did not always work, and (d) when it couldn’t
  86. be used, or failed, then it made things _more_ complicated.
  87. So now it is necessary for users to read some documentation and
  88. because many things can go wrong, those instructions have to be fairly
  89. detailed. You can of course skip over most of this, but if things go
  90. wrong, then I would like to kindly request that you take another look
  91. before asking me for help.
  92. * Menu:
  93. * Basic Concepts, Arguments and Variables: Basic Concepts Arguments and Variables.
  94. * Setting the Username::
  95. * Creating and Storing a Token::
  96. * Github Configuration Variables::
  97. 
  98. File: ghub.info, Node: Basic Concepts Arguments and Variables, Next: Setting the Username, Up: Getting Started
  99. 2.1 Basic Concepts, Arguments and Variables
  100. ===========================================
  101. Originally Ghub supported only Github but now it also supports Gitlab,
  102. Gitea, Gogs and Bitbucket. For the historic reason just given, the
  103. function ‘ghub-request’ defaults to acting on a ‘github’ forge, but can
  104. be told to act on another forge using the FORGE argument.
  105. The FORGE argument only specifies what kind of forge to act on, not
  106. which instance. The HOST argument can be used to select the instance.
  107. For some forges a default instance is defined:
  108. • Forge ‘github’ defaults to host ‘api.github.com’.
  109. • Forge ‘gitlab’ defaults to host ‘gitlab.com/api/v4’.
  110. • Forge ‘bitbucket’ defaults to host ‘api.bitbucket.org/2.0’.
  111. • No canonical host exists for the ‘gitea’ and ‘gogs’ forges and
  112. ‘localhost:3000/api/v1’ is used as the default host in both cases.
  113. Together the FORGE and HOST arguments specify the forge type and
  114. instance. In addition to that, it is also necessary to specify on whose
  115. behalf the request is being made, which can be done using the USERNAME
  116. and AUTH arguments. For example:
  117. (ghub-request "GET" "/user" nil
  118. :forge 'github
  119. :host "api.github.com"
  120. :username "tarsius"
  121. :auth 'forge)
  122. Having to specify these arguments for every request is inconvenient.
  123. Additional variables and convenience functions can be used to make that
  124. unnecessary in most cases.
  125. (But for debugging purposes the above explicit form is very useful.
  126. You will obviously have to change the value of USERNAME and you should
  127. use ‘ghub’ as AUTH when first trying this at home.)
  128. These variables can be set globally and/or for a specific repository.
  129. • For "api.github.com" (aka the API of <https://github.com>) the Git
  130. variable ‘github.user’ specifies the user.
  131. • For another ‘github’ instance the Git variable ‘github.HOST.user’
  132. specifies the user. The HOST in that variable name is the same as
  133. the value of the HOST argument of the called function.
  134. • Instead of specifying the HOST in every function call, the Git
  135. variable ‘github.host’ can be used. This should only be set
  136. locally.
  137. These ‘github’ specific variables are discussed in more detail in
  138. *note Github Configuration Variables::.
  139. For ‘gitlab’ and ‘bitbucket’ forges similar variables are available:
  140. • ‘gitlab.user’ specifies the <https://gitlab.com> user.
  141. • ‘gitlab.HOST.user’ specifies the user for the HOST ‘gitlab’
  142. instance.
  143. • ‘gitlab.host’ specifies the ‘gitlab’ host, unless the HOST argument
  144. is non-nil
  145. • ‘bitbucket.user’ specifies the <https://bitbucket.org> user.
  146. • ‘bitbucket.HOST.user’ specifies the user for the HOST ‘bitbucket’
  147. instance.
  148. • ‘bitbucket.host’ specifies the ‘bitbucket’ host, unless the HOST
  149. argument is non-nil.
  150. For the ‘gitea’ and ‘gogs’ forges some similar variables are
  151. available, however for some of the ‘ghub.*’ variables no equivalent
  152. variable exist for these two forges:
  153. • ‘gitea.user’ is *not* used because no canonical ‘gitea’ instance
  154. exists.
  155. • ‘gitea.HOST.user’ specifies the user for the HOST ‘gitea’ instance.
  156. • ‘gitea.host’ specifies the ‘gitea’ host, unless the HOST argument
  157. is non-nil
  158. • ‘gogs.user’ is *not* used because no canonical ‘gitea’ instance
  159. exists.
  160. • ‘gogs.HOST.user’ specifies the user for the HOST ‘gogs’ instance.
  161. • ‘gogs.host’ specifies the ‘gogs’ host, unless the HOST argument is
  162. non-nil
  163. 
  164. File: ghub.info, Node: Setting the Username, Next: Creating and Storing a Token, Prev: Basic Concepts Arguments and Variables, Up: Getting Started
  165. 2.2 Setting the Username
  166. ========================
  167. Ghub needs to know your username that you use on the host that you want
  168. it to connect to. For each host a different Git variable has to be set
  169. to specify the username on that host. More than one variable is needed
  170. because you might use different usernames on different hosts.
  171. 2.2.1 Setting your Github.com Username
  172. --------------------------------------
  173. To inform Ghub about your "github.com" username do this:
  174. git config --global github.user USERNAME
  175. If you need to identify as another user in a particular repository,
  176. then you have to set that variable locally:
  177. cd /path/to/repo
  178. git config --local github.user USERNAME
  179. 2.2.2 Setting your Gitlab.com Username
  180. --------------------------------------
  181. To inform Ghub about your "gitlab.com" username do this:
  182. git config --global gitlab.user USERNAME
  183. If you need to identify as another user in a particular repository,
  184. then you have to set that variable locally:
  185. cd /path/to/repo
  186. git config --local gitlab.user USERNAME
  187. Make sure you use the correct USERNAME for this forge/host. It might
  188. not be the same as on "github.com"!
  189. 2.2.3 Setting your Github Enterprise Username
  190. ---------------------------------------------
  191. For Github Enterprise instances you have to specify where the API can be
  192. accessed and a different variable has to be used to set the username.
  193. For example if the API is available at ‘https://example.com/api/v3’,
  194. then you should do this:
  195. git config --global github.example.com/api/v3.user USERNAME
  196. Make sure you use the correct USERNAME for this instance. It might
  197. not be the same as on "github.com"!
  198. Doing this only tells Ghub who you are on this host, additionally you
  199. have to tell Ghub which repository are connected to that forge/host,
  200. like so:
  201. cd /path/to/repo
  202. git config --local github.host example.com/api/v3
  203. 2.2.4 Setting your Username for Other Hosts and/or Forges
  204. ---------------------------------------------------------
  205. To inform Ghub about your username on HOST (a FORGE instance) do this:
  206. git config --global FORGE.HOST.user USERNAME
  207. FORGE can be one of ‘bitbucket’, ‘gitea’ or ‘gogs’. It can also be
  208. ‘github’ or ‘gitlab’; but if that is the case, then you should look at
  209. the preceding sections instead, which discuss these cases specifically.
  210. HOST identifies the instance. This actually points at the top-level
  211. endpoint of the API and may contain path components, e.g.:
  212. ‘example.com/api’.
  213. If you need to identify as another user in a particular repository,
  214. then you have to set that variable locally:
  215. cd /path/to/repo
  216. git config --local FORGE.HOST.user USERNAME
  217. 
  218. File: ghub.info, Node: Creating and Storing a Token, Next: Github Configuration Variables, Prev: Setting the Username, Up: Getting Started
  219. 2.3 Creating and Storing a Token
  220. ================================
  221. * Menu:
  222. * Creating a Token::
  223. * Storing a Token::
  224. 
  225. File: ghub.info, Node: Creating a Token, Next: Storing a Token, Up: Creating and Storing a Token
  226. 2.3.1 Creating a Token
  227. ----------------------
  228. To create a token, use the web interface of the forge/host you want to
  229. connect to. Here is a list of pages to do this for certain popular
  230. hosts:
  231. • <https://github.com/settings/tokens>
  232. • <https://gitlab.com/profile/personal_access_tokens>
  233. For other forges we cannot provide a functioning URL because they
  234. contain unknown values such as your name. Just go to the general
  235. settings page of the respective host and then go from there.
  236. Except on ‘gitea’ and ‘gogs’ each token can be limited to certain
  237. "scopes", i.e., it is possible to limit for which purposes any given
  238. token can be used.
  239. Before you create a token to be used for a certain package, you
  240. should consult the documentation of that package, which in turn should
  241. tell you which scopes are needed and why. The Forge package for example
  242. does so in *note (forge)Token Creation::.
  243. 
  244. File: ghub.info, Node: Storing a Token, Prev: Creating a Token, Up: Creating and Storing a Token
  245. 2.3.2 Storing a Token
  246. ---------------------
  247. Please also see *note (auth)Top:: for all the gory details about
  248. Auth-Source.
  249. The variable ‘auth-sources’ controls how and where Auth-Source keeps
  250. its secrets. The default value is a list of three files:
  251. ‘("~/.authinfo" "~/.authinfo.gpg" "~/.netrc")’, but to avoid confusion
  252. you should make sure that only one of these files exists and then you
  253. should also adjust the value of the variable to only ever use that file,
  254. for example:
  255. (setq auth-sources '("~/.authinfo"))
  256. In ‘~/.authinfo’ secrets are stored in plain text. If you don’t want
  257. that, then you should use the encrypted ‘~/.authinfo.gpg’ instead:
  258. (setq auth-sources '("~/.authinfo.gpg"))
  259. Auth-Source also supports storing secrets in various external
  260. key-chains. See *note (auth)Top:: for more information.
  261. The default Auth-Source backends only support storing three values
  262. per entry; the "machine", the "login" and the "password". Because Ghub
  263. uses separate tokens for each package, it has to squeeze four values
  264. into those three slots, and it does that by using "USERNAME^PACKAGE" as
  265. the "login".
  266. Assuming your *Github* username is "ziggy", the package is named
  267. "forge", and you want to access *Github.com* with the *token*
  268. "012345abcdef...", an entry in one of the three mentioned files would
  269. then look like this:
  270. machine api.github.com login ziggy^forge password 012345abcdef...
  271. Assuming your *Gitlab* username is "ziggy", the package is named
  272. "forge", and you want to access *Gitlab.com* with the *token*
  273. "012345abcdef...", an entry in one of the three mentioned files would
  274. then look like this:
  275. machine gitlab.com/api/v4 login ziggy^forge password 012345abcdef...
  276. 
  277. File: ghub.info, Node: Github Configuration Variables, Prev: Creating and Storing a Token, Up: Getting Started
  278. 2.4 Github Configuration Variables
  279. ==================================
  280. The username and, unless you only use Github.com itself, the Github
  281. Enterprise instance have to be configured using Git variables. In rare
  282. cases it might also be necessary to specify the identity of the local
  283. machine, which is done using a lisp variable.
  284. -- Variable: github.user
  285. The Github.com username. This should be set globally and if you
  286. have multiple Github.com user accounts, then you should set this
  287. locally only for those repositories that you want to access using
  288. the secondary identity.
  289. -- Variable: github.HOST.user
  290. This variable serves the same purpose as ‘github.user’ but for the
  291. Github Enterprise instance identified by ‘HOST’.
  292. The reason why separate variables are used is that this makes it
  293. possible to set both values globally instead of having to set one
  294. of the values locally in each and every repository that is
  295. connected to the Github Enterprise instance, not Github.com.
  296. -- Variable: github.host
  297. This variable should only be set locally for a repository and
  298. specifies the Github Enterprise edition that that repository is
  299. connected to. You should not set this globally because then each
  300. and every repository becomes connected to the specified Github
  301. Enterprise instance, including those that should actually be
  302. connected to Github.com.
  303. When this is undefined, then "api.github.com" is used (defined in
  304. the constant ‘ghub-default-host’, which you should never attempt to
  305. change.)
  306. 
  307. File: ghub.info, Node: API, Next: Notes, Prev: Getting Started, Up: Top
  308. 3 API
  309. *****
  310. * Menu:
  311. * Their APIs::
  312. * Making REST Requests::
  313. * Making GraphQL Requests::
  314. * Github Convenience Wrappers::
  315. * Non-Github Convenience Wrappers::
  316. 
  317. File: ghub.info, Node: Their APIs, Next: Making REST Requests, Up: API
  318. 3.1 Their APIs
  319. ==============
  320. Of course this manual does not cover the APIs of all forges that it
  321. supports, but for your convenience, here are the links to their API
  322. manuals:
  323. • Github:
  324. • <https://developer.github.com/v4> (GraphQl)
  325. • <https://developer.github.com/v3> (REST)
  326. • Gitlab:
  327. • <https://docs.gitlab.com/ee/api/README.html>
  328. • Gitea:
  329. • <https://docs.gitea.io/en-us/api-usage>
  330. • <https://try.gitea.io/api/swagger>
  331. • Gogs:
  332. • <https://github.com/gogs/go-gogs-client/wiki>
  333. • Bitbucket:
  334. • <https://developer.atlassian.com/bitbucket/api/2/reference>
  335. 
  336. File: ghub.info, Node: Making REST Requests, Next: Making GraphQL Requests, Prev: Their APIs, Up: API
  337. 3.2 Making REST Requests
  338. ========================
  339. -- Function: ghub-request method resource &optional params &key query
  340. payload headers unpaginate noerror reader username auth host
  341. callback errorback url value error extra method*
  342. This function makes a request for ‘RESOURCE’ using ‘METHOD’.
  343. ‘PARAMS’, ‘QUERY’, ‘PAYLOAD’ and/or ‘HEADERS’ are alists holding
  344. additional request data. The response body is returned and the
  345. response headers are stored in the variable
  346. ‘ghub-response-headers’.
  347. • ‘METHOD’ is the HTTP method, given as a string.
  348. • ‘RESOURCE’ is the resource to access, given as a string
  349. beginning with a slash.
  350. • ‘PARAMS’, ‘QUERY’, ‘PAYLOAD’ and ‘HEADERS’ are alists and are
  351. used to specify request data. All these arguments are alists
  352. that resemble the JSON expected and returned by the Github
  353. API. The keys are symbols and the values stored in the ‘cdr’
  354. (not the ‘cadr’) can be strings, integers, or lists of strings
  355. and integers.
  356. The Github API documentation is vague on how data has to be
  357. transmitted and for a particular resource usually just talks
  358. about "parameters". Generally speaking when the ‘METHOD’ is
  359. "HEAD" or "GET", then they have to be transmitted as a query,
  360. otherwise as a payload.
  361. • Use ‘PARAMS’ to automatically transmit like ‘QUERY’ or
  362. ‘PAYLOAD’ would depending on ‘METHOD’.
  363. • Use ‘QUERY’ to explicitly transmit data as a query.
  364. • Use ‘PAYLOAD’ to explicitly transmit data as a payload.
  365. Instead of an alist, ‘PAYLOAD’ may also be a string, in
  366. which case it gets encoded as UTF-8 but is otherwise
  367. transmitted as-is.
  368. • Use ‘HEADERS’ for those rare resources that require that
  369. the data is transmitted as headers instead of as a query
  370. or payload. When that is the case, then the Github API
  371. documentation usually mentions it explicitly.
  372. • If ‘SILENT’ is non-nil, then progress reports and the like are
  373. not messaged.
  374. • If ‘UNPAGINATE’ is t, then this function makes as many
  375. requests as necessary to get all values. If ‘UNPAGINATE’ is a
  376. natural number, then it gets at most that many pages. For any
  377. other non-nil value it raises an error.
  378. • If ‘NOERROR’ is non-nil, then no error is raised if the
  379. request fails and ‘nil’ is returned instead. If ‘NOERROR’ is
  380. ‘return’, then the error payload is returned instead of ‘nil’.
  381. • If ‘READER’ is non-nil, then it is used to read and return
  382. from the response buffer. The default is
  383. ‘ghub--read-json-payload’. For the very few resources that do
  384. not return JSON, you might want to use ‘ghub--decode-payload’.
  385. • If ‘USERNAME’ is non-nil, then the request is made on behalf
  386. of that user. It is better to specify the user using the Git
  387. variable ‘github.user’ for "api.github.com", or
  388. ‘github.HOST.user’ if connecting to a Github Enterprise
  389. instance.
  390. • Each package that uses Ghub should use its own token. If
  391. ‘AUTH’ is ‘nil’ or unspecified, then the generic ‘ghub’ token
  392. is used instead. This is only acceptable for personal
  393. utilities. A package that is distributed to other users
  394. should always use this argument to identify itself, using a
  395. symbol matching its name.
  396. Package authors who find this inconvenient should write a
  397. wrapper around this function and possibly for the
  398. method-specific functions as well.
  399. Beside ‘nil’, some other symbols have a special meaning too.
  400. ‘none’ means to make an unauthorized request. ‘basic’ means
  401. to make a password based request. If the value is a string,
  402. then it is assumed to be a valid token. ‘basic’ and an
  403. explicit token string are only intended for internal and
  404. debugging uses.
  405. If ‘AUTH’ is a package symbol, then the scopes are specified
  406. using the variable ‘AUTH-github-token-scopes’. It is an error
  407. if that is not specified. See ‘ghub-github-token-scopes’ for
  408. an example.
  409. • If ‘HOST’ is non-nil, then connect to that Github instance.
  410. This defaults to "api.github.com". When a repository is
  411. connected to a Github Enterprise instance, then it is better
  412. to specify that using the Git variable ‘github.host’ instead
  413. of using this argument.
  414. • If ‘FORGE’ is ‘gitlab’, then connect to Gitlab.com or,
  415. depending on ‘HOST’, to another Gitlab instance. This is only
  416. intended for internal use. Instead of using this argument you
  417. should use function ‘glab-request’ and other ‘glab-*’
  418. functions.
  419. • If ‘CALLBACK’ and/or ‘ERRORBACK’ is non-nil, then this
  420. function makes one or more asynchronous requests and calls
  421. ‘CALLBACK’ or ‘ERRORBACK’ when finished. If no error
  422. occurred, then it calls ‘CALLBACK’, unless that is ‘nil’.
  423. If an error occurred, then it calls ‘ERRORBACK’, or if that is
  424. nil, then ‘CALLBACK’. ‘ERRORBACK’ can also be ‘t’, in which
  425. case it signals instead. ‘NOERROR’ is ignored for all
  426. asynchronous requests.
  427. Both callbacks are called with four arguments.
  428. • For ‘CALLBACK’, the combined value of the retrieved
  429. pages. For ‘ERRORBACK’, the error that occurred when
  430. retrieving the last page.
  431. • The headers of the last page as an alist.
  432. • Status information provided by ‘url-retrieve’. Its
  433. ‘:error’ property holds the same information as the first
  434. argument to ‘ERRORBACK’.
  435. • A ‘ghub--req’ struct, which can be passed to
  436. ‘ghub-continue’ (which see) to retrieve the next page, if
  437. any.
  438. -- Function: ghub-continue args
  439. If there is a next page, then this function retrieves that.
  440. This function is only intended to be called from callbacks. If
  441. there is a next page, then that is retrieved and the buffer that
  442. the result will be loaded into is returned, or t if the process has
  443. already completed. If there is no next page, then return nil.
  444. Callbacks are called with four arguments (see ‘ghub-request’). The
  445. forth argument is a ‘ghub--req’ struct, intended to be passed to
  446. this function. A callback may use the struct’s ‘extra’ slot to
  447. pass additional information to the callback that will be called
  448. after the next request. Use the function ‘ghub-req-extra’ to get
  449. and set the value of that slot.
  450. As an example, using ‘ghub-continue’ in a callback like so:
  451. (ghub-get "/users/tarsius/repos" nil
  452. :callback (lambda (value _headers _status req)
  453. (unless (ghub-continue req)
  454. (setq my-value value))))
  455. is equivalent to:
  456. (ghub-get "/users/tarsius/repos" nil
  457. :unpaginate t
  458. :callback (lambda (value _headers _status _req)
  459. (setq my-value value)))
  460. To demonstrate how to pass information from one callback to the
  461. next, here we record when we start fetching each page:
  462. (ghub-get "/users/tarsius/repos" nil
  463. :extra (list (current-time))
  464. :callback (lambda (value _headers _status req)
  465. (push (current-time) (ghub-req-extra req))
  466. (unless (ghub-continue req)
  467. (setq my-times (ghub-req-extra req))
  468. (setq my-value value))))
  469. -- Variable: ghub-response-headers
  470. A select few Github API resources respond by transmitting data in
  471. the response header instead of in the response body. Because there
  472. are so few of these inconsistencies, ‘ghub-request’ always returns
  473. the response body.
  474. To access the response headers use this variable after
  475. ‘ghub-request’ has returned.
  476. -- Function: ghub-response-link-relations req headers payload
  477. This function returns an alist of the link relations in ‘HEADERS’,
  478. or if optional ‘HEADERS’ is nil, then those in
  479. ‘ghub-response-headers’.
  480. When accessing a Bitbucket instance then the link relations are in
  481. ‘PAYLOAD’ instead of ‘HEADERS’, making their API merely RESTish and
  482. forcing this function to append those relations to the value of
  483. ‘ghub-response-headers’, for later use when this function is called
  484. with ‘nil’ for ‘PAYLOAD’.
  485. 
  486. File: ghub.info, Node: Making GraphQL Requests, Next: Github Convenience Wrappers, Prev: Making REST Requests, Up: API
  487. 3.3 Making GraphQL Requests
  488. ===========================
  489. -- Function: ghub-graphql graphql &optional variables &key username
  490. auth host callback silent callback errorback value extra
  491. This function makes a GraphQL request using ‘GRAPHQL’ and
  492. ‘VARIABLES’ as inputs. ‘GRAPHQL’ is a GraphQL string. ‘VARIABLES’
  493. is a JSON-like alist. The other arguments behave as for
  494. ‘ghub-request’ (which see).
  495. The response is returned as a JSON-like alist. Even if the
  496. response contains ‘errors’, this function does not raise an error.
  497. Cursor-handling is likewise left to the caller.
  498. ‘ghub-graphql’ is a thin convenience wrapper around ‘ghub-request’,
  499. similar to ‘ghub-post’ and friends. While the latter only hard-code the
  500. value of the ‘METHOD’ argument, the former also hard-codes ‘RESOURCE’
  501. and constructs ‘PAYLOAD’ from ‘GRAPHQL’ and ‘VARIABLES’. It also drops
  502. ‘UNPAGINATE’, ‘NOERROR’, ‘READER’ (internal functions expect alist-ified
  503. JSON) and ‘FORGE’ (only Github currently supports GraphQL).
  504. ‘ghub-graphql’ does not account for the fact that pagination works
  505. differently in GraphQL than it does in REST, so users of this function
  506. have to deal with that themselves. Likewise error handling works
  507. differently and has to be done by the caller too.
  508. An early attempt at implementing automatic unpaginating for GraphQL
  509. can be found in the ‘faithful-graphql’ branch, provided I haven’t
  510. deleted that by now. On that branch I try to do things as intended by
  511. the designers of GraphQL, using variables and fragments, and drowning in
  512. a sea of boilerplate.
  513. The problem with that approach is that it only works for applications
  514. that fetch specific information on demand and actually want things to be
  515. paginated. I am convinced that GraphQL is very nice for web apps.
  516. However the Forge package for which I have implemented all of this
  517. has very different needs. It wants to fetch "all the data" and "cache"
  518. it locally, so that it is available even when there is no internet
  519. connection. GraphQL was designed around the idea that you should be
  520. able to "ask for what you need and get exactly that". But when that
  521. boils down to "look, if I persist, then you are going to hand me over
  522. all the data anyway, so just caught it up already", then things start to
  523. fall apart. If Github’s GraphQL allowed pagination to be turned off
  524. completely, then teaching ‘ghub-graphql’ about error handling would be
  525. enough.
  526. But it doesn’t and when doing things as intended, then that leads to
  527. huge amounts of repetitive boilerplate, which is so boring to write that
  528. doing it without introducing bugs left and right is near impossible; so
  529. I decided to give up on GraphQL variables, fragments and conditions, and
  530. instead implement something more powerful, though also more opinionated.
  531. -- Function: ghub--graphql-vacuum query variables callback &optional
  532. until &key narrow username auth host forge
  533. This function is an opinionated alternative to ‘ghub-graphql’. It
  534. relies on dark magic to get the job done.
  535. It makes an initial request using ‘QUERY’. It then looks for
  536. paginated edges in the returned data and makes more requests to
  537. resolve them. In order to do so it automatically transforms the
  538. initial ‘QUERY’ into another query suitable for that particular
  539. edge. The data retrieved by subsequent requests is then injected
  540. into the data of the original request before that is returned or
  541. passed to the callback. If subsequently retrieved data features
  542. new paginated edges, then those are followed recursively.
  543. The end result is essentially the same as using ‘ghub-graphql’, if
  544. only it were possible to say "do not paginate anything". The
  545. implementation is much more complicated because it is not possible
  546. to do that.
  547. ‘QUERY’ is a GraphQL query expressed as an s-expression. The
  548. bundled ‘gsexp’ library is used to turn that into a GraphQL query
  549. string. Only a subset of the GraphQL features are supported;
  550. fragments for example are not, and magical stuff happens to
  551. variables. This is not documented yet, I am afraid. Look at
  552. existing callers.
  553. ‘VARIABLES’ is a JSON-like alist as for ‘ghub-graphql’.
  554. ‘UNTIL’ is an alist ‘((EDGE-until . VALUE)...)’. When unpaginating
  555. ‘EDGE’ try not to fetch beyond the element whose first field has
  556. the value ‘VALUE’ and remove that element as well as all "lesser"
  557. elements from the retrieved data if necessary. Look at
  558. ‘forge--pull-repository’ for an example. This is only useful if
  559. you "cache" the response locally and want to avoid fetching data
  560. again that you already have.
  561. Other arguments behave as for ‘ghub-graphql’ and ‘ghub-request’,
  562. more or less.
  563. Using ‘ghub--graphql-vacuum’, the following resource specific
  564. functions are implemented. These functions are not part of the public
  565. API yet and are very much subject to change.
  566. -- Function: ghub-fetch-repository owner name callback &optional until
  567. &key username auth host forge
  568. This function asynchronously fetches forge data about the specified
  569. repository. Once all data has been collected, ‘CALLBACK’ is called
  570. with the data as the only argument.
  571. -- Function: ghub-fetch-issue owner name callback &optional until &key
  572. username auth host forge
  573. This function asynchronously fetches forge data about the specified
  574. issue. Once all data has been collected, ‘CALLBACK’ is called with
  575. the data as the only argument.
  576. -- Function: ghub-fetch-pullreq owner name callback &optional until
  577. &key username auth host forge
  578. This function asynchronously fetches forge data about the specified
  579. pull-request. Once all data has been collected, ‘CALLBACK’ is
  580. called with the data as the only argument.
  581. Note that in order to avoid duplication all of these functions base
  582. their initial query on the query stored in ‘ghub-fetch-repository’. The
  583. latter two pass that query through ‘ghub--graphql-prepare-query’, which
  584. then uses ‘ghub--graphql-narrow-query’ to remove parts the caller is not
  585. interested in. These two functions are also used internally, when
  586. unpaginating, but as demonstrated here they can be useful even before
  587. making an initial request.
  588. 
  589. File: ghub.info, Node: Github Convenience Wrappers, Next: Non-Github Convenience Wrappers, Prev: Making GraphQL Requests, Up: API
  590. 3.4 Github Convenience Wrappers
  591. ===============================
  592. -- Function: ghub-head resource &optional params &key query payload
  593. headers unpaginate noerror reader username auth host callback
  594. errorback
  595. -- Function: ghub-get resource &optional params &key query payload
  596. headers unpaginate noerror reader username auth host callback
  597. errorback
  598. These functions are simple wrappers around ‘ghub-request’. Their
  599. signature is identical to that of the latter, except that they do
  600. not have an argument named ‘METHOD’. The HTTP method is instead
  601. given by the second word in the function name.
  602. As described in the documentation for ‘ghub-request’, it depends on
  603. the used method whether the value of the ‘PARAMS’ argument is used
  604. as the query or the payload. For the "HEAD" and "GET" methods it
  605. is used as the query.
  606. -- Function: ghub-put resource &optional params &key query payload
  607. headers unpaginate noerror reader username auth host callback
  608. errorback
  609. -- Function: ghub-post resource &optional params &key query payload
  610. headers unpaginate noerror reader username auth host callback
  611. errorback
  612. -- Function: ghub-patch resource &optional params &key query payload
  613. headers unpaginate noerror reader username auth host callback
  614. errorback
  615. -- Function: ghub-delete resource &optional params &key query payload
  616. headers unpaginate noerror reader username auth host callback
  617. errorback
  618. These functions are simple wrappers around ‘ghub-request’. Their
  619. signature is identical to that of the latter, except that they do
  620. not have an argument named ‘METHOD’. The HTTP method is instead
  621. given by the second word in the function name.
  622. As described in the documentation for ‘ghub-request’, it depends on
  623. the used method whether the value of the ‘PARAMS’ argument is used
  624. as the query or the payload. For the "PUT", "POST", "PATCH" and
  625. "DELETE" methods it is used as the payload.
  626. -- Function: ghub-wait resource &optional duration &key username auth
  627. host
  628. Some API requests result in an immediate successful response even
  629. when the requested action has not actually been carried out yet.
  630. An example is the request for the creation of a new repository,
  631. which doesn’t cause the repository to immediately become available.
  632. The Github API documentation usually mentions this when describing
  633. an affected resource.
  634. If you want to do something with some resource right after making a
  635. request for its creation, then you might have to wait for it to
  636. actually be created. This function can be used to do so. It
  637. repeatedly tries to access the resource until it becomes available
  638. or until a timeout is reached. In the latter case it signals
  639. ‘ghub-error’.
  640. ‘RESOURCE’ specifies the resource that this function waits for.
  641. ‘DURATION’ specifies the maximum number of seconds to wait for,
  642. defaulting to 64 seconds. Emacs will block during that time, but
  643. the user can abort using ‘C-g’.
  644. The first attempt is made immediately and will often succeed. If
  645. not, then another attempt is made after two seconds, and each
  646. subsequent attempt is made after waiting as long as we already
  647. waited between all preceding attempts combined.
  648. See ‘ghub-request’’s documentation above for information about the
  649. other arguments.
  650. 
  651. File: ghub.info, Node: Non-Github Convenience Wrappers, Prev: Github Convenience Wrappers, Up: API
  652. 3.5 Non-Github Convenience Wrappers
  653. ===================================
  654. ‘ghub-request’ and ‘ghub-METHOD’ can be used to make a request for any
  655. of the supported forge types, but except when making a request for a
  656. ‘github’ instance, then that requires the use of the FORGE argument.
  657. To avoid that, functions named ‘FORGE-request’ and ‘FORGE-METHOD’ are
  658. also available. The following forms are equivalent, for example:
  659. (ghub-get ... :auth 'PACKAGE :forge 'gitlab)
  660. (glab-get ... :auth 'PACKAGE)
  661. These forms would remain equivalent even if you did not specify a
  662. value for the AUTH arguments — but you should not do that if you plan to
  663. share your code with others (see *note Using Ghub in Your Own
  664. Package::). If you do omit AUTH, then the request is made on behalf of
  665. the ‘ghub’ package, *regardless* of the symbol prefix of the function
  666. you use to do so.
  667. All ‘FORGE-request’ and ‘FORGE-METHOD’ functions, including but not
  668. limited to ‘ghub-METHOD’, are very simple wrappers around
  669. ‘ghub-request’. They take fewer arguments than ‘ghub-request’ and
  670. instead pass constant values for the arguments METHOD and/or FORGE.
  671. -- Function: buck-request resource &optional params &key query payload
  672. headers unpaginate noerror reader username auth host callback
  673. errorback
  674. -- Function: glab-request resource &optional params &key query payload
  675. headers unpaginate noerror reader username auth host callback
  676. errorback
  677. -- Function: gogs-request resource &optional params &key query payload
  678. headers unpaginate noerror reader username auth host callback
  679. errorback
  680. Wrappers around ‘ghub-request’ which hardcode the FORGE to either
  681. ‘bitbucket’, ‘gitlab’, ‘gogs’ or ‘gitea’.
  682. -- Function: buck-get resource &optional params &key query payload
  683. headers unpaginate noerror reader username auth host callback
  684. errorback
  685. -- Function: buck-put resource &optional params &key query payload
  686. headers unpaginate noerror reader username auth host callback
  687. errorback
  688. -- Function: buck-post resource &optional params &key query payload
  689. headers unpaginate noerror reader username auth host callback
  690. errorback
  691. -- Function: buck-delete resource &optional params &key query payload
  692. headers unpaginate noerror reader username auth host callback
  693. errorback
  694. -- Function: glab-head resource &optional params &key query payload
  695. headers unpaginate noerror reader username auth host callback
  696. errorback
  697. -- Function: glab-get resource &optional params &key query payload
  698. headers unpaginate noerror reader username auth host callback
  699. errorback
  700. -- Function: glab-put resource &optional params &key query payload
  701. headers unpaginate noerror reader username auth host callback
  702. errorback
  703. -- Function: glab-post resource &optional params &key query payload
  704. headers unpaginate noerror reader username auth host callback
  705. errorback
  706. -- Function: glab-patch resource &optional params &key query payload
  707. headers unpaginate noerror reader username auth host callback
  708. errorback
  709. -- Function: glab-delete resource &optional params &key query payload
  710. headers unpaginate noerror reader username auth host callback
  711. errorback
  712. -- Function: gogs-get resource &optional params &key query payload
  713. headers unpaginate noerror reader username auth host callback
  714. errorback
  715. -- Function: gogs-put resource &optional params &key query payload
  716. headers unpaginate noerror reader username auth host callback
  717. errorback
  718. -- Function: gogs-post resource &optional params &key query payload
  719. headers unpaginate noerror reader username auth host callback
  720. errorback
  721. -- Function: gogs-patch resource &optional params &key query payload
  722. headers unpaginate noerror reader username auth host callback
  723. errorback
  724. -- Function: gogs-delete resource &optional params &key query payload
  725. headers unpaginate noerror reader username auth host callback
  726. errorback
  727. -- Function: gtea-get resource &optional params &key query payload
  728. headers unpaginate noerror reader username auth host callback
  729. errorback
  730. -- Function: gtea-put resource &optional params &key query payload
  731. headers unpaginate noerror reader username auth host callback
  732. errorback
  733. -- Function: gtea-post resource &optional params &key query payload
  734. headers unpaginate noerror reader username auth host callback
  735. errorback
  736. -- Function: gtea-patch resource &optional params &key query payload
  737. headers unpaginate noerror reader username auth host callback
  738. errorback
  739. -- Function: gtea-delete resource &optional params &key query payload
  740. headers unpaginate noerror reader username auth host callback
  741. errorback
  742. Wrappers around ‘FORGE-METHOD’ which hardcode the FORGE to either
  743. ‘bitbucket’, ‘gitlab’, ‘gogs’ or ‘gitea’, and the METHOD to the
  744. implied method.
  745. Note that ‘buck-head’, ‘buck-patch’, ‘gogs-head’ and ‘gtea-head’ do
  746. not exist because the respective APIs do not appear to support
  747. these methods.
  748. 
  749. File: ghub.info, Node: Notes, Next: Function Index, Prev: API, Up: Top
  750. 4 Notes
  751. *******
  752. * Menu:
  753. * Using Ghub in Personal Scripts::
  754. * Using Ghub in Your Own Package::
  755. * Forge Limitations and Notes::
  756. 
  757. File: ghub.info, Node: Using Ghub in Personal Scripts, Next: Using Ghub in Your Own Package, Up: Notes
  758. 4.1 Using Ghub in Personal Scripts
  759. ==================================
  760. You can of course use ‘ghub-request’ and its wrapper functions in your
  761. personal scripts. Unlike when you use Ghub in a package that you
  762. distribute for others to use, you don’t have to explicitly specify a
  763. package in personal scripts.
  764. ;; This is perfectly acceptable in personal scripts ...
  765. (ghub-get "/user")
  766. ;; ... and actually equals to
  767. (ghub-get "/user" nil :auth 'ghub)
  768. ;; In packages you have to specify the package using AUTH.
  769. (ghub-get "/user" nil :auth 'some-package)
  770. When the ‘AUTH’ argument is not specified, then a request is made on
  771. behalf of the ‘ghub’ package itself. Like for any other package you
  772. have to create a dedicated token of coures.
  773. 
  774. File: ghub.info, Node: Using Ghub in Your Own Package, Next: Forge Limitations and Notes, Prev: Using Ghub in Personal Scripts, Up: Notes
  775. 4.2 Using Ghub in Your Own Package
  776. ==================================
  777. Every package should use its own token. This allows you as the author
  778. of some package to only request access to API scopes that are actually
  779. needed, which in turn might make it easier for users to trust your
  780. package not to do unwanted things.
  781. You have to tell ‘ghub-request’ on behalf of which package a request
  782. is being made by passing the symbol ‘PACKAGE’ as the value of its ‘AUTH’
  783. argument.
  784. (ghub-request "GET" "/user" nil :auth 'PACKAGE)
  785. Keep in mind that the users of your package will have to manually
  786. create a suitable token. To make that easier, you should not only link
  787. to this manual but also prominently mention the scopes the token needs;
  788. and explain what they are needed for.
  789. 
  790. File: ghub.info, Node: Forge Limitations and Notes, Prev: Using Ghub in Your Own Package, Up: Notes
  791. 4.3 Forge Limitations and Notes
  792. ===============================
  793. • There are no default Gitea and Gogs instances so the variables
  794. ‘gitea.host’ and ‘gogs.host’ are not taken into account.
  795. • Gitea and Gogs do not support limiting a token to certain scopes.
  796. • The Bitbucket API is fairly broken and my willingness to deal with
  797. that is extremely limited unless someone pays me vast amounts of
  798. money.
  799. • The Gitlab API documentation is not always accurate, though I don’t
  800. have an example at hand. It also isn’t structured well, making it
  801. occasionally difficult to find the information one is looking for.
  802. • Where one would use ‘user/repo’ when accessing another forge, one
  803. has to use ‘user%2Frepo’ when accessing Gitlab, e.g.:
  804. (glab-get "/projects/python-mode-devs%2Fpython-mode")
  805. 
  806. File: ghub.info, Node: Function Index, Next: Variable Index, Prev: Notes, Up: Top
  807. Appendix A Function Index
  808. *************************
  809. [index]
  810. * Menu:
  811. * buck-delete: Non-Github Convenience Wrappers.
  812. (line 50)
  813. * buck-get: Non-Github Convenience Wrappers.
  814. (line 41)
  815. * buck-post: Non-Github Convenience Wrappers.
  816. (line 47)
  817. * buck-put: Non-Github Convenience Wrappers.
  818. (line 44)
  819. * buck-request: Non-Github Convenience Wrappers.
  820. (line 28)
  821. * ghub--graphql-vacuum: Making GraphQL Requests.
  822. (line 57)
  823. * ghub-continue: Making REST Requests.
  824. (line 133)
  825. * ghub-delete: Github Convenience Wrappers.
  826. (line 32)
  827. * ghub-fetch-issue: Making GraphQL Requests.
  828. (line 108)
  829. * ghub-fetch-pullreq: Making GraphQL Requests.
  830. (line 115)
  831. * ghub-fetch-repository: Making GraphQL Requests.
  832. (line 101)
  833. * ghub-get: Github Convenience Wrappers.
  834. (line 9)
  835. * ghub-graphql: Making GraphQL Requests.
  836. (line 6)
  837. * ghub-head: Github Convenience Wrappers.
  838. (line 6)
  839. * ghub-patch: Github Convenience Wrappers.
  840. (line 29)
  841. * ghub-post: Github Convenience Wrappers.
  842. (line 26)
  843. * ghub-put: Github Convenience Wrappers.
  844. (line 23)
  845. * ghub-request: Making REST Requests.
  846. (line 6)
  847. * ghub-response-link-relations: Making REST Requests.
  848. (line 184)
  849. * ghub-wait: Github Convenience Wrappers.
  850. (line 46)
  851. * glab-delete: Non-Github Convenience Wrappers.
  852. (line 68)
  853. * glab-get: Non-Github Convenience Wrappers.
  854. (line 56)
  855. * glab-head: Non-Github Convenience Wrappers.
  856. (line 53)
  857. * glab-patch: Non-Github Convenience Wrappers.
  858. (line 65)
  859. * glab-post: Non-Github Convenience Wrappers.
  860. (line 62)
  861. * glab-put: Non-Github Convenience Wrappers.
  862. (line 59)
  863. * glab-request: Non-Github Convenience Wrappers.
  864. (line 31)
  865. * gogs-delete: Non-Github Convenience Wrappers.
  866. (line 83)
  867. * gogs-get: Non-Github Convenience Wrappers.
  868. (line 71)
  869. * gogs-patch: Non-Github Convenience Wrappers.
  870. (line 80)
  871. * gogs-post: Non-Github Convenience Wrappers.
  872. (line 77)
  873. * gogs-put: Non-Github Convenience Wrappers.
  874. (line 74)
  875. * gogs-request: Non-Github Convenience Wrappers.
  876. (line 34)
  877. * gtea-delete: Non-Github Convenience Wrappers.
  878. (line 98)
  879. * gtea-get: Non-Github Convenience Wrappers.
  880. (line 86)
  881. * gtea-patch: Non-Github Convenience Wrappers.
  882. (line 95)
  883. * gtea-post: Non-Github Convenience Wrappers.
  884. (line 92)
  885. * gtea-put: Non-Github Convenience Wrappers.
  886. (line 89)
  887. 
  888. File: ghub.info, Node: Variable Index, Prev: Function Index, Up: Top
  889. Appendix B Variable Index
  890. *************************
  891. [index]
  892. * Menu:
  893. * ghub-response-headers: Making REST Requests.
  894. (line 174)
  895. * github.host: Github Configuration Variables.
  896. (line 28)
  897. * github.HOST.user: Github Configuration Variables.
  898. (line 18)
  899. * github.user: Github Configuration Variables.
  900. (line 11)
  901. 
  902. Tag Table:
  903. Node: Top764
  904. Node: Introduction2288
  905. Node: Getting Started2693
  906. Node: Basic Concepts Arguments and Variables4375
  907. Node: Setting the Username8245
  908. Node: Creating and Storing a Token11136
  909. Node: Creating a Token11400
  910. Node: Storing a Token12419
  911. Node: Github Configuration Variables14264
  912. Node: API15978
  913. Node: Their APIs16216
  914. Node: Making REST Requests16947
  915. Node: Making GraphQL Requests26379
  916. Node: Github Convenience Wrappers33013
  917. Node: Non-Github Convenience Wrappers36720
  918. Node: Notes42226
  919. Node: Using Ghub in Personal Scripts42433
  920. Node: Using Ghub in Your Own Package43324
  921. Node: Forge Limitations and Notes44259
  922. Node: Function Index45232
  923. Node: Variable Index50881
  924. 
  925. End Tag Table
  926. 
  927. Local Variables:
  928. coding: utf-8
  929. End: