pagure_request_hook.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # -*- coding: utf-8 -*-
  2. """
  3. (c) 2014-2016 - Copyright Red Hat Inc
  4. Authors:
  5. Pierre-Yves Chibon <pingou@pingoured.fr>
  6. """
  7. from __future__ import absolute_import, unicode_literals
  8. import sqlalchemy as sa
  9. import wtforms
  10. try:
  11. from flask_wtf import FlaskForm
  12. except ImportError:
  13. from flask_wtf import Form as FlaskForm
  14. from sqlalchemy.orm import backref, relation
  15. import pagure.lib.git
  16. import pagure.lib.tasks_services
  17. from pagure.hooks import BaseHook, BaseRunner
  18. from pagure.lib.model import BASE, Project
  19. class PagureRequestsTable(BASE):
  20. """Stores information about the pagure requests hook deployed on a
  21. project.
  22. Table -- hook_pagure_requests
  23. """
  24. __tablename__ = "hook_pagure_requests"
  25. id = sa.Column(sa.Integer, primary_key=True)
  26. project_id = sa.Column(
  27. sa.Integer,
  28. sa.ForeignKey("projects.id", onupdate="CASCADE", ondelete="CASCADE"),
  29. nullable=False,
  30. unique=True,
  31. index=True,
  32. )
  33. active = sa.Column(sa.Boolean, nullable=False, default=False)
  34. project = relation(
  35. "Project",
  36. remote_side=[Project.id],
  37. backref=backref(
  38. "pagure_hook_requests",
  39. cascade="delete, delete-orphan",
  40. single_parent=True,
  41. uselist=False,
  42. ),
  43. )
  44. class PagureRequestRunner(BaseRunner):
  45. """Runner for the hook updating the db about requests on push to the
  46. git repo containing the meta-data about pull-requests.
  47. """
  48. @staticmethod
  49. def post_receive(
  50. session, username, project, repotype, repodir, changes, pull_request
  51. ):
  52. """Run the default post-receive hook.
  53. For args, see BaseRunner.runhook.
  54. """
  55. if repotype != "requests":
  56. print(
  57. "The pagure requests hook only runs on the requests "
  58. "git repo."
  59. )
  60. return
  61. if username == "pagure":
  62. # This was an update from inside the UI. Do not trigger further
  63. # database updates, as this has already been done
  64. return
  65. for refname in changes:
  66. (oldrev, newrev) = changes[refname]
  67. if set(newrev) == set(["0"]):
  68. print(
  69. "Deleting a reference/branch, so we won't run the "
  70. "pagure hook"
  71. )
  72. return
  73. commits = pagure.lib.git.get_revs_between(
  74. oldrev, newrev, repodir, refname
  75. )
  76. pagure.lib.tasks_services.load_json_commits_to_db.delay(
  77. name=project.name,
  78. commits=commits,
  79. abspath=repodir,
  80. data_type="pull-request",
  81. agent=username,
  82. namespace=project.namespace,
  83. username=project.user.user if project.is_fork else None,
  84. )
  85. class PagureRequestsForm(FlaskForm):
  86. """Form to configure the pagure hook."""
  87. active = wtforms.BooleanField("Active", [wtforms.validators.Optional()])
  88. class PagureRequestHook(BaseHook):
  89. """Pagure request hook."""
  90. name = "Pagure requests"
  91. description = (
  92. "Pagure specific hook to update pull-requests stored "
  93. "in the database based on the information pushed in the requests "
  94. "git repository."
  95. )
  96. form = PagureRequestsForm
  97. db_object = PagureRequestsTable
  98. backref = "pagure_hook_requests"
  99. form_fields = ["active"]
  100. runner = PagureRequestRunner