Skip to content

Commit

Permalink
Strip down integer ticket numbers (#278)
Browse files Browse the repository at this point in the history
* Strip down integer ticket numbers

* Update test_builder.py

* Add docstring for new test for the strip feature
  • Loading branch information
altendky authored Dec 8, 2020
1 parent 15644ca commit ef1be45
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
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(
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)
)

0 comments on commit ef1be45

Please sign in to comment.