pagure_hook_tickets.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #!/usr/bin/env python
  2. """Pagure specific hook to update tickets stored in the database based on
  3. the information pushed in the tickets git repository.
  4. """
  5. from __future__ import print_function, unicode_literals
  6. import os
  7. import sys
  8. # We need to access the database
  9. if 'PAGURE_CONFIG' not in os.environ \
  10. and os.path.exists('/etc/pagure/pagure.cfg'):
  11. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  12. import pagure.config # noqa: E402
  13. import pagure.lib.tasks_services # noqa: E402
  14. _config = pagure.config.config
  15. abspath = os.path.abspath(os.environ['GIT_DIR'])
  16. def run_as_post_receive_hook():
  17. repo = pagure.lib.git.get_repo_name(abspath)
  18. username = pagure.lib.git.get_username(abspath)
  19. namespace = pagure.lib.git.get_repo_namespace(
  20. abspath, gitfolder=_config['TICKETS_FOLDER'])
  21. if _config.get('HOOK_DEBUG', False):
  22. print('repo:', repo)
  23. print('user:', username)
  24. print('namespace:', namespace)
  25. for line in sys.stdin:
  26. if _config.get('HOOK_DEBUG', False):
  27. print(line)
  28. (oldrev, newrev, refname) = line.strip().split(' ', 2)
  29. if _config.get('HOOK_DEBUG', False):
  30. print(' -- Old rev')
  31. print(oldrev)
  32. print(' -- New rev')
  33. print(newrev)
  34. print(' -- Ref name')
  35. print(refname)
  36. if set(newrev) == set(['0']):
  37. print("Deleting a reference/branch, so we won't run the "
  38. "pagure hook")
  39. return
  40. commits = pagure.lib.git.get_revs_between(
  41. oldrev, newrev, abspath, refname)
  42. pagure.lib.tasks_services.load_json_commits_to_db.delay(
  43. name=repo,
  44. commits=commits,
  45. abspath=abspath,
  46. data_type='ticket',
  47. agent=os.environ.get('GL_USER'),
  48. namespace=namespace,
  49. username=username,
  50. )
  51. def main(args):
  52. run_as_post_receive_hook()
  53. if __name__ == '__main__':
  54. main(sys.argv[1:])