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.

141 lines
6.3 KiB

  1. ;;; gogs.el --- minuscule client library for the Gogs API -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2016-2021 Jonas Bernoulli
  3. ;; Author: Jonas Bernoulli <jonas@bernoul.li>
  4. ;; Homepage: https://github.com/magit/ghub
  5. ;; Keywords: tools
  6. ;; SPDX-License-Identifier: GPL-3.0-or-later
  7. ;; This file is not part of GNU Emacs.
  8. ;; This file is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; This file is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; For a copy of the GPL see https://www.gnu.org/licenses/gpl.txt.
  17. ;;; Commentary:
  18. ;; Gogs is a library that provides basic support for using the Gogs API
  19. ;; from Emacs packages. It abstracts access to API resources using only
  20. ;; a handful of functions that are not resource-specific.
  21. ;; This library is implemented on top of Ghub. Unlike Ghub, Gogs does
  22. ;; not support the guided creation of tokens because Gogs lacks the
  23. ;; features that would be necessary to implement that. Users have to
  24. ;; create tokens through the web interface.
  25. ;;; Code:
  26. (require 'ghub)
  27. (defconst gogs-default-host "localhost:3000/api/v1"
  28. "The default Gogs host.")
  29. ;; HEAD does not appear to be supported.
  30. (cl-defun gogs-get (resource &optional params
  31. &key query payload headers
  32. silent unpaginate noerror reader
  33. username auth host
  34. callback errorback extra)
  35. "Make a `GET' request for RESOURCE, with optional query PARAMS.
  36. Like calling `ghub-request' (which see) with \"GET\" as METHOD
  37. and `gogs' as FORGE."
  38. (ghub-request "GET" resource params :forge 'gogs
  39. :query query :payload payload :headers headers
  40. :silent silent :unpaginate unpaginate
  41. :noerror noerror :reader reader
  42. :username username :auth auth :host host
  43. :callback callback :errorback errorback :extra extra))
  44. (cl-defun gogs-put (resource &optional params
  45. &key query payload headers
  46. silent unpaginate noerror reader
  47. username auth host
  48. callback errorback extra)
  49. "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
  50. Like calling `ghub-request' (which see) with \"PUT\" as METHOD
  51. and `gogs' as FORGE."
  52. (ghub-request "PUT" resource params :forge 'gogs
  53. :query query :payload payload :headers headers
  54. :silent silent :unpaginate unpaginate
  55. :noerror noerror :reader reader
  56. :username username :auth auth :host host
  57. :callback callback :errorback errorback :extra extra))
  58. (cl-defun gogs-post (resource &optional params
  59. &key query payload headers
  60. silent unpaginate noerror reader
  61. username auth host
  62. callback errorback extra)
  63. "Make a `POST' request for RESOURCE, with optional payload PARAMS.
  64. Like calling `ghub-request' (which see) with \"POST\" as METHOD
  65. and `gogs' as FORGE."
  66. (ghub-request "POST" resource params :forge 'gogs
  67. :query query :payload payload :headers headers
  68. :silent silent :unpaginate unpaginate
  69. :noerror noerror :reader reader
  70. :username username :auth auth :host host
  71. :callback callback :errorback errorback :extra extra))
  72. (cl-defun gogs-patch (resource &optional params
  73. &key query payload headers
  74. silent unpaginate noerror reader
  75. username auth host
  76. callback errorback extra)
  77. "Make a `PATCH' request for RESOURCE, with optional payload PARAMS.
  78. Like calling `ghub-request' (which see) with \"PATCH\" as METHOD
  79. and `gogs' as FORGE."
  80. (ghub-request "PATCH" resource params :forge 'gogs
  81. :query query :payload payload :headers headers
  82. :silent silent :unpaginate unpaginate
  83. :noerror noerror :reader reader
  84. :username username :auth auth :host host
  85. :callback callback :errorback errorback :extra extra))
  86. (cl-defun gogs-delete (resource &optional params
  87. &key query payload headers
  88. silent unpaginate noerror reader
  89. username auth host
  90. callback errorback extra)
  91. "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
  92. Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
  93. and `gogs' as FORGE."
  94. (ghub-request "DELETE" resource params :forge 'gogs
  95. :query query :payload payload :headers headers
  96. :silent silent :unpaginate unpaginate
  97. :noerror noerror :reader reader
  98. :username username :auth auth :host host
  99. :callback callback :errorback errorback :extra extra))
  100. (cl-defun gogs-request (method resource &optional params
  101. &key query payload headers
  102. silent unpaginate noerror reader
  103. username auth host
  104. callback errorback extra)
  105. "Make a request for RESOURCE and return the response body.
  106. Like calling `ghub-request' (which see) with `gogs' as FORGE."
  107. (ghub-request method resource params :forge 'gogs
  108. :query query :payload payload :headers headers
  109. :silent silent :unpaginate unpaginate
  110. :noerror noerror :reader reader
  111. :username username :auth auth :host host
  112. :callback callback :errorback errorback :extra extra))
  113. (cl-defun gogs-repository-id (owner name &key username auth host)
  114. "Return the id of the repository specified by OWNER, NAME and HOST."
  115. (number-to-string
  116. (cdr (assq 'id (gogs-get (format "/repos/%s/%s" owner name)
  117. nil :username username :auth auth :host host)))))
  118. ;;; _
  119. (provide 'gogs)
  120. ;;; gogs.el ends here