Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add changelog entry to update notes for automatic updates #3548

Merged
merged 1 commit into from Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: 'models.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
1 change: 1 addition & 0 deletions news/3192.feature
@@ -0,0 +1 @@
Automatically created updates (e.g. Fedora Rawhide single package updates) now include a changelog entry in the update notes.