PageRenderTime 54ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/elpa/gh-20120927.1646/gh-repos.el

https://bitbucket.org/exu/emacs.d.prelude
Emacs Lisp | 267 lines | 200 code | 38 blank | 29 comment | 2 complexity | a698a472bbee7c36e357e82638c151bc MD5 | raw file
Possible License(s): CC-BY-SA-4.0, GPL-2.0, GPL-3.0
  1. ;;; gh-repos.el --- repos module for gh.el
  2. ;; Copyright (C) 2011 Yann Hodique
  3. ;; Author: Yann Hodique <yann.hodique@gmail.com>
  4. ;; Keywords:
  5. ;; This file is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation; either version 2, or (at your option)
  8. ;; any later version.
  9. ;; This file is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs; see the file COPYING. If not, write to
  15. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. ;; Boston, MA 02111-1307, USA.
  17. ;;; Commentary:
  18. ;;
  19. ;;; Code:
  20. (eval-when-compile
  21. (require 'cl))
  22. ;;;###autoload
  23. (require 'eieio)
  24. (require 'gh-api)
  25. (require 'gh-auth)
  26. (require 'gh-common)
  27. ;;;###autoload
  28. (defclass gh-repos-api (gh-api-v3)
  29. ((repo-cls :allocation :class :initform gh-repos-repo))
  30. "Repos API")
  31. ;;;###autoload
  32. (defclass gh-repos-repo-stub (gh-object)
  33. ((name :initarg :name)
  34. (description :initarg :description)
  35. (homepage :initarg :homepage)
  36. (private :initarg :private))
  37. "Class for user-created repository objects")
  38. (defmethod gh-object-read-into ((stub gh-repos-repo-stub) data)
  39. (call-next-method)
  40. (with-slots (name description homepage private)
  41. stub
  42. (setq name (gh-read data 'name)
  43. description (gh-read data 'description)
  44. homepage (gh-read data 'homepage)
  45. private (gh-read data 'private))))
  46. ;;;###autoload
  47. (defclass gh-repos-repo (gh-repos-repo-stub)
  48. ((url :initarg :url)
  49. (html-url :initarg :html-url)
  50. (clone-url :initarg :clone-url)
  51. (git-url :initarg :git-url)
  52. (ssh-url :initarg :ssh-url)
  53. (svn-url :initarg :svn-url)
  54. (mirror-url :initarg :mirror-url)
  55. (owner :initarg :owner :initform nil)
  56. (language :initarg :language)
  57. (fork :initarg :fork)
  58. (forks :initarg :forks)
  59. (watchers :initarg :watchers)
  60. (size :initarg :size)
  61. (master-branch :initarg :master-branch)
  62. (open-issues :initarg :open-issues)
  63. (pushed-at :initarg :pushed-at)
  64. (created-at :initarg :created-at)
  65. (updated-at :initarg :updated-at)
  66. (organisation :initarg :organisation :initform nil)
  67. (parent :initarg :parent)
  68. (source :initarg :source)
  69. (owner-cls :allocation :class :initform gh-user)
  70. (organisation-cls :allocation :class :initform gh-user)
  71. (parent-cls :allocation :class :initform gh-repos-repo)
  72. (source-cls :allocation :class :initform gh-repos-repo))
  73. "Class for GitHub repositories")
  74. (defmethod gh-object-read-into ((repo gh-repos-repo) data)
  75. (call-next-method)
  76. (with-slots (url html-url clone-url git-url ssh-url svn-url mirror-url
  77. owner language fork forks watchers size master-branch
  78. open-issues pushed-at created-at
  79. organisation parent source)
  80. repo
  81. (setq url (gh-read data 'url)
  82. html-url (gh-read data 'html_url)
  83. clone-url (gh-read data 'clone_url)
  84. git-url (gh-read data 'git_url)
  85. ssh-url (gh-read data 'ssh_url)
  86. svn-url (gh-read data 'svn_url)
  87. mirror-url (gh-read data 'mirror_url)
  88. owner (gh-object-read (or (oref repo :owner)
  89. (oref repo owner-cls))
  90. (gh-read data 'owner))
  91. language (gh-read data 'language)
  92. fork (gh-read data 'fork)
  93. forks (gh-read data 'forks)
  94. watchers (gh-read data 'watchers)
  95. size (gh-read data 'size)
  96. master-branch (gh-read data 'master-branch)
  97. open-issues (gh-read data 'open_issues)
  98. pushed-at (gh-read data 'pushed_at)
  99. created-at (gh-read data 'created_at)
  100. organisation (gh-object-read (or (oref repo :organisation)
  101. (oref repo organisation-cls))
  102. (gh-read data 'organisation))
  103. parent (gh-object-read (oref repo parent-cls)
  104. (gh-read data 'parent))
  105. source (gh-object-read (oref repo source-cls)
  106. (gh-read data 'source)))))
  107. (defclass gh-repos-ref (gh-object)
  108. ((label :initarg :label)
  109. (ref :initarg :ref :initform nil)
  110. (sha :initarg :sha :initform nil)
  111. (user :initarg :user :initform nil)
  112. (repo :initarg :repo :initform nil)
  113. (user-cls :allocation :class :initform gh-user)
  114. (repo-cls :allocation :class :initform gh-repos-repo)))
  115. (defmethod gh-object-read-into ((r gh-repos-ref) data)
  116. (call-next-method)
  117. (with-slots (label ref sha user repo)
  118. r
  119. (setq label (gh-read data 'label)
  120. ref (gh-read data 'ref)
  121. sha (gh-read data 'sha)
  122. user (gh-object-read (or (oref r :user)
  123. (oref r user-cls))
  124. (gh-read data 'user))
  125. repo (gh-object-read (or (oref r :repo)
  126. (oref r repo-cls))
  127. (gh-read data 'repo)))))
  128. (defmethod gh-repos-user-list ((api gh-repos-api) &optional username)
  129. (gh-api-authenticated-request
  130. api (gh-object-list-reader (oref api repo-cls)) "GET"
  131. (format "/users/%s/repos" (or username (gh-api-get-username api)))))
  132. (defmethod gh-repos-org-list ((api gh-repos-api) org)
  133. (gh-api-authenticated-request
  134. api (gh-object-list-reader (oref api repo-cls)) "GET"
  135. (format "/orgs/%s/repos" org)))
  136. (defmethod gh-repos-repo-to-obj ((repo gh-repos-repo-stub)
  137. &rest caps)
  138. (let ((has_issues (plist-member caps :issues))
  139. (has_wiki (plist-member caps :wiki))
  140. (has_downloads (plist-member caps :downloads)))
  141. `(("name" . ,(oref repo :name))
  142. ,@(when (slot-boundp repo :homepage)
  143. (list (cons "homepage" (oref repo :homepage))))
  144. ,@(when (slot-boundp repo :description)
  145. (list (cons "description" (oref repo :description))))
  146. ,@(when (slot-boundp repo :private)
  147. (list (cons "public" (not (oref repo :private)))))
  148. ,@(when has_issues
  149. (list (cons "has_issues" (plist-get caps :issues))))
  150. ,@(when has_wiki
  151. (list (cons "has_wiki" (plist-get caps :wiki))))
  152. ,@(when has_downloads
  153. (list (cons "has_downloads" (plist-get caps :downloads)))))))
  154. (defmethod gh-repos-repo-new ((api gh-repos-api) repo-stub
  155. &optional org &rest caps)
  156. (gh-api-authenticated-request
  157. api (gh-object-reader (oref api repo-cls)) "POST"
  158. (if org (format "/orgs/%s/repos" org)
  159. "/user/repos")
  160. (apply 'gh-repos-repo-to-obj repo-stub caps)))
  161. (defmethod gh-repos-repo-get ((api gh-repos-api) repo-id &optional user)
  162. (gh-api-authenticated-request
  163. api (gh-object-reader (oref api repo-cls)) "GET"
  164. (format "/repos/%s/%s"
  165. (or user (gh-api-get-username api))
  166. repo-id)))
  167. (defmethod gh-repos-repo-update ((api gh-repos-api) repo-stub
  168. &optional user &rest caps)
  169. (gh-api-authenticated-request
  170. api (gh-object-reader (oref api repo-cls)) "PATCH"
  171. (format "/repos/%s/%s"
  172. (or user (gh-api-get-username api))
  173. (oref repo-stub :name))
  174. (apply 'gh-repos-repo-to-obj repo-stub caps)))
  175. (defmethod gh-repos-repo-rename ((api gh-repos-api) repo-stub new-name
  176. &optional user)
  177. (let ((new-stub (gh-repos-repo-stub "repo" :name new-name)))
  178. (gh-api-authenticated-request
  179. api (gh-object-reader (oref api repo-cls)) "PATCH"
  180. (format "/repos/%s/%s"
  181. (or user (gh-api-get-username api))
  182. (oref repo-stub :name))
  183. (gh-repos-repo-to-obj new-stub))))
  184. (defmethod gh-repos-repo-contributors ((api gh-repos-api) repo)
  185. (gh-api-authenticated-request
  186. api (gh-object-reader (oref api repo-cls)) "GET"
  187. (format "/repos/%s/%s/contributors"
  188. (oref (oref repo :owner) :login)
  189. (oref repo :name))))
  190. ;;; TODO: generate some useful objects with the return values
  191. (defmethod gh-repos-repo-languages ((api gh-repos-api) repo)
  192. (gh-api-authenticated-request
  193. api nil "GET" (format "/repos/%s/%s/languages"
  194. (oref (oref repo :owner) :login)
  195. (oref repo :name))))
  196. (defmethod gh-repos-repo-teams ((api gh-repos-api) repo)
  197. (gh-api-authenticated-request
  198. api nil "GET" (format "/repos/%s/%s/teams"
  199. (oref (oref repo :owner) :login)
  200. (oref repo :name))))
  201. (defmethod gh-repos-repo-tags ((api gh-repos-api) repo)
  202. (gh-api-authenticated-request
  203. api nil "GET" (format "/repos/%s/%s/tags"
  204. (oref (oref repo :owner) :login)
  205. (oref repo :name))))
  206. (defmethod gh-repos-repo-branches ((api gh-repos-api) repo)
  207. (gh-api-authenticated-request
  208. api nil "GET" (format "/repos/%s/%s/branches"
  209. (oref (oref repo :owner) :login)
  210. (oref repo :name))))
  211. ;;; Forks sub-API
  212. (defmethod gh-repos-forks-list ((api gh-repos-api) repo)
  213. (gh-api-authenticated-request
  214. api (gh-object-list-reader (oref api repo-cls)) "GET"
  215. (format "/repos/%s/%s/forks"
  216. (oref (oref repo :owner) :login)
  217. (oref repo :name))))
  218. (defmethod gh-repos-fork ((api gh-repos-api) repo &optional org)
  219. (gh-api-authenticated-request
  220. api (gh-object-reader (oref api repo-cls)) "POST"
  221. (format "/repos/%s/%s/forks"
  222. (oref (oref repo :owner) :login)
  223. (oref repo :name))
  224. nil (when org `(("org" . ,org)))))
  225. (provide 'gh-repos)
  226. ;;; gh-repos.el ends here
  227. ;; Local Variables:
  228. ;; indent-tabs-mode: nil
  229. ;; End: