Repository navigation
ci: Automatically publish github releases #2488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| """Choose the GitHub release flags for a package tag. | ||
|
|
||
| Takes a ``guppylang-v<version>`` or ``guppylang-internals-v<version>`` tag as a | ||
| positional argument and reads the local Git tags in the current repository. | ||
| Fetch all release tags before running the script so the comparison is complete. | ||
|
|
||
| Writes ``package``, ``version``, ``prerelease``, and ``latest`` to stdout as one | ||
| ``key=value`` pair per line. The flags are lowercase ``true`` or ``false``. | ||
| If ``--github-output <path>`` is given, appends the same lines to that file. | ||
|
|
||
| Only the highest stable ``guppylang`` tag can be marked latest. Comparing all | ||
| tags also accounts for releases that have not yet been published. | ||
| """ | ||
|
|
||
| # ruff: noqa: INP001 -- Release scripts are standalone entry points. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import subprocess | ||
| from pathlib import Path | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from compute_versions import parse_guppy_version | ||
|
|
||
| if TYPE_CHECKING: | ||
| from collections.abc import Iterable | ||
|
|
||
|
|
||
| def release_metadata(tag: str, tags: Iterable[str]) -> dict[str, str]: | ||
| """Return the package, version, and release flags for ``tag``. | ||
|
|
||
| Stable internals releases and older stable Guppy releases get neither the | ||
| latest nor the prerelease flag. Alpha, beta, and release candidates for | ||
| either package get the prerelease flag. | ||
| """ | ||
| for package in ("guppylang", "guppylang-internals"): | ||
| prefix = f"{package}-v" | ||
| if tag.startswith(prefix): | ||
| version = tag.removeprefix(prefix) | ||
| parsed = parse_guppy_version(version) | ||
| break | ||
| else: | ||
| raise ValueError(f"Not a Guppy package tag: {tag!r}") | ||
|
|
||
| latest = package == "guppylang" and not parsed.is_prerelease | ||
| if latest: | ||
| for other_tag in tags: | ||
| if not other_tag.startswith("guppylang-v"): | ||
| continue | ||
| try: | ||
| other = parse_guppy_version(other_tag.removeprefix("guppylang-v")) | ||
| except ValueError: | ||
| continue | ||
| if not other.is_prerelease and other > parsed: | ||
| latest = False | ||
| break | ||
|
|
||
| return { | ||
| "package": package, | ||
| "version": version, | ||
| "prerelease": str(parsed.is_prerelease).lower(), | ||
| "latest": str(latest).lower(), | ||
| } | ||
|
|
||
|
|
||
| def main(argv: list[str] | None = None) -> int: | ||
| parser = argparse.ArgumentParser(description=__doc__) | ||
| parser.add_argument("tag", help="Package tag to publish.") | ||
| parser.add_argument("--github-output", help="Append metadata to GITHUB_OUTPUT.") | ||
| args = parser.parse_args(argv) | ||
| tags = subprocess.check_output( | ||
| ["git", "tag", "--list", "guppylang-v*"], # noqa: S607 | ||
| text=True, | ||
| ).splitlines() | ||
| metadata = release_metadata(args.tag, tags) | ||
| output = "".join(f"{key}={value}\n" for key, value in metadata.items()) | ||
| print(output, end="") | ||
| if args.github_output: | ||
| with Path(args.github_output).open("a", encoding="utf-8") as file: | ||
| file.write(output) | ||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| """Tests for classifying package tags before publishing GitHub releases.""" | ||
|
|
||
| # ruff: noqa: S603, S607 -- Tests run git in a temporary repository. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import subprocess | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| import gh_release_metadata as rm | ||
| import pytest | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("version", "other_versions", "latest"), | ||
| [ | ||
| ("1.0.0", [], True), | ||
| ("1.3.0", ["1.2.9", "1.3.0-rc1", "1.4.0-a0"], True), | ||
| ("1.3.1", ["1.3.0", "1.4.0-rc0"], True), | ||
| ("1.2.10", ["1.3.0"], False), | ||
| ("1.3.1", ["1.3.2"], False), | ||
| ("1.9.9", ["1.10.0"], False), | ||
| ("1.10.0", ["1.9.9", "1.10.0"], True), | ||
| ("1.2.10", ["1.2.9"], True), | ||
| ("1.2.9", ["1.2.10"], False), | ||
| ("1.99.99", ["2.0.0"], False), | ||
| ], | ||
| ) | ||
| def test_stable_guppy_latest( | ||
| version: str, other_versions: list[str], latest: bool | ||
| ) -> None: | ||
| tags = [f"guppylang-v{other}" for other in other_versions] | ||
| metadata = rm.release_metadata(f"guppylang-v{version}", tags) | ||
| assert metadata == { | ||
| "package": "guppylang", | ||
| "version": version, | ||
| "prerelease": "false", | ||
| "latest": str(latest).lower(), | ||
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("package", ["guppylang", "guppylang-internals"]) | ||
| @pytest.mark.parametrize("version", ["2.0.0-a0", "2.0.0-b1", "2.0.0-rc2", "2.0.0rc3"]) | ||
| def test_prereleases(package: str, version: str) -> None: | ||
| metadata = rm.release_metadata(f"{package}-v{version}", ["guppylang-v1.0.0"]) | ||
| assert metadata["package"] == package | ||
| assert metadata["version"] == version | ||
| assert metadata["prerelease"] == "true" | ||
| assert metadata["latest"] == "false" | ||
|
|
||
|
|
||
| def test_stable_internals_gets_neither_label() -> None: | ||
| metadata = rm.release_metadata("guppylang-internals-v2.0.0", []) | ||
| assert metadata["prerelease"] == "false" | ||
| assert metadata["latest"] == "false" | ||
|
|
||
|
|
||
| def test_other_packages_and_malformed_tags_do_not_affect_latest() -> None: | ||
| metadata = rm.release_metadata( | ||
| "guppylang-v1.0.0", | ||
| ["guppylang-internals-v9.0.0", "other-v9.0.0", "guppylang-vinvalid"], | ||
| ) | ||
| assert metadata["latest"] == "true" | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("tag", ["other-v1.0.0", "guppylang-vinvalid"]) | ||
| def test_invalid_release_tag(tag: str) -> None: | ||
| with pytest.raises(ValueError, match=r"Not a Guppy package tag|Cannot parse"): | ||
| rm.release_metadata(tag, []) | ||
|
|
||
|
|
||
| def test_cli_compares_git_tags_and_appends_outputs( | ||
| tmp_path: Path, monkeypatch: pytest.MonkeyPatch | ||
| ) -> None: | ||
| monkeypatch.chdir(tmp_path) | ||
| subprocess.run(["git", "init", "-q"], check=True) | ||
| subprocess.run( | ||
| [ | ||
| "git", | ||
| "-c", | ||
| "user.name=Test", | ||
| "-c", | ||
| "user.email=test@example.com", | ||
| "commit", | ||
| "-qm", | ||
| "Initial commit", | ||
| "--allow-empty", | ||
| ], | ||
| check=True, | ||
| ) | ||
| for tag in ("guppylang-v1.2.10", "guppylang-v1.3.0", "guppylang-v1.4.0-a0"): | ||
| subprocess.run(["git", "tag", tag], check=True) | ||
|
|
||
| output = tmp_path / "output" | ||
| output.write_text("existing=value\n") | ||
| assert rm.main(["guppylang-v1.2.10", "--github-output", str(output)]) == 0 | ||
| assert output.read_text() == ( | ||
| "existing=value\npackage=guppylang\nversion=1.2.10\n" | ||
| "prerelease=false\nlatest=false\n" | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could reduce the number of cases here, some of them are definitely redundant, like