Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions .github/scripts/is_newest_testing_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025-present Amazon.com, Inc. or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import argparse

from packaging.version import InvalidVersion, Version


def is_newest(candidate: str, existing_tags: list[str]) -> bool:
"""True if candidate is >= every v<semver> tag in existing_tags.

Non-version tags (latest, malformed) are ignored. With no existing
version tags the candidate is newest by default.
"""
candidate_version = Version(candidate)
highest = candidate_version
for tag in existing_tags:
if not tag.startswith("v"):
continue
try:
version = Version(tag[1:])
except InvalidVersion:
continue
if version > highest:
highest = version
return candidate_version >= highest


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Decide whether a release version is the newest published."
)
parser.add_argument("--candidate", required=True)
parser.add_argument(
"tags", nargs="*", help="Existing image tags, e.g. v1.2.1 v2.0.0 latest"
)
args = parser.parse_args(argv)

print("true" if is_newest(args.candidate, args.tags) else "false")
return 0


if __name__ == "__main__":
raise SystemExit(main())
46 changes: 46 additions & 0 deletions .github/scripts/latest_testing_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025-present Amazon.com, Inc. or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import argparse

from packaging.version import InvalidVersion, Version


def latest_version(existing_tags: list[str]) -> str:
"""Return the highest v<semver> tag, or empty if there are none.

Non-version tags (latest, malformed) are ignored.
"""
highest: Version | None = None
highest_tag = ""
for tag in existing_tags:
if not tag.startswith("v"):
continue
try:
version = Version(tag[1:])
Comment on lines +20 to +23

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex AI review · Finding arf_v1_bn7u2dvrf6j45ju4qaataf7xkx

[P2] Version() accepts abbreviated PEP 440 values such as 1 and 1.2, although this workflow only publishes versions with an X.Y.Z prefix. A stray registry tag such as v999 is therefore selected as newest, causing attempts to use nonexistent v999-x86_64 and v999-arm64 images and blocking releases. Full-match the supported tag grammar before parsing, and add regression cases for v1 and v1.2.

except InvalidVersion:
continue
if highest is None or version > highest:
highest = version
highest_tag = tag
return highest_tag


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description="Print the highest published testing version tag."
)
parser.add_argument(
"tags", nargs="*", help="Existing image tags, e.g. v1.2.1 v2.0.0 latest"
)
args = parser.parse_args(argv)

print(latest_version(args.tags))
return 0


if __name__ == "__main__":
raise SystemExit(main())
38 changes: 38 additions & 0 deletions .github/scripts/parse_testing_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025-present Amazon.com, Inc. or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0
from __future__ import annotations

import os
import re

# A release-tag component naming the testing package: testing-v<x.y.z> with an
# optional pre-release suffix. Anchored and matched against a single comma-split
# component so prefixes like "mytesting-v1.2.1" are rejected.
_COMPONENT = re.compile(r"testing-v([0-9]+\.[0-9]+\.[0-9]+[0-9A-Za-z.-]*)\Z")


def parse_testing_version(release_tag: str) -> str:
"""Return the testing version named by the release tag, or empty if none."""
for part in release_tag.split(","):
match = _COMPONENT.fullmatch(part.strip())
if match:
return match.group(1)
return ""


def main():
release_tag = os.environ.get("RELEASE_TAG", "")
tag_version = parse_testing_version(release_tag)

github_output = os.environ.get("GITHUB_OUTPUT")
if github_output:
with open(github_output, "a", encoding="utf-8") as f:
f.write(f"tag_version={tag_version}\n")

print(tag_version)


if __name__ == "__main__":
main()
42 changes: 42 additions & 0 deletions .github/scripts/tests/test_is_newest_testing_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025-present Amazon.com, Inc. or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from is_newest_testing_version import is_newest


