From d7844eded2f8b5d0de5625e7d4ba0fa23bbfc34c Mon Sep 17 00:00:00 2001 From: Charles Beauville Date: Mon, 29 Apr 2024 16:17:28 +0200 Subject: [PATCH] Fix linting errors and add entry test --- src/py/flwr_tool/update_changelog.py | 67 ++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/src/py/flwr_tool/update_changelog.py b/src/py/flwr_tool/update_changelog.py index 40856a3cb5f6..bde171a28624 100644 --- a/src/py/flwr_tool/update_changelog.py +++ b/src/py/flwr_tool/update_changelog.py @@ -39,19 +39,22 @@ def _get_latest_tag(gh_api): def _add_shorlog(new_version, shortlog): """Update the markdown file with the new version information or update existing logs.""" token = f"" - entry = f"\n### Thanks to our contributors\n\nWe would like to give our special thanks to all the contributors who made the new version of Flower possible (in `git shortlog` order):\n\n{shortlog} {token}" + entry = ( + "\n### Thanks to our contributors\n\n" + "We would like to give our special thanks to all the contributors " + "who made the new version of Flower possible " + f"(in `git shortlog` order):\n\n{shortlog} {token}" + ) current_date = date.today() - with open(CHANGELOG_FILE, "r") as file: + with open(CHANGELOG_FILE, encoding="utf-8") as file: content = file.readlines() - with open(CHANGELOG_FILE, "w") as file: + with open(CHANGELOG_FILE, "w", encoding="utf-8") as file: for line in content: if token in line: - print("Updating existing entry in the markdown file.") file.write(f"{shortlog} {token}\n") elif "## Unreleased" in line: - print("Inserting new version entry in the markdown file.") file.write(f"## {new_version} ({current_date})\n{entry}\n") else: file.write(line) @@ -111,7 +114,19 @@ def _extract_changelog_entry(pr_info): "sub_scope": pr_sub_scope, "subject": pr_subject, } - else: + + if not pr_info.body: + return None, { + "type": "unknown", + "scope": "unknown", + "sub_scope": "unknown", + "subject": "unknown", + } + + entry_match = re.search( + f"{CHANGELOG_SECTION_HEADER}(.+?)(?=##|$)", pr_info.body, re.DOTALL + ) + if not entry_match: return None, { "type": "unknown", "scope": "unknown", @@ -119,6 +134,33 @@ def _extract_changelog_entry(pr_info): "subject": "unknown", } + entry_text = entry_match.group(1).strip() + + # Remove markdown comments + entry_text = re.sub(r"", "", entry_text, flags=re.DOTALL).strip() + + token_markers = { + "general": "", + "skip": "", + "baselines": "", + "examples": "", + "sdk": "", + "simulations": "", + } + + # Find the token based on the presence of its marker in entry_text + token = next( + (token for token, marker in token_markers.items() if marker in entry_text), + None, + ) + + return entry_text, { + "type": "unknown", + "scope": token, + "sub_scope": "unknown", + "subject": "unknown", + } + def _update_changelog(prs): """Update the changelog file with entries from provided pull requests.""" @@ -141,6 +183,9 @@ def _update_changelog(prs): for pr_info in prs: pr_entry_text, parsed_title = _extract_changelog_entry(pr_info) + if "scope" not in parsed_title: + continue + category = parsed_title["scope"] # Skip if PR should be skipped or already in changelog @@ -272,9 +317,11 @@ def _insert_entry_no_desc(content, pr_reference, unreleased_index): def _bump_minor_version(tag): """Bump the minor version of the tag.""" - major, minor, patch = [ - int(x) for x in re.match(r"v(\d+)\.(\d+)\.(\d+)", tag.name).groups() - ] + match = re.match(r"v(\d+)\.(\d+)\.(\d+)", tag.name) + if match is None: + print("Wrong tag format.") + return + major, minor, _ = [int(x) for x in match.groups()] # Increment the minor version and reset patch version new_version = f"v{major}.{minor + 1}.0" return new_version @@ -295,6 +342,8 @@ def main(): new_version = _bump_minor_version(latest_tag) _add_shorlog(new_version, shortlog) + print("Changelog updated succesfully.") + if __name__ == "__main__": main()