From 053ab19ee3e1856c3b2d54f50b980d53ddec9a26 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 25 2019 08:22:39 +0000 Subject: [PATCH 1/2] Send a notification upon editing the initial comment of a PR We're sending notification when someone edits the initial comment of a PR. This notification can now be using any of the bus we support or simply web-hooks. Fixes https://pagure.io/pagure/issue/4398 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/api/fork.py b/pagure/api/fork.py index 9451f94..64eb542 100644 --- a/pagure/api/fork.py +++ b/pagure/api/fork.py @@ -491,6 +491,18 @@ def api_pull_request_update(repo, requestid, username=None, namespace=None): request.title = form.title.data.strip() request.initial_comment = form.initial_comment.data.strip() flask.g.session.add(request) + if not request.private and not request.project.private: + pagure.lib.notify.log( + request.project, + topic="pull-request.initial_comment.edited", + msg={ + "pullrequest": request.to_json( + public=True, with_comments=False + ), + "project": request.project.to_json(public=True), + "agent": flask.g.fas_user.username, + }, + ) try: # Link the PR to issue(s) if there is such link pagure.lib.query.link_pr_to_issue_on_description( diff --git a/pagure/ui/fork.py b/pagure/ui/fork.py index ef5b312..6262e20 100644 --- a/pagure/ui/fork.py +++ b/pagure/ui/fork.py @@ -538,6 +538,18 @@ def request_pull_edit(repo, requestid, username=None, namespace=None): request.title = form.title.data.strip() request.initial_comment = form.initial_comment.data.strip() flask.g.session.add(request) + if not request.private and not request.project.private: + pagure.lib.notify.log( + request.project, + topic="pull-request.initial_comment.edited", + msg={ + "pullrequest": request.to_json( + public=True, with_comments=False + ), + "project": request.project.to_json(public=True), + "agent": flask.g.fas_user.username, + }, + ) try: # Link the PR to issue(s) if there is such link pagure.lib.query.link_pr_to_issue_on_description( From 7190cfcb83b0563597f671d6def21e181f83281f Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Apr 26 2019 08:52:19 +0000 Subject: [PATCH 2/2] Send notifications on tag creation and tag and branch deletion Fixes https://pagure.io/pagure/issue/4400 Fixes https://pagure.io/pagure/issue/4399 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/hooks/default.py b/pagure/hooks/default.py index 770ea6c..6b1b99e 100644 --- a/pagure/hooks/default.py +++ b/pagure/hooks/default.py @@ -28,6 +28,9 @@ _config = pagure.config.reload_config() _log = logging.getLogger(__name__) +FEDMSG_INIT = False + + def send_fedmsg_notifications(project, topic, msg): """ If the user or admin asked for fedmsg notifications on commit, this will do it. @@ -43,13 +46,16 @@ def send_fedmsg_notifications(project, topic, msg): if always_fedmsg or (project.fedmsg_hook and project.fedmsg_hook.active): if _config.get("FEDMSG_NOTIFICATIONS", True): try: + global FEDMSG_INIT print(" - to fedmsg") import fedmsg config = fedmsg.config.load_config([], None) config["active"] = True config["endpoints"]["relay_inbound"] = config["relay_inbound"] - fedmsg.init(name="relay_inbound", **config) + if not FEDMSG_INIT: + fedmsg.init(name="relay_inbound", **config) + FEDMSG_INIT = True pagure.lib.notify.fedmsg_publish(topic=topic, msg=msg) except Exception: @@ -116,6 +122,42 @@ def send_webhook_notifications(project, topic, msg): ) +def send_action_notification( + session, subject, action, project, repodir, user, refname, rev +): + """ Send out-going notifications about the branch that was just deleted. + """ + email = pagure.lib.git.get_author_email(rev, repodir) + name = pagure.lib.git.get_author(rev, repodir) + author = pagure.lib.query.search_user(session, email=email) + if author: + author = author.to_json(public=True) + else: + author = name + + topic = "git.%s.%s" % (subject, action) + msg = dict( + authors=[author], + agent=user, + repo=project.to_json(public=True) + if not isinstance(project, six.string_types) + else project, + ) + if subject == "branch": + msg["branch"] = refname + elif subject == "tag": + msg["tag"] = refname + + # Send blink notification to any 3rd party plugins, if there are any + pagure.lib.notify.blinker_publish(topic, msg) + + if not project.private: + send_fedmsg_notifications(project, topic, msg) + send_stomp_notifications(project, topic, msg) + send_mqtt_notifications(project, topic, msg) + send_webhook_notifications(project, topic, msg) + + def send_notifications(session, project, repodir, user, refname, revs, forced): """ Send out-going notifications about the commits that have just been pushed. @@ -271,13 +313,54 @@ class DefaultRunner(BaseRunner): forced = False if set(newrev) == set(["0"]): - print( - "Deleting a reference/branch, so we won't run the " - "pagure hook" - ) - return + if refname.startswith("refs/tags"): + refname = refname.replace("refs/tags/", "") + send_action_notification( + session, + "tag", + "deletion", + project, + repodir, + username, + refname, + oldrev, + ) + print("Deleting a tag, so we won't run the " "pagure hook") + elif refname.startswith("refs/heads/"): + refname = refname.replace("refs/heads/", "") + send_action_notification( + session, + "branch", + "deletion", + project, + repodir, + username, + refname, + oldrev, + ) + print( + "Deleting a branch, so we won't run the " "pagure hook" + ) + else: + print( + "Deleting %s, so we wont run the pagure hook nor " + "send notifications" + ) + continue elif set(oldrev) == set(["0"]): oldrev = "^%s" % oldrev + if refname.startswith("refs/tags"): + refname = refname.replace("refs/tags/", "") + send_action_notification( + session, + "tag", + "creation", + project, + repodir, + username, + refname, + newrev, + ) elif pagure.lib.git.is_forced_push(oldrev, newrev, repodir): forced = True base = pagure.lib.git.get_base_revision(