ftp_deploy.utils.repo: 50 total statements, 15.2% covered

Generated: Thu 2013-12-19 21:13 GMT

Source file: /var/www/service.dev/service/ftp_deploy/utils/repo.py

Stats: 7 executed, 39 missed, 4 excluded, 18 ignored

  1. import pycurl
  2. import json
  3. from .curl import curl_connection
  4. from .decorators import check
  5. class bitbucket_check(curl_connection):
  6. """Bitbucket check class contain all checking points for bitbucket repository, return True if fail"""
  7. def __init__(self, username, password, service):
  8. super(bitbucket_check, self).__init__(username, password)
  9. self.service = service
  10. @check('Bitbucket')
  11. def check_authentication(self):
  12. self.authenticate()
  13. self.perform('https://bitbucket.org/api/1.0/user/repositories')
  14. if self.curl.getinfo(pycurl.HTTP_CODE) != 200:
  15. raise Exception("Login Fail")
  16. @check('Bitbucket')
  17. def check_repo_exist(self):
  18. self.authenticate()
  19. repos = json.loads(self.perform('https://bitbucket.org/api/1.0/user/repositories'))
  20. for repo in repos:
  21. if repo['slug'] == self.service.repo_slug_name:
  22. return False, ''
  23. raise Exception("Repository %s doesn't exist" % self.service.repo_slug_name)
  24. @check('Bitbucket')
  25. def check_branch_exist(self):
  26. self.authenticate()
  27. url = 'https://bitbucket.org/api/1.0/repositories/%s/%s/branches' % (self.username, self.service.repo_slug_name)
  28. branches = json.loads(self.perform(url))
  29. try:
  30. branches[self.service.repo_branch]
  31. except KeyError, e:
  32. raise Exception("Branch %s doesn't exist" % self.service.repo_branch)
  33. @check('Bitbucket')
  34. def check_hook_exist(self):
  35. self.authenticate()
  36. url = 'https://bitbucket.org/api/1.0/repositories/%s/%s/services' % (self.username, self.service.repo_slug_name)
  37. hooks = json.loads(self.perform(url))
  38. if type(hooks) == list:
  39. for hook in hooks:
  40. if len(hook['service']['fields']) > 0:
  41. value = hook['service']['fields'][0]['value']
  42. if value.find(str(self.service.hook_url())) != -1 and hook['service']['type'] == 'POST':
  43. return False, ''
  44. raise Exception("Hook is not set up")
  45. def check_all(self):
  46. status = self.check_authentication()
  47. if status[0] == True:
  48. return status
  49. status = self.check_repo_exist()
  50. if status[0] == True:
  51. return status
  52. status = self.check_branch_exist()
  53. if status[0] == True:
  54. return status
  55. return False, ''