]> git.ipfire.org Git - pbs.git/commitdiff
build comments: Create own class and move logic to send messages
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 19:37:35 +0000 (19:37 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 29 Dec 2017 19:37:35 +0000 (19:37 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/buildservice/builds.py
src/buildservice/users.py
src/templates/messages/builds/new-comment.markdown [new file with mode: 0644]

index 42b22ee34b857a0a70c75d9626457f56b8216197..77113574415f43dfa15f4f0b1d5a29e04fad7fcc 100644 (file)
@@ -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
index ae19952bbe0a01dfc5f9f8bb34cc8a4de94b2ef4..6f9974c17a0ce304258683540ba5ed071d359f71 100644 (file)
@@ -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)
index 3f8fe40e30c83b46388657de3f90452e1c1d8417..0c945d850076665bfbcf01e8103b5d0f70db6e21 100644 (file)
@@ -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 (file)
index 0000000..d3e95fc
--- /dev/null
@@ -0,0 +1,3 @@
+Subject: {{ _("%(user)s commented on %(build)s") % { "user" : user.realname, "build" : build }}
+
+{% for line in text.splitlines() %}  {{ line }}{% end %}