From 8eaa9adf96edec4c344f4948de356d2c945f94d7 Mon Sep 17 00:00:00 2001 From: anar Date: Apr 15 2018 14:36:11 +0000 Subject: Add an option to include diff in pull request notification --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 3f4a35e..f81c763 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -1778,7 +1778,9 @@ def new_pull_request(session, branch_from, log_action(session, 'created', request, user_obj) if notify: - pagure.lib.notify.notify_new_pull_request(request) + with_diff = repo_to.settings + .get('notify_on_pull-request_with_diff_flag') + pagure.lib.notify.notify_new_pull_request(request, with_diff) pagure.lib.notify.log( request.project, diff --git a/pagure/lib/model.py b/pagure/lib/model.py index b9c1e35..0a8c688 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -530,6 +530,7 @@ class Project(BASE): 'pull_request_access_only': False, 'roadmap_on_issues_page': False, 'notify_on_pull-request_flag': False, + 'notify_on_pull-request_with_diff_flag': False, 'notify_on_commit_flag': False, } diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index e9ad0b0..2cbdfd7 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -26,6 +26,7 @@ from email.header import Header from email.mime.text import MIMEText import flask +import pygit2 import pagure.lib import pagure.lib.tasks_services from pagure.config import config as pagure_config @@ -34,6 +35,7 @@ from pagure.config import config as pagure_config _log = logging.getLogger(__name__) +MAX_EMAIL_LENGTH = 500000 REPLY_MSG = 'To reply, visit the link below' if pagure_config['EVENTSOURCE_SOURCE']: REPLY_MSG += ' or just reply to this email' @@ -582,10 +584,32 @@ The pull-request: `%s` of project: `%s` has been %s by %s. ) -def notify_new_pull_request(request): +def notify_new_pull_request(request, with_diff=False): ''' Notify the people following a project that a new pull-request was added to it. ''' + + diff = "" + if with_diff: + if request.remote: + repopath = pagure.utils.get_remote_repo_path( + request.remote_git, request.branch_from) + parentpath = pagure.utils.get_repo_path(request.project) + else: + repo_from = request.project_from + parentpath = pagure.utils.get_repo_path(request.project) + repopath = parentpath + if repo_from: + repopath = pagure.utils.get_repo_path(repo_from) + + repo_obj = pygit2.Repository(repopath) + orig_repo = pygit2.Repository(parentpath) + + diff_commits, diff = pagure.lib.git.diff_pull_request( + flask.g.session, request, repo_obj, orig_repo, + requestfolder=pagure_config['REQUESTS_FOLDER']) + diff = diff.patch[:MAX_EMAIL_LENGTH] + text = u""" %s opened a new pull-request against the project: `%s` that you are following: `` @@ -594,6 +618,10 @@ def notify_new_pull_request(request): %s %s + +~~~~~~~~~ + +%s """ % (request.user.user, request.project.name, request.title, @@ -602,7 +630,9 @@ def notify_new_pull_request(request): pagure_config['APP_URL'], _fullname_to_url(request.project.fullname), 'pull-request', - request.id)) + request.id), + diff) + mail_to = _get_emails_for_obj(request) send_email(