Browse Source

Add pull request ID to push notification payload

Signed-off-by: Nikola Forró <nforro@redhat.com>
Nikola Forró 1 month ago
parent
commit
8ed510d99c

+ 24 - 4
pagure/hooks/__init__.py

@@ -62,7 +62,15 @@ class BaseRunner(object):
 
     @classmethod
     def runhook(
-        cls, session, username, hooktype, project, repotype, repodir, changes
+        cls,
+        session,
+        username,
+        hooktype,
+        project,
+        repotype,
+        repodir,
+        changes,
+        pull_request,
     ):
         """Run a specific hook on a project.
 
@@ -81,6 +89,8 @@ class BaseRunner(object):
             changes (dict): A dict with keys being the ref to update, values
                 being a tuple of (from, to).
                 For example: {'refs/heads/master': (hash_from, hash_to), ...}
+            pull_request (model.PullRequest or None): The pull request whose
+                merge is initiating this hook run.
         """
         if hooktype == "pre-receive":
             cls.pre_receive(
@@ -90,6 +100,7 @@ class BaseRunner(object):
                 repotype=repotype,
                 repodir=repodir,
                 changes=changes,
+                pull_request=pull_request,
             )
         elif hooktype == "update":
             cls.update(
@@ -99,6 +110,7 @@ class BaseRunner(object):
                 repotype=repotype,
                 repodir=repodir,
                 changes=changes,
+                pull_request=pull_request,
             )
 
         elif hooktype == "post-receive":
@@ -109,12 +121,15 @@ class BaseRunner(object):
                 repotype=repotype,
                 repodir=repodir,
                 changes=changes,
+                pull_request=pull_request,
             )
         else:
             raise ValueError('Invalid hook type "%s"' % hooktype)
 
     @staticmethod
-    def pre_receive(session, username, project, repotype, repodir, changes):
+    def pre_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the pre-receive tasks of a hook.
 
         For args, see BaseRunner.runhook.
@@ -122,7 +137,9 @@ class BaseRunner(object):
         pass
 
     @staticmethod
-    def update(session, username, project, repotype, repodir, changes):
+    def update(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the update tasks of a hook.
 
         For args, see BaseRunner.runhook.
@@ -131,7 +148,9 @@ class BaseRunner(object):
         pass
 
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the post-receive tasks of a hook.
 
         For args, see BaseRunner.runhook.
@@ -384,6 +403,7 @@ def run_project_hooks(
                     repotype=repotype,
                     repodir=repodir,
                     changes=changes,
+                    pull_request=pull_request,
                 )
             except Exception as e:
                 if hooktype != "pre-receive" or debug:

+ 6 - 2
pagure/hooks/default.py

@@ -163,7 +163,7 @@ def send_action_notification(
 
 
 def send_notifications(
-    session, project, repodir, user, refname, revs, forced, oldrev
+    session, project, repodir, user, refname, revs, forced, oldrev, pr_id
 ):
     """Send out-going notifications about the commits that have just been
     pushed.
@@ -212,6 +212,7 @@ def send_notifications(
             repo=project.to_json(public=True)
             if not isinstance(project, six.string_types)
             else project,
+            pull_request_id=pr_id,
         )
 
         # Send blink notification to any 3rd party plugins, if there are any
@@ -313,7 +314,9 @@ class DefaultRunner(BaseRunner):
     """Runner for the default hook."""
 
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the default post-receive hook.
 
         For args, see BaseRunner.runhook.
@@ -442,6 +445,7 @@ class DefaultRunner(BaseRunner):
                 commits,
                 forced,
                 oldrev,
+                pull_request.id if pull_request else None,
             )
 
             # Now display to the user if this isn't the default branch links to

+ 3 - 1
pagure/hooks/mail.py

@@ -70,7 +70,9 @@ class MailForm(FlaskForm):
 
 class MailRunner(BaseRunner):
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the multimail post-receive hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/mirror_hook.py

@@ -67,7 +67,9 @@ class MirrorRunner(BaseRunner):
     """Runner for the mirror hook."""
 
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the default post-receive hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/pagure_force_commit.py

@@ -63,7 +63,9 @@ class PagureForceCommitRunner(BaseRunner):
     """Runner for the hook blocking force push."""
 
     @staticmethod
-    def pre_receive(session, username, project, repotype, repodir, changes):
+    def pre_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the pre-receive tasks of a hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/pagure_hook.py

@@ -214,7 +214,9 @@ class PagureRunner(BaseRunner):
     """Runner for the pagure's specific git hook."""
 
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the default post-receive hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/pagure_no_new_branches.py

@@ -59,7 +59,9 @@ class PagureNoNewBranchRunner(BaseRunner):
     """Runner for the hook blocking new branches from being created."""
 
     @staticmethod
-    def pre_receive(session, username, project, repotype, repodir, changes):
+    def pre_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the pre-receive tasks of a hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/pagure_request_hook.py

@@ -64,7 +64,9 @@ class PagureRequestRunner(BaseRunner):
     """
 
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the default post-receive hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/pagure_ticket_hook.py

@@ -65,7 +65,9 @@ class PagureTicketRunner(BaseRunner):
     """Runner for the git hook updating the DB of tickets on push."""
 
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the post-receive tasks of a hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/pagure_unsigned_commits.py

@@ -64,7 +64,9 @@ class PagureUnsignerRunner(BaseRunner):
     """Runner for the hook blocking unsigned commits."""
 
     @staticmethod
-    def pre_receive(session, username, project, repotype, repodir, changes):
+    def pre_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Run the pre-receive tasks of a hook.
 
         For args, see BaseRunner.runhook.

+ 3 - 1
pagure/hooks/rtd.py

@@ -101,7 +101,9 @@ will have to provide below.
 
 class RtdRunner(BaseRunner):
     @staticmethod
-    def post_receive(session, username, project, repotype, repodir, changes):
+    def post_receive(
+        session, username, project, repotype, repodir, changes, pull_request
+    ):
         """Perform the RTD Post Receive hook.
 
         For arguments, see BaseRunner.runhook.

+ 2 - 0
tests/test_pagure_send_notification.py

@@ -74,6 +74,7 @@ class PagureHooksDefault(tests.SimplePagureTest):
             [sha],
             False,
             oldsha,
+            None,
         )
         (_, args, kwargs) = fedmsg.mock_calls[0]
         self.assertEqual(args[1], "git.receive")
@@ -88,6 +89,7 @@ class PagureHooksDefault(tests.SimplePagureTest):
                 "folder1/folder2/fileŠ": "A",
             },
         )
+        self.assertIsNone(args[2]["pull_request_id"])
 
 
 if __name__ == "__main__":