Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ci:
autofix_prs: false
autoupdate_schedule: "quarterly"
skip: [no-commit-to-branch]
skip: [no-commit-to-branch, validate-codecov-yaml]
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v6.0.0
Expand Down Expand Up @@ -33,6 +33,11 @@ repos:
files: ^.*\.(py|md|rst|yml)$
- repo: local
hooks:
- id: validate-codecov-yaml
name: validate codecov.yml
entry: python scripts/check_codecov_yaml.py
language: python
files: ^codecov\.yml$
- id: check-space-packet-parser-metadata
name: check space_packet_parser metadata
entry: python scripts/check_metadata.py
Expand Down
68 changes: 68 additions & 0 deletions scripts/check_codecov_yaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""Validate codecov.yml by posting it to Codecov's validation endpoint."""

from __future__ import annotations

import argparse
import sys
from pathlib import Path
from urllib import error, request

CODECOV_VALIDATE_URL = "https://codecov.io/validate"
NETWORK_TIMEOUT_SECONDS = 10
REPO_ROOT = Path(__file__).parent.parent


def validate_codecov_yaml(file_path: Path) -> int:
"""Validate a Codecov YAML file against Codecov's endpoint."""
Comment thread
medley56 marked this conversation as resolved.
try:
request_body = file_path.read_bytes()
except OSError as exc:
print(f"Unable to read {file_path}: {exc}", file=sys.stderr)
return 1

Comment thread
medley56 marked this conversation as resolved.
validate_request = request.Request(CODECOV_VALIDATE_URL, data=request_body, method="POST")
try:
with request.urlopen(validate_request, timeout=NETWORK_TIMEOUT_SECONDS) as response: # noqa: S310
body = response.read().decode("utf-8", errors="replace").strip()
except error.HTTPError as exc:
body = exc.read().decode("utf-8", errors="replace").strip()
print(f"{file_path} validation failed with HTTP {exc.code}.", file=sys.stderr)
if body:
print(body, file=sys.stderr)
return 1
except (error.URLError, TimeoutError, OSError) as exc:
print(f"Unable to reach Codecov validation endpoint: {exc}", file=sys.stderr)
return 1

if body:
print(body)
print(f"{file_path} is valid.")
return 0


def main() -> int:
"""Parse CLI args and validate codecov.yml."""
parser = argparse.ArgumentParser()
parser.add_argument(
"--file",
type=Path,
default=REPO_ROOT / "codecov.yml",
help="Path to the codecov YAML file to validate.",
)
parser.add_argument(
"files",
nargs="*",
type=Path,
help="Optional file paths provided by pre-commit.",
)
args = parser.parse_args()
files_to_validate = args.files or [args.file]
exit_code = 0
for file_path in files_to_validate:
if validate_codecov_yaml(file_path) != 0:
exit_code = 1
return exit_code


if __name__ == "__main__":
raise SystemExit(main())