diff --git a/src/towncrier/_builder.py b/src/towncrier/_builder.py index 6135df83..2a8f0b22 100644 --- a/src/towncrier/_builder.py +++ b/src/towncrier/_builder.py @@ -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): @@ -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 @@ -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. diff --git a/src/towncrier/newsfragments/126.feature b/src/towncrier/newsfragments/126.feature new file mode 100644 index 00000000..be6e6332 --- /dev/null +++ b/src/towncrier/newsfragments/126.feature @@ -0,0 +1 @@ +Ticket number from file names will be stripped down to avoid ticket links such as ``#007``. \ No newline at end of file diff --git a/src/towncrier/test/test_builder.py b/src/towncrier/test/test_builder.py index e6d0ed01..7f48a2e9 100644 --- a/src/towncrier/test/test_builder.py +++ b/src/towncrier/test/test_builder.py @@ -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) + )