Skip to content

Commit 1033ba9

Browse files
chore: Add new file and add argument for consistency
1 parent 2fe07af commit 1033ba9

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
)

codecov-cli/codecov_cli/services/report/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def send_create_report_request(
6666
headers = get_token_header(token)
6767
upload_url = enterprise_url or CODECOV_INGEST_URL
6868
service_part = (service or "").strip()
69-
validate_upload_url_base_parts(upload_url, service_part, encoded_slug)
69+
validate_upload_url_base_parts(upload_url, service_part, encoded_slug, CODECOV_INGEST_URL)
7070
validate_url_report_path_segments(commit_sha, code)
7171
url = f"{upload_url.rstrip('/')}/upload/{service_part}/{encoded_slug}/commits/{commit_sha}/reports"
7272
return send_post_request(url=url, headers=headers, data=data)

0 commit comments

Comments
 (0)