Skip to content

Commit

Permalink
release-tools: improve changelog help and inform of incorrect deb ema…
Browse files Browse the repository at this point in the history
…il (canonical#13369)

* release-tools: improved help and inform of incorrect deb email

* fixed flake8 failure
  • Loading branch information
ernestl authored Nov 21, 2023
1 parent 89cdb0c commit dc51d60
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
23 changes: 20 additions & 3 deletions release-tools/changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@

import debian.changelog

# Pattern to validate environment variable "DEBEMAIL"
env_deb_email_pattern = re.compile(r"^[a-zA-Z]+\s[a-zA-Z]+ <[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}>$")


def parse_arguments():
parser = argparse.ArgumentParser(description="automatic changelog writer for snapd")
parser.add_argument("version", type=str, help="new snapd version")
parser.add_argument("lpbug", type=str, help="new snapd major release LP bug")
parser.add_argument("version", type=str, help="new snapd version in the format <major>.<minor>[.<fix>]")
parser.add_argument("lpbug", type=str, help="new snapd major release LP bug number")
parser.add_argument(
"changelog",
type=argparse.FileType("r"),
help="path to new changelog entry as generated by snappy-dch",
help="path to either updated NEWS.md or new changelog entry as generated by snappy-dch",
)
return parser.parse_args()

Expand Down Expand Up @@ -226,7 +229,21 @@ def read_changelogs_news_md(changelog: io.TextIOWrapper, new_version: str):
return new_changelog_str + "\n"


def validate_env_deb_email():
env_deb_email = os.environ.get('DEBEMAIL')
if not env_deb_email:
raise RuntimeError('cannot find environment variable "DEBEMAIL", please provide DEBEMAIL="FirstName LastName <valid-email-address>"')
elif not env_deb_email_pattern.match(env_deb_email):
raise RuntimeError('environment variable "DEBEMAIL" uses incorrect format, expecting DEBEMAIL="FirstName LastName <valid-email-address>"')


def main(opts):
try:
validate_env_deb_email()
except RuntimeError as e:
print(f"{e}")
exit(1)

this_script = os.path.realpath(__file__)
snapd_root_git_dir = os.path.dirname(os.path.dirname(this_script))
snapd_packaging_dir = os.path.join(snapd_root_git_dir, "packaging")
Expand Down
24 changes: 23 additions & 1 deletion release-tools/test/test_changelog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/python3

from io import StringIO
import os
import unittest

import changelog
Expand Down Expand Up @@ -35,5 +36,26 @@ def test_happy(self):
def test_version_not_found(self):
with self.assertRaises(RuntimeError) as cm:
changelog.read_changelogs_news_md(self.news_md, "1.1")
print(dir(cm.exception))
self.assertEqual(str(cm.exception), 'cannot find expected version "1.1" in first header, found "New in snapd 2.60.3:"')

def test_deb_email_happy(self):
original = os.environ.get('DEBEMAIL')
os.environ['DEBEMAIL'] = "FirstName LastName <[email protected]>"
try:
changelog.validate_env_deb_email()
finally:
if original:
os.environ['DEBEMAIL'] = original

def test_deb_email_errors(self):
original = os.environ.get('DEBEMAIL')
os.environ['DEBEMAIL'] = ""
with self.assertRaises(RuntimeError) as e:
changelog.validate_env_deb_email()
self.assertEqual(str(e.exception), 'cannot find environment variable "DEBEMAIL", please provide DEBEMAIL="FirstName LastName <valid-email-address>"')
os.environ['DEBEMAIL'] = "FirstName LastName <firstname.lastname.com>"
with self.assertRaises(RuntimeError) as e:
changelog.validate_env_deb_email()
self.assertEqual(str(e.exception), 'environment variable "DEBEMAIL" uses incorrect format, expecting DEBEMAIL="FirstName LastName <valid-email-address>"')
if original:
os.environ['DEBEMAIL'] = original

0 comments on commit dc51d60

Please sign in to comment.