- Notifications
You must be signed in to change notification settings - Fork673
feat(cli): do not require config file to run CLI#1743
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions.pre-commit-config.yaml
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
14 changes: 11 additions & 3 deletionsdocs/cli-usage.rst
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
154 changes: 94 additions & 60 deletionsgitlab/config.py
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 |
---|---|---|
@@ -20,27 +20,67 @@ | ||
import shlex | ||
import subprocess | ||
from os.path import expanduser, expandvars | ||
from pathlib import Path | ||
JohnVillalovos marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
from typing import List, Optional, Union | ||
from gitlab.const importDEFAULT_URL,USER_AGENT | ||
_DEFAULT_FILES: List[str] = [ | ||
"/etc/python-gitlab.cfg", | ||
str(Path.home() / ".python-gitlab.cfg"), | ||
] | ||
HELPER_PREFIX = "helper:" | ||
HELPER_ATTRIBUTES = ["job_token", "http_password", "private_token", "oauth_token"] | ||
def _resolve_file(filepath: Union[Path, str]) -> str: | ||
resolved = Path(filepath).resolve(strict=True) | ||
return str(resolved) | ||
def _get_config_files( | ||
config_files: Optional[List[str]] = None, | ||
) -> Union[str, List[str]]: | ||
""" | ||
Return resolved path(s) to config files if they exist, with precedence: | ||
1. Files passed in config_files | ||
2. File defined in PYTHON_GITLAB_CFG | ||
3. User- and system-wide config files | ||
""" | ||
resolved_files = [] | ||
if config_files: | ||
for config_file in config_files: | ||
try: | ||
resolved = _resolve_file(config_file) | ||
except OSError as e: | ||
raise GitlabConfigMissingError(f"Cannot read config from file: {e}") | ||
resolved_files.append(resolved) | ||
return resolved_files | ||
try: | ||
env_config = os.environ["PYTHON_GITLAB_CFG"] | ||
return _resolve_file(env_config) | ||
except KeyError: | ||
pass | ||
except OSError as e: | ||
raise GitlabConfigMissingError( | ||
f"Cannot read config from PYTHON_GITLAB_CFG: {e}" | ||
) | ||
for config_file in _DEFAULT_FILES: | ||
try: | ||
resolved = _resolve_file(config_file) | ||
except OSError: | ||
continue | ||
resolved_files.append(resolved) | ||
return resolved_files | ||
class ConfigError(Exception): | ||
pass | ||
@@ -66,155 +106,149 @@ def __init__( | ||
self, gitlab_id: Optional[str] = None, config_files: Optional[List[str]] = None | ||
) -> None: | ||
self.gitlab_id = gitlab_id | ||
self.http_username: Optional[str] = None | ||
self.http_password: Optional[str] = None | ||
self.job_token: Optional[str] = None | ||
self.oauth_token: Optional[str] = None | ||
self.private_token: Optional[str] = None | ||
self.api_version: str = "4" | ||
self.order_by: Optional[str] = None | ||
self.pagination: Optional[str] = None | ||
self.per_page: Optional[int] = None | ||
self.retry_transient_errors: bool = False | ||
self.ssl_verify: Union[bool, str] = True | ||
self.timeout: int = 60 | ||
self.url: str = DEFAULT_URL | ||
self.user_agent: str = USER_AGENT | ||
self._files = _get_config_files(config_files) | ||
if self._files: | ||
self._parse_config() | ||
def _parse_config(self) -> None: | ||
_config = configparser.ConfigParser() | ||
_config.read(self._files) | ||
if self.gitlab_id is None: | ||
try: | ||
self.gitlab_id = _config.get("global", "default") | ||
except Exception as e: | ||
raise GitlabIDError( | ||
"Impossible to get the gitlab id (not specified in config file)" | ||
) from e | ||
try: | ||
self.url = _config.get(self.gitlab_id, "url") | ||
nejch marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
except Exception as e: | ||
raise GitlabDataError( | ||
"Impossible to get gitlab details from " | ||
f"configuration ({self.gitlab_id})" | ||
) from e | ||
try: | ||
self.ssl_verify = _config.getboolean("global", "ssl_verify") | ||
except ValueError: | ||
# Value Error means the option exists but isn't a boolean. | ||
# Get as a string instead as it should then be a local path to a | ||
# CA bundle. | ||
try: | ||
self.ssl_verify = _config.get("global", "ssl_verify") | ||
except Exception: | ||
pass | ||
except Exception: | ||
pass | ||
try: | ||
self.ssl_verify = _config.getboolean(self.gitlab_id, "ssl_verify") | ||
except ValueError: | ||
# Value Error means the option exists but isn't a boolean. | ||
# Get as a string instead as it should then be a local path to a | ||
# CA bundle. | ||
try: | ||
self.ssl_verify = _config.get(self.gitlab_id, "ssl_verify") | ||
except Exception: | ||
pass | ||
except Exception: | ||
pass | ||
try: | ||
self.timeout = _config.getint("global", "timeout") | ||
except Exception: | ||
pass | ||
try: | ||
self.timeout = _config.getint(self.gitlab_id, "timeout") | ||
except Exception: | ||
pass | ||
try: | ||
self.private_token = _config.get(self.gitlab_id, "private_token") | ||
except Exception: | ||
pass | ||
try: | ||
self.oauth_token = _config.get(self.gitlab_id, "oauth_token") | ||
except Exception: | ||
pass | ||
try: | ||
self.job_token = _config.get(self.gitlab_id, "job_token") | ||
except Exception: | ||
pass | ||
try: | ||
self.http_username = _config.get(self.gitlab_id, "http_username") | ||
self.http_password = _config.get(self.gitlab_id, "http_password") | ||
except Exception: | ||
pass | ||
self._get_values_from_helper() | ||
try: | ||
self.api_version = _config.get("global", "api_version") | ||
except Exception: | ||
pass | ||
try: | ||
self.api_version = _config.get(self.gitlab_id, "api_version") | ||
except Exception: | ||
pass | ||
if self.api_version not in ("4",): | ||
raise GitlabDataError(f"Unsupported API version: {self.api_version}") | ||
for section in ["global", self.gitlab_id]: | ||
try: | ||
self.per_page = _config.getint(section, "per_page") | ||
except Exception: | ||
pass | ||
if self.per_page is not None and not 0 <= self.per_page <= 100: | ||
raise GitlabDataError(f"Unsupported per_page number: {self.per_page}") | ||
try: | ||
self.pagination = _config.get(self.gitlab_id, "pagination") | ||
except Exception: | ||
pass | ||
try: | ||
self.order_by = _config.get(self.gitlab_id, "order_by") | ||
except Exception: | ||
pass | ||
try: | ||
self.user_agent = _config.get("global", "user_agent") | ||
except Exception: | ||
pass | ||
try: | ||
self.user_agent = _config.get(self.gitlab_id, "user_agent") | ||
except Exception: | ||
pass | ||
try: | ||
self.retry_transient_errors = _config.getboolean( | ||
"global", "retry_transient_errors" | ||
) | ||
except Exception: | ||
pass | ||
try: | ||
self.retry_transient_errors = _config.getboolean( | ||
self.gitlab_id, "retry_transient_errors" | ||
) | ||
except Exception: | ||
2 changes: 1 addition & 1 deletionrequirements-test.txt
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
coverage | ||
httmock | ||
pytest==6.2.5 | ||
pytest-console-scripts==1.2.1 | ||
pytest-cov | ||
responses |
39 changes: 39 additions & 0 deletionstests/functional/cli/test_cli.py
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
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.