-
Notifications
You must be signed in to change notification settings - Fork 13
Add local pre-commit Codecov YAML validator #269
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+77
−1
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d60792c
Add local pre-commit hook for Codecov YAML validation
Copilot f177447
Handle pre-commit filename args and lint compliance for Codecov hook
Copilot 077ead8
Fix Codecov validator request handling and add unit tests
Copilot 3506af9
Remove Codecov validator unit tests per review
Copilot 5e02e81
Skip networked Codecov hook in pre-commit.ci
Copilot 6c7dfef
Fix Ruff S310 violation in Codecov hook
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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 |
|---|---|---|
| @@ -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.""" | ||
| try: | ||
| request_body = file_path.read_bytes() | ||
| except OSError as exc: | ||
| print(f"Unable to read {file_path}: {exc}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
|
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()) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.