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.

140 lines
6.3 KiB

  1. ;;; gogs.el --- minuscule client library for the Gogs 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. ;; Gogs is a library that provides basic support for using the Gogs 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, Gogs does
  21. ;; not support the guided creation of tokens because Gogs 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 gogs-default-host "localhost:3000/api/v1"
  27. "The default Gogs host.")
  28. ;; HEAD does not appear to be supported.
  29. (cl-defun gogs-get (resource &optional params
  30. &key query payload headers
  31. silent unpaginate noerror reader
  32. username auth host
  33. callback errorback extra)
  34. "Make a `GET' request for RESOURCE, with optional query PARAMS.
  35. Like calling `ghub-request' (which see) with \"GET\" as METHOD
  36. and `gogs' as FORGE."
  37. (ghub-request "GET" resource params :forge 'gogs
  38. :query query :payload payload :headers headers
  39. :silent silent :unpaginate unpaginate
  40. :noerror noerror :reader reader
  41. :username username :auth auth :host host
  42. :callback callback :errorback errorback :extra extra))
  43. (cl-defun gogs-put (resource &optional params
  44. &key query payload headers
  45. silent unpaginate noerror reader
  46. username auth host
  47. callback errorback extra)
  48. "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
  49. Like calling `ghub-request' (which see) with \"PUT\" as METHOD
  50. and `gogs' as FORGE."
  51. (ghub-request "PUT" resource params :forge 'gogs
  52. :query query :payload payload :headers headers
  53. :silent silent :unpaginate unpaginate
  54. :noerror noerror :reader reader
  55. :username username :auth auth :host host
  56. :callback callback :errorback errorback :extra extra))
  57. (cl-defun gogs-post (resource &optional params
  58. &key query payload headers
  59. silent unpaginate noerror reader
  60. username auth host
  61. callback errorback extra)
  62. "Make a `POST' request for RESOURCE, with optional payload PARAMS.
  63. Like calling `ghub-request' (which see) with \"POST\" as METHOD
  64. and `gogs' as FORGE."
  65. (ghub-request "POST" resource params :forge 'gogs
  66. :query query :payload payload :headers headers
  67. :silent silent :unpaginate unpaginate
  68. :noerror noerror :reader reader
  69. :username username :auth auth :host host
  70. :callback callback :errorback errorback :extra extra))
  71. (cl-defun gogs-patch (resource &optional params
  72. &key query payload headers
  73. silent unpaginate noerror reader
  74. username auth host
  75. callback errorback extra)
  76. "Make a `PATCH' request for RESOURCE, with optional payload PARAMS.
  77. Like calling `ghub-request' (which see) with \"PATCH\" as METHOD
  78. and `gogs' as FORGE."
  79. (ghub-request "PATCH" resource params :forge 'gogs
  80. :query query :payload payload :headers headers
  81. :silent silent :unpaginate unpaginate
  82. :noerror noerror :reader reader
  83. :username username :auth auth :host host
  84. :callback callback :errorback errorback :extra extra))
  85. (cl-defun gogs-delete (resource &optional params
  86. &key query payload headers
  87. silent unpaginate noerror reader
  88. username auth host
  89. callback errorback extra)
  90. "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
  91. Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
  92. and `gogs' as FORGE."
  93. (ghub-request "DELETE" resource params :forge 'gogs
  94. :query query :payload payload :headers headers
  95. :silent silent :unpaginate unpaginate
  96. :noerror noerror :reader reader
  97. :username username :auth auth :host host
  98. :callback callback :errorback errorback :extra extra))
  99. (cl-defun gogs-request (method resource &optional params
  100. &key query payload headers
  101. silent unpaginate noerror reader
  102. username auth host
  103. callback errorback extra)
  104. "Make a request for RESOURCE and return the response body.
  105. Like calling `ghub-request' (which see) with `gogs' as FORGE."
  106. (ghub-request method resource params :forge 'gogs
  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 gogs-repository-id (owner name &key username auth host)
  113. "Return the id of the repository specified by OWNER, NAME and HOST."
  114. (number-to-string
  115. (cdr (assq 'id (gogs-get (format "/repos/%s/%s" owner name)
  116. nil :username username :auth auth :host host)))))
  117. ;;; _
  118. (provide 'gogs)
  119. ;;; gogs.el ends here