repospannerhook 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  4. (c) 2018 - Copyright Red Hat Inc
  5. Authors:
  6. Patrick Uiterwijk <puiterwijk@redhat.com>
  7. """
  8. from __future__ import print_function
  9. import os
  10. import sys
  11. # These fields get filled in by upload-repospanner-hooks
  12. os.environ["PAGURE_CONFIG"] = "${config}"
  13. PYPATH = "${pypath}"
  14. # Prepare code imports
  15. if PYPATH:
  16. sys.path.append(PYPATH)
  17. import pagure
  18. import pagure.lib.query
  19. from pagure.hooks import run_project_hooks, extract_changes
  20. from pagure.config import config as pagure_config
  21. # Get information from the environment
  22. hooktype = os.path.basename(sys.argv[0])
  23. is_internal = os.environ.get("extra_internal", False) == "yes"
  24. pushuser = os.environ["extra_username"]
  25. repotype = os.environ["extra_repotype"]
  26. project_name = os.environ["extra_project_name"]
  27. project_user = os.environ.get("extra_project_user", None) or None
  28. project_namespace = os.environ.get("extra_project_namespace", None) or None
  29. pruid = os.environ.get("extra_pull_request_uid", None)
  30. changes = extract_changes(from_stdin=hooktype != "update")
  31. session = pagure.lib.query.create_session(pagure_config["DB_URL"])
  32. if not session:
  33. raise Exception("Unable to initialize db session")
  34. gitdir = os.path.abspath(os.environ["GIT_DIR"])
  35. project = pagure.lib.query._get_project(
  36. session, project_name, project_user, project_namespace
  37. )
  38. if not project:
  39. print("No project found")
  40. sys.exit(1)
  41. pull_request = None
  42. if pruid:
  43. pull_request = pagure.lib.query.get_request_by_uid(
  44. session, pruid
  45. )
  46. run_project_hooks(
  47. session,
  48. pushuser,
  49. project,
  50. hooktype,
  51. repotype,
  52. gitdir,
  53. changes,
  54. is_internal,
  55. pull_request,
  56. )