Skip to content

Commit

Permalink
Merge pull request #89 from saadmk11/use-regex
Browse files Browse the repository at this point in the history
Use `regex.sub` to replace old versions with new ones
  • Loading branch information
saadmk11 committed Aug 13, 2023
2 parents 9a41457 + 7658202 commit 507ac7f
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions src/main.py
@@ -1,3 +1,4 @@
import re
from collections.abc import Generator
from functools import cache, cached_property
from typing import Any
Expand Down Expand Up @@ -169,8 +170,12 @@ def _update_workflow(self, workflow_path: str) -> set[str]:
gha_utils.echo(
f'Updating "{action}" with "{updated_action}"...'
)
updated_workflow_data = updated_workflow_data.replace(
action, updated_action
updated_workflow_data = re.sub(
rf"({action})(\s+|$)",
rf"{updated_action}\2",
updated_workflow_data,
0,
re.MULTILINE,
)
else:
gha_utils.echo(f'No updates found for "{action_repository}"')
Expand Down Expand Up @@ -213,7 +218,9 @@ def _generate_updated_item_markdown(
f"branch on {version_data['commit_date']}\n"
)

def _get_github_releases(self, action_repository: str) -> list[dict[str, Any]]:
def _get_github_releases(
self, action_repository: str
) -> list[dict[str, str | Version | LegacyVersion]]:
"""Get the GitHub releases using GitHub API"""
url = f"{self.github_api_url}/repos/{action_repository}/releases?per_page=50"

Expand Down Expand Up @@ -261,22 +268,23 @@ def _release_filter_function(self):
checks = []

if ReleaseType.MAJOR in self.user_config.release_types:
checks.append(lambda r, c: r["tag_name_parsed"].major > c.major)
checks.append(lambda r, c: r.major > c.major)

if ReleaseType.MINOR in self.user_config.release_types:
checks.append(
lambda r, c: r["tag_name_parsed"].major == c.major
and r["tag_name_parsed"].minor > c.minor,
lambda r, c: r.major == c.major and r.minor > c.minor,
)

if ReleaseType.PATCH in self.user_config.release_types:
checks.append(
lambda r, c: r["tag_name_parsed"].major == c.major
and r["tag_name_parsed"].minor == c.minor
and r["tag_name_parsed"].micro > c.micro
lambda r, c: r.major == c.major
and r.minor == c.minor
and r.micro > c.micro
)

def filter_func(release_tag: str, current_version: Version) -> bool:
def filter_func(
release_tag: LegacyVersion | Version, current_version: Version
) -> bool:
return any(check(release_tag, current_version) for check in checks)

return filter_func
Expand Down Expand Up @@ -305,7 +313,7 @@ def _get_latest_version_release(
latest_release = next(
filter(
lambda r: self._release_filter_function(
r, parsed_current_version
r["tag_name_parsed"], parsed_current_version
),
github_releases,
),
Expand Down

0 comments on commit 507ac7f

Please sign in to comment.