Skip to content

Commit

Permalink
fix issues around duplicate taskfile loading (#195)
Browse files Browse the repository at this point in the history
Signed-off-by: hirokuni-kitahara <[email protected]>
  • Loading branch information
hirokuni-kitahara authored Oct 4, 2023
1 parent e8d9e78 commit 8dcbb75
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions ansible_risk_insight/dependency_finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
requirements_yml = "requirements.yml"
galaxy_yml = "galaxy.yml"
GALAXY_yml = "GALAXY.yml"
github_workflows_dir = ".github/workflows"


def find_dependency(type, target, dependency_dir, use_ansible_path=False):
Expand Down Expand Up @@ -154,6 +155,7 @@ def find_role_dependency(target):
],
recursive=True,
)
role_meta_files = [fpath for fpath in role_meta_files if github_workflows_dir not in fpath]
main_yaml = ""
if len(role_meta_files) > 0:
for rf in role_meta_files:
Expand Down Expand Up @@ -205,6 +207,7 @@ def find_collection_dependency(target):
requirements = {}
# collection dir installed by ansible-galaxy command
manifest_json_files = safe_glob(os.path.join(target, "**", collection_manifest_json), recursive=True)
manifest_json_files = [fpath for fpath in manifest_json_files if github_workflows_dir not in fpath]
logger.debug("found meta files {}".format(manifest_json_files))
manifest_json = ""
if len(manifest_json_files) > 0:
Expand Down Expand Up @@ -301,6 +304,7 @@ def load_dependency_from_galaxy(path):
yaml_path = ""
galaxy_yml_files = safe_glob(os.path.join(path, "**", galaxy_yml), recursive=True)
galaxy_yml_files.extend(safe_glob(os.path.join(path, "**", GALAXY_yml), recursive=True))
galaxy_yml_files = [fpath for fpath in galaxy_yml_files if github_workflows_dir not in fpath]
logger.debug("found meta files {}".format(galaxy_yml_files))
if len(galaxy_yml_files) > 0:
for g in galaxy_yml_files:
Expand All @@ -324,6 +328,7 @@ def load_existing_dependency_dir(dependency_dir):
# recursive=True,
# )
collection_meta_files = safe_glob(os.path.join(dependency_dir, "**", collection_manifest_json), recursive=True)
collection_meta_files = [fpath for fpath in collection_meta_files if github_workflows_dir not in fpath]
requirements = {
"roles": [],
"collections": [],
Expand Down
3 changes: 3 additions & 0 deletions ansible_risk_insight/finder.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

playbook_taskfile_dir_patterns = ["tasks", "playbooks"]

github_workflows_dir = ".github/workflows"


class Singleton(type):
_instances = {}
Expand Down Expand Up @@ -321,6 +323,7 @@ def find_best_repo_root_path(path):
def find_collection_name_of_repo(path):
pattern = os.path.join(path, "**/galaxy.yml")
found_galaxy_ymls = safe_glob(pattern, recursive=True)
found_galaxy_ymls = [fpath for fpath in found_galaxy_ymls if github_workflows_dir not in fpath]

# skip galaxy ymls found in collections/roles in the repository
_galaxy_ymls = []
Expand Down
8 changes: 8 additions & 0 deletions ansible_risk_insight/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,16 @@ def run(self, load_data=None, load_json_path="", collection_name_of_project=""):
mappings["roles"].append([role_path, r.key])

taskfiles = [tf for r in roles for tf in r.taskfiles if r.fqcn != ld.target_name]
loaded_absolute_path_list = []
for r in roles:
for tf in r.taskfiles:
if r.fqcn != ld.target_name:
loaded_absolute_path_list.append(os.path.join(basedir, r.defined_in, tf.defined_in))
for taskfile_path in ld.taskfiles:
try:
abs_path = os.path.join(basedir, taskfile_path)
if abs_path in loaded_absolute_path_list:
continue
tf = load_taskfile(
path=taskfile_path,
yaml_str=ld.taskfile_yaml,
Expand Down

0 comments on commit 8dcbb75

Please sign in to comment.