def test_is_newest():
test_cases = [
# Newest so far
("2.0.0", ["v1.2.1", "v1.1.3", "latest"], True),
# First ever release
("1.0.0", [], True),
("1.0.0", ["latest"], True),
# Equal to the highest counts as newest (idempotent re-publish)
("2.0.0", ["v2.0.0", "latest"], True),
# Backport below the highest is NOT newest
("1.1.3", ["v1.2.1", "v2.0.0", "latest"], False),
("1.9.9", ["v2.0.0"], False),
# Malformed and non-version tags are ignored
("2.0.1", ["v2.0.0", "notaversion", "latest", "v"], True),
# Pre-release ordering
("2.0.0", ["v2.0.0rc1"], True),
("2.0.0rc1", ["v2.0.0"], False),
]

for candidate, tags, expected in test_cases:
result = is_newest(candidate, tags)
# Assert is expected in test functions
assert result == expected, ( # noqa: S101
f"Expected {expected} but got {result} for {candidate} against {tags}"
)


if __name__ == "__main__":
test_is_newest()
sys.exit(0)
44 changes: 44 additions & 0 deletions .github/scripts/tests/test_latest_testing_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2025-present Amazon.com, Inc. or its affiliates.
#
# SPDX-License-Identifier: Apache-2.0
import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from latest_testing_version import latest_version


def test_latest_version():
test_cases = [
# Highest of several
(["v1.2.1", "v2.0.0", "v1.1.3", "latest"], "v2.0.0"),
# Single version
(["v1.0.0"], "v1.0.0"),
# Order does not matter
(["v2.0.0", "v1.0.0"], "v2.0.0"),
# Non-version and malformed tags ignored
(["v2.0.0", "latest", "notaversion", "v"], "v2.0.0"),
# No version tags
([], ""),
(["latest"], ""),
# Pre-release orders below its release
(["v2.0.0rc1", "v2.0.0"], "v2.0.0"),
(["v2.0.0rc1"], "v2.0.0rc1"),
# Original tag text is preserved, not normalized
(["v2.0.0-beta"], "v2.0.0-beta"),
(["v1.0.0", "v2.0.0-beta"], "v2.0.0-beta"),
]

for tags, expected in test_cases:
result = latest_version(tags)
# Assert is expected in test functions
assert result == expected, ( # noqa: S101
f"Expected '{expected}' but got '{result}' for {tags}"
)


if __name__ == "__main__":
test_latest_version()
sys.exit(0)
48 changes: 48 additions & 0 deletions .github/scripts/tests/test_parse_testing_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#!/usr/bin/env python3

import os
import sys

sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

from parse_testing_version import parse_testing_version


def test_parse_testing_version():
test_cases = [
# Testing-only tag enables the job
("testing-v2.0.0", "2.0.0"),
("testing-v1.2.1", "1.2.1"),
# Combined comma-separated tags resolve to the testing version
("sdk-v2.0.0,testing-v2.0.0", "2.0.0"),
("testing-v2.0.0,sdk-v2.1.0", "2.0.0"),
("otel-v1.0.0,testing-v1.2.1,sdk-v2.0.0", "1.2.1"),
# SDK-only or OTel-only tags do not enable the job
("sdk-v2.1.0", ""),
("otel-v1.0.0", ""),
("sdk-v2.0.0,otel-v1.0.0", ""),
# Malformed or unrelated prefixes must not match
("not-testing-v1.2.1", ""),
("sdk-v2.0.0,mytesting-v1.2.1", ""),
("testing-v1.2", ""),
("testing-version-1.2.1", ""),
# No release tag
("", ""),
("v2.0.0", ""),
("random-text", ""),
# Pre-release suffix is kept
("testing-v2.0.0rc1", "2.0.0rc1"),
("testing-v2.0.0-beta,sdk-v1.0.0", "2.0.0-beta"),
]

for input_text, expected in test_cases:
result = parse_testing_version(input_text)
# Assert is expected in test functions
assert result == expected, ( # noqa: S101
f"Expected '{expected}' but got '{result}' for input: {input_text}"
)


if __name__ == "__main__":
test_parse_testing_version()
sys.exit(0)
Loading
Loading