Skip to content

Commit

Permalink
Merge pull request #84 from saadmk11/fix-lagacy-version-issue
Browse files Browse the repository at this point in the history
Warn users when GitHub Release do not use Semantic Versioning specification
  • Loading branch information
saadmk11 authored Aug 5, 2023
2 parents 83f971d + c2ac0bd commit e60d25a
Showing 1 changed file with 44 additions and 26 deletions.
70 changes: 44 additions & 26 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,11 +226,20 @@ def _get_github_releases(self, action_repository: str) -> list[dict[str, Any]]:
response_data = response.json()

if response_data:
# Sort through the releases returned
# by GitHub API using tag_name
releases = [
{
"published_at": release["published_at"],
"html_url": release["html_url"],
"tag_name": release["tag_name"],
"tag_name_parsed": parse(release["tag_name"]),
}
for release in response_data
if not release["prerelease"]
]
# Sort through the releases returned by GitHub API using tag_name
return sorted(
filter(lambda r: not r["prerelease"], response_data),
key=lambda r: parse(r["tag_name"]),
releases,
key=lambda r: r["tag_name_parsed"],
reverse=True,
)

Expand All @@ -253,19 +262,19 @@ def _release_filter_function(self):
checks = []

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

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

if ReleaseType.PATCH in self.user_config.release_types:
checks.append(
lambda r, c: parse(r["tag_name"]).major == c.major
and parse(r["tag_name"]).minor == c.minor
and parse(r["tag_name"]).micro > c.micro
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
)

def filter_func(release_tag: str, current_version: Version) -> bool:
Expand All @@ -278,31 +287,40 @@ def _get_latest_version_release(
) -> dict[str, str]:
"""Get the latest release"""
github_releases = self._get_github_releases(action_repository)
latest_release: dict[str, Any] = {}

if not github_releases:
return {}
return latest_release

parsed_current_version: LegacyVersion | Version = parse(current_version)
latest_release: dict[str, Any]

if isinstance(parsed_current_version, LegacyVersion):
gha_utils.warning(
f"Current version (`{current_version}`) of `{action_repository}` does not follow "
"Semantic Versioning specification. This can yield unexpected results, "
"please be careful while using the updates suggested by this action."
)
latest_release = github_releases[0]
else:
latest_release = next(
filter(
lambda r: self._release_filter_function(r, parsed_current_version),
github_releases,
),
{},
)
try:
latest_release = next(
filter(
lambda r: self._release_filter_function(
r, parsed_current_version
),
github_releases,
),
{},
)
except AttributeError:
latest_release = github_releases[0]
gha_utils.warning(
f"GitHub releases of `{action_repository}` does not follow "
"Semantic Versioning specification. This can yield unexpected results, "
"please be careful while using the updates suggested by this action."
)

if latest_release:
return {
"published_at": latest_release["published_at"],
"html_url": latest_release["html_url"],
"tag_name": latest_release["tag_name"],
}
return {}
return latest_release

def _get_commit_data(
self, action_repository: str, tag_or_branch_name: str
Expand Down

0 comments on commit e60d25a

Please sign in to comment.