Skip to content
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

Bugfix: Invalid values for grouped arguments via environment variables are not handled #239

Merged
merged 4 commits into from
Jun 6, 2022

Conversation

priitlatt
Copy link
Contributor

@priitlatt priitlatt commented Jun 6, 2022

Grouped CLI arguments that can be also defined via environment variables (not just CLI switches) can cause unexpected exceptions when invalid values are provided as environment variable value.

For example action app-store-connect publish has argument group "Apple's altool configuration options" that contains switch --altool-retries, which is supposed to be a positive integer. Its value can also be specified by environment variable APP_STORE_CONNECT_ALTOOL_RETRIES. It is expected that the same values either from CLI or from environment yield in exactly the same result, but it is not the case as of now. Invalid values from CLI args are handled properly

$ app-store-connect publish --path app.ipa --altool-retries -1
usage: app-store-connect publish [-h] [--log-stream {stderr,stdout}] [--no-color] [--version] [-s] [-v] [--path artifact-path [artifact-path ...]] ...
app-store-connect publish: error: argument --altool-retries: Provided value "-1" is not valid

while the same value from respective envirionment variable results in an unhandled exception:

$ APP_STORE_CONNECT_ALTOOL_RETRIES='-1' app-store-connect publish --path app.ipa
Executing AppStoreConnect action publish failed unexpectedly. Detailed logs are available at "/var/folders/vs/tcrc5cns67zgynxt6fssjdg80000gn/T/codemagic-06-06-22.log". To see more details about the error, add `--verbose` command line option.

with full exception traceback being

[12:03:44 06-06-2022] ERROR cli_app.py:115 > Exception traceback:
Traceback (most recent call last):
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/typed_cli_argument.py", line 112, in from_environment_variable_default
    return cls(os.environ[cls.environment_variable_key], from_environment=True)
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/typed_cli_argument.py", line 37, in __call__
    return super().__call__(*args, **kwargs)
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/typed_cli_argument.py", line 86, in __init__
    self.value: T = self._parse_value()
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/typed_cli_argument.py", line 129, in _parse_value
    return self._apply_type(value)
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/typed_cli_argument.py", line 124, in _apply_type
    raise argparse.ArgumentTypeError(f'Provided value "{value}" is not valid')
argparse.ArgumentTypeError: Provided value "-1" is not valid

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/argument.py", line 86, in from_args
    return self.value.type.from_environment_variable_default()
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/typed_cli_argument.py", line 114, in from_environment_variable_default
    raise ValueError(str(ate)) from ate
ValueError: Provided value "-1" is not valid

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/cli_app.py", line 202, in invoke_cli
    CliApp._running_app._invoke_action(args)
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/cli_app.py", line 154, in _invoke_action
    action_args = {
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/cli_app.py", line 155, in <dictcomp>
    arg_type.value.key: arg_type.from_args(args, arg_type.get_default())
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/argument.py", line 88, in from_args
    self.raise_argument_error(str(ve))
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/argument.py", line 125, in raise_argument_error
    raise argparse.ArgumentError(self._parser_argument, message)
  File "/Users/priit/.pyenv/versions/3.8.10/lib/python3.8/site-packages/codemagic_cli_tools-0.27.3-py3.8.egg/codemagic/cli/argument/argument_properties.py", line 35, in _parser_argument
    return getattr(self, '__parser_argument')
AttributeError: 'PublishArgument' object has no attribute '__parser_argument'

This happens because the mechanism that copies duplicates argument instance with custom group had a fault that did not persist the argparse registered argument access. In this PR parser argument access is reworked in a way that the original argument is registered as a CLI argument, and duplicate reuses the parser argument from original when need be.

@priitlatt priitlatt changed the title Bugfix: grouped args env var values Bugfix: Invalid values for grouped arguments via environment variables are not handled Jun 6, 2022
@priitlatt priitlatt marked this pull request as ready for review June 6, 2022 09:10
@priitlatt priitlatt merged commit 07322dc into master Jun 6, 2022
@priitlatt priitlatt deleted the bugfix/grouped-args-env-var-values branch June 6, 2022 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant