From 327b884a445d1f5c489a6b9fe8aff19ab6c7af22 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 05 2018 15:04:31 +0000 Subject: Add an option to notify on flags being added to a commit Fixes to #2947 Signed-off-by: Pierre-Yves Chibon --- diff --git a/pagure/lib/__init__.py b/pagure/lib/__init__.py index 636d5e0..29d1841 100644 --- a/pagure/lib/__init__.py +++ b/pagure/lib/__init__.py @@ -1427,6 +1427,9 @@ def add_commit_flag( # Make sure we won't have SQLAlchemy error before we continue session.flush() + if repo.settings.get('notify_on_commit_flag'): + pagure.lib.notify.notify_commit_flag(c_flag, username) + pagure.lib.notify.log( repo, topic='commit.flag.%s' % action, diff --git a/pagure/lib/model.py b/pagure/lib/model.py index f5c9430..57915dd 100644 --- a/pagure/lib/model.py +++ b/pagure/lib/model.py @@ -469,6 +469,13 @@ class Project(BASE): return 'project' @property + def mail_id(self): + ''' Return a unique representation of the project as string that + can be used when sending emails. + ''' + return '%s-project-%s' % (self.fullname, self.id) + + @property def path(self): ''' Return the name of the git repo on the filesystem. ''' return '%s.git' % self.fullname @@ -521,6 +528,7 @@ class Project(BASE): 'pull_request_access_only': False, 'roadmap_on_issues_page': False, 'notify_on_pull-request_flag': False, + 'notify_on_commit_flag': False, } if self._settings: @@ -2035,6 +2043,14 @@ class PullRequestFlag(BASE): foreign_keys=[pull_request_uid], remote_side=[PullRequest.uid]) + @property + def mail_id(self): + ''' Return a unique representation of the flag as string that + can be used when sending emails. + ''' + return '%s-pull-request-%s-%s' % ( + self.pull_request.project.name, self.pull_request.uid, self.id) + def to_json(self, public=False): ''' Returns a dictionary representation of the pull-request. @@ -2103,12 +2119,32 @@ class CommitFlag(BASE): __table_args__ = (sa.UniqueConstraint('commit_hash', 'uid'),) + project = relation( + 'Project', foreign_keys=[project_id], remote_side=[Project.id], + backref=backref( + 'commit_flags', cascade="delete, delete-orphan", + ), + single_parent=True) + user = relation('User', foreign_keys=[user_id], remote_side=[User.id], backref=backref( 'commit_flags', order_by="CommitFlag.date_created")) + @property + def isa(self): + ''' A string to allow finding out that this is a commit flag. ''' + return 'commit-flag' + + @property + def mail_id(self): + ''' Return a unique representation of the flag as string that + can be used when sending emails. + ''' + return '%s-commit-%s-%s' % ( + self.project.name, self.project.id, self.id) + def to_json(self, public=False): ''' Returns a dictionary representation of the commit flag. diff --git a/pagure/lib/notify.py b/pagure/lib/notify.py index 9030e09..42d8e3a 100644 --- a/pagure/lib/notify.py +++ b/pagure/lib/notify.py @@ -128,17 +128,19 @@ def _get_emails_for_obj(obj): emails.add(user.default_email) # Add people that commented on the issue/PR - for comment in obj.comments: - if comment.user.default_email: - emails.add(comment.user.default_email) + if obj.isa in ['issue', 'pull-request']: + for comment in obj.comments: + if comment.user.default_email: + emails.add(comment.user.default_email) # Add the person that opened the issue/PR if obj.user.default_email: emails.add(obj.user.default_email) # Add the person assigned to the issue/PR - if obj.assignee and obj.assignee.default_email: - emails.add(obj.assignee.default_email) + if obj.isa in ['issue', 'pull-request']: + if obj.assignee and obj.assignee.default_email: + emails.add(obj.assignee.default_email) # Add public notifications to lists/users set project-wide if obj.isa == 'issue' and not obj.private: @@ -161,11 +163,12 @@ def _get_emails_for_obj(obj): emails.remove(watcher.user.default_email) # Add/Remove people who explicitly asked to be added/removed - for watcher in obj.watchers: - if not watcher.watch and watcher.user.default_email in emails: - emails.remove(watcher.user.default_email) - elif watcher.watch: - emails.add(watcher.user.default_email) + if obj.isa in ['issue', 'pull-request']: + for watcher in obj.watchers: + if not watcher.watch and watcher.user.default_email in emails: + emails.remove(watcher.user.default_email) + elif watcher.watch: + emails.add(watcher.user.default_email) # Drop the email used by pagure when sending emails = _clean_emails( @@ -783,3 +786,34 @@ To view more about the commits, visit: ','.join(mail_to), project_name=project.fullname ) + + +def notify_commit_flag(flag, user): + ''' Notify the people following a project that a new flag was added + to one of its commit. + ''' + text = u""" +%s flagged the commit `%s` as %s: %s + +%s +""" % (flag.username, + flag.commit_hash, + flag.status, + flag.comment, + _build_url( + pagure_config['APP_URL'], + _fullname_to_url(flag.project.fullname), + 'c', + flag.commit_hash)) + mail_to = _get_emails_for_obj(flag) + + send_email( + text, + 'Coommit #%s - %s: %s' % ( + flag.commit_hash, flag.username, flag.status), + ','.join(mail_to), + mail_id=flag.mail_id, + in_reply_to=flag.project.mail_id, + project_name=flag.project.fullname, + user_from=flag.username, + )