From dc11847afb3287c3f70272e08a536e608d7addf3 Mon Sep 17 00:00:00 2001 From: Maksudul Haque Date: Sat, 5 Aug 2023 19:24:29 +0600 Subject: [PATCH 1/2] Warn users when GitHub Release do not use Semantic Versioning specification --- src/main.py | 69 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 43 insertions(+), 26 deletions(-) diff --git a/src/main.py b/src/main.py index 64e5e69..5bf2f10 100644 --- a/src/main.py +++ b/src/main.py @@ -226,11 +226,19 @@ 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": 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"], reverse=True, ) @@ -253,19 +261,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"].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"].major == c.major + and r["tag_name"].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"].major == c.major + and r["tag_name"].minor == c.minor + and r["tag_name"].micro > c.micro ) def filter_func(release_tag: str, current_version: Version) -> bool: @@ -278,31 +286,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 From c2ac0bd3a2903345b4694cee8e42f31452a9f5fc Mon Sep 17 00:00:00 2001 From: Maksudul Haque Date: Sat, 5 Aug 2023 20:10:38 +0600 Subject: [PATCH 2/2] Fix Tag name --- src/main.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.py b/src/main.py index 5bf2f10..5881fdc 100644 --- a/src/main.py +++ b/src/main.py @@ -230,7 +230,8 @@ def _get_github_releases(self, action_repository: str) -> list[dict[str, Any]]: { "published_at": release["published_at"], "html_url": release["html_url"], - "tag_name": parse(release["tag_name"]), + "tag_name": release["tag_name"], + "tag_name_parsed": parse(release["tag_name"]), } for release in response_data if not release["prerelease"] @@ -238,7 +239,7 @@ def _get_github_releases(self, action_repository: str) -> list[dict[str, Any]]: # Sort through the releases returned by GitHub API using tag_name return sorted( releases, - key=lambda r: r["tag_name"], + key=lambda r: r["tag_name_parsed"], reverse=True, ) @@ -261,19 +262,19 @@ def _release_filter_function(self): checks = [] if ReleaseType.MAJOR in self.user_config.release_types: - checks.append(lambda r, c: 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: r["tag_name"].major == c.major - and 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: r["tag_name"].major == c.major - and r["tag_name"].minor == c.minor - and 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: