]> git.ipfire.org Git - pbs.git/commitdiff
builds: Unify failure/finished methods
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Oct 2022 12:44:57 +0000 (12:44 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 14 Oct 2022 12:44:57 +0000 (12:44 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/buildservice/builds.py
src/templates/builds/messages/failed.txt [new file with mode: 0644]
src/templates/builds/messages/finished.txt [new file with mode: 0644]

index 183dd8365a8f994bdb89f97dfa923b23722735e1..a036ed11b606547ec8df238dc8f39e3d8b9454cf 100644 (file)
@@ -206,7 +206,9 @@ dist_templates_builds_DATA = \
 templates_buildsdir = $(templatesdir)/builds
 
 dist_templates_builds_messages_DATA = \
-       src/templates/builds/messages/comment.txt
+       src/templates/builds/messages/comment.txt \
+       src/templates/builds/messages/failed.txt \
+       src/templates/builds/messages/finished.txt
 
 templates_builds_messagesdir = $(templates_buildsdir)/messages
 
index 7b077630882a4ee4323ae3f39201110a695b1a1a..37e9acf6b6b2eccecf0cc00fb25e9725680ecd7b 100644 (file)
@@ -477,11 +477,11 @@ class Build(base.DataObject):
 
                # If all jobs have finished, the build has finished
                elif all((j.has_finished() for j in self.jobs)):
-                       return await self.finished()
+                       return await self.finished(success=True)
 
                # If there are any failed jobs, the build has failed
                elif any((j.has_failed() for j in self.jobs)):
-                       return await self.failed()
+                       return await self.finished(success=False)
 
        ## Comment stuff
 
@@ -601,11 +601,23 @@ class Build(base.DataObject):
 
        # Actions
 
-       async def finished(self):
+       async def finished(self, success):
                """
                        Called when this build has successfully finished
                """
-               pass # XXX TODO
+               # Mark as finished
+               self._set_attribute_now("finished_at")
+
+               # Mark as failed if the build was not successful
+               if not success:
+                       self._set_attribute("failed", True)
+
+               # Notify everyone this build has finished...
+               if success:
+                       self._send_email("builds/messages/finished.txt")
+               # ... or that it has failed
+               else:
+                       self._send_email("builds/messages/failed.txt")
 
        def has_finished(self):
                """
@@ -616,11 +628,17 @@ class Build(base.DataObject):
 
                return False
 
-       async def failed(self):
+       def _send_email(self, *args, exclude=None, **kwargs):
                """
-                       Called when the build has failed
+                       Convenience function which sends an email to everybody who would care
                """
-               pass # XXX TODO
+               for user in self.watchers:
+                       # Skip some people
+                       if exclude and user in exclude:
+                               continue
+
+                       # Send an email to the user
+                       user.send_email(*args, build=self, **kwargs)
 
        @lazy_property
        def repo(self):
@@ -747,13 +765,7 @@ class Comment(base.DataObject):
 
        def notify(self):
                """
-                       Notifies all watchers about this comment
+                       Notifies all watchers about this comment (except the user who posted it)
                """
-               for user in self.build.watchers:
-                       # Skip the user who posted it
-                       if user == self.user:
-                               continue
-
-                       # Send an email
-                       user.send_email("builds/messages/comment.txt",
-                               build=self.build, comment=self)
+               self.build._send_email("builds/messages/comment.txt",
+                       exclude=[self.user], build=self.build, comment=self)
diff --git a/src/templates/builds/messages/failed.txt b/src/templates/builds/messages/failed.txt
new file mode 100644 (file)
index 0000000..5280e97
--- /dev/null
@@ -0,0 +1,3 @@
+Subject: [{{ build }}] {{ _("Build Failed") }}
+
+XXX TODO
diff --git a/src/templates/builds/messages/finished.txt b/src/templates/builds/messages/finished.txt
new file mode 100644 (file)
index 0000000..b96fdba
--- /dev/null
@@ -0,0 +1,3 @@
+Subject: [{{ build }}] {{ _("Build Finished") }}
+
+XXX TODO