From c0da028190dc0ae6f52773874f0fe2c677b64b21 Mon Sep 17 00:00:00 2001 From: Windsland52 <86581225+Windsland52@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:04:42 +0800 Subject: [PATCH 1/2] fix(release): take the MaaPiCli entrypoint from its own release The CLI package shipped the MaaPiCli binary from the MaaFramework runtime bundle, and that copy lags the ProjectInterface revisions: MaaFramework v5.15.0-beta.1 still deserializes `welcome` as a single string, so M9A's PI 2.10 `welcome` array made interface.json fail to load and the CLI exited before showing a single option (MAA1999/m9a-cli#1). MaaFramework is dropping MaaPiCli from its MAA-* packages anyway, so the runtime bundle can no longer be the source of the entrypoint. Download MaaPiCli from the MaaXYZ/MaaPiCli release assets for the build platform/arch, ship it as the M9A entrypoint, and drop whatever copy the MaaFramework bundle still carries. `--maapicli-tag` pins an exact tag; empty resolves the latest release. Also run the packaged CLI once before uploading: it must parse the packaged interface.json and reach the controller prompt, so a CLI build that cannot load the interface fails in the build instead of on a user's machine. That smoke reproduces MAA1999/m9a-cli#1 against the published v4.13.1 package and passes once the entrypoint comes from MaaXYZ/MaaPiCli. --- .github/workflows/release.yml | 16 ++- .github/workflows/test.yml | 23 ++++ README.md | 20 ++- tests/test_install_cli.py | 106 ++++++++++++++++ tools/ci/install_cli.py | 223 ++++++++++++++++++++++++++++++---- 5 files changed, 359 insertions(+), 29 deletions(-) create mode 100644 .github/workflows/test.yml create mode 100644 tests/test_install_cli.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 627ed42..abffcfd 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -38,6 +38,10 @@ on: description: Release body markdown. Leave empty to use the fallback body. required: false default: "" + maapicli_tag: + description: Exact MaaXYZ/MaaPiCli release tag. Leave empty to use its latest release. + required: false + default: "" permissions: contents: write @@ -55,6 +59,7 @@ jobs: is_latest_framework: ${{ steps.meta.outputs.is_latest_framework }} is_prerelease: ${{ steps.meta.outputs.is_prerelease }} release_body: ${{ steps.meta.outputs.release_body }} + maapicli_tag: ${{ steps.meta.outputs.maapicli_tag }} steps: - id: meta shell: bash @@ -65,6 +70,7 @@ jobs: source_sha="$(jq -r '.client_payload.source_sha // ""' "$GITHUB_EVENT_PATH")" maa_framework_channel="$(jq -r '.client_payload.maa_framework_channel // .inputs.maa_framework_channel // "stable"' "$GITHUB_EVENT_PATH")" maa_framework_version="$(jq -r '.client_payload.maa_framework_version // .inputs.maa_framework_version // ""' "$GITHUB_EVENT_PATH")" + maapicli_tag="$(jq -r '.client_payload.maapicli_tag // .inputs.maapicli_tag // ""' "$GITHUB_EVENT_PATH")" is_latest_framework="false" if [ "$maa_framework_version" = "latest" ]; then maa_framework_channel="stable" @@ -136,6 +142,7 @@ jobs: echo "maa_framework_version=$maa_framework_version" echo "is_latest_framework=$is_latest_framework" echo "is_prerelease=$is_prerelease" + echo "maapicli_tag=$maapicli_tag" echo "release_body<<__M9A_RELEASE_BODY__" printf '%s\n' "$release_body" echo "__M9A_RELEASE_BODY__" @@ -216,8 +223,9 @@ jobs: shell: bash env: PYTHONPATH: M9A/tools/ci + GH_TOKEN: ${{ github.token }} run: | - ./install/python/python.exe tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform win32 + ./install/python/python.exe tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform win32 --arch ${{ matrix.arch }} --maapicli-tag "${{ needs.meta.outputs.maapicli_tag }}" cp -rpvn install/python/. install-cli/python/ rm -rf install-cli/deps 2>/dev/null || true @@ -304,8 +312,9 @@ jobs: shell: bash env: PYTHONPATH: M9A/tools/ci + GH_TOKEN: ${{ github.token }} run: | - python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform linux + python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform linux --arch ${{ matrix.arch }} --maapicli-tag "${{ needs.meta.outputs.maapicli_tag }}" cp -rpvn install/python/. install-cli/python/ rm -rf install-cli/deps 2>/dev/null || true if [ -f "install-cli/M9A" ]; then chmod +x install-cli/M9A; fi @@ -392,8 +401,9 @@ jobs: shell: bash env: PYTHONPATH: M9A/tools/ci + GH_TOKEN: ${{ github.token }} run: | - ./install/python/bin/python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform darwin + ./install/python/bin/python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform darwin --arch ${{ matrix.arch }} --maapicli-tag "${{ needs.meta.outputs.maapicli_tag }}" cp -rpvn install/python/. install-cli/python/ if [ -f "install-cli/M9A" ]; then chmod +x install-cli/M9A; fi rm -rf install-cli/deps 2>/dev/null || true diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..7c57eb3 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,23 @@ +name: Test packaging scripts + +on: + push: + branches: [main] + pull_request: + +permissions: + contents: read + +jobs: + test: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v7 + + - name: Set up pytest + run: | + python3 -m venv .venv + .venv/bin/pip install --quiet pytest + + - name: Run tests + run: .venv/bin/python -m pytest tests -q diff --git a/README.md b/README.md index e3a55a2..fd100ac 100644 --- a/README.md +++ b/README.md @@ -10,27 +10,39 @@ version, and release notes. 1. `MAA1999/M9A` publishes a normal release. 2. The M9A workflow sends a `repository_dispatch` event to `MAA1999/m9a-cli`. -3. This repository clones the matching M9A ref, resolves MaaFramework from an exact version or the stable/beta/alpha channel, and - builds `PiCLI` artifacts. +3. This repository clones the matching M9A ref, resolves MaaFramework from an exact version or the stable/beta/alpha channel, takes the + MaaPiCli entrypoint from the `MAAXYZ/MaaPiCli` release assets, and builds `PiCLI` artifacts. 4. The artifacts are published to the same tag name in this repository, reusing the release notes passed from `MAA1999/M9A`. Manual runs are available through `workflow_dispatch`. +The entrypoint is no longer taken from the MaaFramework runtime bundle: that copy +lags the ProjectInterface revisions (MaaFramework v5.15.0-beta.1 still parsed +`welcome` as a single string, so an interface.json using the PI 2.10 array failed +to load), and MaaFramework is dropping MaaPiCli from its `MAA-*` packages anyway. +`--maapicli-tag` pins an exact MaaXYZ/MaaPiCli release; empty resolves the latest. + ## Local Smoke Test From this repository root: ```powershell -python tools/ci/install_cli.py --source-dir D:\02code\M9A --deps-dir D:\02code\M9A\deps --output-dir install-cli-smoke --version v0.0.0-local --platform win32 +python tools/ci/install_cli.py --source-dir D:\02code\M9A --deps-dir D:\02code\M9A\deps --output-dir install-cli-smoke --version v0.0.0-local --platform win32 --arch x86_64 ``` The command expects a prepared MaaFramework `deps` directory. CI downloads it from `MaaXYZ/MaaFramework`. `--python-dir` defaults to `install/python`, which -`tools/ci/setup_embed_python.py` prepares. +`tools/ci/setup_embed_python.py` prepares. MaaPiCli is downloaded from +`MaaXYZ/MaaPiCli` into `install/maapicli` (set `GH_TOKEN` to avoid API rate +limits); pass `--maapicli-tag v5.14.3` to pin it. The build strips the MaaFramework copy bundled in the embedded interpreter's `maa` wheel -- the Agent reaches the flattened copy at the package root through `MAAFW_BINARY_PATH` -- and then runs the packaged Agent once. A `--source-dir` that predates the `agent/maafw_paths.py` helper is rejected before anything is stripped, so a stale M9A ref cannot produce a package that only breaks on the user machine. + +Finally the packaged CLI is started once against the packaged `interface.json` and +must reach the controller prompt, so a CLI build that cannot load the interface +fails before the artifact is uploaded. diff --git a/tests/test_install_cli.py b/tests/test_install_cli.py new file mode 100644 index 0000000..1daff55 --- /dev/null +++ b/tests/test_install_cli.py @@ -0,0 +1,106 @@ +from __future__ import annotations + +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "tools" / "ci")) + +import install_cli + + +def test_normalize_arch_maps_release_asset_names() -> None: + assert install_cli.normalize_arch("AMD64") == "x86_64" + assert install_cli.normalize_arch("x86_64") == "x86_64" + assert install_cli.normalize_arch("arm64") == "aarch64" + assert install_cli.normalize_arch("aarch64") == "aarch64" + + +def test_normalize_arch_rejects_unknown_machine() -> None: + with pytest.raises(RuntimeError): + install_cli.normalize_arch("mips") + + +def test_maapicli_asset_name_matches_release_assets() -> None: + assert ( + install_cli.maapicli_asset_name("win32", "x86_64", "v5.14.3") + == "MAA-MaaPiCli-windows-x86_64-v5.14.3.zip" + ) + assert ( + install_cli.maapicli_asset_name("linux", "aarch64", "v5.14.3") + == "MAA-MaaPiCli-linux-aarch64-v5.14.3.zip" + ) + assert ( + install_cli.maapicli_asset_name("darwin", "arm64", "v5.14.3") + == "MAA-MaaPiCli-macos-aarch64-v5.14.3.zip" + ) + + +def test_maapicli_asset_os_rejects_unknown_platform() -> None: + with pytest.raises(RuntimeError): + install_cli.maapicli_asset_os("freebsd") + + +def test_cli_binary_names_per_platform() -> None: + assert install_cli.cli_binary_name("win32") == "M9A.exe" + assert install_cli.cli_binary_name("linux") == "M9A" + assert install_cli.maapicli_binary_name("win32") == "MaaPiCli.exe" + assert install_cli.maapicli_binary_name("darwin") == "MaaPiCli" + + +def test_resolve_maapicli_tag_prefers_explicit_tag(tmp_path: Path) -> None: + # An explicit tag must not reach the releases API. + assert install_cli.resolve_maapicli_tag("v5.14.3") == "v5.14.3" + + +def test_maapicli_binary_path_finds_bin_layout(tmp_path: Path) -> None: + binary = tmp_path / "bin" / "MaaPiCli.exe" + binary.parent.mkdir() + binary.write_bytes(b"cli") + + assert install_cli.maapicli_binary_path(tmp_path, "win32") == binary + + +def test_maapicli_binary_path_fails_when_missing(tmp_path: Path) -> None: + with pytest.raises(FileNotFoundError): + install_cli.maapicli_binary_path(tmp_path, "linux") + + +def test_install_maapicli_replaces_bundled_binary(tmp_path: Path) -> None: + maapicli_dir = tmp_path / "maapicli" + extracted = maapicli_dir / "bin" / "MaaPiCli.exe" + extracted.parent.mkdir(parents=True) + extracted.write_bytes(b"fixed cli") + + output_dir = tmp_path / "install-cli" + output_dir.mkdir() + (output_dir / "M9A.exe").write_bytes(b"stale m9a") + (output_dir / "MaaPiCli.exe").write_bytes(b"bundled cli") + + target = install_cli.install_maapicli(output_dir, maapicli_dir, "win32") + + assert target == output_dir / "M9A.exe" + assert target.read_bytes() == b"fixed cli" + # The copy MaaFramework still bundles must not survive next to the entrypoint. + assert not (output_dir / "MaaPiCli.exe").exists() + + +def test_assert_interface_parse_output_accepts_controller_prompt() -> None: + install_cli.assert_interface_parse_output("### Select controller ###\n\n\t1. Controller.Adb\n") + + +def test_assert_interface_parse_output_rejects_schema_mismatch() -> None: + with pytest.raises(RuntimeError, match="json is not an InterfaceData"): + install_cli.assert_interface_parse_output( + 'json is not an InterfaceData [error_key=welcome] [json={"welcome":["a"]}]' + ) + with pytest.raises(RuntimeError): + install_cli.assert_interface_parse_output( + "[ERR] Failed to parse interface.json\n\nPress Enter to continue...\n" + ) + + +def test_assert_interface_parse_output_rejects_missing_prompt() -> None: + with pytest.raises(RuntimeError, match="controller prompt"): + install_cli.assert_interface_parse_output("Loading...\n") diff --git a/tools/ci/install_cli.py b/tools/ci/install_cli.py index 3e8e52c..0133c53 100644 --- a/tools/ci/install_cli.py +++ b/tools/ci/install_cli.py @@ -2,13 +2,49 @@ import argparse import json +import os +import platform as host_platform import re import shutil import subprocess import sys +import urllib.request +import zipfile from pathlib import Path from typing import Any, cast +MAA_PICLI_REPO = "MaaXYZ/MaaPiCli" + +# MaaPiCli release assets are named MAA-MaaPiCli---.zip. +MAA_PICLI_ASSET_OS = { + "win32": "windows", + "windows": "windows", + "linux": "linux", + "darwin": "macos", + "macos": "macos", + "osx": "macos", +} + +MAA_PICLI_ASSET_ARCH = { + "x86_64": "x86_64", + "amd64": "x86_64", + "aarch64": "aarch64", + "arm64": "aarch64", +} + +# MaaPiCli prints these when the interface tree does not match the ProjectInterface +# schema it was built against (a `welcome` array against a string-only field, say). +INTERFACE_PARSE_FAILURE_MARKERS = ( + "json is not an InterfaceData", + "Failed to parse interface.json", +) + +# The CLI resolves interface.json, every import and every option before asking for a +# controller, so the prompt is proof that the whole tree parsed. +CONTROLLER_PROMPT_MARKER = "Select controller" + +CLI_SMOKE_TIMEOUT_SECONDS = 60 + def strip_html_tags(text: str) -> str: text = re.sub(r"", "\n", text, flags=re.IGNORECASE) @@ -266,29 +302,158 @@ def install_manifest_cache(source_dir: Path, output_dir: Path) -> None: print("Warning: users will do full manifest check on first run.") -def rename_cli_binary(output_dir: Path) -> None: - """Rename the MaaPiCli entrypoint to M9A. +def cli_binary_name(platform_name: str) -> str: + return "M9A.exe" if platform_name.startswith("win") else "M9A" + + +def maapicli_binary_name(platform_name: str) -> str: + return "MaaPiCli.exe" if platform_name.startswith("win") else "MaaPiCli" + + +def normalize_arch(machine: str) -> str: + arch = MAA_PICLI_ASSET_ARCH.get(machine.strip().lower()) + if arch is None: + raise RuntimeError(f"Unsupported MaaPiCli architecture: {machine}") + return arch + + +def host_arch() -> str: + machine = host_platform.machine() + # Windows ARM64 shells can report AMD64 from platform.machine(). + if host_platform.system() == "Windows" and "ARM64" in os.environ.get("PROCESSOR_IDENTIFIER", ""): + machine = "aarch64" + return normalize_arch(machine) + + +def maapicli_asset_os(platform_name: str) -> str: + asset_os = MAA_PICLI_ASSET_OS.get(platform_name) + if asset_os is None: + raise RuntimeError(f"Unsupported MaaPiCli release platform: {platform_name}") + return asset_os + + +def maapicli_asset_name(platform_name: str, arch: str, tag: str) -> str: + return f"MAA-MaaPiCli-{maapicli_asset_os(platform_name)}-{normalize_arch(arch)}-{tag}.zip" + + +def github_headers(accept: str) -> dict[str, str]: + headers = {"Accept": accept, "User-Agent": "m9a-cli-packaging"} + token = os.environ.get("GH_TOKEN") or os.environ.get("GITHUB_TOKEN") + if token: + headers["Authorization"] = f"Bearer {token}" + return headers + + +def github_json(url: str) -> Any: + request = urllib.request.Request(url, headers=github_headers("application/vnd.github+json")) + with urllib.request.urlopen(request, timeout=60) as response: + return json.load(response) + + +def resolve_maapicli_tag(tag: str) -> str: + if tag: + return tag + release = github_json(f"https://api.github.com/repos/{MAA_PICLI_REPO}/releases/latest") + resolved = release.get("tag_name") if isinstance(release, dict) else None + if not isinstance(resolved, str) or not resolved: + raise RuntimeError(f"No MaaPiCli release found in {MAA_PICLI_REPO}") + return resolved + + +def maapicli_binary_path(root: Path, platform_name: str) -> Path: + name = maapicli_binary_name(platform_name) + for candidate in (root / "bin" / name, root / name): + if candidate.is_file(): + return candidate + raise FileNotFoundError(f"MaaPiCli binary not found under {root} (expected bin/{name})") - MaaPiCli is being split out to MaaXYZ/MaaPiCli and may be dropped from the - MAA-* runtime packages of a future MaaFramework release. A package without - the CLI entrypoint is unusable, so a missing binary must fail the build - here instead of shipping an entrypoint-less archive. + +def download_maapicli(maapicli_dir: Path, platform_name: str, arch: str, tag: str) -> Path: + resolved_tag = resolve_maapicli_tag(tag) + asset = maapicli_asset_name(platform_name, arch, resolved_tag) + url = f"https://github.com/{MAA_PICLI_REPO}/releases/download/{resolved_tag}/{asset}" + maapicli_dir.mkdir(parents=True, exist_ok=True) + archive = maapicli_dir / asset + + print(f"Downloading MaaPiCli {resolved_tag}: {url}") + # Public release assets, and urllib forwards custom headers across the redirect to the + # object host, so the token is only used for the API lookup above. + request = urllib.request.Request(url, headers={"User-Agent": "m9a-cli-packaging"}) + with urllib.request.urlopen(request, timeout=300) as response, open(archive, "wb") as out_file: + shutil.copyfileobj(response, out_file) + with zipfile.ZipFile(archive) as zip_file: + zip_file.extractall(maapicli_dir) + archive.unlink() + + framework_version = maapicli_dir / "maafw-version.txt" + if framework_version.is_file(): + print(f"MaaPiCli {resolved_tag} targets {framework_version.read_text(encoding='utf-8').strip()}") + return maapicli_binary_path(maapicli_dir, platform_name) + + +def install_maapicli(output_dir: Path, maapicli_dir: Path, platform_name: str) -> Path: + """Ship the MaaPiCli entrypoint from MaaXYZ/MaaPiCli instead of the MaaFramework bundle. + + MaaFramework is moving MaaPiCli out of its MAA-* runtime packages, and the copy it still + bundles lags the ProjectInterface revisions: MaaFramework v5.15.0-beta.1 deserializes + `welcome` as a single string, so an interface.json using the ordered array of PI 2.10 + fails to load. The MaaPiCli releases carry the current parser, so the entrypoint is + taken from them and any copy left in the runtime bundle is dropped. """ - candidates = [ - (output_dir / "MaaPiCli.exe", output_dir / "M9A.exe"), - (output_dir / "MaaPiCli", output_dir / "M9A"), - ] - for source, target in candidates: - if source.exists(): - if target.exists(): - target.unlink() - source.rename(target) - return - raise RuntimeError( - f"MaaPiCli binary not found in the package; the MaaFramework runtime bundle " - f"at {output_dir} no longer ships one. Fetch MaaPiCli from MaaXYZ/MaaPiCli " - f"(asset prefix MAA-MaaPiCli-, os segment windows/linux/macos) instead." - ) + binary = maapicli_binary_path(maapicli_dir, platform_name) + target = output_dir / cli_binary_name(platform_name) + for stale in (target, output_dir / maapicli_binary_name(platform_name)): + if stale.exists(): + stale.unlink() + + shutil.copy2(binary, target) + if not platform_name.startswith("win"): + target.chmod(0o755) + print(f"CLI entrypoint: {target} (from {binary})") + return target + + +def assert_interface_parse_output(output: str) -> None: + for marker in INTERFACE_PARSE_FAILURE_MARKERS: + if marker in output: + raise RuntimeError( + f"Packaged CLI cannot parse the packaged interface.json ({marker}):\n{output[-4000:]}" + ) + if CONTROLLER_PROMPT_MARKER not in output: + raise RuntimeError( + "Packaged CLI never reached the controller prompt, so interface.json did not " + f"fully resolve:\n{output[-4000:]}" + ) + + +def smoke_interface_parse(output_dir: Path, platform_name: str) -> None: + """Run the packaged CLI once so a package the CLI cannot load never ships. + + Reads stdin from /dev/null so the controller prompt does not block, and accepts a + timeout only when the prompt already appeared in the captured output. + """ + binary = output_dir / cli_binary_name(platform_name) + if not binary.is_file(): + raise FileNotFoundError(f"CLI entrypoint not found: {binary}") + + try: + result = subprocess.run( + [str(binary)], + cwd=output_dir, + stdin=subprocess.DEVNULL, + capture_output=True, + timeout=CLI_SMOKE_TIMEOUT_SECONDS, + check=False, + ) + output = (result.stdout + result.stderr).decode("utf-8", "replace") + except subprocess.TimeoutExpired as exc: + partial = (exc.stdout or b"") + (exc.stderr or b"") + assert_interface_parse_output(partial.decode("utf-8", "replace")) + print("Packaged CLI interface smoke passed (timed out at the controller prompt).") + return + + assert_interface_parse_output(output) + print("Packaged CLI interface smoke passed.") def parse_args() -> argparse.Namespace: @@ -300,6 +465,17 @@ def parse_args() -> argparse.Namespace: parser.add_argument("--python-dir", type=Path, default=root / "install" / "python") parser.add_argument("--version", default="v0.0.1") parser.add_argument("--platform", default=sys.platform) + parser.add_argument( + "--arch", + default=host_arch(), + help="Release architecture (x86_64 or aarch64). Defaults to the host.", + ) + parser.add_argument("--maapicli-dir", type=Path, default=root / "install" / "maapicli") + parser.add_argument( + "--maapicli-tag", + default="", + help="Exact MaaXYZ/MaaPiCli release tag. Empty resolves the latest release.", + ) return parser.parse_args() @@ -308,6 +484,7 @@ def main() -> None: source_dir = args.source_dir.resolve() deps_dir = args.deps_dir.resolve() output_dir = args.output_dir.resolve() + maapicli_dir = args.maapicli_dir.resolve() if output_dir.exists(): shutil.rmtree(output_dir) @@ -320,7 +497,9 @@ def main() -> None: install_chores(source_dir, output_dir) install_agent(source_dir, output_dir, args.platform) install_manifest_cache(source_dir, output_dir) - rename_cli_binary(output_dir) + download_maapicli(maapicli_dir, args.platform, args.arch, args.maapicli_tag) + install_maapicli(output_dir, maapicli_dir, args.platform) + smoke_interface_parse(output_dir, args.platform) smoke_packaged_agent(args.python_dir.resolve(), output_dir, args.platform) print(f"Install to {output_dir} successfully.") From cae19b73d07a13165f12fef7ca3fefdd9d1b188d Mon Sep 17 00:00:00 2001 From: Windsland52 <86581225+Windsland52@users.noreply.github.com> Date: Sun, 1 Nov 2026 16:19:07 +0800 Subject: [PATCH 2/2] fix(release): harden the MaaPiCli entrypoint build Review follow-ups on the entrypoint change: - Clear the MaaPiCli download directory before extracting, so a tag bumped between runs cannot leave assets from the previous tag behind. - Run the packaged CLI smoke with GH_TOKEN/GITHUB_TOKEN removed from the child environment: the binary comes from another repository and has no business seeing the release token. - Keep workflow permissions read-only by default and grant `contents: write` only to the release job that publishes; the build jobs only upload artifacts. - Pass `maapicli_tag` to the build step through the environment instead of interpolating the expression straight into the shell command. --- .github/workflows/release.yml | 13 +++++++++---- tests/test_install_cli.py | 12 ++++++++++++ tools/ci/install_cli.py | 14 +++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index abffcfd..f9231a4 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -44,7 +44,7 @@ on: default: "" permissions: - contents: write + contents: read jobs: meta: @@ -224,8 +224,9 @@ jobs: env: PYTHONPATH: M9A/tools/ci GH_TOKEN: ${{ github.token }} + MAAPICLI_TAG: ${{ needs.meta.outputs.maapicli_tag }} run: | - ./install/python/python.exe tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform win32 --arch ${{ matrix.arch }} --maapicli-tag "${{ needs.meta.outputs.maapicli_tag }}" + ./install/python/python.exe tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform win32 --arch ${{ matrix.arch }} --maapicli-tag "$MAAPICLI_TAG" cp -rpvn install/python/. install-cli/python/ rm -rf install-cli/deps 2>/dev/null || true @@ -313,8 +314,9 @@ jobs: env: PYTHONPATH: M9A/tools/ci GH_TOKEN: ${{ github.token }} + MAAPICLI_TAG: ${{ needs.meta.outputs.maapicli_tag }} run: | - python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform linux --arch ${{ matrix.arch }} --maapicli-tag "${{ needs.meta.outputs.maapicli_tag }}" + python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform linux --arch ${{ matrix.arch }} --maapicli-tag "$MAAPICLI_TAG" cp -rpvn install/python/. install-cli/python/ rm -rf install-cli/deps 2>/dev/null || true if [ -f "install-cli/M9A" ]; then chmod +x install-cli/M9A; fi @@ -402,8 +404,9 @@ jobs: env: PYTHONPATH: M9A/tools/ci GH_TOKEN: ${{ github.token }} + MAAPICLI_TAG: ${{ needs.meta.outputs.maapicli_tag }} run: | - ./install/python/bin/python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform darwin --arch ${{ matrix.arch }} --maapicli-tag "${{ needs.meta.outputs.maapicli_tag }}" + ./install/python/bin/python3 tools/ci/install_cli.py --source-dir M9A --deps-dir deps --output-dir install-cli --version "${{ needs.meta.outputs.tag }}" --platform darwin --arch ${{ matrix.arch }} --maapicli-tag "$MAAPICLI_TAG" cp -rpvn install/python/. install-cli/python/ if [ -f "install-cli/M9A" ]; then chmod +x install-cli/M9A; fi rm -rf install-cli/deps 2>/dev/null || true @@ -417,6 +420,8 @@ jobs: if: ${{ needs.meta.outputs.tag != 'v0.0.0-local' }} needs: [meta, windows, linux, macos] runs-on: ubuntu-latest + permissions: + contents: write steps: - uses: actions/download-artifact@v8 with: diff --git a/tests/test_install_cli.py b/tests/test_install_cli.py index 1daff55..c270317 100644 --- a/tests/test_install_cli.py +++ b/tests/test_install_cli.py @@ -86,6 +86,18 @@ def test_install_maapicli_replaces_bundled_binary(tmp_path: Path) -> None: assert not (output_dir / "MaaPiCli.exe").exists() +def test_subprocess_env_drops_release_credentials(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("GH_TOKEN", "secret") + monkeypatch.setenv("GITHUB_TOKEN", "secret") + monkeypatch.setenv("PATH", "/usr/bin") + + env = install_cli.subprocess_env_without_credentials() + + assert "GH_TOKEN" not in env + assert "GITHUB_TOKEN" not in env + assert env["PATH"] == "/usr/bin" + + def test_assert_interface_parse_output_accepts_controller_prompt() -> None: install_cli.assert_interface_parse_output("### Select controller ###\n\n\t1. Controller.Adb\n") diff --git a/tools/ci/install_cli.py b/tools/ci/install_cli.py index 0133c53..e5bc140 100644 --- a/tools/ci/install_cli.py +++ b/tools/ci/install_cli.py @@ -45,6 +45,10 @@ CLI_SMOKE_TIMEOUT_SECONDS = 60 +# The packaged CLI comes from another repository, so it must not inherit the release token +# that the build step needs for the GitHub API and the release upload. +CREDENTIAL_ENV_VARS = ("GH_TOKEN", "GITHUB_TOKEN") + def strip_html_tags(text: str) -> str: text = re.sub(r"", "\n", text, flags=re.IGNORECASE) @@ -372,7 +376,10 @@ def download_maapicli(maapicli_dir: Path, platform_name: str, arch: str, tag: st resolved_tag = resolve_maapicli_tag(tag) asset = maapicli_asset_name(platform_name, arch, resolved_tag) url = f"https://github.com/{MAA_PICLI_REPO}/releases/download/{resolved_tag}/{asset}" - maapicli_dir.mkdir(parents=True, exist_ok=True) + # Start from an empty directory so a previous tag cannot leave assets behind. + if maapicli_dir.exists(): + shutil.rmtree(maapicli_dir) + maapicli_dir.mkdir(parents=True) archive = maapicli_dir / asset print(f"Downloading MaaPiCli {resolved_tag}: {url}") @@ -426,6 +433,10 @@ def assert_interface_parse_output(output: str) -> None: ) +def subprocess_env_without_credentials() -> dict[str, str]: + return {key: value for key, value in os.environ.items() if key not in CREDENTIAL_ENV_VARS} + + def smoke_interface_parse(output_dir: Path, platform_name: str) -> None: """Run the packaged CLI once so a package the CLI cannot load never ships. @@ -444,6 +455,7 @@ def smoke_interface_parse(output_dir: Path, platform_name: str) -> None: capture_output=True, timeout=CLI_SMOKE_TIMEOUT_SECONDS, check=False, + env=subprocess_env_without_credentials(), ) output = (result.stdout + result.stderr).decode("utf-8", "replace") except subprocess.TimeoutExpired as exc: