|
1 | 1 | import logging |
2 | 2 | import pathlib |
| 3 | +import typing |
3 | 4 |
|
4 | 5 | import yaml |
5 | 6 |
|
| 7 | +from codecov_cli.helpers.versioning_systems import get_versioning_system |
| 8 | + |
6 | 9 | logger = logging.getLogger("codecovcli") |
7 | 10 |
|
8 | 11 | CODECOV_API_URL = "https://api.codecov.io" |
9 | 12 | LEGACY_CODECOV_API_URL = "https://codecov.io" |
10 | 13 |
|
| 14 | +# Relative to the project root |
| 15 | +CODECOV_YAML_RECOGNIZED_DIRECTORIES = [ |
| 16 | + "", |
| 17 | + ".github/", |
| 18 | + "dev/", |
| 19 | +] |
| 20 | + |
| 21 | +CODECOV_YAML_RECOGNIZED_FILENAMES = [ |
| 22 | + "codecov.yml", |
| 23 | + "codecov.yaml", |
| 24 | + ".codecov.yml", |
| 25 | + ".codecov.yaml", |
| 26 | +] |
| 27 | + |
| 28 | + |
| 29 | +def _find_codecov_yamls(): |
| 30 | + vcs = get_versioning_system() |
| 31 | + vcs_root = vcs.get_network_root() if vcs else None |
| 32 | + project_root = vcs_root if vcs_root else pathlib.Path.cwd() |
| 33 | + |
| 34 | + yamls = [] |
| 35 | + for directory in CODECOV_YAML_RECOGNIZED_DIRECTORIES: |
| 36 | + dir_candidate = project_root / directory |
| 37 | + if not dir_candidate.exists() or not dir_candidate.is_dir(): |
| 38 | + continue |
| 39 | + |
| 40 | + for filename in CODECOV_YAML_RECOGNIZED_FILENAMES: |
| 41 | + file_candidate = dir_candidate / filename |
| 42 | + if file_candidate.exists() and file_candidate.is_file(): |
| 43 | + yamls.append(file_candidate) |
| 44 | + |
| 45 | + return yamls |
| 46 | + |
| 47 | + |
| 48 | +def load_cli_config(codecov_yml_path: typing.Optional[pathlib.Path]): |
| 49 | + if not codecov_yml_path: |
| 50 | + yamls = _find_codecov_yamls() |
| 51 | + codecov_yml_path = yamls[0] if yamls else None |
| 52 | + |
| 53 | + if not codecov_yml_path: |
| 54 | + logger.warning("No config file could be found. Ignoring config.") |
| 55 | + return None |
| 56 | + |
| 57 | + if not codecov_yml_path.exists() or not codecov_yml_path.is_file(): |
| 58 | + logger.warning( |
| 59 | + f"Config file {codecov_yml_path} not found, or is not a file. Ignoring config." |
| 60 | + ) |
| 61 | + return None |
11 | 62 |
|
12 | | -def load_cli_config(codecov_yml_path: pathlib.Path): |
13 | | - if codecov_yml_path.exists() and codecov_yml_path.is_file(): |
14 | | - logger.debug(f"Loading config from {codecov_yml_path}") |
15 | | - with open(codecov_yml_path, "r") as file_stream: |
16 | | - return yaml.safe_load(file_stream.read()) |
17 | | - logger.warning( |
18 | | - f"Config file {codecov_yml_path} not found, or is not a file. Ignoring config." |
19 | | - ) |
20 | | - return None |
| 63 | + logger.debug(f"Loading config from {codecov_yml_path}") |
| 64 | + with open(codecov_yml_path, "r") as file_stream: |
| 65 | + return yaml.safe_load(file_stream.read()) |
0 commit comments