From 285f6113bbcc5e21b9343504aa1f62bc98f6cc5f Mon Sep 17 00:00:00 2001 From: Alex Omar Date: Wed, 2 Dec 2026 16:49:27 -0800 Subject: [PATCH] Require codeowners for each example --- .github/CODEOWNERS | 2 + .github/scripts/check_examples.py | 90 ++++++++++++++++++++ .github/scripts/tests/test_check_examples.py | 74 ++++++++++++++++ .github/workflows/quality.yml | 13 +++ CONTRIBUTING.md | 23 ++++- 5 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 .github/scripts/check_examples.py create mode 100644 .github/scripts/tests/test_check_examples.py diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 3f9e90c..f51cfe1 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -13,4 +13,6 @@ # Sample maintainers. Each sample's folder holds its code, README, images, and # tests; own the whole folder. The nightly Newton-main workflow assigns failure # issues to these owners. See CONTRIBUTING.md. +# New sample template (replace placeholders and uncomment): +# /examples/// @your-handle /examples/multiphysics/franka_pulley_mechanical_advantage/ @felixmey diff --git a/.github/scripts/check_examples.py b/.github/scripts/check_examples.py new file mode 100644 index 0000000..260864a --- /dev/null +++ b/.github/scripts/check_examples.py @@ -0,0 +1,90 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +"""Validate sample layout and explicit per-sample CODEOWNERS entries.""" + +from __future__ import annotations + +import subprocess +import sys +from pathlib import Path, PurePosixPath + + +def validate_examples(files: list[str], codeowners: str) -> list[str]: + errors = [] + samples: set[str] = set() + entry_points: set[str] = set() + shared = {"examples/__init__.py", "examples/__main__.py"} + for name in sorted(files): + path = PurePosixPath(name) + parts = path.parts + if not parts or parts[0] != "examples" or name in shared: + continue + if len(parts) == 3 and path.name == "__init__.py": + continue + if len(parts) < 4: + errors.append( + f"{name}: misplaced file; put each sample and its supporting files in " + "examples/// (see CONTRIBUTING.md)." + ) + continue + sample = "/".join(parts[:3]) + samples.add(sample) + if path.match("example_*.py"): + if len(parts) != 4: + errors.append( + f"{name}: example entry points must be directly inside " + "examples///; remove extra directory levels." + ) + else: + entry_points.add(sample) + + # Exact keys intentionally exclude wildcard and topic-wide rules. Retain + # the last occurrence, so an ownerless duplicate cannot count as owned. + rules = {} + for line in codeowners.splitlines(): + fields = line.split("#", 1)[0].split() + if fields: + rules[fields[0]] = fields[1:] + + for sample in sorted(samples): + if sample not in entry_points: + errors.append( + f"{sample}/: missing a direct example_*.py entry point; expected " + "examples///example_.py." + ) + pattern = f"/{sample}/" + if not any(owner.startswith("@") and len(owner) > 1 for owner in rules.get(pattern, [])): + errors.append( + f"{sample}/: missing an explicit owned folder rule in .github/CODEOWNERS. " + f"Add '{pattern} @your-handle'; wildcard and topic-wide rules do not count." + ) + return errors + + +def main() -> int: + root = Path(__file__).resolve().parents[2] + # Include untracked, non-ignored files for local use as well as CI's checkout. + result = subprocess.run( + ["git", "ls-files", "--cached", "--others", "--exclude-standard", "-z", "--", "examples"], + cwd=root, + check=True, + capture_output=True, + text=True, + ) + files = [name for name in result.stdout.split("\0") if name] + codeowners = root / ".github/CODEOWNERS" + if not codeowners.is_file(): + print("ERROR: .github/CODEOWNERS is missing; add an explicit owner rule for every sample.", file=sys.stderr) + return 1 + errors = validate_examples(files, codeowners.read_text(encoding="utf-8")) + for error in errors: + print(f"ERROR: {error}", file=sys.stderr) + if errors: + return 1 + print("All example folders follow the required layout and have explicit CODEOWNERS entries.") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/.github/scripts/tests/test_check_examples.py b/.github/scripts/tests/test_check_examples.py new file mode 100644 index 0000000..7147546 --- /dev/null +++ b/.github/scripts/tests/test_check_examples.py @@ -0,0 +1,74 @@ +# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. +# SPDX-License-Identifier: Apache-2.0 + +import importlib.util +import unittest +from pathlib import Path + +spec = importlib.util.spec_from_file_location( + "check_examples", Path(__file__).resolve().parents[1] / "check_examples.py" +) +checker = importlib.util.module_from_spec(spec) +spec.loader.exec_module(checker) + +SAMPLE = "examples/rigid_body/pendulum" +ENTRY = f"{SAMPLE}/example_pendulum.py" +RULE = f"/{SAMPLE}/ @maintainer" + + +class ExampleValidationTests(unittest.TestCase): + def test_valid_layout_with_support_files_and_shared_modules(self): + files = [ + "examples/__init__.py", + "examples/__main__.py", + "examples/rigid_body/__init__.py", + ENTRY, + f"{SAMPLE}/README.md", + f"{SAMPLE}/tests/test_pendulum.py", + f"{SAMPLE}/images/preview.jpg", + ] + self.assertEqual(checker.validate_examples(files, RULE), []) + + def test_misplaced_files(self): + for path in ("examples/example_pendulum.py", "examples/rigid_body/example_pendulum.py"): + with self.subTest(path=path): + errors = checker.validate_examples([path], RULE) + self.assertTrue(any(path in error and "misplaced" in error for error in errors)) + + def test_nested_example_is_rejected_even_with_valid_entry_point(self): + errors = checker.validate_examples([ENTRY, f"{SAMPLE}/extra/example_other.py"], RULE) + self.assertTrue(any("extra directory levels" in error for error in errors)) + + def test_folder_without_entry_point(self): + errors = checker.validate_examples([f"{SAMPLE}/README.md"], RULE) + self.assertTrue(any("missing a direct example_*.py" in error for error in errors)) + + def test_requires_exact_owned_folder_rule(self): + for rule in ( + "", + f"# {RULE}", + "/examples/ @maintainer", + "/examples/rigid_body/*/ @maintainer", + f"{SAMPLE}/ @maintainer", + f"/{SAMPLE} @maintainer", + f"/{SAMPLE}/", + f"{RULE}\n/{SAMPLE}/ # clear owners", + f"/{SAMPLE}/example_pendulum.py @maintainer", + ): + with self.subTest(rule=rule): + errors = checker.validate_examples([ENTRY], rule) + self.assertTrue(any("missing an explicit owned folder rule" in error for error in errors)) + + def test_every_sample_is_checked(self): + other = "examples/soft_body/cloth" + errors = checker.validate_examples([ENTRY, f"{other}/example_cloth.py"], RULE) + self.assertEqual(len(errors), 1) + self.assertIn(other, errors[0]) + + def test_teams_multiple_owners_and_comments(self): + rule = f"/{SAMPLE}/ @org/team @maintainer # sample owners" + self.assertEqual(checker.validate_examples([ENTRY], rule), []) + + +if __name__ == "__main__": + unittest.main() diff --git a/.github/workflows/quality.yml b/.github/workflows/quality.yml index 201c251..8a03291 100644 --- a/.github/workflows/quality.yml +++ b/.github/workflows/quality.yml @@ -17,6 +17,19 @@ concurrency: cancel-in-progress: true jobs: + example-structure: + name: Example layout and CODEOWNERS + runs-on: ubuntu-24.04 + timeout-minutes: 5 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - name: Test example validation + run: python3 -m unittest discover -s .github/scripts/tests -v + - name: Validate all example folders and owners + run: python3 .github/scripts/check_examples.py + cpu: name: CPU / Python ${{ matrix.python-version }} runs-on: ubuntu-24.04 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 38d2e8e..e3c3285 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -14,7 +14,7 @@ Contributions are currently limited to Nvidians only. Each sample lives in its own folder, which holds everything that belongs to it: ``` -examples/// +examples/// __init__.py example_.py # run with: uv run -m examples README.md # usage, configuration, and validation @@ -29,7 +29,26 @@ Add a card for the sample to the table in the top-level [README](README.md#examp ## Registering as a Contributor -To register as a contributor, please add your sample's folder to [`.github/CODEOWNERS`](.github/CODEOWNERS) with your GitHub handle, for example `/examples/// @your-handle`. This is how you identify yourself as a maintainer for your example. When the nightly run against Newton `main` fails, the issue is assigned to the owners of the failing sample. +To register as a contributor, please add your sample's folder to [`.github/CODEOWNERS`](.github/CODEOWNERS) with your GitHub handle, for example `/examples/// @your-handle`. This is how you identify yourself as a maintainer for your example. When the nightly run against Newton `main` fails, the issue is assigned to the owners of the failing sample. + +For example, the existing sample and a newly contributed sample would look like: + +```text +# Existing sample +/examples/multiphysics/franka_pulley_mechanical_advantage/ @felixmey +# New sample: replace the topic, example folder, and handle with your own +/examples/rigid_body/my_new_example/ @your-handle +``` + +PR CI checks every sample, including existing ones. Each sample must live at +`examples///` with its `example_*.py` entry point directly +inside that folder. Tests, images, and other supporting files belong inside the +same folder. Only `examples/__init__.py`, `examples/__main__.py`, and topic-level +`__init__.py` files are allowed outside sample folders. + +Each sample must have an exact, root-anchored folder entry with a trailing `/` +and at least one GitHub owner in CODEOWNERS, as shown above. A wildcard or +topic-wide rule does not satisfy this check. ## Remediation Process