From: Michael Tremer Date: Fri, 14 Oct 2022 12:44:57 +0000 (+0000) Subject: builds: Unify failure/finished methods X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9a83e9017876c1dd092ebabf0d76acff6a7f029e;p=pbs.git builds: Unify failure/finished methods Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index 183dd836..a036ed11 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 diff --git a/src/buildservice/builds.py b/src/buildservice/builds.py index 7b077630..37e9acf6 100644 --- a/src/buildservice/builds.py +++ b/src/buildservice/builds.py @@ -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 index 00000000..5280e97b --- /dev/null +++ b/src/templates/builds/messages/failed.txt @@ -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 index 00000000..b96fdba8 --- /dev/null +++ b/src/templates/builds/messages/finished.txt @@ -0,0 +1,3 @@ +Subject: [{{ build }}] {{ _("Build Finished") }} + +XXX TODO