# HG changeset patch # User Thomas De Schampheleire # Date 2020-06-15 13:43:55 # Node ID 833b93e83349282a970534df26385f7a8bdee884 # Parent 2589ee18c7962517690d8c77e22a79bf9e77839d 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. diff --git a/kallithea/config/rcextensions/__init__.py b/kallithea/config/rcextensions/__init__.py --- a/kallithea/config/rcextensions/__init__.py +++ b/kallithea/config/rcextensions/__init__.py @@ -104,6 +104,28 @@ def CREATE_USER_HOOK(*args, **kwargs): #============================================================================== +# 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 diff --git a/kallithea/lib/hooks.py b/kallithea/lib/hooks.py --- a/kallithea/lib/hooks.py +++ b/kallithea/lib/hooks.py @@ -218,6 +218,19 @@ def log_create_user(user_dict, created_b 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. diff --git a/kallithea/model/pull_request.py b/kallithea/model/pull_request.py --- a/kallithea/model/pull_request.py +++ b/kallithea/model/pull_request.py @@ -33,6 +33,7 @@ from tg import request 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 @@ class CreatePullRequestAction(object): 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