From: Michael Tremer Date: Fri, 29 Dec 2017 19:37:35 +0000 (+0000) Subject: build comments: Create own class and move logic to send messages X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d0bce25d917514874320b3867f9bec805d2a4488;p=pbs.git build comments: Create own class and move logic to send messages Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 42b22ee3..77113574 100644 --- a/Makefile.am +++ b/Makefile.am @@ -247,6 +247,11 @@ templates_errorsdir = $(templatesdir)/errors templates_messagesdir = $(templatesdir)/messages +dist_templates_messages_builds_DATA = \ + src/templates/messages/builds/new-comment.markdown + +templates_messages_buildsdir = $(templates_messagesdir)/builds + dist_templates_messages_jobs_DATA = \ src/templates/messages/jobs/failed.markdown \ src/templates/messages/jobs/finished.markdown diff --git a/src/buildservice/builds.py b/src/buildservice/builds.py index ae19952b..6f9974c1 100644 --- a/src/buildservice/builds.py +++ b/src/buildservice/builds.py @@ -824,20 +824,20 @@ class Build(base.DataObject): return comments def add_comment(self, user, text, score): - # Add the new comment to the database. - id = self.db.execute("INSERT INTO \ - builds_comments(build_id, user_id, text, score, time_created) \ - VALUES(%s, %s, %s, %s, NOW())", + res = self.db.get("INSERT INTO builds_comments(build_id, user_id, \ + text, score) VALUES(%s, %s, %s, %s) RETURNING *", self.id, user.id, text, score) + comment = BuildComment(self.backend, res.id, data=res) + # Update the score cache - self.score += score + self.score += comment.score - # Send the new comment to all watchers and stuff. - self.send_comment_message(id) + # Send the new comment to all watchers and stuff + comment.send_message() - # Return the ID of the newly created comment. - return id + # Return the newly created comment + return comment @lazy_property def score(self): @@ -862,27 +862,6 @@ class Build(base.DataObject): return [users.User(self.backend, u.id) for u in users] - def send_comment_message(self, comment_id): - comment = self.db.get("SELECT * FROM builds_comments WHERE id = %s", - comment_id) - - assert comment - assert comment.build_id == self.id - - # Get user who wrote the comment. - user = self.backend.users.get_by_id(comment.user_id) - - format = { - "build_name" : self.name, - "user_name" : user.realname, - } - - # XXX create beautiful message - - self.backend.messages.send_to_all(self.message_recipients, - N_("%(user_name)s commented on %(build_name)s"), - comment.text, format) - ## Logging stuff def get_log(self, comments=True, repo=True, limit=None): @@ -1086,3 +1065,27 @@ class Build(base.DataObject): # Update all bugs linked to this build. for bug_id in self.get_bug_ids(): self._update_bug(bug_id, status=status, resolution=resolution, comment=comment) + + +class BuildComment(base.DataObject): + table = "builds_comments" + + @lazy_property + def build(self): + return self.backend.builds.get_by_id(self.data.build_id) + + @lazy_property + def user(self): + return self.backend.users.get_by_id(self.data.user_id) + + @property + def text(self): + return self.data.text + + @property + def score(self): + return self.data.score + + def send_message(self): + self.backend.messages.send_template_to_many(self.build.message_recipients, "builds/new-comment", + sender=self.user.envelope_from, build=self.build, user=self.user, text=self.text) diff --git a/src/buildservice/users.py b/src/buildservice/users.py index 3f8fe40e..0c945d85 100644 --- a/src/buildservice/users.py +++ b/src/buildservice/users.py @@ -340,6 +340,10 @@ class User(base.DataObject): return firstname + @property + def envelope_from(self): + return "%s <%s>" % (self.realname, self.email) + @lazy_property def emails(self): res = self.backend.users._get_user_emails("SELECT * FROM users_emails \ diff --git a/src/templates/messages/builds/new-comment.markdown b/src/templates/messages/builds/new-comment.markdown new file mode 100644 index 00000000..d3e95fc1 --- /dev/null +++ b/src/templates/messages/builds/new-comment.markdown @@ -0,0 +1,3 @@ +Subject: {{ _("%(user)s commented on %(build)s") % { "user" : user.realname, "build" : build }} + +{% for line in text.splitlines() %} {{ line }}{% end %}