Skip to content

Commit

Permalink
fix line number detection for task loading
Browse files Browse the repository at this point in the history
Signed-off-by: hirokuni-kitahara <[email protected]>
  • Loading branch information
hirokuni-kitahara committed Jan 9, 2024
1 parent eafe7d4 commit 54f0694
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
26 changes: 26 additions & 0 deletions ansible_risk_insight/model_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ def load_play(
task_blocks, _ = get_task_blocks(task_dict_list=v)
if task_blocks is None:
continue
last_task_line_num = -1
for task_dict in task_blocks:
task_loading["total"] += 1
i = task_count
Expand All @@ -498,11 +499,14 @@ def load_play(
parent_key=pbObj.key,
parent_local_key=pbObj.local_key,
yaml_lines=yaml_lines,
previous_task_line=last_task_line_num,
basedir=basedir,
)
pre_tasks.append(t)
if t:
task_loading["success"] += 1
if t.line_num_in_file and len(t.line_num_in_file) == 2:
last_task_line_num = t.line_num_in_file[1]
except TaskFormatError as exc:
error = exc
if skip_task_format_error:
Expand All @@ -523,6 +527,7 @@ def load_play(
task_blocks, _ = get_task_blocks(task_dict_list=v)
if task_blocks is None:
continue
last_task_line_num = -1
for task_dict in task_blocks:
i = task_count
task_loading["total"] += 1
Expand All @@ -539,11 +544,14 @@ def load_play(
parent_key=pbObj.key,
parent_local_key=pbObj.local_key,
yaml_lines=yaml_lines,
previous_task_line=last_task_line_num,
basedir=basedir,
)
tasks.append(t)
if t:
task_loading["success"] += 1
if t.line_num_in_file and len(t.line_num_in_file) == 2:
last_task_line_num = t.line_num_in_file[1]
except TaskFormatError as exc:
error = exc
if skip_task_format_error:
Expand All @@ -564,6 +572,7 @@ def load_play(
task_blocks, _ = get_task_blocks(task_dict_list=v)
if task_blocks is None:
continue
last_task_line_num = -1
for task_dict in task_blocks:
i = task_count
task_loading["total"] += 1
Expand All @@ -580,11 +589,14 @@ def load_play(
parent_key=pbObj.key,
parent_local_key=pbObj.local_key,
yaml_lines=yaml_lines,
previous_task_line=last_task_line_num,
basedir=basedir,
)
post_tasks.append(t)
if t:
task_loading["success"] += 1
if t.line_num_in_file and len(t.line_num_in_file) == 2:
last_task_line_num = t.line_num_in_file[1]
except TaskFormatError as exc:
error = exc
if skip_task_format_error:
Expand All @@ -605,6 +617,7 @@ def load_play(
task_blocks, _ = get_task_blocks(task_dict_list=v)
if task_blocks is None:
continue
last_task_line_num = -1
for task_dict in task_blocks:
i = task_count
task_loading["total"] += 1
Expand All @@ -621,11 +634,14 @@ def load_play(
parent_key=pbObj.key,
parent_local_key=pbObj.local_key,
yaml_lines=yaml_lines,
previous_task_line=last_task_line_num,
basedir=basedir,
)
handlers.append(t)
if t:
task_loading["success"] += 1
if t.line_num_in_file and len(t.line_num_in_file) == 2:
last_task_line_num = t.line_num_in_file[1]
except TaskFormatError as exc:
error = exc
if skip_task_format_error:
Expand Down Expand Up @@ -1440,6 +1456,7 @@ def load_task(
parent_key="",
parent_local_key="",
yaml_lines="",
previous_task_line=-1,
basedir="",
):

Expand Down Expand Up @@ -1481,7 +1498,12 @@ def load_task(
module_name=module_name,
module_options=module_options,
task_options=task_options,
previous_task_line=previous_task_line,
)
print("[DEBUG] fullpath:", fullpath)
print("[DEBUG] index:", index)
print("[DEBUG] task_name:", task_name)
print("[DEBUG] taskObj.line_num_in_file:", taskObj.line_num_in_file)

# module_options can be passed as a string like below
#
Expand Down Expand Up @@ -1642,6 +1664,7 @@ def load_taskfile(path, yaml_str="", role_name="", collection_name="", basedir="
"failure": 0,
"errors": [],
}
last_task_line_num = -1
for i, t_dict in enumerate(task_dicts):
task_loading["total"] += 1
error = None
Expand All @@ -1655,11 +1678,14 @@ def load_taskfile(path, yaml_str="", role_name="", collection_name="", basedir="
yaml_lines=yaml_lines,
parent_key=tfObj.key,
parent_local_key=tfObj.local_key,
previous_task_line=last_task_line_num,
basedir=basedir,
)
tasks.append(t)
if t:
task_loading["success"] += 1
if t.line_num_in_file and len(t.line_num_in_file) == 2:
last_task_line_num = t.line_num_in_file[1]
except TaskFormatError as exc:
error = exc
if skip_task_format_error:
Expand Down
8 changes: 7 additions & 1 deletion ansible_risk_insight/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1110,7 +1110,7 @@ class Task(Object, Resolvable):
module_info: dict = field(default_factory=dict)
include_info: dict = field(default_factory=dict)

def set_yaml_lines(self, fullpath="", yaml_lines="", task_name="", module_name="", module_options=None, task_options=None):
def set_yaml_lines(self, fullpath="", yaml_lines="", task_name="", module_name="", module_options=None, task_options=None, previous_task_line=-1):
if not task_name and not module_options:
return

Expand All @@ -1127,6 +1127,12 @@ def set_yaml_lines(self, fullpath="", yaml_lines="", task_name="", module_name="
# - if module option is dict, at least one key is included
candidate_line_nums = []
for i, line in enumerate(lines):

# skip lines until `previous_task_line` if provided
if previous_task_line > 0:
if i <= previous_task_line - 1:
continue

if task_name:
if task_name in line:
candidate_line_nums.append(i)
Expand Down

0 comments on commit 54f0694

Please sign in to comment.