Repository: fedora-infra/bodhi

Issue:
Crash in automatic update handler when retrieving changelog
From bodhi-consumer logs:
```
2020-10-25 12:20:31,033 INFO  [fedora_messaging.twisted.protocol][MainThread] Consuming message from topic org.fedoraproject.prod.buildsys.tag (message id 07963dc6-da34-40bc-8210-12dd66606f5f)
2020-10-25 12:20:31,039 INFO  [bodhi][PoolThread-twisted.internet.reactor-1] Received message from fedora-messaging with topic: org.fedoraproject.prod.buildsys.tag
2020-10-25 12:20:31,040 INFO  [bodhi][PoolThread-twisted.internet.reactor-1] dummy-test-package-gloster-0-1884.fc34 tagged into f34-updates-candidate
2020-10-25 12:20:31,045 INFO  [bodhi][PoolThread-twisted.internet.reactor-1] Build was not submitted, skipping
2020-10-25 12:20:31,132 ERROR [bodhi][PoolThread-twisted.internet.reactor-1] No rpm headers found in koji for 'dummy-test-package-gloster-0-1884.fc34': Unable to handle message in Automatic Update handler: Id: 07963dc6-da34-40bc-8210-12dd66606f5f
Topic: org.fedoraproject.prod.buildsys.tag
Headers: {
    "fedora_messaging_schema": "base.message",
    "fedora_messaging_severity": 20,
    "sent-at": "2020-10-25T12:20:30+00:00"
}
Body: {
    "build_id": 1634123,
    "instance": "primary",
    "name": "dummy-test-package-gloster",
    "owner": "packagerbot/os-master02.iad2.fedoraproject.org",
    "release": "1884.fc34",
    "tag": "f34-updates-candidate",
    "tag_id": 27040,
    "user": "packagerbot/os-master02.iad2.fedoraproject.org",
    "version": "0"
}
Traceback (most recent call last):
  File "/usr/local/lib/python3.8/site-packages/bodhi/server/consumers/__init__.py", line 79, in __call__
    handler_info.handler(msg)
  File "/usr/local/lib/python3.8/site-packages/bodhi/server/consumers/automatic_updates.py", line 144, in __call__
    changelog = build.get_changelog(lastupdate=True)
  File "/usr/local/lib/python3.8/site-packages/bodhi/server/models.py", line 1701, in get_changelog
    rpm_header = get_rpm_header(self.nvr)
  File "/usr/local/lib/python3.8/site-packages/bodhi/server/util.py", line 103, in get_rpm_header
    raise ValueError("No rpm headers found in koji for %r" % nvr)
ValueError: No rpm headers found in koji for 'dummy-test-package-gloster-0-1884.fc34'
2020-10-25 12:20:31,153 WARNI [fedora_messaging.twisted.protocol][MainThread] Returning message id 07963dc6-da34-40bc-8210-12dd66606f5f to the queue
```


Discussion:

Resolving patch:
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
@@ -141,7 +141,15 @@ def __call__(self, message: fedora_messaging.api.Message) -> None:
                 dbsession.add(user)
 
             log.debug(f"Creating new update for {bnvr}.")
-            changelog = build.get_changelog(lastupdate=True)
+            try:
+                changelog = build.get_changelog(lastupdate=True)
+            except ValueError:
+                # Often due to bot-generated builds
+                # https://github.com/fedora-infra/bodhi/issues/4146
+                changelog = None
+            except Exception:
+                # Re-raise exception, so that the message can be re-queued
+                raise
             closing_bugs = []
             if changelog:
                 log.debug("Adding changelog to update notes.")


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
@@ -150,6 +150,29 @@ def test_bug_added(self, mock_generate_changelog):
         assert len(update.bugs) > 0
         assert update.bugs[0].bug_id == 112233
 
+    @mock.patch('bodhi.server.models.RpmBuild.get_changelog')
+    def test_changelog_handled_exception(self, mock_generate_changelog):
+        """Assert that update creation is succesful if get_changelog() raises ValueError."""
+        mock_generate_changelog.side_effect = ValueError('Handled exception')
+
+        # 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 == "Automatic update for colord-1.3.4-1.fc26."
+
+    @mock.patch('bodhi.server.models.RpmBuild.get_changelog')
+    def test_changelog_unhandled_exception(self, mock_generate_changelog):
+        """Assert that update creation is not succesful if get_changelog() raises Exception."""
+        mock_generate_changelog.side_effect = Exception('Unhandled exception')
+        with pytest.raises(Exception) as exc:
+            self.handler(self.sample_message)
+        assert str(exc.value) == 'Unhandled exception'
+
     def test_consume_with_orphan_build(self, caplog):
         """
         Assert existing builds without an update can be handled.