summaryrefslogtreecommitdiff
path: root/buildscripts/linter
diff options
context:
space:
mode:
authorDavid Bradford <david.bradford@mongodb.com>2020-04-29 15:25:17 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-05-06 13:12:44 +0000
commit74ea6aa4dfd7b3b4acf090084d36891834a49385 (patch)
tree17da63a8f40caa926e5d0e08185ba1e7f604746d /buildscripts/linter
parente2ccbbd922d42fc0f68608816c9a1265c6c87409 (diff)
downloadmongo-74ea6aa4dfd7b3b4acf090084d36891834a49385.tar.gz
SERVER-47796: Ensure linters run against enterprise modules
Diffstat (limited to 'buildscripts/linter')
-rw-r--r--buildscripts/linter/filediff.py56
1 files changed, 56 insertions, 0 deletions
diff --git a/buildscripts/linter/filediff.py b/buildscripts/linter/filediff.py
new file mode 100644
index 00000000000..222e33df932
--- /dev/null
+++ b/buildscripts/linter/filediff.py
@@ -0,0 +1,56 @@
+"""Modules for find which files should be linted."""
+import os
+import sys
+from typing import Tuple, List, Dict, Callable
+
+from git import Repo
+import structlog
+
+# Get relative imports to work when the package is not installed on the PYTHONPATH.
+if __name__ == "__main__" and __package__ is None:
+ sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(os.path.realpath(__file__)))))
+
+# pylint: disable=wrong-import-position
+from buildscripts.linter import git
+from buildscripts.patch_builds.change_data import find_changed_files_in_repos
+# pylint: enable=wrong-import-position
+
+LOGGER = structlog.get_logger(__name__)
+
+
+def _get_revision_for_repo(path: str) -> str:
+ """
+ Get the git revision for the given git repository.
+
+ :param path: Path to git repository.
+ :return: Git revision to compare against for given repo.
+ """
+ if "enterprise" in path:
+ return os.environ.get("ENTERPRISE_REV")
+ return os.environ.get("REVISION")
+
+
+def _get_repos_and_revisions() -> Tuple[List[Repo], Dict[str, str]]:
+ """Get the repo object and a map of revisions to compare against."""
+ modules = git.get_module_paths()
+ repos = [Repo(path) for path in modules]
+ revision_map = {repo.git_dir: _get_revision_for_repo(repo.git_dir) for repo in repos}
+ return repos, revision_map
+
+
+def gather_changed_files_for_lint(is_interesting_file: Callable):
+ """
+ Get the files that have changes since the last git commit.
+
+ :param is_interesting_file: Filter for whether a file should be returned.
+ :return: List of files for linting.
+ """
+ repos, revision_map = _get_repos_and_revisions()
+ LOGGER.info("revisions", revision=revision_map)
+
+ candidate_files = find_changed_files_in_repos(repos, revision_map)
+ files = [filename for filename in candidate_files if is_interesting_file(filename)]
+
+ LOGGER.info("Found files to lint", files=files)
+
+ return files