Repository: fedora-infra/bodhi

Issue:
RFE: Autoclose Rawhide bugs
I have just build rubygem-its. Because I have referenced bugzilla ticket in the changelog as well as in git log \[[1]\], I would expect Bodhi to associate this ticket with the Bodhi update \[[2]\] and handle the bug properly, i.e. close it as soon as the update lands in Rawhide. Unfortunately, this is probably not the case ATM.



[1]: https://src.fedoraproject.org/rpms/rubygem-rspec-its/c/3714b55268393f5398de3a56db57e7b19a32a2c7?branch=master
[2]: https://bodhi.fedoraproject.org/updates/FEDORA-2020-60bb269473


Discussion:
This can be implemented in the code that SHOULD automatically output the changelog in the release notes, added since v5.0: https://github.com/fedora-infra/bodhi/commit/54210f7e1932b763103ad0a38c3bb818862bc081

But it seems this is not working at all (I've never seen changelog entries in any update so far). @ryanlerch do you have any idea why we don't get any output from `generate_changelog()`?
It seems koji's `getLatestBuilds()` only return the latest build, not a list of latest builds, so `Build.get_latest()` always returns nothing.

I've opened a PR to Koji to correct this behavior: https://pagure.io/koji/pull-request/2015

Resolving patch:
diff --git a/bodhi/server/config.py b/bodhi/server/config.py
--- a/bodhi/server/config.py
+++ b/bodhi/server/config.py
@@ -278,6 +278,10 @@ class BodhiConfig(dict):
         'bz_products': {
             'value': ['Fedora', 'Fedora EPEL', 'Fedora Modules'],
             'validator': _generate_list_validator(',')},
+        'bz_regex': {
+            'value': (r'(?:fix(?:es)?|close(?:s)?)\s'
+                      r'(?:fedora|epel|rh(?:bz)?)#(\d{5,})'),
+            'validator': str},
         'bz_server': {
             'value': 'https://bugzilla.redhat.com/xmlrpc.cgi',
             'validator': str},
diff --git a/bodhi/server/consumers/automatic_updates.py b/bodhi/server/consumers/automatic_updates.py
--- a/bodhi/server/consumers/automatic_updates.py
+++ b/bodhi/server/consumers/automatic_updates.py
@@ -23,13 +23,14 @@
 """
 
 import logging
+import re
 
 import fedora_messaging
 
 from bodhi.server import buildsys
 from bodhi.server.config import config
-from bodhi.server.models import Build, ContentType, Package, Release
-from bodhi.server.models import Update, UpdateStatus, UpdateType, User
+from bodhi.server.models import (
+    Bug, Build, ContentType, Package, Release, Update, UpdateStatus, UpdateType, User)
 from bodhi.server.util import transactional_session_maker
 
 log = logging.getLogger('bodhi')
@@ -140,7 +141,9 @@ def __call__(self, message: fedora_messaging.api.Message) -> None:
 
             log.debug(f"Creating new update for {bnvr}.")
             changelog = build.get_changelog(lastupdate=True)
+            closing_bugs = []
             if changelog:
+                log.debug("Adding changelog to update notes.")
                 notes = f"""Automatic update for {bnvr}.
 
 ##### **Changelog**
@@ -148,11 +151,23 @@ def __call__(self, message: fedora_messaging.api.Message) -> None:
 ```
 {changelog}
 ```"""
+
+                for b in re.finditer(config.get('bz_regex'), changelog, re.IGNORECASE):
+                    idx = int(b.group(1))
+                    log.debug(f'Adding bug #{idx} to the update.')
+                    bug = Bug.get(idx)
+                    if bug is None:
+                        bug = Bug(bug_id=idx)
+                        dbsession.add(bug)
+                        dbsession.flush()
+                    if bug not in closing_bugs:
+                        closing_bugs.append(bug)
             else:
                 notes = f"Automatic update for {bnvr}."
             update = Update(
                 release=rel,
                 builds=[build],
+                bugs=closing_bugs,
                 notes=notes,
                 type=UpdateType.unspecified,
                 stable_karma=3,


Test patch:
diff --git a/bodhi/tests/server/consumers/test_automatic_updates.py b/bodhi/tests/server/consumers/test_automatic_updates.py
--- a/bodhi/tests/server/consumers/test_automatic_updates.py
+++ b/bodhi/tests/server/consumers/test_automatic_updates.py
@@ -123,6 +123,32 @@ def test_changelog(self, mock_generate_changelog, changelog):
         else:  # no changelog
             assert update.notes == "Automatic update for colord-1.3.4-1.fc26."
 
+    @mock.patch('bodhi.server.models.RpmBuild.get_changelog')
+    def test_bug_added(self, mock_generate_changelog):
+        """Assert that a bug is added to the update if proper string is in changelog."""
+        changelog = ('* Sat Aug  3 2013 Fedora Releng <rel-eng@lists.fedoraproject.org> - 2\n'
+                     '- Added a free money feature.\n- Fix rhbz#112233.')
+
+        mock_generate_changelog.return_value = changelog
+
+        # process the message
+        self.handler(self.sample_message)
+
+        # check if the update exists...
+        update = self.db.query(Update).filter(
+            Update.builds.any(Build.nvr == self.sample_nvr)
+        ).first()
+
+        assert update.notes == f"""Automatic update for colord-1.3.4-1.fc26.
+
+##### **Changelog**
+
+```
+{changelog}
+```"""
+        assert len(update.bugs) > 0
+        assert update.bugs[0].bug_id == 112233
+
     def test_consume_with_orphan_build(self, caplog):
         """
         Assert existing builds without an update can be handled.