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.

155 lines
7.2 KiB

  1. ;;; glab.el --- minuscule client library for the Gitlab API -*- 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. ;; Glab is a library that provides basic support for using the Gitlab API
  18. ;; from Emacs packages. It abstracts access to API resources using only
  19. ;; a handful of functions that are not resource-specific.
  20. ;; This library is implemented on top of Ghub. Unlike Ghub, Glab does
  21. ;; not support the guided creation of tokens because Gitlab lacks the
  22. ;; features that would be necessary to implement that. Users have to
  23. ;; create tokens through the web interface.
  24. ;;; Code:
  25. (require 'ghub)
  26. (defconst glab-default-host "gitlab.com/api/v4"
  27. "The default host that is used if `glab.host' is not set.")
  28. (cl-defun glab-head (resource &optional params
  29. &key query payload headers
  30. silent unpaginate noerror reader
  31. username auth host
  32. callback errorback extra)
  33. "Make a `HEAD' request for RESOURCE, with optional query PARAMS.
  34. Like calling `ghub-request' (which see) with \"HEAD\" as METHOD
  35. and `gitlab' as FORGE."
  36. (ghub-request "HEAD" resource params :forge 'gitlab
  37. :query query :payload payload :headers headers
  38. :silent silent :unpaginate unpaginate
  39. :noerror noerror :reader reader
  40. :username username :auth auth :host host
  41. :callback callback :errorback errorback :extra extra))
  42. (cl-defun glab-get (resource &optional params
  43. &key query payload headers
  44. silent unpaginate noerror reader
  45. username auth host
  46. callback errorback extra)
  47. "Make a `GET' request for RESOURCE, with optional query PARAMS.
  48. Like calling `ghub-request' (which see) with \"GET\" as METHOD
  49. and `gitlab' as FORGE."
  50. (ghub-request "GET" resource params :forge 'gitlab
  51. :query query :payload payload :headers headers
  52. :silent silent :unpaginate unpaginate
  53. :noerror noerror :reader reader
  54. :username username :auth auth :host host
  55. :callback callback :errorback errorback :extra extra))
  56. (cl-defun glab-put (resource &optional params
  57. &key query payload headers
  58. silent unpaginate noerror reader
  59. username auth host
  60. callback errorback extra)
  61. "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
  62. Like calling `ghub-request' (which see) with \"PUT\" as METHOD
  63. and `gitlab' as FORGE."
  64. (ghub-request "PUT" resource params :forge 'gitlab
  65. :query query :payload payload :headers headers
  66. :silent silent :unpaginate unpaginate
  67. :noerror noerror :reader reader
  68. :username username :auth auth :host host
  69. :callback callback :errorback errorback :extra extra))
  70. (cl-defun glab-post (resource &optional params
  71. &key query payload headers
  72. silent unpaginate noerror reader
  73. username auth host
  74. callback errorback extra)
  75. "Make a `POST' request for RESOURCE, with optional payload PARAMS.
  76. Like calling `ghub-request' (which see) with \"POST\" as METHOD
  77. and `gitlab' as FORGE."
  78. (ghub-request "POST" resource params :forge 'gitlab
  79. :query query :payload payload :headers headers
  80. :silent silent :unpaginate unpaginate
  81. :noerror noerror :reader reader
  82. :username username :auth auth :host host
  83. :callback callback :errorback errorback :extra extra))
  84. (cl-defun glab-patch (resource &optional params
  85. &key query payload headers
  86. silent unpaginate noerror reader
  87. username auth host
  88. callback errorback extra)
  89. "Make a `PATCH' request for RESOURCE, with optional payload PARAMS.
  90. Like calling `ghub-request' (which see) with \"PATCH\" as METHOD
  91. and `gitlab' as FORGE."
  92. (ghub-request "PATCH" resource params :forge 'gitlab
  93. :query query :payload payload :headers headers
  94. :silent silent :unpaginate unpaginate
  95. :noerror noerror :reader reader
  96. :username username :auth auth :host host
  97. :callback callback :errorback errorback :extra extra))
  98. (cl-defun glab-delete (resource &optional params
  99. &key query payload headers
  100. silent unpaginate noerror reader
  101. username auth host
  102. callback errorback extra)
  103. "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
  104. Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
  105. and `gitlab' as FORGE."
  106. (ghub-request "DELETE" resource params :forge 'gitlab
  107. :query query :payload payload :headers headers
  108. :silent silent :unpaginate unpaginate
  109. :noerror noerror :reader reader
  110. :username username :auth auth :host host
  111. :callback callback :errorback errorback :extra extra))
  112. (cl-defun glab-request (method resource &optional params
  113. &key query payload headers
  114. silent unpaginate noerror reader
  115. username auth host
  116. callback errorback extra)
  117. "Make a request for RESOURCE and return the response body.
  118. Like calling `ghub-request' (which see) with `gitlab' as FORGE."
  119. (ghub-request method resource params :forge 'gitlab
  120. :query query :payload payload :headers headers
  121. :silent silent :unpaginate unpaginate
  122. :noerror noerror :reader reader
  123. :username username :auth auth :host host
  124. :callback callback :errorback errorback :extra extra))
  125. (cl-defun glab-repository-id (owner name &key username auth host)
  126. "Return the id of the repository specified by OWNER, NAME and HOST."
  127. (number-to-string
  128. (cdr (assq 'id (glab-get (format "/projects/%s%%2F%s"
  129. (replace-regexp-in-string "/" "%2F" owner)
  130. name)
  131. nil :username username :auth auth :host host)))))
  132. ;;; _
  133. (provide 'glab)
  134. ;;; glab.el ends here