Repository: fedora-infra/bodhi

Issue:
500 error rendering specific update
From the production logs:

``` python
Traceback (most recent call last):
  File "/usr/lib/python2.7/site-packages/pyramid_mako/__init__.py", line 148, in __call__
    result = template.render_unicode(**system)
  File "/usr/lib/python2.7/site-packages/mako/template.py", line 452, in render_unicode
    as_unicode=True)
  File "/usr/lib/python2.7/site-packages/mako/runtime.py", line 783, in _render
    **_kwargs_for_callable(callable_, data))
  File "/usr/lib/python2.7/site-packages/mako/runtime.py", line 815, in _render_context
    _exec_template(inherit, lclcontext, args=args, kwargs=kwargs)
  File "/usr/lib/python2.7/site-packages/mako/runtime.py", line 841, in _exec_template
    callable_(context, *args, **kwargs)
  File "master_html", line 238, in render_body

  File "/usr/lib/python2.7/site-packages/bodhi/templates/update.html", line 613, in render_body
    ${self.fragments.comment(comment, display_update=False)}
  File "fragments_html", line 123, in render_comment

  File "/usr/lib/python2.7/site-packages/bodhi/util.py", line 474, in bug_link
    url = "https://bugzilla.redhat.com/show_bug.cgi?id=" + str(bug.bug_id)
AttributeError: 'NoneType' object has no attribute 'bug_id'


```



Discussion:
https://bodhi.fedoraproject.org/updates/FEDORA-2015-16259

```
>>> db.query(m.BugKarma).filter_by(bug_id=None).count()
10L
```

I'm guessing this happens when a bug is removed after feedback is given for it.


Resolving patch:
diff --git a/bodhi/models/models.py b/bodhi/models/models.py
--- a/bodhi/models/models.py
+++ b/bodhi/models/models.py
@@ -1308,8 +1308,10 @@ def update_bugs(self, bug_ids, session):
         for bug in to_remove:
             self.bugs.remove(bug)
             if len(bug.updates) == 0:
-                log.debug("Destroying stray Bugzilla #%d" % bug.bug_id)
-                session.delete(bug)
+                # Don't delete the Bug instance if there is any associated BugKarma
+                if not session.query(BugKarma).filter_by(bug_id=bug.bug_id).count():
+                    log.debug("Destroying stray Bugzilla #%d" % bug.bug_id)
+                    session.delete(bug)
         session.flush()
 
         new = []


Test patch:
diff --git a/bodhi/tests/models/test_models.py b/bodhi/tests/models/test_models.py
--- a/bodhi/tests/models/test_models.py
+++ b/bodhi/tests/models/test_models.py
@@ -27,7 +27,8 @@
 
 from bodhi import models as model, buildsys, mail
 from bodhi.models import (UpdateStatus, UpdateType, UpdateRequest,
-                          UpdateSeverity, UpdateSuggestion, ReleaseState)
+                          UpdateSeverity, UpdateSuggestion, ReleaseState,
+                          BugKarma)
 from bodhi.tests.models import ModelTest
 from bodhi.config import config
 from bodhi.exceptions import BodhiException
@@ -381,6 +382,17 @@ def test_update_bugs(self):
         eq_(self.db.query(model.Bug)
                 .filter_by(bug_id=1234).first(), None)
 
+        # Try removing a bug when it already has BugKarma
+        karma = BugKarma(bug_id=4321, karma=1)
+        self.db.add(karma)
+        self.db.flush()
+        bugs = ['5678']
+        update.update_bugs(bugs, session)
+        assert len(update.bugs) == 1
+        assert update.bugs[0].bug_id == 5678
+        eq_(self.db.query(model.Bug)
+                .filter_by(bug_id=4321).count(), 1)
+
     def test_unicode_bug_title(self):
         bug = self.obj.bugs[0]
         bug.title = u'foo\xe9bar'