Skip to content

Commit

Permalink
Make codeowners rules optional by group (#64)
Browse files Browse the repository at this point in the history
Also use the `after` argument in `required_codeowners_list()`, which
wasn't being used before.
  • Loading branch information
KyleFromNVIDIA authored Jan 30, 2025
1 parent 5e57ae2 commit b012497
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
55 changes: 45 additions & 10 deletions src/rapids_pre_commit_hooks/codeowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ def project_codeowners(category: str) -> CodeownersTransform:
def required_codeowners_list(
files: list[str], owners: list[CodeownersTransform], after: list[str] = []
) -> list[RequiredCodeownersLine]:
return [RequiredCodeownersLine(file=file, owners=owners) for file in files]
return [
RequiredCodeownersLine(file=file, owners=owners, after=after) for file in files
]


REQUIRED_CI_CODEOWNERS_LINES = required_codeowners_list(
Expand Down Expand Up @@ -120,13 +122,16 @@ def required_codeowners_list(
),
],
)
REQUIRED_CODEOWNERS_LINES = [
*REQUIRED_CI_CODEOWNERS_LINES,
*REQUIRED_PACKAGING_CODEOWNERS_LINES,
*REQUIRED_CPP_CODEOWNERS_LINES,
*REQUIRED_PYTHON_CODEOWNERS_LINES,
*REQUIRED_CMAKE_CODEOWNERS_LINES,
]


def required_codeowners_lines(args: argparse.Namespace) -> list[RequiredCodeownersLine]:
return [
*(REQUIRED_CI_CODEOWNERS_LINES if args.ci else []),
*(REQUIRED_PACKAGING_CODEOWNERS_LINES if args.packaging else []),
*(REQUIRED_CPP_CODEOWNERS_LINES if args.cpp else []),
*(REQUIRED_PYTHON_CODEOWNERS_LINES if args.python else []),
*(REQUIRED_CMAKE_CODEOWNERS_LINES if args.cmake else []),
]


def parse_codeowners_line(line: str, skip: int) -> CodeownersLine | None:
Expand Down Expand Up @@ -164,7 +169,7 @@ def check_codeowners_line(
codeowners_line: CodeownersLine,
found_files: list[tuple[RequiredCodeownersLine, tuple[int, int]]],
) -> None:
for required_codeowners_line in REQUIRED_CODEOWNERS_LINES:
for required_codeowners_line in required_codeowners_lines(args):
if required_codeowners_line.file == codeowners_line.file.filename:
required_owners = [
required_owner(project_prefix=args.project_prefix)
Expand Down Expand Up @@ -229,7 +234,7 @@ def check_codeowners(linter: Linter, args: argparse.Namespace) -> None:
check_codeowners_line(linter, args, codeowners_line, found_files)

new_text = ""
for required_codeowners_line in REQUIRED_CODEOWNERS_LINES:
for required_codeowners_line in required_codeowners_lines(args):
if required_codeowners_line.file not in map(
lambda line: line[0].file, found_files
):
Expand Down Expand Up @@ -258,6 +263,36 @@ def main() -> None:
help="project prefix to insert for project-specific team names",
required=True,
)
m.argparser.add_argument(
"--ci",
help="enforce rules for CI codeowners",
action=argparse.BooleanOptionalAction,
default=True,
)
m.argparser.add_argument(
"--packaging",
help="enforce rules for packaging codeowners",
action=argparse.BooleanOptionalAction,
default=True,
)
m.argparser.add_argument(
"--cpp",
help="enforce rules for C++ codeowners",
action=argparse.BooleanOptionalAction,
default=True,
)
m.argparser.add_argument(
"--python",
help="enforce rules for Python codeowners",
action=argparse.BooleanOptionalAction,
default=True,
)
m.argparser.add_argument(
"--cmake",
help="enforce rules for CMake codeowners",
action=argparse.BooleanOptionalAction,
default=True,
)
with m.execute() as ctx:
ctx.add_check(check_codeowners)

Expand Down
4 changes: 2 additions & 2 deletions test/rapids_pre_commit_hooks/test_codeowners.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@
]

patch_required_codeowners_lines = patch(
"rapids_pre_commit_hooks.codeowners.REQUIRED_CODEOWNERS_LINES",
MOCK_REQUIRED_CODEOWNERS_LINES,
"rapids_pre_commit_hooks.codeowners.required_codeowners_lines",
lambda _args: MOCK_REQUIRED_CODEOWNERS_LINES,
)


Expand Down

0 comments on commit b012497

Please sign in to comment.