|
| 1 | +import typing |
| 2 | +from urllib.parse import urlparse |
| 3 | + |
| 4 | +import click |
| 5 | + |
| 6 | +from codecov_cli.helpers.config import CODECOV_INGEST_URL |
| 7 | +from codecov_cli.helpers.git import GitService |
| 8 | + |
| 9 | +# The set of acceptable upload services from Shelter |
| 10 | +_CODECOV_UPLOAD_SERVICES = frozenset(s.value for s in GitService) | {"github-actions"} |
| 11 | + |
| 12 | + |
| 13 | +def validate_upload_url_base_parts( |
| 14 | + upload_url: str, |
| 15 | + service: str, |
| 16 | + slug: typing.Optional[str], |
| 17 | + default_base_url_for_error: str = CODECOV_INGEST_URL, |
| 18 | +) -> None: |
| 19 | + parsed = urlparse(upload_url) |
| 20 | + if parsed.scheme not in ("http", "https") or not parsed.netloc: |
| 21 | + raise click.ClickException( |
| 22 | + f"Invalid Codecov base URL {upload_url!r}. Use an absolute http(s) URL with a host " |
| 23 | + f"(default: {default_base_url_for_error})." |
| 24 | + ) |
| 25 | + if not slug or not str(slug).strip(): |
| 26 | + raise click.ClickException( |
| 27 | + "Repository slug is missing or empty. Pass -r / --slug owner/repo (or set SLUG)." |
| 28 | + ) |
| 29 | + if not service: |
| 30 | + allowed = ", ".join(sorted(_CODECOV_UPLOAD_SERVICES)) |
| 31 | + raise click.ClickException( |
| 32 | + "Upload service is missing (Codecov requires it for upload URLs). " |
| 33 | + f"Pass --git-service with one of: {allowed}" |
| 34 | + ) |
| 35 | + if service not in _CODECOV_UPLOAD_SERVICES: |
| 36 | + allowed = ", ".join(sorted(_CODECOV_UPLOAD_SERVICES)) |
| 37 | + raise click.ClickException( |
| 38 | + f"Invalid upload service {service!r}. Use one of: {allowed}" |
| 39 | + ) |
| 40 | + |
| 41 | + |
| 42 | +def validate_url_commit_sha(commit_sha: typing.Optional[str]) -> None: |
| 43 | + if commit_sha is None or not str(commit_sha).strip(): |
| 44 | + raise click.ClickException( |
| 45 | + "Commit SHA is missing or empty. Pass -C / --sha / --commit-sha." |
| 46 | + ) |
| 47 | + |
| 48 | +# This function includes validate_commit_sha check |
| 49 | +def validate_url_report_path_segments(commit_sha: typing.Optional[str], report_code: typing.Optional[str]) -> None: |
| 50 | + validate_url_commit_sha(commit_sha) |
| 51 | + if report_code is None or not str(report_code).strip(): |
| 52 | + raise click.ClickException( |
| 53 | + "Report code is missing or empty. Pass --code / --report-code." |
| 54 | + ) |
0 commit comments