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.

128 lines
5.8 KiB

  1. ;;; buck.el --- minuscule client library for the Bitbucket 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. ;; Buck is a library that provides basic support for using the Bitbucket 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, Buck does
  21. ;; not support the guided creation of tokens because Bitbucket 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 buck-default-host "api.bitbucket.org/2.0"
  27. "The default host that is used if `buck.host' is not set.")
  28. ;; HEAD and PATCH are not supported according to
  29. ;; https://developer.atlassian.com/bitbucket/api/2/reference/meta/uri-uuid
  30. (cl-defun buck-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 `bitbucket' as FORGE."
  38. (ghub-request "GET" resource params :forge 'bitbucket
  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 buck-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 `bitbucket' as FORGE."
  52. (ghub-request "PUT" resource params :forge 'bitbucket
  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 buck-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 `bitbucket' as FORGE."
  66. (ghub-request "POST" resource params :forge 'bitbucket
  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 buck-delete (resource &optional params
  73. &key query payload headers
  74. silent unpaginate noerror reader
  75. username auth host
  76. callback errorback extra)
  77. "Make a `DELETE' request for RESOURCE, with optional payload PARAMS.
  78. Like calling `ghub-request' (which see) with \"DELETE\" as METHOD
  79. and `bitbucket' as FORGE."
  80. (ghub-request "DELETE" resource params :forge 'bitbucket
  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 buck-request (method resource &optional params
  87. &key query payload headers
  88. silent unpaginate noerror reader
  89. username auth host
  90. callback errorback extra)
  91. "Make a request for RESOURCE and return the response body.
  92. Like calling `ghub-request' (which see) with `bitbucket' as FORGE."
  93. (ghub-request method resource params :forge 'bitbucket
  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 buck-repository-id (owner name &key username auth host)
  100. "Return the id of the repository specified by OWNER, NAME and HOST."
  101. (substring (cdr (assq 'uuid
  102. (buck-get (format "/repositories/%s/%s" owner name)
  103. nil
  104. :username username :auth auth :host host)))
  105. 1 -1))
  106. ;;; _
  107. (provide 'buck)
  108. ;;; buck.el ends here