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

Strip down integer ticket numbers #278

Merged
merged 8 commits into from
Dec 8, 2020
12 changes: 11 additions & 1 deletion src/towncrier/_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@
from jinja2 import Template


def strip_if_integer_string(s):
try:
i = int(s)
except ValueError:
return s

return str(i)


# Returns ticket, category and counter or (None, None, None) if the basename
# could not be parsed or doesn't contain a valid category.
def parse_newfragment_basename(basename, definitions):
Expand All @@ -21,6 +30,7 @@ def parse_newfragment_basename(basename, definitions):
return invalid
if len(parts) == 2:
ticket, category = parts
ticket = strip_if_integer_string(ticket)
return (ticket, category, 0) if category in definitions else invalid

# There are at least 3 parts. Search for a valid category from the second
Expand All @@ -35,7 +45,7 @@ def parse_newfragment_basename(basename, definitions):
# NOTE: This allows news fragment names like fix-1.2.3.feature or
# something-cool.feature.ext for projects that don't use ticket
# numbers in news fragment names.
ticket = parts[i-1]
ticket = strip_if_integer_string(parts[i-1])
counter = 0
# Use the following part as the counter if it exists and is a valid
# digit.
Expand Down
1 change: 1 addition & 0 deletions src/towncrier/newsfragments/126.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ticket number from file names will be stripped down to avoid ticket links such as ``#007``.
15 changes: 15 additions & 0 deletions src/towncrier/test/test_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,18 @@ def test_dots_in_ticket_name_and_counter(self):
parse_newfragment_basename("baz.1.2.feature.3", ["feature"]),
("2", "feature", 3),
)

def test_strip(self):
"""Leading spaces and subsequent leading zeros are stripped
when parsing newsfragment names into ticket numbers etc.
"""
self.assertEqual(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor comment

would be nice to have a docstring explaing the scope/purpose/expectation of this test :)

This can serve as the developer documentation for this feature.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/twisted/towncrier/pull/278/files#diff-b7254cf4717de60e5d2e57233db4f5cbb47871439b71f2280bed581ff9fcf851R71-R73

        """Leading spaces and subsequent leading zeros are stripped
        when parsing newsfragment names into ticket numbers etc.
        """

parse_newfragment_basename(" 007.feature", ["feature"]),
("7", "feature", 0)
)

def test_strip_with_counter(self):
self.assertEqual(
parse_newfragment_basename(" 007.feature.3", ["feature"]),
("7", "feature", 3)
)