|
22 | 22 | #!/usr/bin/env python3 |
23 | 23 |
|
24 | 24 | import os |
25 | | -import sys |
26 | 25 |
|
27 | 26 | CHANGELOG_PATH = os.environ.get("CHANGELOG_PATH", "CHANGELOG.md") |
28 | 27 | VERSION = os.environ['VERSION'] |
|
32 | 31 | def overwrite_changelog_section(changelog_text: str, new_content: str): |
33 | 32 | # Find the section for the specified version |
34 | 33 | version_pattern = f"## {VERSION}\n" |
35 | | - bracketed_version_pattern = f"## [{VERSION}]\n" |
| 34 | + unformmatted_prev_version_pattern = f"## {PREV_VERSION}\n" |
36 | 35 | prev_version_pattern = f"## [{PREV_VERSION}]\n" |
37 | 36 | print(f"latest version: {VERSION}") |
38 | 37 | print(f"prev_version: {PREV_VERSION}") |
39 | 38 |
|
40 | | - # Try both unbracketed and bracketed version patterns |
41 | | - version_index = changelog_text.find(version_pattern) |
42 | | - if version_index == -1: |
43 | | - version_index = changelog_text.find(bracketed_version_pattern) |
44 | | - if version_index == -1: |
45 | | - # If version not found, add it at the top (after the first line) |
46 | | - first_newline = changelog_text.find('\n') |
47 | | - if first_newline == -1: |
48 | | - # If no newline found, just prepend |
49 | | - return f"## [{VERSION}]\n\n{changelog_text}" |
50 | | - return f"{changelog_text[:first_newline + 1]}## [{VERSION}]\n\n{changelog_text[first_newline + 1:]}" |
51 | | - else: |
52 | | - # Using bracketed version |
53 | | - version_pattern = bracketed_version_pattern |
54 | | - |
55 | | - notes_start_index = version_index + len(version_pattern) |
56 | | - notes_end_index = changelog_text.find(prev_version_pattern, notes_start_index) if PREV_VERSION and prev_version_pattern in changelog_text else len(changelog_text) |
| 39 | + notes_start_index = changelog_text.find(version_pattern) + len(version_pattern) |
| 40 | + notes_end_index = changelog_text.find(prev_version_pattern, notes_start_index) if PREV_VERSION and (prev_version_pattern in changelog_text or unformmatted_prev_version_pattern in changelog_text) else len(changelog_text) |
57 | 41 |
|
58 | 42 | if new_content: |
59 | 43 | return changelog_text[:notes_start_index] + f"{new_content}\n" + changelog_text[notes_end_index:] |
60 | 44 | else: |
61 | 45 | changeset_lines = changelog_text[notes_start_index:notes_end_index].split("\n") |
62 | | - # Ensure we have at least 2 lines before removing them |
63 | | - if len(changeset_lines) < 2: |
64 | | - print("Warning: Changeset content has fewer than 2 lines") |
65 | | - parsed_lines = "\n".join(changeset_lines) |
66 | | - else: |
67 | | - # Remove the first two lines from the regular changeset format, ex: \n### Patch Changes |
68 | | - parsed_lines = "\n".join(changeset_lines[2:]) |
69 | | - updated_changelog = changelog_text[:notes_start_index] + parsed_lines + changelog_text[notes_end_index:] |
70 | | - # Ensure version number is bracketed |
71 | | - updated_changelog = updated_changelog.replace(f"## {VERSION}", f"## [{VERSION}]") |
72 | | - return updated_changelog |
| 46 | + filtered_lines = [] |
| 47 | + for line in changeset_lines: |
| 48 | + # If the previous line is a changeset format |
| 49 | + if len(filtered_lines) > 1 and filtered_lines[-1].startswith("### "): |
| 50 | + # Remove the last two lines from the filted_lines |
| 51 | + filtered_lines.pop() |
| 52 | + filtered_lines.pop() |
| 53 | + else: |
| 54 | + filtered_lines.append(line.strip()) |
73 | 55 |
|
74 | | -try: |
75 | | - print(f"Reading changelog from: {CHANGELOG_PATH}") |
76 | | - with open(CHANGELOG_PATH, 'r') as f: |
77 | | - changelog_content = f.read() |
| 56 | + # Prepend a new line to the first line of filtered_lines |
| 57 | + if filtered_lines: |
| 58 | + filtered_lines[0] = "\n" + filtered_lines[0] |
78 | 59 |
|
79 | | - print(f"Changelog content length: {len(changelog_content)} characters") |
80 | | - print("First 200 characters of changelog:") |
81 | | - print(changelog_content[:200]) |
82 | | - print("----------------------------------------------------------------------------------") |
| 60 | + # Print filted_lines wiht a "\n" at the end of each line |
| 61 | + for line in filtered_lines: |
| 62 | + print(line.strip()) |
| 63 | + |
| 64 | + parsed_lines = "\n".join(line for line in filtered_lines) |
| 65 | + updated_changelog = changelog_text[:notes_start_index] + parsed_lines + changelog_text[notes_end_index:] |
| 66 | + return updated_changelog |
83 | 67 |
|
84 | | - new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT) |
85 | | - |
86 | | - print("New changelog content:") |
87 | | - print("----------------------------------------------------------------------------------") |
88 | | - print(new_changelog) |
89 | | - print("----------------------------------------------------------------------------------") |
90 | | - |
91 | | - print(f"Writing updated changelog back to: {CHANGELOG_PATH}") |
92 | | - with open(CHANGELOG_PATH, 'w') as f: |
93 | | - f.write(new_changelog) |
| 68 | +with open(CHANGELOG_PATH, 'r') as f: |
| 69 | + changelog_content = f.read() |
94 | 70 |
|
95 | | - print(f"{CHANGELOG_PATH} updated successfully!") |
| 71 | +new_changelog = overwrite_changelog_section(changelog_content, NEW_CONTENT) |
| 72 | +# print("----------------------------------------------------------------------------------") |
| 73 | +# print(new_changelog) |
| 74 | +# print("----------------------------------------------------------------------------------") |
| 75 | +# Write back to CHANGELOG.md |
| 76 | +with open(CHANGELOG_PATH, 'w') as f: |
| 77 | + f.write(new_changelog) |
96 | 78 |
|
97 | | -except FileNotFoundError: |
98 | | - print(f"Error: Changelog file not found at {CHANGELOG_PATH}") |
99 | | - sys.exit(1) |
100 | | -except Exception as e: |
101 | | - print(f"Error updating changelog: {str(e)}") |
102 | | - print(f"Current working directory: {os.getcwd()}") |
103 | | - sys.exit(1) |
| 79 | +print(f"{CHANGELOG_PATH} updated successfully!") |
0 commit comments