-
Notifications
You must be signed in to change notification settings - Fork 12.5k
ci: add dependency audit workflow #3138
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
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3741020
ci: add dependency audit workflow
PascalThuet ef4edcf
ci: split dependency audit schedule matrix
PascalThuet 6403fa9
ci: harden dependency audit sync checks
PascalThuet 0fa9d54
ci: align security workflow python pin
PascalThuet 6c6ab78
ci: refresh dependency audit baseline
PascalThuet 7032366
docs: clarify security snapshot audit
PascalThuet 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| """Check that committed security audit requirements are up to date.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
| from pathlib import Path | ||
|
|
||
|
|
||
| REPO_ROOT = Path(__file__).resolve().parents[2] | ||
| COMMITTED_REQUIREMENTS = REPO_ROOT / ".github" / "security-audit-requirements.txt" | ||
| DEPENDENCY_INPUTS = ("pyproject.toml", ".github/security-audit-requirements.txt") | ||
|
|
||
|
|
||
| def _dependency_diff_refs() -> tuple[str, str]: | ||
| base_ref = os.environ.get("DEPENDENCY_DIFF_BASE", "").strip() | ||
| head_ref = os.environ.get("DEPENDENCY_DIFF_HEAD", "").strip() or "HEAD" | ||
| if base_ref and not set(base_ref) <= {"0"}: | ||
| return base_ref, head_ref | ||
| # Fallback when no usable base is supplied (push with an all-zero | ||
| # ``github.event.before``, manual dispatch, etc.). ``HEAD^`` fails on a | ||
| # shallow checkout or a single-commit repo; that ``git diff`` error is | ||
| # caught by the caller and deliberately treated as "inputs changed" so the | ||
| # audit runs anyway — failing safe (audit) rather than skipping silently. | ||
| return "HEAD^", "HEAD" | ||
|
|
||
|
|
||
| def _dependency_inputs_changed() -> bool: | ||
| base_ref, head_ref = _dependency_diff_refs() | ||
| try: | ||
| result = subprocess.run( | ||
| [ | ||
| "git", | ||
| "diff", | ||
| "--name-only", | ||
| base_ref, | ||
| head_ref, | ||
| "--", | ||
| *DEPENDENCY_INPUTS, | ||
| ], | ||
| check=True, | ||
| cwd=REPO_ROOT, | ||
| stderr=subprocess.PIPE, | ||
| stdout=subprocess.PIPE, | ||
| text=True, | ||
| ) | ||
| except subprocess.CalledProcessError as exc: | ||
| print( | ||
| "Could not determine changed dependency inputs; checking requirements.", | ||
| file=sys.stderr, | ||
| ) | ||
| if exc.stderr: | ||
| print(exc.stderr.strip(), file=sys.stderr) | ||
| return True | ||
|
|
||
| changed_inputs = [line for line in result.stdout.splitlines() if line] | ||
| if not changed_inputs: | ||
| print("Dependency audit inputs unchanged; sync check skipped.") | ||
| return False | ||
|
|
||
| print(f"Dependency audit inputs changed: {', '.join(changed_inputs)}") | ||
| return True | ||
|
|
||
|
|
||
| def main() -> int: | ||
| if not _dependency_inputs_changed(): | ||
| return 0 | ||
|
|
||
| generated_requirements_env = os.environ.get("GENERATED_REQUIREMENTS", "").strip() | ||
| if not generated_requirements_env: | ||
| print( | ||
| "GENERATED_REQUIREMENTS must be set to the temporary output file path.", | ||
| file=sys.stderr, | ||
| ) | ||
| return 1 | ||
|
|
||
| generated_requirements = Path(generated_requirements_env) | ||
| generated_requirements.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| subprocess.run( | ||
| [ | ||
| "uv", | ||
| "pip", | ||
| "compile", | ||
| "pyproject.toml", | ||
| "--extra", | ||
| "test", | ||
| "--universal", | ||
| "--upgrade", | ||
| "--generate-hashes", | ||
| "--quiet", | ||
| "--no-header", | ||
| "--output-file", | ||
| str(generated_requirements), | ||
| ], | ||
| check=True, | ||
| cwd=REPO_ROOT, | ||
| ) | ||
|
|
||
| committed = COMMITTED_REQUIREMENTS.read_text(encoding="utf-8") | ||
| generated = generated_requirements.read_text(encoding="utf-8") | ||
| if committed == generated: | ||
| return 0 | ||
|
|
||
| print( | ||
| "Regenerate .github/security-audit-requirements.txt with the documented " | ||
| "uv pip compile command.", | ||
| file=sys.stderr, | ||
| ) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| raise SystemExit(main()) | ||
Oops, something went wrong.
Oops, something went wrong.
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.