def find_changed_tests( # pylint: disable=too-many-locals branch_name, base_commit, max_revisions, buildvariant, check_evergreen): """Find the changed tests. Use git to find which files have changed in this patch. TODO: This should be expanded to search for enterprise modules. The returned file paths are in normalized form (see os.path.normpath(path)). """ changed_tests = [] repo = git.Repository(".") if base_commit is None: base_commit = repo.get_merge_base([branch_name + "@{upstream}", "HEAD"]) if check_evergreen: # We're going to check up to 200 commits in Evergreen for the last scheduled one. # The current commit will be activated in Evergreen; we use --skip to start at the # previous commit when trying to find the most recent preceding commit that has been # activated. revs_to_check = repo.git_rev_list([base_commit, "--max-count=200", "--skip=1"]).splitlines() last_activated = find_last_activated_task(revs_to_check, buildvariant, branch_name) if last_activated is None: # When the current commit is the first time 'buildvariant' has run, there won't be a # commit among 'revs_to_check' that's been activated in Evergreen. We handle this by # only considering tests changed in the current commit. last_activated = "HEAD" print("Comparing current branch against", last_activated) revisions = repo.git_rev_list([base_commit + "..." + last_activated]).splitlines() base_commit = last_activated else: revisions = repo.git_rev_list([base_commit + "...HEAD"]).splitlines() revision_count = len(revisions) if revision_count > max_revisions: print(("There are too many revisions included ({}). This is likely because your base" " branch is not {}. You can allow us to review more than {} revisions by using" " the --maxRevisions option.".format(revision_count, branch_name, max_revisions))) return changed_tests changed_files = repo.git_diff(["--name-only", base_commit]).splitlines() # New files ("untracked" in git terminology) won't show up in the git diff results. untracked_files = repo.git_status(["--porcelain"]).splitlines() # The lines with untracked files start with '?? '. for line in untracked_files: if line.startswith("?"): (_, line) = line.split(" ", 1) changed_files.append(line) for line in changed_files: line = line.rstrip() # Check that the file exists because it may have been moved or deleted in the patch. if os.path.splitext(line)[1] != ".js" or not os.path.isfile(line): continue if "jstests" in line: path = os.path.normpath(line) changed_tests.append(path) return changed_tests
def fetch_test_lifecycle(metadata_repo_url, references_file, lifecycle_file, project, revision): # noqa: D214,D405,D406,D407,D411,D413 """Fetch the test lifecycle file for the revision in the repository this script is invoked. Args: metadata_repo_url: the git repository URL for the metadata repository containing the test lifecycle file. references_file: the relative path from the root of the metadata repository to the references file. lifecycle_file: the relative path from the root of the metadata repository to the test lifecycle file. project: the Evergreen project name. revision: the current repository revision. """ metadata_repo = MetadataRepository( _clone_repository(metadata_repo_url, project), references_file, lifecycle_file) mongo_repo = git.Repository(os.getcwd()) metadata_revision = _get_metadata_revision(metadata_repo, mongo_repo, project, revision) if metadata_revision: LOGGER.info("Using metadata repository revision '%s'", metadata_revision) result = metadata_repo.get_lifecycle_file_content(metadata_revision) else: result = None return result
def __init__(self, project, lifecycle_file, metadata_repo_url=None, references_file=None, jira_issue_creator=None, git_info=None, model_config=None): """Initalize the LifecycleTagsFile. Arguments: project: The Evergreen project name, e.g. "mongodb-mongo-master". lifecycle_file: The path to the lifecycle tags file. If 'metadata_repo_url' is specified, this path must be relative to the root of the metadata repository. metadata_repo_url: The URL of the metadat repository that contains the test lifecycle tags file. references_file: The path to the references file in the metadata repository. jira_issue_creator: A JiraIssueCreator instance. git_info: A tuple containing the git user's name and email to set before committing. model_config: The model configuration as a Config instance. """ self.project = project self.mongo_repo = git.Repository(os.getcwd()) self.mongo_revision = self.mongo_repo.get_current_revision() # The branch name is the same on both repositories. self.mongo_branch = self.mongo_repo.get_branch_name() self.metadata_branch = project if metadata_repo_url: # The file can be found in another repository. We clone it. self.metadata_repo = self._clone_repository( metadata_repo_url, self.project) self.relative_lifecycle_file = lifecycle_file self.lifecycle_file = os.path.join(self.metadata_repo.directory, lifecycle_file) self.relative_references_file = references_file self.references_file = os.path.join(self.metadata_repo.directory, references_file) if git_info: self.metadata_repo.configure("user.name", git_info[0]) self.metadata_repo.configure("user.email", git_info[1]) else: self.metadata_repo = None self.relative_lifecycle_file = lifecycle_file self.lifecycle_file = lifecycle_file self.relative_references_file = None self.references_file = None self.metadata_repo_url = metadata_repo_url self.lifecycle = ci_tags.TagsConfig.from_file(self.lifecycle_file, cmp_func=compare_tags) self.jira_issue_creator = jira_issue_creator self.model_config = model_config self.changelog_lifecycle = TagsConfigWithChangelog(self.lifecycle)
def test_base_gito_methods_errors(self): params = ["param1", "param2", "param3"] repo = _git.Repository("/tmp") self._check_gito_command_error(repo.git_add, "add", params) self._check_gito_command_error(repo.git_commit, "commit", params) self._check_gito_command_error(repo.git_diff, "diff", params) self._check_gito_command_error(repo.git_log, "log", params) self._check_gito_command_error(repo.git_push, "push", params) self._check_gito_command_error(repo.git_fetch, "fetch", params) self._check_gito_command_error(repo.git_ls_files, "ls-files", params) self._check_gito_command_error(repo.git_rev_parse, "rev-parse", params) self._check_gito_command_error(repo.git_rm, "rm", params) self._check_gito_command_error(repo.git_show, "show", params)