Copilot commented on code in PR #63901: URL: https://github.com/apache/airflow/pull/63901#discussion_r3025333854
########## dev/breeze/tests/test_reproduce_ci.py: ########## @@ -0,0 +1,401 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +from __future__ import annotations + +from unittest import mock + +import click +import click.testing +import pytest + +from airflow_breeze.params.build_ci_params import BuildCiParams +from airflow_breeze.utils.reproduce_ci import ( + ReproductionCommand, + build_local_reproduction_commands, + build_reproduction_command_from_context, + print_local_reproduction, + should_print_local_reproduction, +) + + [email protected]( + ("env_vars", "expected"), + [ + ({"CI": "true", "GITHUB_ACTIONS": "true"}, True), + ({"CI": "true", "GITHUB_ACTIONS": "false"}, False), + ({"CI": "false", "GITHUB_ACTIONS": "true"}, False), + ({}, False), + ], +) +def test_should_print_local_reproduction_only_in_github_actions(env_vars, expected, monkeypatch): + monkeypatch.delenv("CI", raising=False) + monkeypatch.delenv("GITHUB_ACTIONS", raising=False) + for key, value in env_vars.items(): + monkeypatch.setenv(key, value) + assert should_print_local_reproduction() is expected + + +def test_build_local_reproduction_commands_builds_ci_image_locally(monkeypatch): + monkeypatch.delenv("GITHUB_REF", raising=False) + monkeypatch.setenv("GITHUB_SHA", "abc123") + monkeypatch.setenv("GITHUB_RUN_ID", "98765") + build_params = BuildCiParams( + github_repository="someone/airflow", + platform="linux/arm64", + python="3.11", + ) + + commands = build_local_reproduction_commands( + command_params=build_params, + main_command=ReproductionCommand(argv=["breeze", "build-docs", "--docs-only"]), + ) + + assert [command.comment for command in commands] == [ + "Check out the same commit", + "Build the CI image locally", + None, + ] + assert commands[0].argv == ["git", "checkout", "abc123"] + assert commands[1].argv == [ + "breeze", + "ci-image", + "build", + "--github-repository", + "someone/airflow", + "--platform", + "linux/arm64", + "--python", + "3.11", + ] + + [email protected]("pr_ref_kind", ["merge", "head"]) +def test_build_local_reproduction_commands_fetches_pull_request_ref(pr_ref_kind, monkeypatch): + github_ref = f"refs/pull/42/{pr_ref_kind}" + monkeypatch.setenv("GITHUB_REF", github_ref) + monkeypatch.setenv("GITHUB_SHA", "merge-sha") + monkeypatch.setenv("GITHUB_RUN_ID", "98765") + build_params = BuildCiParams( + github_repository="someone/airflow", + platform="linux/amd64", + python="3.10", + ) + + commands = build_local_reproduction_commands( + command_params=build_params, + main_command=ReproductionCommand(argv=["breeze", "build-docs", "--docs-only"]), + ) + + assert [command.comment for command in commands] == [ + f"Check out the same code as CI (pull request {pr_ref_kind} ref)", + None, + "Build the CI image locally", + None, + ] + assert commands[0].argv == [ + "git", + "fetch", + "https://github.com/someone/airflow.git", + github_ref, + ] + assert commands[1].argv == ["git", "checkout", "FETCH_HEAD"] + + +def test_build_local_reproduction_commands_builds_ci_image_for_default_repo(monkeypatch): + monkeypatch.delenv("GITHUB_RUN_ID", raising=False) + monkeypatch.delenv("GITHUB_REF", raising=False) + monkeypatch.setenv("GITHUB_SHA", "def456") + build_params = BuildCiParams(platform="linux/amd64", python="3.10") + + commands = build_local_reproduction_commands( + command_params=build_params, + main_command=ReproductionCommand(argv=["breeze", "build-docs", "--docs-only"]), + ) + + assert commands[1].argv == [ + "breeze", + "ci-image", + "build", + "--platform", + "linux/amd64", + "--python", + "3.10", + ] + + [email protected]("airflow_breeze.utils.reproduce_ci.get_console", autospec=True) +def test_print_local_reproduction_renders_copyable_commands(mock_get_console, monkeypatch): + monkeypatch.setenv("CI", "true") + monkeypatch.setenv("GITHUB_ACTIONS", "true") + + print_local_reproduction( + [ + ReproductionCommand(argv=["git", "checkout", "abc123"], comment="Check out the same commit"), + ReproductionCommand( + argv=["breeze", "build-docs", "--docs-only"], + comment="Run the same Breeze command locally", + ), + ] + ) + + assert mock_get_console.return_value.print.call_count == 2 + rendered_output = mock_get_console.return_value.print.call_args_list[1].args[0] + assert "# 1. Check out the same commit" in rendered_output + assert "git checkout abc123" in rendered_output + assert "breeze build-docs --docs-only" in rendered_output + + +# --------------------------------------------------------------------------- +# Tests for build_reproduction_command_from_context +# --------------------------------------------------------------------------- + + +def _build_test_command(**options): + """Build a simple click command with the given options for testing.""" + + @click.command("test-cmd") + def cmd(**kwargs): + pass + + for _name, opt in options.items(): + cmd = opt(cmd) + return cmd + + +def _invoke_and_get_context(cmd, args, env=None): + """Invoke a click command and capture the context.""" + captured_ctx = {} + + original_invoke = cmd.invoke + + def patched_invoke(ctx): + captured_ctx["ctx"] = ctx + return original_invoke(ctx) + + cmd.invoke = patched_invoke + runner = click.testing.CliRunner(env=env or {}) + result = runner.invoke(cmd, args, catch_exceptions=False) + assert result.exit_code == 0, result.output + return captured_ctx["ctx"] + + +class TestBuildReproductionCommandFromContext: + """Tests for the generic Click context-based command renderer.""" + + def test_simple_bool_flag_emitted_when_true(self): + @click.command("my-cmd") + @click.option("--verbose-output", is_flag=True, default=False) + def cmd(**kwargs): + pass + + ctx = _invoke_and_get_context(cmd, ["--verbose-output"]) + result = build_reproduction_command_from_context(ctx) + assert result.argv == ["my-cmd", "--verbose-output"] + + def test_simple_bool_flag_omitted_when_default(self): + @click.command("my-cmd") + @click.option("--verbose-output", is_flag=True, default=False) + def cmd(**kwargs): + pass + + ctx = _invoke_and_get_context(cmd, []) + result = build_reproduction_command_from_context(ctx) + assert result.argv == ["my-cmd"] + + def test_flag_pair_emits_positive_side(self): + @click.command("my-cmd") + @click.option("--force/--no-force", default=False) + def cmd(**kwargs): + pass + + ctx = _invoke_and_get_context(cmd, ["--force"]) + result = build_reproduction_command_from_context(ctx) + assert "--force" in result.argv + assert "--no-force" not in result.argv Review Comment: There’s no test asserting that flag pairs with both short and long aliases (e.g. `"-f", "--force/--no-force"`) render using the preferred long form. Adding a focused test here would prevent regressions once the flag-pair rendering is updated to consistently choose the long option. ########## dev/breeze/src/airflow_breeze/utils/reproduce_ci.py: ########## @@ -0,0 +1,221 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Helpers for printing local reproduction instructions in CI logs.""" + +from __future__ import annotations + +import os +import shlex +from dataclasses import dataclass +from typing import TYPE_CHECKING + +import click +from click.core import ParameterSource +from rich.markup import escape + +from airflow_breeze.global_constants import APACHE_AIRFLOW_GITHUB_REPOSITORY +from airflow_breeze.utils.console import get_console +from airflow_breeze.utils.run_utils import commit_sha + +if TYPE_CHECKING: + from airflow_breeze.params.build_ci_params import BuildCiParams + from airflow_breeze.params.shell_params import ShellParams + +# Options that are side-effect-only or not meaningful for reproduction (safety net; +# expose_value=False options like --verbose/--dry-run/--answer are already excluded +# automatically because they don't appear in ctx.params). +_EXCLUDED_PARAMS: frozenset[str] = frozenset( + { + "verbose", + "dry_run", + "answer", + "include_success_outputs", + "debug_resources", + "skip_cleanup", + } +) + +# These sources represent values explicitly provided by the user or CI. +_EXPLICIT_SOURCES: frozenset[ParameterSource] = frozenset( + { + ParameterSource.COMMANDLINE, + ParameterSource.ENVIRONMENT, + ParameterSource.PROMPT, + } +) + + +@dataclass +class ReproductionCommand: + argv: list[str] + comment: str | None = None + + +def build_reproduction_command_from_context( + ctx: click.Context, + *, + comment: str = "Run the same Breeze command locally", +) -> ReproductionCommand: + """Reconstruct the CLI invocation from the current click Context. + + Iterates over every parameter defined on the command, uses + ``ctx.get_parameter_source()`` to identify explicitly-provided values + (COMMANDLINE / ENVIRONMENT / PROMPT), and emits only those. DEFAULT + and DEFAULT_MAP values are omitted to keep the output concise. + + This removes the need for per-command builder functions. + """ + argv: list[str] = ctx.command_path.split() + + for param in ctx.command.params: + if not getattr(param, "expose_value", True): + continue + if param.name is None or param.name in _EXCLUDED_PARAMS: + continue + + value = ctx.params.get(param.name) + source = ctx.get_parameter_source(param.name) + + if isinstance(param, click.Argument): + continue # collected after options + + if not isinstance(param, click.Option): + continue + + # Flag pair (e.g. --force-sa-warnings/--no-force-sa-warnings): + # emit the appropriate side only when explicitly provided. + if param.is_flag and param.secondary_opts: + if source in _EXPLICIT_SOURCES: + flag = param.opts[0] if value else param.secondary_opts[0] Review Comment: `build_reproduction_command_from_context()` claims to “prefer long form” later (`param.opts[-1]`), but flag pairs currently emit `param.opts[0]` / `secondary_opts[0]`, which will pick the short alias when both are present (e.g. `-f` instead of `--force`). To keep output consistent and copy/paste friendly, select the long form for both sides (e.g. `param.opts[-1]` and `param.secondary_opts[-1]`). ```suggestion # Prefer long-form alias for both sides of the flag pair. flag = param.opts[-1] if value else param.secondary_opts[-1] ``` ########## dev/breeze/src/airflow_breeze/utils/reproduce_ci.py: ########## @@ -0,0 +1,221 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Helpers for printing local reproduction instructions in CI logs.""" + +from __future__ import annotations + +import os +import shlex +from dataclasses import dataclass +from typing import TYPE_CHECKING + +import click +from click.core import ParameterSource +from rich.markup import escape + +from airflow_breeze.global_constants import APACHE_AIRFLOW_GITHUB_REPOSITORY +from airflow_breeze.utils.console import get_console +from airflow_breeze.utils.run_utils import commit_sha + +if TYPE_CHECKING: + from airflow_breeze.params.build_ci_params import BuildCiParams + from airflow_breeze.params.shell_params import ShellParams + +# Options that are side-effect-only or not meaningful for reproduction (safety net; +# expose_value=False options like --verbose/--dry-run/--answer are already excluded +# automatically because they don't appear in ctx.params). +_EXCLUDED_PARAMS: frozenset[str] = frozenset( + { + "verbose", + "dry_run", + "answer", + "include_success_outputs", + "debug_resources", + "skip_cleanup", + } +) + +# These sources represent values explicitly provided by the user or CI. +_EXPLICIT_SOURCES: frozenset[ParameterSource] = frozenset( + { + ParameterSource.COMMANDLINE, + ParameterSource.ENVIRONMENT, + ParameterSource.PROMPT, + } +) + + +@dataclass +class ReproductionCommand: + argv: list[str] + comment: str | None = None + + +def build_reproduction_command_from_context( + ctx: click.Context, + *, + comment: str = "Run the same Breeze command locally", +) -> ReproductionCommand: + """Reconstruct the CLI invocation from the current click Context. + + Iterates over every parameter defined on the command, uses + ``ctx.get_parameter_source()`` to identify explicitly-provided values + (COMMANDLINE / ENVIRONMENT / PROMPT), and emits only those. DEFAULT + and DEFAULT_MAP values are omitted to keep the output concise. + + This removes the need for per-command builder functions. + """ + argv: list[str] = ctx.command_path.split() + + for param in ctx.command.params: + if not getattr(param, "expose_value", True): + continue + if param.name is None or param.name in _EXCLUDED_PARAMS: + continue + + value = ctx.params.get(param.name) + source = ctx.get_parameter_source(param.name) + + if isinstance(param, click.Argument): + continue # collected after options + + if not isinstance(param, click.Option): + continue + + # Flag pair (e.g. --force-sa-warnings/--no-force-sa-warnings): + # emit the appropriate side only when explicitly provided. + if param.is_flag and param.secondary_opts: + if source in _EXPLICIT_SOURCES: + flag = param.opts[0] if value else param.secondary_opts[0] + argv.append(flag) + continue + + # Simple boolean flag (no secondary_opts) + if param.is_flag: + if value and source in _EXPLICIT_SOURCES: + argv.append(param.opts[-1]) + continue + + # Non-flag option: only emit explicitly-provided values + if source not in _EXPLICIT_SOURCES: + continue + if value is None: + continue + + flag = param.opts[-1] # prefer long form + + # Multiple option (e.g. --package-filter repeated) + if param.multiple: + for item in value: + argv.extend([flag, str(item)]) + continue + + argv.extend([flag, str(value)]) + + # Append positional arguments at the end + for param in ctx.command.params: + if isinstance(param, click.Argument) and param.name is not None: + value = ctx.params.get(param.name) + if value: + if isinstance(value, (list, tuple)): + argv.extend(str(v) for v in value) + else: + argv.append(str(value)) + + return ReproductionCommand(argv=argv, comment=comment) + + +def build_checkout_reproduction_commands(github_repository: str) -> list[ReproductionCommand]: + """Build git commands needed to reproduce the current CI checkout locally.""" + github_ref = os.environ.get("GITHUB_REF", "") + github_ref_parts = github_ref.split("/") + if len(github_ref_parts) == 4 and github_ref_parts[:2] == ["refs", "pull"]: + pull_request_ref_kind = github_ref_parts[3] + return [ + ReproductionCommand( + argv=[ + "git", + "fetch", + f"https://github.com/{github_repository}.git", + github_ref, + ], + comment=f"Check out the same code as CI (pull request {pull_request_ref_kind} ref)", + ), + ReproductionCommand( + argv=["git", "checkout", "FETCH_HEAD"], + ), + ] Review Comment: For PR runs, checking out `FETCH_HEAD` after fetching `refs/pull/...` can drift over time because the ref can move as new commits are pushed, so the printed instructions may not reproduce the exact code that the failing run used. Since GitHub Actions provides `GITHUB_SHA`, prefer checking out that SHA (optionally still doing the fetch first to ensure the SHA is present) to make the reproduction instructions stable. ########## dev/breeze/src/airflow_breeze/utils/reproduce_ci.py: ########## @@ -0,0 +1,221 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Helpers for printing local reproduction instructions in CI logs.""" + +from __future__ import annotations + +import os +import shlex +from dataclasses import dataclass +from typing import TYPE_CHECKING + +import click +from click.core import ParameterSource +from rich.markup import escape + +from airflow_breeze.global_constants import APACHE_AIRFLOW_GITHUB_REPOSITORY +from airflow_breeze.utils.console import get_console +from airflow_breeze.utils.run_utils import commit_sha + +if TYPE_CHECKING: + from airflow_breeze.params.build_ci_params import BuildCiParams + from airflow_breeze.params.shell_params import ShellParams + +# Options that are side-effect-only or not meaningful for reproduction (safety net; +# expose_value=False options like --verbose/--dry-run/--answer are already excluded +# automatically because they don't appear in ctx.params). +_EXCLUDED_PARAMS: frozenset[str] = frozenset( + { + "verbose", + "dry_run", + "answer", + "include_success_outputs", + "debug_resources", + "skip_cleanup", + } +) + +# These sources represent values explicitly provided by the user or CI. +_EXPLICIT_SOURCES: frozenset[ParameterSource] = frozenset( + { + ParameterSource.COMMANDLINE, + ParameterSource.ENVIRONMENT, + ParameterSource.PROMPT, + } +) + + +@dataclass +class ReproductionCommand: + argv: list[str] + comment: str | None = None + + +def build_reproduction_command_from_context( + ctx: click.Context, + *, + comment: str = "Run the same Breeze command locally", +) -> ReproductionCommand: + """Reconstruct the CLI invocation from the current click Context. + + Iterates over every parameter defined on the command, uses + ``ctx.get_parameter_source()`` to identify explicitly-provided values + (COMMANDLINE / ENVIRONMENT / PROMPT), and emits only those. DEFAULT + and DEFAULT_MAP values are omitted to keep the output concise. + + This removes the need for per-command builder functions. + """ + argv: list[str] = ctx.command_path.split() + + for param in ctx.command.params: + if not getattr(param, "expose_value", True): + continue + if param.name is None or param.name in _EXCLUDED_PARAMS: + continue + + value = ctx.params.get(param.name) + source = ctx.get_parameter_source(param.name) + + if isinstance(param, click.Argument): + continue # collected after options + + if not isinstance(param, click.Option): + continue + + # Flag pair (e.g. --force-sa-warnings/--no-force-sa-warnings): + # emit the appropriate side only when explicitly provided. + if param.is_flag and param.secondary_opts: + if source in _EXPLICIT_SOURCES: + flag = param.opts[0] if value else param.secondary_opts[0] + argv.append(flag) + continue + + # Simple boolean flag (no secondary_opts) + if param.is_flag: + if value and source in _EXPLICIT_SOURCES: + argv.append(param.opts[-1]) + continue + + # Non-flag option: only emit explicitly-provided values + if source not in _EXPLICIT_SOURCES: + continue + if value is None: + continue + + flag = param.opts[-1] # prefer long form + + # Multiple option (e.g. --package-filter repeated) + if param.multiple: + for item in value: + argv.extend([flag, str(item)]) + continue + + argv.extend([flag, str(value)]) Review Comment: For Click options with `nargs > 1`, `ctx.params[param.name]` is a tuple, and the current logic will render it as a Python tuple string (e.g. `--opt ('a', 'b')`) rather than separate CLI arguments (`--opt a b`). This produces a reproduction command that won’t round-trip. Adjust rendering to detect tuple/list values for non-`multiple` options (and/or check `param.nargs`) and extend `argv` with each element as its own argument. ```suggestion # Non-multiple option. For options with nargs > 1, Click provides a tuple/list # here, so we should expand each element as its own CLI argument instead of # stringifying the whole collection. if isinstance(value, (list, tuple)): argv.append(flag) argv.extend(str(v) for v in value) else: argv.extend([flag, str(value)]) ``` ########## dev/breeze/src/airflow_breeze/utils/reproduce_ci.py: ########## @@ -0,0 +1,221 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. +"""Helpers for printing local reproduction instructions in CI logs.""" + +from __future__ import annotations + +import os +import shlex +from dataclasses import dataclass +from typing import TYPE_CHECKING + +import click +from click.core import ParameterSource +from rich.markup import escape + +from airflow_breeze.global_constants import APACHE_AIRFLOW_GITHUB_REPOSITORY +from airflow_breeze.utils.console import get_console +from airflow_breeze.utils.run_utils import commit_sha + +if TYPE_CHECKING: + from airflow_breeze.params.build_ci_params import BuildCiParams + from airflow_breeze.params.shell_params import ShellParams + +# Options that are side-effect-only or not meaningful for reproduction (safety net; +# expose_value=False options like --verbose/--dry-run/--answer are already excluded +# automatically because they don't appear in ctx.params). +_EXCLUDED_PARAMS: frozenset[str] = frozenset( + { + "verbose", + "dry_run", + "answer", + "include_success_outputs", + "debug_resources", + "skip_cleanup", + } +) + +# These sources represent values explicitly provided by the user or CI. +_EXPLICIT_SOURCES: frozenset[ParameterSource] = frozenset( + { + ParameterSource.COMMANDLINE, + ParameterSource.ENVIRONMENT, + ParameterSource.PROMPT, + } +) + + +@dataclass +class ReproductionCommand: + argv: list[str] + comment: str | None = None + + +def build_reproduction_command_from_context( + ctx: click.Context, + *, + comment: str = "Run the same Breeze command locally", +) -> ReproductionCommand: + """Reconstruct the CLI invocation from the current click Context. + + Iterates over every parameter defined on the command, uses + ``ctx.get_parameter_source()`` to identify explicitly-provided values + (COMMANDLINE / ENVIRONMENT / PROMPT), and emits only those. DEFAULT + and DEFAULT_MAP values are omitted to keep the output concise. + + This removes the need for per-command builder functions. + """ + argv: list[str] = ctx.command_path.split() + + for param in ctx.command.params: + if not getattr(param, "expose_value", True): + continue + if param.name is None or param.name in _EXCLUDED_PARAMS: + continue + + value = ctx.params.get(param.name) + source = ctx.get_parameter_source(param.name) + + if isinstance(param, click.Argument): + continue # collected after options + + if not isinstance(param, click.Option): + continue + + # Flag pair (e.g. --force-sa-warnings/--no-force-sa-warnings): + # emit the appropriate side only when explicitly provided. + if param.is_flag and param.secondary_opts: + if source in _EXPLICIT_SOURCES: + flag = param.opts[0] if value else param.secondary_opts[0] + argv.append(flag) + continue + + # Simple boolean flag (no secondary_opts) + if param.is_flag: + if value and source in _EXPLICIT_SOURCES: + argv.append(param.opts[-1]) + continue + + # Non-flag option: only emit explicitly-provided values + if source not in _EXPLICIT_SOURCES: + continue + if value is None: + continue + + flag = param.opts[-1] # prefer long form + + # Multiple option (e.g. --package-filter repeated) + if param.multiple: + for item in value: + argv.extend([flag, str(item)]) + continue + + argv.extend([flag, str(value)]) + + # Append positional arguments at the end + for param in ctx.command.params: + if isinstance(param, click.Argument) and param.name is not None: + value = ctx.params.get(param.name) + if value: + if isinstance(value, (list, tuple)): + argv.extend(str(v) for v in value) + else: + argv.append(str(value)) + + return ReproductionCommand(argv=argv, comment=comment) + + +def build_checkout_reproduction_commands(github_repository: str) -> list[ReproductionCommand]: + """Build git commands needed to reproduce the current CI checkout locally.""" + github_ref = os.environ.get("GITHUB_REF", "") + github_ref_parts = github_ref.split("/") + if len(github_ref_parts) == 4 and github_ref_parts[:2] == ["refs", "pull"]: + pull_request_ref_kind = github_ref_parts[3] + return [ + ReproductionCommand( + argv=[ + "git", + "fetch", + f"https://github.com/{github_repository}.git", + github_ref, + ], + comment=f"Check out the same code as CI (pull request {pull_request_ref_kind} ref)", + ), + ReproductionCommand( + argv=["git", "checkout", "FETCH_HEAD"], + ), + ] + + current_commit_sha = os.environ.get("GITHUB_SHA") or os.environ.get("COMMIT_SHA") or commit_sha() + if not current_commit_sha or current_commit_sha == "COMMIT_SHA_NOT_FOUND": + return [] + return [ + ReproductionCommand( + argv=["git", "checkout", current_commit_sha], + comment="Check out the same commit", + ) + ] + + +def build_ci_image_reproduction_command(command_params: ShellParams | BuildCiParams) -> ReproductionCommand: + """Build the CI image preparation command for local reproduction.""" + # Current CI jobs restore images from stash keys rather than GitHub Actions artifacts, + # so building locally is the reliable reproduction path. + command = ["breeze", "ci-image", "build"] + if command_params.github_repository != APACHE_AIRFLOW_GITHUB_REPOSITORY: + command.extend(["--github-repository", command_params.github_repository]) + command.extend(["--platform", command_params.platform, "--python", command_params.python]) + return ReproductionCommand( + argv=command, + comment="Build the CI image locally", + ) + + +def build_local_reproduction_commands( + command_params: ShellParams | BuildCiParams, + main_command: ReproductionCommand, +) -> list[ReproductionCommand]: + """Build the ordered list of local reproduction commands.""" + commands = build_checkout_reproduction_commands(command_params.github_repository) + commands.append(build_ci_image_reproduction_command(command_params)) + commands.append(main_command) + return commands + + +def should_print_local_reproduction() -> bool: + """Return True when local reproduction instructions should be printed.""" + return ( + os.environ.get("CI", "").lower() == "true" and os.environ.get("GITHUB_ACTIONS", "").lower() == "true" + ) + + +def print_local_reproduction(commands: list[ReproductionCommand]) -> None: + """Print local reproduction commands in CI logs.""" + if not should_print_local_reproduction() or not commands: + return + lines: list[str] = [] + step_number = 0 + for command in commands: + if command.comment: + if lines: + lines.append("") + step_number += 1 + lines.append(f"# {step_number}. {command.comment}") + lines.append(shlex.join(command.argv)) + rendered = "\n".join(lines) + get_console().print("\n[warning]HOW TO REPRODUCE LOCALLY[/]\n") + get_console().print(f"[info]{escape(rendered)}[/]\n", soft_wrap=True) Review Comment: `get_console()` is called twice in succession; if it ever becomes non-trivial (or returns non-identical instances), this could be inefficient or inconsistent. Store it in a local variable once and reuse it for both prints. ```suggestion console = get_console() console.print("\n[warning]HOW TO REPRODUCE LOCALLY[/]\n") console.print(f"[info]{escape(rendered)}[/]\n", soft_wrap=True) ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
