6
6
import frontmatter
7
7
from git import Commit , Repo
8
8
9
- CHANGELOG_PATH = "changelog/"
9
+ DEFAULT_CHANGELOG_PATH = "changelog/"
10
+ DEFAULT_INITIAL_GIT_TAG_VERSION = "1.0.0"
11
+ FILENAME_DATE_FORMAT = "%Y%m%d"
12
+ FRONTMATTER_DATE_FORMAT = "%Y-%m-%d"
10
13
11
14
PRELUDE_ENTRIES = ["prelude" ]
12
15
BREAKING_CHANGE_ENTRIES = ["breaking" , "major" ]
13
16
FEATURE_ENTRIES = ["feat" , "feature" ]
14
17
BUGFIX_ENTRIES = ["fix" , "bugfix" , "hotfix" , "patch" ]
15
18
16
19
17
- class ChangeType (StrEnum ):
20
+ class ChangeKind (StrEnum ):
18
21
PRELUDE = "prelude"
19
22
BREAKING = "breaking"
20
23
FEATURE = "feature"
@@ -23,7 +26,7 @@ class ChangeType(StrEnum):
23
26
24
27
25
28
class ChangeMeta :
26
- def __init__ (self , date : datetime , kind : ChangeType , title : str ):
29
+ def __init__ (self , date : datetime , kind : ChangeKind , title : str ):
27
30
self .date = date
28
31
self .kind = kind
29
32
self .title = title
@@ -33,7 +36,7 @@ def get_changelog_entries(
33
36
previous_version_commit : Commit ,
34
37
repo : Repo ,
35
38
changelog_sub_path : str ,
36
- ) -> list [tuple [ChangeType , str ]]:
39
+ ) -> list [tuple [ChangeKind , str ]]:
37
40
changelog = []
38
41
39
42
# Compare previous version commit with current working tree
@@ -77,40 +80,51 @@ def extract_changelog_data(working_dir: str, file_path: str) -> (ChangeMeta, str
77
80
return change_meta , contents
78
81
79
82
80
- def extract_date_and_kind_from_file_name (file_name : str ) -> (datetime , ChangeType ):
83
+ def extract_date_and_kind_from_file_name (file_name : str ) -> (datetime , ChangeKind ):
81
84
match = re .match (r"(\d{8})_([a-zA-Z]+)_(.+)\.md" , file_name )
82
85
if not match :
83
86
raise Exception (f"{ file_name } - doesn't match expected pattern" )
84
87
85
88
date_str , kind_str , _ = match .groups ()
86
89
try :
87
- date = datetime . datetime . strptime (date_str , "%Y%m%d" ). date ( )
88
- except Exception :
89
- raise Exception (f"{ file_name } - date part { date_str } is not in the expected format YYYYMMDD " )
90
+ date = parse_change_date (date_str , FILENAME_DATE_FORMAT )
91
+ except Exception as e :
92
+ raise Exception (f"{ file_name } - { e } " )
90
93
91
- kind = get_change_type (kind_str )
94
+ kind = get_change_kind (kind_str )
92
95
93
96
return date , kind
94
97
95
98
96
- def get_change_type (kind_str : str ) -> ChangeType :
99
+ def parse_change_date (date_str : str , date_format : str ) -> datetime :
100
+ try :
101
+ date = datetime .datetime .strptime (date_str , date_format ).date ()
102
+ except Exception :
103
+ raise Exception (f"date { date_str } is not in the expected format { date_format } " )
104
+
105
+ return date
106
+
107
+
108
+ def get_change_kind (kind_str : str ) -> ChangeKind :
97
109
if kind_str .lower () in PRELUDE_ENTRIES :
98
- return ChangeType .PRELUDE
110
+ return ChangeKind .PRELUDE
99
111
if kind_str .lower () in BREAKING_CHANGE_ENTRIES :
100
- return ChangeType .BREAKING
112
+ return ChangeKind .BREAKING
101
113
elif kind_str .lower () in FEATURE_ENTRIES :
102
- return ChangeType .FEATURE
114
+ return ChangeKind .FEATURE
103
115
elif kind_str .lower () in BUGFIX_ENTRIES :
104
- return ChangeType .FIX
116
+ return ChangeKind .FIX
105
117
else :
106
- return ChangeType .OTHER
118
+ return ChangeKind .OTHER
107
119
108
120
109
121
def strip_changelog_entry_frontmatter (file_contents : str ) -> (ChangeMeta , str ):
110
122
"""Strip the front matter from a changelog entry."""
111
123
data = frontmatter .loads (file_contents )
112
124
113
- meta = ChangeMeta (date = data ["date" ], title = str (data ["title" ]), kind = get_change_type (str (data ["kind" ])))
125
+ kind = get_change_kind (str (data ["kind" ]))
126
+ date = parse_change_date (str (data ["date" ]), FRONTMATTER_DATE_FORMAT )
127
+ meta = ChangeMeta (date = date , title = str (data ["title" ]), kind = kind )
114
128
115
129
## Add newline to contents so the Markdown file also contains a newline at the end
116
130
contents = data .content + "\n "
0 commit comments