| |
@@ -6,6 +6,7 @@
|
| |
from __future__ import print_function
|
| |
|
| |
import os
|
| |
+ import logging
|
| |
import sys
|
| |
|
| |
import pygit2
|
| |
@@ -15,6 +16,7 @@
|
| |
import pagure.exceptions # noqa: E402
|
| |
import pagure.lib.link # noqa: E402
|
| |
import pagure.lib.tasks # noqa: E402
|
| |
+ import pagure.lib.tasks_services # noqa: E402
|
| |
|
| |
|
| |
if 'PAGURE_CONFIG' not in os.environ \
|
| |
@@ -23,9 +25,123 @@
|
| |
|
| |
|
| |
_config = pagure.config.config
|
| |
+ _log = logging.getLogger(__name__)
|
| |
abspath = os.path.abspath(os.environ['GIT_DIR'])
|
| |
|
| |
|
| |
+ def send_fedmsg_notifications(project, topic, msg):
|
| |
+ ''' If the user asked for fedmsg notifications on commit, this will
|
| |
+ do it.
|
| |
+ '''
|
| |
+ import fedmsg
|
| |
+ config = fedmsg.config.load_config([], None)
|
| |
+ config['active'] = True
|
| |
+ config['endpoints']['relay_inbound'] = config['relay_inbound']
|
| |
+ fedmsg.init(name='relay_inbound', **config)
|
| |
+
|
| |
+ pagure.lib.notify.log(
|
| |
+ project=project,
|
| |
+ topic=topic,
|
| |
+ msg=msg,
|
| |
+ redis=None, # web-hook notification are handled separately
|
| |
+ )
|
| |
+
|
| |
+
|
| |
+ def send_webhook_notifications(project, topic, msg):
|
| |
+ ''' If the user asked for webhook notifications on commit, this will
|
| |
+ do it.
|
| |
+ '''
|
| |
+
|
| |
+ pagure.lib.tasks_services.webhook_notification.delay(
|
| |
+ topic=topic,
|
| |
+ msg=msg,
|
| |
+ namespace=project.namespace,
|
| |
+ name=project.name,
|
| |
+ user=project.user.username if project.is_fork else None,
|
| |
+ )
|
| |
+
|
| |
+
|
| |
+ def send_notifications(session, project, revs):
|
| |
+ ''' Send out-going notifications about the commits that have just been
|
| |
+ pushed.
|
| |
+ '''
|
| |
+
|
| |
+ auths = set()
|
| |
+ for rev in revs:
|
| |
+ email = pagure.lib.git.get_author_email(rev, abspath)
|
| |
+ name = pagure.lib.git.get_author(rev, abspath)
|
| |
+ author = pagure.lib.search_user(session, email=email) or name
|
| |
+ auths.add(author)
|
| |
+
|
| |
+ authors = []
|
| |
+ for author in auths:
|
| |
+ if isinstance(author, basestring):
|
| |
+ author = author
|
| |
+ else:
|
| |
+ author = author.to_json(public=True)
|
| |
+ authors.append(author)
|
| |
+
|
| |
+ if revs:
|
| |
+ revs.reverse()
|
| |
+ print("* Publishing information for %i commits" % len(revs))
|
| |
+
|
| |
+ fedmsg_hook = pagure.lib.plugins.get_plugin('Fedmsg')
|
| |
+ fedmsg_hook.db_object()
|
| |
+
|
| |
+ if project.fedmsg_hook.active:
|
| |
+ try:
|
| |
+ print(" - to fedmsg")
|
| |
+ send_fedmsg_notifications(project, topic, msg)
|
| |
+ except Exception as err:
|
| |
+ _log.exception(
|
| |
+ 'Error sending fedmsg notifications on commit push')
|
| |
+ if project.settings.get('Web-hooks') and not project.private:
|
| |
+ try:
|
| |
+ print(" - to web-hooks")
|
| |
+ send_webhook_notifications(project, topic, msg)
|
| |
+ except Exception as err:
|
| |
+ _log.exception(
|
| |
+ 'Error sending web-hook notifications on commit push')
|
| |
+
|
| |
+
|
| |
+ def inform_pull_request_urls(project, commits, refname, default_branch):
|
| |
+ ''' Inform the user about the URLs to open a new pull-request or visit
|
| |
+ the existing one.
|
| |
+ '''
|
| |
+ target_repo = project
|
| |
+ if project.is_fork:
|
| |
+ target_repo = project.parent
|
| |
+
|
| |
+ if commits and refname != default_branch\
|
| |
+ and target_repo.settings.get('pull_requests', True):
|
| |
+ print()
|
| |
+ prs = pagure.lib.search_pull_requests(
|
| |
+ session,
|
| |
+ project_id_from=project.id,
|
| |
+ status='Open',
|
| |
+ branch_from=refname,
|
| |
+ )
|
| |
+ # Link to existing PRs if there are any
|
| |
+ seen = len(prs) != 0
|
| |
+ for pr in prs:
|
| |
+ print('View pull-request for %s' % refname)
|
| |
+ print(' %s/%s/pull-request/%s' % (
|
| |
+ _config['APP_URL'].rstrip('/'),
|
| |
+ pr.project.url_path,
|
| |
+ pr.id)
|
| |
+ )
|
| |
+ # If no existing PRs, provide the link to open one
|
| |
+ if not seen:
|
| |
+ print('Create a pull-request for %s' % refname)
|
| |
+ print(' %s/%s/diff/%s..%s' % (
|
| |
+ _config['APP_URL'].rstrip('/'),
|
| |
+ project.url_path,
|
| |
+ default_branch,
|
| |
+ refname)
|
| |
+ )
|
| |
+ print()
|
| |
+
|
| |
+
|
| |
def run_as_post_receive_hook():
|
| |
|
| |
repo = pagure.lib.git.get_repo_name(abspath)
|
| |
@@ -65,6 +181,13 @@
|
| |
print("Deleting a reference/branch, so we won't run the "
|
| |
"pagure hook")
|
| |
return
|
| |
+ elif set(oldrev) == set(['0']):
|
| |
+ oldrev = '^%s' % oldrev
|
| |
+ elif pagure.lib.git.is_forced_push(oldrev, newrev, abspath):
|
| |
+ forced = True
|
| |
+ base = pagure.lib.git.get_base_revision(oldrev, newrev, abspath)
|
| |
+ if base:
|
| |
+ oldrev = base[0]
|
| |
|
| |
refname = refname.replace('refs/heads/', '')
|
| |
commits = pagure.lib.git.get_revs_between(
|
| |
@@ -76,6 +199,10 @@
|
| |
else:
|
| |
print('Sending to redis to send commit notification emails')
|
| |
|
| |
+ # This is logging the commit to the log table in the DB so we can
|
| |
+ # render commits in the calendar heatmap.
|
| |
+ # It is also sending emails about commits to people using the
|
| |
+ # 'watch' feature to be made aware of new commits.
|
| |
pagure.lib.tasks_services.log_commit_send_notifications.delay(
|
| |
name=repo,
|
| |
commits=commits,
|
| |
@@ -86,38 +213,13 @@
|
| |
username=username,
|
| |
)
|
| |
|
| |
- target_repo = project
|
| |
- if project.is_fork:
|
| |
- target_repo = project.parent
|
| |
-
|
| |
- if commits and refname != default_branch\
|
| |
- and target_repo.settings.get('pull_requests', True):
|
| |
- print()
|
| |
- prs = pagure.lib.search_pull_requests(
|
| |
- session,
|
| |
- project_id_from=project.id,
|
| |
- status='Open',
|
| |
- branch_from=refname,
|
| |
- )
|
| |
- # Link to existing PRs if there are any
|
| |
- seen = len(prs) != 0
|
| |
- for pr in prs:
|
| |
- print('View pull-request for %s' % refname)
|
| |
- print(' %s/%s/pull-request/%s' % (
|
| |
- _config['APP_URL'].rstrip('/'),
|
| |
- pr.project.url_path,
|
| |
- pr.id)
|
| |
- )
|
| |
- # If no existing PRs, provide the link to open one
|
| |
- if not seen:
|
| |
- print('Create a pull-request for %s' % refname)
|
| |
- print(' %s/%s/diff/%s..%s' % (
|
| |
- _config['APP_URL'].rstrip('/'),
|
| |
- project.url_path,
|
| |
- default_branch,
|
| |
- refname)
|
| |
- )
|
| |
- print()
|
| |
+ # This one is sending fedmsg and web-hook notifications for project
|
| |
+ # that set them up
|
| |
+ send_notifications(session, project, commits)
|
| |
+
|
| |
+ # Now display to the user if this isn't the default branch links to
|
| |
+ # open a new pr or review the existing one
|
| |
+ inform_pull_request_urls(project, commits, refname, default_branch)
|
| |
|
| |
# Schedule refresh of all opened PRs
|
| |
parent = project.parent or project
|
| |
This way we can finally split the web-hooks notifications from the fedmsg
ones while not duplicating the entire logic around how to build the
payload sent.
Fixes https://pagure.io/pagure/issue/2413
Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr