PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/libsaas/services/bitbucket/repositories.py

http://github.com/ducksboard/libsaas
Python | 198 lines | 131 code | 26 blank | 41 comment | 2 complexity | ca5be54ef382ecdd4afa4d7b6326acaa MD5 | raw file
  1. from libsaas import http, parsers
  2. from libsaas.services import base
  3. from . import resource, privileges, issues, links, changesets, services
  4. class Repos(resource.BitBucketResource):
  5. path = 'repositories'
  6. @base.apimethod
  7. def get(self, *args, **kwargs):
  8. """
  9. Fetch all repositories you have access to.
  10. """
  11. url = '{0}/user/repositories/'.format(self.parent.get_url())
  12. return http.Request('GET', url), parsers.parse_json
  13. @base.apimethod
  14. def search(self, name=None):
  15. """
  16. Search for repositories with the given name.
  17. """
  18. params = base.get_params(('name', ), locals())
  19. request = http.Request('GET', self.get_url(), params)
  20. return request, parsers.parse_json
  21. @base.apimethod
  22. def create(self, name, scm=None, is_private=False):
  23. """
  24. Create a new repository.
  25. :var name: the repository name.
  26. :var scm: the type of repository you want to create, can be:
  27. git: for git repository
  28. hg: for mercurial repository
  29. """
  30. params = base.get_params(('name', 'scm'), locals())
  31. params['is_private'] = 'true' if is_private else 'false'
  32. request = http.Request('POST', self.get_url(), params)
  33. return request, parsers.parse_json
  34. @base.apimethod
  35. def delete(self):
  36. """
  37. Delete a repository.
  38. """
  39. request = http.Request('DELETE', self.get_url())
  40. return request, parsers.parse_json
  41. class Repo(resource.BitBucketResource):
  42. def __init__(self, parent, user, repo):
  43. self.parent = parent
  44. self.user = user
  45. self.repo = repo
  46. def get_url(self):
  47. return '{0}/repositories/{1}/{2}'.format(self.parent.get_url(),
  48. self.user, self.repo)
  49. def require_item(self):
  50. pass
  51. def require_collection(self):
  52. raise base.MethodNotSupported()
  53. @base.apimethod
  54. def tags(self):
  55. """
  56. Fetch the repository tags.
  57. """
  58. url = '{0}/tags/'.format(self.get_url())
  59. request = http.Request('GET', url)
  60. return request, parsers.parse_json
  61. @base.apimethod
  62. def branches(self):
  63. """
  64. Fetch the repository branches.
  65. """
  66. url = '{0}/branches/'.format(self.get_url())
  67. request = http.Request('GET', url)
  68. return request, parsers.parse_json
  69. @base.apimethod
  70. def invite(self, user, permission):
  71. """
  72. Invite a user to participate in the repository, with the given
  73. permissions.
  74. :var user: The email of the user to invite.
  75. :vartype user: str
  76. :var permission: The permission to grant (either read or write)
  77. :vartype permission: str
  78. """
  79. url = '{0}/invitations/{1}/{2}/{3}'.format(self.parent.get_url(),
  80. self.user, self.repo, user)
  81. params = base.get_params(('permission', ), locals())
  82. request = http.Request('POST', url, params)
  83. return request, parsers.parse_json
  84. @base.apimethod
  85. def followers(self):
  86. """
  87. Fetch the followers of this repo.
  88. """
  89. request = http.Request('GET', '{0}/followers/'.format(self.get_url()))
  90. return request, parsers.parse_json
  91. @base.apimethod
  92. def events(self, start=0, limit=15, etype=None):
  93. """
  94. Fetch events for this repository.
  95. :var start: Event start, default is 0.
  96. :var limit: Event result limit, default is 15.
  97. :var type: Event type, for example 'issue_comment'.
  98. """
  99. params = base.get_params(('start', 'limit', 'etype'), locals())
  100. url = '{0}/events/'.format(self.get_url())
  101. request = http.Request('GET', url, params)
  102. return request, parsers.parse_json
  103. @base.resource(privileges.RepoPrivileges)
  104. def privileges(self, specific_user=None):
  105. """
  106. Return a resource corresponding to all privileges from this repo,
  107. either for everyone or for a specific user.
  108. """
  109. return privileges.RepoPrivileges(
  110. self, self.user, self.repo, specific_user)
  111. @base.resource(issues.RepoIssue)
  112. def issue(self, id):
  113. """
  114. Return a resource corresponding to an issue from this repo.
  115. """
  116. return issues.RepoIssue(self, id)
  117. @base.resource(issues.RepoIssues)
  118. def issues(self):
  119. """
  120. Return a resource corresponding to all issues from this repo.
  121. """
  122. return issues.RepoIssues(self)
  123. @base.resource(links.RepoLink)
  124. def link(self, id):
  125. """
  126. Reurn a resource corresponding to a link from this repo.
  127. """
  128. return links.RepoLink(self, id)
  129. @base.resource(links.RepoLinks)
  130. def links(self):
  131. """
  132. Return a resouce corresponding to all the links from this repo.
  133. """
  134. return links.RepoLinks(self)
  135. @base.resource(changesets.Changeset)
  136. def changeset(self, changeset_md5):
  137. """
  138. Return a resource corresponding to a changeset for this repo.
  139. """
  140. return changesets.Changeset(self, changeset_md5)
  141. @base.resource(changesets.Changesets)
  142. def changesets(self):
  143. """
  144. Return a resource corresponding to all the changesets for this repo.
  145. """
  146. return changesets.Changesets(self)
  147. @base.resource(services.Service)
  148. def service(self, service_id):
  149. """
  150. Return a resource corresponding to one service for this repo.
  151. """
  152. return services.Service(self, service_id)
  153. @base.resource(services.Services)
  154. def services(self):
  155. """
  156. Return a resource corresponding to all the services for this repo.
  157. """
  158. return services.Services(self)