Skip to content

Commit

Permalink
Add changelog to update notes for automatic updates
Browse files Browse the repository at this point in the history
move changelog generation from mail.py to util.py
then, Add changelog to update notes for automatic updates

Fixes: fedora-infra#3192

Signed-off-by: Ryan Lerch <rlerch@redhat.com>
  • Loading branch information
ryanlerch committed Oct 14, 2019
1 parent afc354c commit 78c6f23
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
15 changes: 13 additions & 2 deletions bodhi/server/consumers/automatic_updates.py
Expand Up @@ -30,7 +30,7 @@
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.util import transactional_session_maker
from bodhi.server.util import generate_changelog, transactional_session_maker

log = logging.getLogger('bodhi')

Expand Down Expand Up @@ -139,10 +139,21 @@ def __call__(self, message: fedora_messaging.api.Message) -> None:
dbsession.add(user)

log.debug(f"Creating new update for {bnvr}.")
changelog = generate_changelog(build)
if changelog:
notes = f"""Automatic update for {bnvr}.
##### **Changelog**
```
{changelog}
```"""
else:
notes = f"Automatic update for {bnvr}."
update = Update(
release=rel,
builds=[build],
notes=f"Automatic update for {bnvr}.",
notes=notes,
type=UpdateType.unspecified,
stable_karma=3,
unstable_karma=-3,
Expand Down
24 changes: 5 additions & 19 deletions bodhi/server/mail.py
Expand Up @@ -24,7 +24,7 @@

from bodhi.server import log
from bodhi.server.config import config
from bodhi.server.util import get_rpm_header, get_absolute_path
from bodhi.server.util import get_rpm_header, get_absolute_path, generate_changelog

if typing.TYPE_CHECKING: # pragma: no cover
from bodhi.server.models import Update # noqa: 401
Expand Down Expand Up @@ -332,26 +332,12 @@ def get_template(update: 'Update', use_template: str = 'fedora_errata_template')
i += 1
info['references'] += line

# Find the most recent update for this package, other than this one
try:
lastpkg = build.get_latest()
except AttributeError:
# Not all build types have the get_latest() method, such as ModuleBuilds.
lastpkg = None

# Grab the RPM header of the previous update, and generate a ChangeLog
# generate a ChangeLog
info['changelog'] = ""
if lastpkg:
oldh = get_rpm_header(lastpkg)
oldtime = oldh['changelogtime']
text = oldh['changelogtext']
del oldh
if not text:
oldtime = 0
elif isinstance(oldtime, list):
oldtime = oldtime[0]
changelog = generate_changelog(build)
if changelog is not None:
info['changelog'] = "ChangeLog:\n\n%s%s" % \
(build.get_changelog(oldtime), line)
(changelog, line)

templates.append((info['subject'], use_template % info))

Expand Down
30 changes: 30 additions & 0 deletions bodhi/server/util.py
Expand Up @@ -103,6 +103,36 @@ def get_rpm_header(nvr, tries=0):
raise ValueError("No rpm headers found in koji for %r" % nvr)


def generate_changelog(build) -> typing.Optional[str]:
"""
Generate a changelog for a given build.
Args:
build: the build to create a changelog for.
Returns:
a changelog of changes between the given build, and the previous one.
or returns None if the build type doesn't have the get_latest() method.
"""
# Find the most recent update for this package, other than this one
try:
lastpkg = build.get_latest()
except AttributeError:
# Not all build types have the get_latest() method, such as ModuleBuilds.
return None

# Grab the RPM header of the previous update, and generate a ChangeLog
oldh = get_rpm_header(lastpkg)
oldtime = oldh['changelogtime']
text = oldh['changelogtext']

if not text:
oldtime = 0
elif isinstance(oldtime, list):
oldtime = oldtime[0]

return build.get_changelog(oldtime)


def build_evr(build):
"""
Return a tuple of strings of the given build's epoch, version, and release.
Expand Down
31 changes: 31 additions & 0 deletions bodhi/tests/server/consumers/test_automatic_updates.py
Expand Up @@ -92,6 +92,37 @@ def test_consume(self, caplog):

assert not any(r.levelno >= logging.WARNING for r in caplog.records)

@pytest.mark.parametrize('changelog', (True, None, ""))
@mock.patch('bodhi.server.consumers.automatic_updates.generate_changelog')
def test_changelog(self, mock_generate_changelog, changelog):
"""Assert that update notes contain the changelog if it exists."""
if changelog:
# fill the changelog here rather than in the decorator
changelog = ('* Sat Aug 3 2013 Fedora Releng <rel-eng@lists.fedoraproject.org> - 2\n'
'- Added a free money feature.\n* Tue Jun 11 2013 Randy <bowlofeggs@fpo>'
' - 2.0.1-2\n- Make users ☺\n')

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()

if changelog:
assert update.notes == f"""Automatic update for colord-1.3.4-1.fc26.
##### **Changelog**
```
{changelog}
```"""
else: # no changelog
assert update.notes == "Automatic update for colord-1.3.4-1.fc26."

def test_consume_with_orphan_build(self, caplog):
"""
Assert existing builds without an update can be handled.
Expand Down

0 comments on commit 78c6f23

Please sign in to comment.