1515
1616def categorize_pr (title : str ) -> str :
1717 title = title .lower ().strip ()
18- if title .startswith (' feat' ):
19- return ' feat'
20- elif title .startswith (' fix' ):
21- return ' fix'
22- elif title .startswith (' chore' ):
23- return ' chore'
24- elif title .startswith (' revert' ):
25- return ' revert'
18+ if title .startswith (" feat" ):
19+ return " feat"
20+ elif title .startswith (" fix" ):
21+ return " fix"
22+ elif title .startswith (" chore" ):
23+ return " chore"
24+ elif title .startswith (" revert" ):
25+ return " revert"
2626 else :
27- return ' misc'
27+ return " misc"
2828
2929
3030def get_initial_commit (github_repo ):
@@ -48,12 +48,14 @@ def main():
4848
4949 try :
5050 tags = list (github_repo .get_tags ())
51- if not tags :
51+ stable_tags = [t for t in tags if not re .search (r"rc\d+$" , t .name )]
52+ if stable_tags :
53+ previous_release = stable_tags [0 ].name
54+ else :
5255 print ("First release - using full history" )
5356 previous_release = get_initial_commit (github_repo )
54- else :
55- previous_release = tags [0 ].name
56- print (f"Generating changelog: { previous_release } → { current_release } " )
57+
58+ print (f"Generating changelog: { previous_release } → { current_release } " )
5759 except Exception as e :
5860 print (f"Error finding previous release: { e } " )
5961 sys .exit (1 )
@@ -63,15 +65,9 @@ def main():
6365
6466 if not commits :
6567 print ("No commits found in range" )
66- sys . exit ( 1 )
68+ return
6769
68- categories = {
69- 'feat' : [],
70- 'fix' : [],
71- 'chore' : [],
72- 'revert' : [],
73- 'misc' : []
74- }
70+ categories = {"feat" : [], "fix" : [], "chore" : [], "revert" : [], "misc" : []}
7571
7672 pr_set = set ()
7773 for commit in reversed (commits ):
@@ -81,63 +77,60 @@ def main():
8177 pr_set .add (pr .number )
8278
8379 category = categorize_pr (pr .title )
84- pr_entry = (f"- { pr .title } ([#{ pr .number } ]({ pr .html_url } )) "
85- f"by @{ pr .user .login } " )
80+ pr_entry = f"- { pr .title } ([#{ pr .number } ]({ pr .html_url } )) by @{ pr .user .login } "
8681 categories [category ].append (pr_entry )
8782
8883 if not pr_set :
8984 print ("No PRs found in range" )
90- sys . exit ( 1 )
85+ return
9186
9287 release_date = str (commits [- 1 ].commit .author .date ).split (" " )[0 ]
9388 release_url = f"https://github.com/{ REPO_NAME } /releases/tag/{ current_release } "
9489
95- major_minor_parts = current_release .split ('.' )[:2 ]
96- major_minor = '.' .join (major_minor_parts )
90+ major_minor_parts = current_release .split ("." )[:2 ]
91+ major_minor = "." .join (major_minor_parts )
9792 changelog_file = os .path .join (CHANGELOG_DIR , f"CHANGELOG-{ major_minor } .md" )
9893
9994 os .makedirs (CHANGELOG_DIR , exist_ok = True )
10095
101- changelog_content = [
102- f"# [{ current_release } ]({ release_url } ) ({ release_date } )\n \n "
103- ]
96+ changelog_content = [f"# [{ current_release } ]({ release_url } ) ({ release_date } )\n \n " ]
10497
105- if categories [' feat' ]:
98+ if categories [" feat" ]:
10699 changelog_content .append ("## New Features\n \n " )
107- changelog_content .append ("\n " .join (categories [' feat' ]) + "\n \n " )
100+ changelog_content .append ("\n " .join (categories [" feat" ]) + "\n \n " )
108101
109- if categories [' fix' ]:
102+ if categories [" fix" ]:
110103 changelog_content .append ("## Bug Fixes\n \n " )
111- changelog_content .append ("\n " .join (categories [' fix' ]) + "\n \n " )
104+ changelog_content .append ("\n " .join (categories [" fix" ]) + "\n \n " )
112105
113- if categories [' chore' ]:
106+ if categories [" chore" ]:
114107 changelog_content .append ("## Maintenance\n \n " )
115- changelog_content .append ("\n " .join (categories [' chore' ]) + "\n \n " )
108+ changelog_content .append ("\n " .join (categories [" chore" ]) + "\n \n " )
116109
117- if categories [' revert' ]:
110+ if categories [" revert" ]:
118111 changelog_content .append ("## Reverts\n \n " )
119- changelog_content .append ("\n " .join (categories [' revert' ]) + "\n \n " )
112+ changelog_content .append ("\n " .join (categories [" revert" ]) + "\n \n " )
120113
121- if categories [' misc' ]:
114+ if categories [" misc" ]:
122115 changelog_content .append ("## Other Changes\n \n " )
123- changelog_content .append ("\n " .join (categories [' misc' ]) + "\n \n " )
116+ changelog_content .append ("\n " .join (categories [" misc" ]) + "\n \n " )
124117
125118 try :
126119 with open (changelog_file ) as f :
127120 existing_content = f .read ()
128121 except FileNotFoundError :
129122 existing_content = ""
130123
131- lines = existing_content .split (' \n ' ) if existing_content else []
124+ lines = existing_content .split (" \n " ) if existing_content else []
132125
133- new_content = '' .join (changelog_content )
126+ new_content = "" .join (changelog_content )
134127 if lines :
135- new_lines = new_content .rstrip ().split (' \n ' ) + ['' ] + lines
128+ new_lines = new_content .rstrip ().split (" \n " ) + ["" ] + lines
136129 else :
137- new_lines = new_content .rstrip ().split (' \n ' )
130+ new_lines = new_content .rstrip ().split (" \n " )
138131
139132 with open (changelog_file , "w" ) as f :
140- f .write (' \n ' .join (new_lines ))
133+ f .write (" \n " .join (new_lines ))
141134
142135 print (f"Changelog has been updated: { changelog_file } " )
143136 print (f"Found { len (pr_set )} PRs for { current_release } " )
0 commit comments