Skip to content

Commit edb2fb5

Browse files
committed
Using ChangeEntry type
1 parent 8d87332 commit edb2fb5

File tree

3 files changed

+30
-33
lines changed

3 files changed

+30
-33
lines changed

scripts/release/changelog.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,19 @@ class ChangeKind(StrEnum):
2626
OTHER = "other"
2727

2828

29-
class ChangeMeta:
30-
def __init__(self, date: datetime, kind: ChangeKind, title: str):
29+
class ChangeEntry:
30+
def __init__(self, date: datetime, kind: ChangeKind, title: str, contents: str):
3131
self.date = date
3232
self.kind = kind
3333
self.title = title
34+
self.contents = contents
3435

3536

3637
def get_changelog_entries(
3738
previous_version_commit: Commit,
3839
repo: Repo,
3940
changelog_sub_path: str,
40-
) -> list[tuple[ChangeKind, str]]:
41+
) -> list[ChangeEntry]:
4142
changelog = []
4243

4344
# Compare previous version commit with current working tree
@@ -51,34 +52,33 @@ def get_changelog_entries(
5152
for diff_item in diff_index.iter_change_type("A"):
5253
file_path = diff_item.b_path
5354

54-
change_meta, contents = extract_changelog_data(repo.working_dir, file_path)
55-
56-
changelog.append((str(change_meta.kind), contents))
55+
change_entry = extract_changelog_entry(repo.working_dir, file_path)
56+
changelog.append(change_entry)
5757

5858
return changelog
5959

6060

61-
def extract_changelog_data(working_dir: str, file_path: str) -> (ChangeMeta, str):
61+
def extract_changelog_entry(working_dir: str, file_path: str) -> ChangeEntry:
6262
file_name = os.path.basename(file_path)
6363
date, kind = extract_date_and_kind_from_file_name(file_name)
6464

6565
abs_file_path = os.path.join(working_dir, file_path)
6666
with open(abs_file_path, "r") as file:
6767
file_content = file.read()
6868

69-
change_meta, contents = strip_changelog_entry_frontmatter(file_content)
69+
change_entry = extract_changelog_entry_from_contents(file_content)
7070

71-
if change_meta.date != date:
71+
if change_entry.date != date:
7272
raise Exception(
73-
f"{file_name} - date in front matter {change_meta.date} does not match date extracted from file name {date}"
73+
f"{file_name} - date in front matter {change_entry.date} does not match date extracted from file name {date}"
7474
)
7575

76-
if change_meta.kind != kind:
76+
if change_entry.kind != kind:
7777
raise Exception(
78-
f"{file_name} - kind in front matter {change_meta.kind} does not match kind extracted from file name {kind}"
78+
f"{file_name} - kind in front matter {change_entry.kind} does not match kind extracted from file name {kind}"
7979
)
8080

81-
return change_meta, contents
81+
return change_entry
8282

8383

8484
def extract_date_and_kind_from_file_name(file_name: str) -> (datetime, ChangeKind):
@@ -119,18 +119,15 @@ def get_change_kind(kind_str: str) -> ChangeKind:
119119
return ChangeKind.OTHER
120120

121121

122-
def strip_changelog_entry_frontmatter(file_contents: str) -> (ChangeMeta, str):
123-
"""Strip the front matter from a changelog entry."""
122+
def extract_changelog_entry_from_contents(file_contents: str) -> ChangeEntry:
124123
data = frontmatter.loads(file_contents)
125124

126125
kind = get_change_kind(str(data["kind"]))
127126
date = parse_change_date(str(data["date"]), FRONTMATTER_DATE_FORMAT)
128-
meta = ChangeMeta(date=date, title=str(data["title"]), kind=kind)
129-
130127
## Add newline to contents so the Markdown file also contains a newline at the end
131128
contents = data.content + "\n"
132129

133-
return meta, contents
130+
return ChangeEntry(date=date, title=str(data["title"]), kind=kind, contents=contents)
134131

135132

136133
def get_changelog_filename(title: str, kind: ChangeKind, date: datetime) -> str:

scripts/release/changelog_test.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
from changelog import (
55
MAX_TITLE_LENGTH,
66
ChangeKind,
7+
extract_changelog_entry_from_contents,
78
extract_date_and_kind_from_file_name,
89
sanitize_title,
9-
strip_changelog_entry_frontmatter,
1010
)
1111

1212

@@ -94,14 +94,13 @@ def test_strip_changelog_entry_frontmatter():
9494
* Added new property [spec.search](https://www.mongodb.com/docs/kubernetes/current/mongodb/specification/#spec-search) to enable MongoDB Search.
9595
"""
9696

97-
change_meta, contents = strip_changelog_entry_frontmatter(file_contents)
98-
99-
assert change_meta.title == "This is my change"
100-
assert change_meta.kind == ChangeKind.FEATURE
101-
assert change_meta.date == datetime.date(2025, 7, 10)
97+
change_entry = extract_changelog_entry_from_contents(file_contents)
10298

99+
assert change_entry.title == "This is my change"
100+
assert change_entry.kind == ChangeKind.FEATURE
101+
assert change_entry.date == datetime.date(2025, 7, 10)
103102
assert (
104-
contents
103+
change_entry.contents
105104
== """* **MongoDB**: public search preview release of MongoDB Search (Community Edition) is now available.
106105
* Added new property [spec.search](https://www.mongodb.com/docs/kubernetes/current/mongodb/specification/#spec-search) to enable MongoDB Search.
107106
"""

scripts/release/release_notes.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from scripts.release.changelog import (
88
DEFAULT_CHANGELOG_PATH,
99
DEFAULT_INITIAL_GIT_TAG_VERSION,
10+
ChangeEntry,
1011
ChangeKind,
1112
get_changelog_entries,
1213
)
@@ -44,23 +45,23 @@ def generate_release_notes(
4445

4546
parameters = {
4647
"version": version,
47-
"preludes": [c[1] for c in changelog if c[0] == ChangeKind.PRELUDE],
48-
"breaking_changes": [c[1] for c in changelog if c[0] == ChangeKind.BREAKING],
49-
"features": [c[1] for c in changelog if c[0] == ChangeKind.FEATURE],
50-
"fixes": [c[1] for c in changelog if c[0] == ChangeKind.FIX],
51-
"others": [c[1] for c in changelog if c[0] == ChangeKind.OTHER],
48+
"preludes": [c.contents for c in changelog if c.kind == ChangeKind.PRELUDE],
49+
"breaking_changes": [c.contents for c in changelog if c.kind == ChangeKind.BREAKING],
50+
"features": [c.contents for c in changelog if c.kind == ChangeKind.FEATURE],
51+
"fixes": [c.contents for c in changelog if c.kind == ChangeKind.FIX],
52+
"others": [c.contents for c in changelog if c.kind == ChangeKind.OTHER],
5253
}
5354

5455
return template.render(parameters)
5556

5657

5758
def calculate_next_version_with_changelog(
5859
repo: Repo, changelog_sub_path: str, initial_commit_sha: str | None, initial_version: str
59-
) -> (str, list[tuple[ChangeKind, str]]):
60+
) -> (str, list[ChangeEntry]):
6061
previous_version_tag, previous_version_commit = find_previous_version(repo, initial_commit_sha)
6162

62-
changelog: list[tuple[ChangeKind, str]] = get_changelog_entries(previous_version_commit, repo, changelog_sub_path)
63-
changelog_kinds = list[ChangeKind](map(lambda x: x[0], changelog))
63+
changelog: list[ChangeEntry] = get_changelog_entries(previous_version_commit, repo, changelog_sub_path)
64+
changelog_kinds = list(set(entry.kind for entry in changelog))
6465

6566
# If there is no previous version tag, we start with the initial version tag
6667
if not previous_version_tag:

0 commit comments

Comments
 (0)