Skip to content

Commit

Permalink
Fix update changelog script
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesbvll committed Dec 5, 2024
1 parent a6cfe71 commit 22cdfe8
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions dev/flwr_dev/update_changelog.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,12 @@
from github.Repository import Repository
from github.Tag import Tag

from flwr_dev.common import get_git_root

REPO_NAME = "adap/flower"
CHANGELOG_FILE = "doc/source/ref-changelog.md"
CHANGELOG_SECTION_HEADER = "### Changelog entry"

# Load the TOML configuration
with (pathlib.Path(__file__).parents[1].resolve() / "changelog_config.toml").open(
"rb"
) as file:
CONFIG = tomllib.load(file)

# Extract types, project, and scope from the config
TYPES = "|".join(CONFIG["type"])
PROJECTS = "|".join(CONFIG["project"]) + "|\\*"
SCOPE = CONFIG["scope"]
ALLOWED_VERBS = CONFIG["allowed_verbs"]

# Construct the pattern
PATTERN_TEMPLATE = CONFIG["pattern_template"]
PATTERN = PATTERN_TEMPLATE.format(types=TYPES, projects=PROJECTS, scope=SCOPE)


def _get_latest_tag(gh_api: Github) -> tuple[Repository, Optional[Tag]]:
"""Retrieve the latest tag from the GitHub repository."""
Expand Down Expand Up @@ -141,10 +127,11 @@ def _format_pr_reference(title: str, number: int, url: str) -> str:

def _extract_changelog_entry(
pr_info: PullRequest,
pattern: str,
) -> dict[str, str]:
"""Extract the changelog entry from a pull request's body."""
# Use regex search to find matches
match = re.search(PATTERN, pr_info.title)
match = re.search(pattern, pr_info.title)
if match:
# Extract components from the regex groups
pr_type = match.group(1)
Expand All @@ -168,7 +155,7 @@ def _extract_changelog_entry(
}


def _update_changelog(prs: set[PullRequest]) -> bool:
def _update_changelog(prs: set[PullRequest], pattern: str) -> bool:
"""Update the changelog file with entries from provided pull requests."""
breaking_changes = False
unknown_changes = False
Expand All @@ -188,7 +175,7 @@ def _update_changelog(prs: set[PullRequest]) -> bool:
)

for pr_info in prs:
parsed_title = _extract_changelog_entry(pr_info)
parsed_title = _extract_changelog_entry(pr_info, pattern)

# Skip if PR should be skipped or already in changelog
if (
Expand Down Expand Up @@ -282,14 +269,29 @@ def generate_changelog(
):
"""Update changelog using the descriptions of PRs since the latest tag."""
# Initialize GitHub Client with provided token (as argument)

# Load the TOML configuration
with (pathlib.Path(get_git_root()) / "dev" / "changelog_config.toml").open(
"rb"
) as file:
config = tomllib.load(file)

# Extract types, project, and scope from the config
types = "|".join(config["type"])
projects = "|".join(config["project"]) + "|\\*"
scope = config["scope"]

# Construct the pattern
pattern_template = config["pattern_template"]
pattern = pattern_template.format(types=types, projects=projects, scope=scope)
gh_api = Github(gh_token)
repo, latest_tag = _get_latest_tag(gh_api)
if not latest_tag:
print("No tags found in the repository.")
return

shortlog, prs = _get_pull_requests_since_tag(repo, latest_tag)
if _update_changelog(prs):
if _update_changelog(prs, pattern):
new_version = _bump_minor_version(latest_tag)
if not new_version:
print("Wrong tag format.")
Expand Down

0 comments on commit 22cdfe8

Please sign in to comment.