changeset 8650:833b93e83349

hooks: add extensible create-pullrequest hook Add a hook that will be called when a new pull request is created, and which can be implemented in the 'extensions' package.
author Thomas De Schampheleire <thomas.de_schampheleire@nokia.com>
date Mon, 15 Jun 2020 13:43:55 +0200
parents 2589ee18c796
children 495dea7c2a13
files kallithea/config/rcextensions/__init__.py kallithea/lib/hooks.py kallithea/model/pull_request.py
diffstat 3 files changed, 38 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/kallithea/config/rcextensions/__init__.py	Wed Sep 30 16:57:05 2020 +0200
+++ b/kallithea/config/rcextensions/__init__.py	Mon Jun 15 13:43:55 2020 +0200
@@ -104,6 +104,28 @@
 
 
 #==============================================================================
+# POST CREATE PULLREQUEST HOOK
+#==============================================================================
+# this function will be executed after a pull request is created
+def CREATE_PULLREQUEST_HOOK(*args, **kwargs):
+    """
+    Post create pull request HOOK
+    kwargs available:
+      :param pull_request_id:
+      :param title:
+      :param description:
+      :param created_on:
+      :param org_repo_id:
+      :param org_ref:
+      :param other_repo_id:
+      :param other_ref:
+      :param created_by:
+    There are other fields in 'class PullRequest' (kallithea/model/db.py) which
+    may or may not be useful for this hook.
+    """
+
+
+#==============================================================================
 # POST DELETE REPOSITORY HOOK
 #==============================================================================
 # this function will be executed after each repository deletion
--- a/kallithea/lib/hooks.py	Wed Sep 30 16:57:05 2020 +0200
+++ b/kallithea/lib/hooks.py	Mon Jun 15 13:43:55 2020 +0200
@@ -218,6 +218,19 @@
         callback(created_by=created_by, **user_dict)
 
 
+def log_create_pullrequest(pullrequest_dict, created_by, **kwargs):
+    """
+    Post create pull request hook.
+
+    :param pullrequest_dict: dict dump of pull request object
+    """
+    from kallithea import EXTENSIONS
+    callback = getattr(EXTENSIONS, 'CREATE_PULLREQUEST_HOOK', None)
+    if callable(callback):
+        return callback(created_by=created_by, **pullrequest_dict)
+
+    return 0
+
 def log_delete_repository(repository_dict, deleted_by, **kwargs):
     """
     Post delete repository Hook.
--- a/kallithea/model/pull_request.py	Wed Sep 30 16:57:05 2020 +0200
+++ b/kallithea/model/pull_request.py	Mon Jun 15 13:43:55 2020 +0200
@@ -33,6 +33,7 @@
 from tg.i18n import ugettext as _
 
 from kallithea.lib import helpers as h
+from kallithea.lib.hooks import log_create_pullrequest
 from kallithea.lib.utils2 import ascii_bytes, extract_mentioned_users
 from kallithea.model.db import ChangesetStatus, PullRequest, PullRequestReviewer, User
 from kallithea.model.meta import Session
@@ -296,6 +297,8 @@
         mention_recipients = extract_mentioned_users(self.description)
         PullRequestModel().add_reviewers(created_by, pr, self.reviewers, mention_recipients)
 
+        log_create_pullrequest(pr.get_dict(), created_by)
+
         return pr