def discard_undesired_fixing_commits(self, commits: List[str]): """ Given a list of commits, discard commits that do not modify at least one Ansible file. Note, the update occurs in-place. That is, the original list is updated. Parameters ---------- commits : List[str] List of commit hash """ # get a sorted list of commits in ascending order of date self.sort_commits(commits) for commit in RepositoryMining(self.path_to_repo, from_commit=commits[0], # first commit in commits to_commit=commits[-1], # last commit in commits only_in_branch=self.branch).traverse_commits(): # if none of the modified files is a Ansible file, then discard the commit if not any(modified_file.change_type == ModificationType.MODIFY and filters.is_ansible_file( modified_file.new_path) for modified_file in commit.modifications): if commit.hash in commits: commits.remove(commit.hash)
def is_data_changed(self) -> bool: for modified_file in self.commit.modified_files: if modified_file.change_type != ModificationType.MODIFY or not filters.is_ansible_file( modified_file.new_path): continue try: source_code_before = yaml.safe_load( modified_file.source_code_before) source_code_current = yaml.safe_load(modified_file.source_code) data_before = [ value for key, value in utils.key_value_list(source_code_before) if key in CONFIG_DATA_MODULES ] data_current = [ value for key, value in utils.key_value_list(source_code_current) if key in CONFIG_DATA_MODULES ] return data_before != data_current except yaml.YAMLError: pass return False
def discard_undesired_fixing_commits(self, commits: List[str]): """ Given a list of commits, discard commits that do not modify at least one Ansible file. Note, the update occurs in-place. That is, the original list is updated. Parameters ---------- commits : List[str] List of commit hashes """ self.sort_commits(commits) for commit in Repository( self.path_to_repo, from_commit=commits[0], # first commit in commits to_commit=commits[-1], # last commit in commits only_in_branch=self.branch).traverse_commits(): i = 0 # if none of the modified files is a Ansible file then discard the commit while i < len(commit.modified_files): if commit.modified_files[ i].change_type != ModificationType.MODIFY: i += 1 elif not filters.is_ansible_file( commit.modified_files[i].new_path): i += 1 else: break if i == len(commit.modified_files) and commit.hash in commits: commits.remove(commit.hash)
def is_include_changed(self) -> bool: for modified_file in self.commit.modified_files: if modified_file.change_type != ModificationType.MODIFY or not filters.is_ansible_file( modified_file.new_path): continue try: source_code_before = yaml.safe_load( modified_file.source_code_before) source_code_current = yaml.safe_load(modified_file.source_code) includes_before = [ value for key, value in utils.key_value_list(source_code_before) if key in ('include', 'include_role', 'include_tasks', 'include_vars', 'import_playbook', 'import_tasks', 'import_role') ] includes_current = [ value for key, value in utils.key_value_list(source_code_current) if key in ('include', 'include_role', 'include_tasks', 'include_vars', 'import_playbook', 'import_tasks', 'import_role') ] return includes_before != includes_current except yaml.YAMLError: pass return False
def ignore_file(self, path_to_file: str, content: str = None): """ Ignore non-Ansible files. Parameters ---------- path_to_file: str The filepath (e.g., repominer/mining/base.py). content: str The file content. Returns ------- bool True if the file is not an Ansible file, and must be ignored. False, otherwise. """ return not filters.is_ansible_file(path_to_file)
def service_changed(self) -> bool: for modification in self.commit.modifications: if modification.change_type != ModificationType.MODIFY or not filters.is_ansible_file( modification.new_path): continue try: source_code_before = yaml.safe_load(modification.source_code_before) source_code_current = yaml.safe_load(modification.source_code) services_before = [value for key, value in utils.key_value_list(source_code_before) if key == 'service'] services_current = [value for key, value in utils.key_value_list(source_code_current) if key == 'service'] return services_before != services_current except yaml.YAMLError: pass return False
def ignore_file(self, path_to_file: str, content: str = None): return not is_ansible_file(path_to_file)
def test_is_ansible_file_true(self): assert filters.is_ansible_file('playbooks/task.yml') assert filters.is_ansible_file('meta/task.yml') assert filters.is_ansible_file('tasks/task.yml') assert filters.is_ansible_file('handlers/task.yml') assert filters.is_ansible_file('roles/task.yml')
def test_is_ansible_file_false(self): assert not filters.is_ansible_file('test/task.yml') assert not filters.is_ansible_file('repominer/task.yml')