forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
A general mechanism for classifying CI changes (pantsbuild#17218)
Generalizes our "docs_only" mechanism to other categories of affected things: - "rust" - "release" - "ci_config" - "docs_only" - "docs" Currently sets, but does not act on, any of these except docs_only. Future changes will apply the other categories in useful ways.
- Loading branch information
Showing
4 changed files
with
183 additions
and
73 deletions.
There are no files selected for viewing
This file contains 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 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,67 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
from __future__ import annotations | ||
|
||
import enum | ||
import fnmatch | ||
import sys | ||
from collections import defaultdict | ||
|
||
# This script may be run in CI before Pants is bootstrapped, so it must be kept simple | ||
# and runnable without `./pants run`. | ||
|
||
|
||
class Affected(enum.Enum): | ||
docs = "docs" | ||
rust = "rust" | ||
release = "release" | ||
ci_config = "ci_config" | ||
other = "other" | ||
|
||
|
||
_docs_globs = ["docs/*", "build-support/bin/generate_user_list.py"] | ||
_rust_globs = ["src/rust/engine/*", "rust-toolchain", "build-support/bin/rust/*"] | ||
_release_globs = [ | ||
"pants.toml", | ||
"src/python/pants/VERSION", | ||
"src/python/pants/notes/*", | ||
"src/python/pants/init/BUILD", | ||
"build-support/bin/release.sh", | ||
"build-support/bin/release_helper.py", | ||
] | ||
_ci_config_globs = [ | ||
"build-support/bin/classify_changed_files.py", | ||
"build-support/bin/generate_github_workflows.py", | ||
] | ||
|
||
|
||
_affected_to_globs = { | ||
Affected.docs: _docs_globs, | ||
Affected.rust: _rust_globs, | ||
Affected.release: _release_globs, | ||
Affected.ci_config: _ci_config_globs, | ||
} | ||
|
||
|
||
def classify(changed_files: list[str]) -> set[Affected]: | ||
classified: dict[Affected, set[str]] = defaultdict(set) | ||
for affected, globs in _affected_to_globs.items(): | ||
for pattern in globs: | ||
classified[affected].update(fnmatch.filter(changed_files, pattern)) | ||
ret = {k for k, v in classified.items() if v} | ||
if set(changed_files) - set().union(*classified.values()): | ||
ret.add(Affected.other) | ||
return ret | ||
|
||
|
||
def main() -> None: | ||
if len(sys.argv) < 2: | ||
return | ||
affecteds = classify(sys.argv[1].split("|")) | ||
for affected in sorted([a.name for a in affecteds]): | ||
print(affected) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains 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,24 @@ | ||
# Copyright 2022 Pants project contributors (see CONTRIBUTORS.md). | ||
# Licensed under the Apache License, Version 2.0 (see LICENSE). | ||
|
||
import pytest | ||
from classify_changed_files import Affected, classify | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["changed_files", "expected"], | ||
( | ||
[["docs/path/to/some/doc", "docs/path/to/some/other/doc"], {Affected.docs}], | ||
[["src/rust/engine/path/to/file.rs"], {Affected.rust}], | ||
[["src/python/pants/VERSION"], {Affected.release}], | ||
[["build-support/bin/generate_github_workflows.py"], {Affected.ci_config}], | ||
[["src/python/pants/whatever.py"], {Affected.other}], | ||
[["docs/path/to/some/doc", "rust-toolchain"], {Affected.docs, Affected.rust}], | ||
[ | ||
["docs/path/to/some/doc", "rust-toolchain", "src/python/pants/whatever.py"], | ||
{Affected.docs, Affected.rust, Affected.other}, | ||
], | ||
), | ||
) | ||
def test_classification(changed_files, expected): | ||
assert classify(changed_files) == expected |
Oops, something went wrong.