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.

129 lines
5.8 KiB

  1. ;;; buck.el --- minuscule client library for the Bitbucket 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. ;; Buck is a library that provides basic support for using the Bitbucket 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, Buck does
  22. ;; not support the guided creation of tokens because Bitbucket 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 buck-default-host "api.bitbucket.org/2.0"
  28. "The default host that is used if `buck.host' is not set.")
  29. ;; HEAD and PATCH are not supported according to
  30. ;; https://developer.atlassian.com/bitbucket/api/2/reference/meta/uri-uuid
  31. (cl-defun buck-get (resource &optional params
  32. &key query payload headers
  33. silent unpaginate noerror reader
  34. username auth host
  35. callback errorback extra)
  36. "Make a `GET' request for RESOURCE, with optional query PARAMS.
  37. Like calling `ghub-request' (which see) with \"GET\" as METHOD
  38. and `bitbucket' as FORGE."
  39. (ghub-request "GET" resource params :forge 'bitbucket
  40. :query query :payload payload :headers headers
  41. :silent silent :unpaginate unpaginate
  42. :noerror noerror :reader reader
  43. :username username :auth auth :host host
  44. :callback callback :errorback errorback :extra extra))
  45. (cl-defun buck-put (resource &optional params
  46. &key query payload headers
  47. silent unpaginate noerror reader
  48. username auth host
  49. callback errorback extra)
  50. "Make a `PUT' request for RESOURCE, with optional payload PARAMS.
  51. Like calling `ghub-request' (which see) with \"PUT\" as METHOD
  52. and `bitbucket' as FORGE."
  53. (ghub-request "PUT" resource params :forge 'bitbucket
  54. :query query :payload payload :headers headers
  55. :silent silent :unpaginate unpaginate
  56. :noerror noerror :reader reader
  57. :username username :auth auth :host host
  58. :callback callback :errorback errorback :extra extra))
  59. (cl-defun buck-post (resource &optional params
  60. &key query payload headers
  61. silent unpaginate noerror reader
  62. username auth host
  63. callback errorback extra)
  64. "Make a `POST' request for RESOURCE, with optional payload PARAMS.
  65. Like calling `ghub-request' (which see) with \"POST\" as METHOD
  66. and `bitbucket' as FORGE."
  67. (ghub-request "POST" resource params :forge 'bitbucket
  68. :query query :payload payload :headers headers
  69. :silent silent :unpaginate unpaginate
  70. :noerror noerror :reader reader
  71. :username username :auth auth :host host
  72. :callback callback :errorback errorback :extra extra))
  73. (cl-defun buck-delete (resource &optional params
  74. &key query payload headers
  75. silent unpaginate noerror reader
  76. username auth host
  77. callback errorback extra)
  78. "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
  79. Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
  80. and `bitbucket' as FORGE."
  81. (ghub-request "DELETE" resource params :forge 'bitbucket
  82. :query query :payload payload :headers headers
  83. :silent silent :unpaginate unpaginate
  84. :noerror noerror :reader reader
  85. :username username :auth auth :host host
  86. :callback callback :errorback errorback :extra extra))
  87. (cl-defun buck-request (method resource &optional params
  88. &key query payload headers
  89. silent unpaginate noerror reader
  90. username auth host
  91. callback errorback extra)
  92. "Make a request for RESOURCE and return the response body.
  93. Like calling `ghub-request' (which see) with `bitbucket' as FORGE."
  94. (ghub-request method resource params :forge 'bitbucket
  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 buck-repository-id (owner name &key username auth host)
  101. "Return the id of the repository specified by OWNER, NAME and HOST."
  102. (substring (cdr (assq 'uuid
  103. (buck-get (format "/repositories/%s/%s" owner name)
  104. nil
  105. :username username :auth auth :host host)))
  106. 1 -1))
  107. ;;; _
  108. (provide 'buck)
  109. ;;; buck.el ends here