rtd_hook.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #!/usr/bin/env python
  2. """Pagure specific hook to trigger a build on a readthedocs.org project.
  3. """
  4. from __future__ import print_function, unicode_literals
  5. import os
  6. import sys
  7. import requests
  8. if 'PAGURE_CONFIG' not in os.environ \
  9. and os.path.exists('/etc/pagure/pagure.cfg'):
  10. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  11. import pagure # noqa: E402
  12. import pagure.exceptions # noqa: E402
  13. import pagure.lib.link # noqa: E402
  14. import pagure.lib.plugins # noqa: E402
  15. _config = pagure.config.config
  16. abspath = os.path.abspath(os.environ['GIT_DIR'])
  17. def run_as_post_receive_hook():
  18. reponame = pagure.lib.git.get_repo_name(abspath)
  19. username = pagure.lib.git.get_username(abspath)
  20. namespace = pagure.lib.git.get_repo_namespace(abspath)
  21. session = pagure.lib.create_session(_config['DB_URL'])
  22. if _config.get('HOOK_DEBUG', False):
  23. print('repo: ', reponame)
  24. print('user: ', username)
  25. print('namespace:', namespace)
  26. repo = pagure.lib.get_authorized_project(
  27. session, reponame, user=username, namespace=namespace)
  28. if not repo:
  29. print('Unknown repo %s of username: %s' % (reponame, username))
  30. session.close()
  31. sys.exit(1)
  32. hook = pagure.lib.plugins.get_plugin('Read the Doc')
  33. hook.db_object()
  34. # Get the list of branches
  35. branches = [
  36. branch.strip()
  37. for branch in repo.rtd_hook.branches.split(',')
  38. if repo.rtd_hook]
  39. # Remove empty branches
  40. branches = [
  41. branch.strip()
  42. for branch in branches
  43. if branch]
  44. url = repo.rtd_hook.api_url
  45. if not url:
  46. print('No API url specified to trigger the build, please update '
  47. 'the configuration')
  48. session.close()
  49. return 1
  50. if not repo.rtd_hook.api_token:
  51. print('No API token specified to trigger the build, please update '
  52. 'the configuration')
  53. session.close()
  54. return 1
  55. for line in sys.stdin:
  56. if _config.get('HOOK_DEBUG', False):
  57. print(line)
  58. (oldrev, newrev, refname) = line.strip().split(' ', 2)
  59. refname = refname.replace('refs/heads/', '')
  60. if branches:
  61. if refname in branches:
  62. print('Starting RTD build at %s' % (url))
  63. requests.post(
  64. url,
  65. data={
  66. 'branches': refname,
  67. 'token': repo.rtd_hook.api_token
  68. },
  69. timeout=60,
  70. )
  71. else:
  72. print('Starting RTD build at %s' % (url))
  73. requests.post(
  74. url,
  75. data={
  76. 'branches': refname,
  77. 'token': repo.rtd_hook.api_token
  78. },
  79. timeout=60,
  80. )
  81. session.close()
  82. def main(args):
  83. run_as_post_receive_hook()
  84. if __name__ == '__main__':
  85. main(sys.argv[1:])