]> git.ipfire.org Git - pbs.git/commitdiff
monitorings: Update bug report after the build has finished
authorMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 May 2023 14:14:12 +0000 (14:14 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 29 May 2023 14:14:12 +0000 (14:14 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/bugtracker.py
src/buildservice/builds.py
src/buildservice/releasemonitoring.py

index ea6c5ba0b03e10b87a0c90d44addbd0a6d323483..c64e7fa9b64003dbd2bef24aa69b4acee02b393d 100644 (file)
@@ -330,21 +330,17 @@ class Bug(base.Object):
 
        async def _update(self, data):
                """
-                       Updates this bug
+                       Low-level updater
                """
                response = await self.bugzilla._request("PUT", "/rest/bug/%s" % self.id, data=data)
 
                # XXX Update data from response
 
-       # Close
-
-       async def close(self, resolution, comment=None):
-               data = {
-                       "status"     : "CLOSED",
-                       "resolution" : resolution,
-               }
-
-               # Add the comment
+       async def update(self, comment=None, **data):
+               """
+                       Updates the bug
+               """
+               # Comment
                if comment:
                        data |= {
                                "comment" : {
@@ -352,10 +348,21 @@ class Bug(base.Object):
                                }
                        }
 
+               return await self._update(data)
+
+       # Close
+
+       async def close(self, resolution, comment=None):
+               args = {
+                       "status"     : "CLOSED",
+                       "resolution" : resolution,
+                       "comment"    : comment,
+               }
+
                log.debug("Closing bug #%s" % self.id)
 
                # Send the request
-               await self._update(data)
+               await self.update(**args)
 
        # Attachments
 
index c079444553eb057b98006eed9d55b11f50a365a7..0cf4823c7f56a6568fa509356fdc95ca7b3b18a9 100644 (file)
@@ -718,6 +718,10 @@ class Build(base.DataObject):
                if self.group:
                        await self.group._build_finished(self)
 
+               # Notify release monitoring that this build has finished
+               if self.monitoring_release:
+                       await self.monitoring_release._build_finished()
+
                return builds
 
        def has_finished(self):
@@ -856,6 +860,26 @@ class Build(base.DataObject):
                """
                return await self.backend.bugzilla.get_bugs(self.bug_ids)
 
+       # Monitoring Release
+
+       @lazy_property
+       def monitoring_release(self):
+               """
+                       Returns the Monitoring Release
+               """
+               return self.backend.monitorings._get_release("""
+                       SELECT
+                               *
+                       FROM
+                               release_monitoring_releases
+                       WHERE
+                               build_id = %s
+                       """, self.id,
+
+                       # Populate cache
+                       build=self,
+               )
+
        # Deprecation
 
        def _deprecate_others(self):
index 22daa5c85546565ecf1edb3417ee85e930a8b754..a85d2710715a0dab97b8272dadc94b9f041df902 100644 (file)
@@ -50,8 +50,14 @@ A build has been created to test this release:
   %(url)s
 """
 
-BUG_DESCRIPTION_WITH_BUILD = BUG_DESCRIPTION + """\n
-XXX PUT STUFF ABOUT THE BUILD HERE
+# This comment is being added to the bug report when the build has been successful
+BUG_BUILD_SUCCESSFUL = "The build has been successful."
+
+# This command is being added to the bug report when the build has failed.
+BUG_BUILD_FAILED = """\
+The build has failed.
+
+Log files of the failed jobs have been attached for you to investigate why.
 """
 
 class ReleaseExistsError(Exception):
@@ -218,6 +224,14 @@ class Monitorings(base.Object):
                        for monitoring in monitorings:
                                tg.create_task(monitoring.check())
 
+       # Releases
+
+       def _get_releases(self, query, *args, **kwargs):
+               return self.db.fetch_many(Release, query, *args, **kwargs)
+
+       def _get_release(self, query, *args, **kwargs):
+               return self.db.fetch_one(Release, query, *args, **kwargs)
+
 
 class Monitoring(base.DataObject):
        table = "release_monitorings"
@@ -341,11 +355,13 @@ class Monitoring(base.DataObject):
 
        # Releases
 
-       def _get_releases(self, query, *args):
-               return self.db.fetch_many(Release, query, *args, monitoring=self)
+       def _get_releases(self, query, *args, **kwargs):
+               return self.backend.monitorings._get_releases(query, *args,
+                       monitoring=self, **kwargs)
 
-       def _get_release(self, query, *args):
-               return self.db.fetch_one(Release, query, *args, monitoring=self)
+       def _get_release(self, query, *args, **kwargs):
+               return self.backend.monitorings._get_release(query, *args,
+                       monitoring=self, **kwargs)
 
        @property
        def latest_release(self):
@@ -747,3 +763,35 @@ class Release(base.DataObject):
                        result.append(line)
 
                return result
+
+       async def _build_finished(self):
+               """
+                       Called when the build has finished
+               """
+               # Fetch the bug report
+               bug = await self.get_bug()
+
+               # If the build has been successful, ...
+               if self.build.is_successful():
+                       await bug.update(comment=BUG_BUILD_SUCCESSFUL)
+
+               # If the build failed, ...
+               elif self.build.has_failed():
+                       # Say that the build has failed
+                       await bug.update(comment=BUG_BUILD_FAILED)
+
+                       # Append any logfiles from failed jobs
+                       for job in self.build.jobs:
+                               if not job.has_failed():
+                                       continue
+
+                               # Open the logfile
+                               try:
+                                       log = await job.open_log()
+                               except FileNotFoundError as e:
+                                       log.warning("Could not open log file for %s" % job)
+                                       continue
+
+                               # Attach it to the bug
+                               await bug.attach(summary="Log file for %s" % job, filename="%s.log" % job,
+                                       data=log, content_type="text/plain")