Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate content support fixes #4688

Draft
wants to merge 68 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
f8125f3
fix logger initialization
barryyosi-panw Nov 24, 2024
e51c267
introduce content paths update functionallity
barryyosi-panw Nov 25, 2024
77f746d
introduce content paths update functionallity
barryyosi-panw Nov 25, 2024
23bf25b
introduce content paths update functionallity
barryyosi-panw Nov 25, 2024
a3dea68
changelog
barryyosi-panw Nov 25, 2024
b4bba06
reloading dependent modules
barryyosi-panw Nov 25, 2024
3c2d474
reloading dependent modules
barryyosi-panw Nov 25, 2024
25720c1
reloading dependent modules
barryyosi-panw Nov 25, 2024
e77552a
reloading dependent modules
barryyosi-panw Nov 25, 2024
011988d
reloading dependent modules
barryyosi-panw Nov 25, 2024
bfecd42
ruff
barryyosi-panw Nov 25, 2024
b15aa5c
reloading dependent modules
barryyosi-panw Nov 25, 2024
e438656
reloading dependent modules
barryyosi-panw Nov 25, 2024
93e32a0
reloading dependent modules
barryyosi-panw Nov 25, 2024
d4ca72c
reloading dependent modules
barryyosi-panw Nov 25, 2024
395318f
reloading dependent modules
barryyosi-panw Nov 26, 2024
49d8a3e
fix ymlsplitter
barryyosi-panw Nov 26, 2024
8a1b45f
revert
barryyosi-panw Nov 26, 2024
b845ab3
logs
barryyosi-panw Nov 28, 2024
a927436
cancel install-hooks in advance
barryyosi-panw Nov 28, 2024
70abb2f
Merge branch 'master' into validate-content-support-fixes
barryyosi-panw Nov 28, 2024
c21ffdd
logs
barryyosi-panw Nov 28, 2024
6b3fc70
logs
barryyosi-panw Nov 28, 2024
e683c2a
logs
barryyosi-panw Nov 28, 2024
8e7559f
error handeling
barryyosi-panw Nov 30, 2024
525c0d3
error-handling
barryyosi-panw Nov 30, 2024
7da49ab
try writing to logfile
barryyosi-panw Nov 30, 2024
138cb05
pre-commit fix
barryyosi-panw Nov 30, 2024
58116e7
fix precommit run hook
barryyosi-panw Nov 30, 2024
1821e2a
precommit
barryyosi-panw Nov 30, 2024
f20f8f8
precommit fix
barryyosi-panw Nov 30, 2024
dfabf4f
precommit fix
barryyosi-panw Nov 30, 2024
5b4f13d
precommit
barryyosi-panw Nov 30, 2024
d6872be
prepare_hooks
barryyosi-panw Nov 30, 2024
d15d134
precommit
barryyosi-panw Nov 30, 2024
a9fcce8
precommit
barryyosi-panw Nov 30, 2024
93400f6
precommit
barryyosi-panw Nov 30, 2024
5980173
precommit
barryyosi-panw Dec 1, 2024
eaabdb8
precommit
barryyosi-panw Dec 1, 2024
31a84a2
preprocess-files
barryyosi-panw Dec 1, 2024
5b8ecf7
group_by_language
barryyosi-panw Dec 1, 2024
a335535
groupbylanguage
barryyosi-panw Dec 1, 2024
29188f7
group_by_language
barryyosi-panw Dec 1, 2024
ff6f003
group_by_language
barryyosi-panw Dec 1, 2024
4a3cfd6
group_by_language
barryyosi-panw Dec 1, 2024
6938381
group_by_language
barryyosi-panw Dec 1, 2024
7eb9202
group_by_language
barryyosi-panw Dec 1, 2024
b760ec7
group_by_language
barryyosi-panw Dec 1, 2024
adff430
serialize parsing
barryyosi-panw Dec 1, 2024
e2fc929
postinit
barryyosi-panw Dec 1, 2024
4f9df21
logs
barryyosi-panw Dec 1, 2024
30cce41
logs
barryyosi-panw Dec 1, 2024
7b2f60b
logs
barryyosi-panw Dec 2, 2024
e50960a
logs
barryyosi-panw Dec 2, 2024
120ecb1
logs
barryyosi-panw Dec 2, 2024
c1fe1c5
logs
barryyosi-panw Dec 2, 2024
1c24019
logs
barryyosi-panw Dec 2, 2024
4cc2b01
logs
barryyosi-panw Dec 2, 2024
cedcd1d
logs
barryyosi-panw Dec 2, 2024
e2aa14c
logs
barryyosi-panw Dec 2, 2024
864c2e5
logs
barryyosi-panw Dec 2, 2024
84bf1c3
logs
barryyosi-panw Dec 2, 2024
d68fca6
write precommit results to file
barryyosi-panw Dec 2, 2024
1a1c5a0
fix write results to file
barryyosi-panw Dec 2, 2024
2af372b
remove redundant logs
barryyosi-panw Dec 2, 2024
6e7068b
writing pre commit results to file
barryyosi-panw Dec 2, 2024
e1321f5
writing pre-commit output to file
barryyosi-panw Dec 3, 2024
f2e23d6
changelog
barryyosi-panw Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .changelog/4688.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Determining logger setup via environment variable. Writing pre-commit results to files.
type: feature
pr_number: 4688
8 changes: 7 additions & 1 deletion demisto_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
if __name__ in ["__main__", "demisto_sdk"]:
import os

if os.environ.get("DEMISTO_SDK_SKIP_LOGGER_SETUP", "False").lower() not in [
"true",
"yes",
"1",
]:
from demisto_sdk.commands.common.logger import logging_setup

logging_setup(initial=True, calling_function="__init__")
3 changes: 3 additions & 0 deletions demisto_sdk/commands/common/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,18 +913,21 @@ def get_file(

def get_file_or_remote(file_path: Path, clear_cache=False):
content_path = get_content_path()
logger.debug(f'get_file_or_remote {content_path=}')
relative_file_path = None
if file_path.is_absolute():
absolute_file_path = file_path
try:
relative_file_path = file_path.relative_to(content_path)
logger.debug(f'get_file_or_remote {relative_file_path=}')
except ValueError:
logger.debug(
f"{file_path} is not a subpath of {content_path}. If the file does not exists locally, it could not be fetched."
)
else:
absolute_file_path = content_path / file_path
relative_file_path = file_path
logger.debug(f'get_file_or_remote {relative_file_path=}')
try:
return get_file(absolute_file_path, clear_cache=clear_cache)
except FileNotFoundError:
Expand Down
78 changes: 57 additions & 21 deletions demisto_sdk/commands/pre_commit/pre_commit_command.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import multiprocessing
import os
import re
Expand Down Expand Up @@ -118,6 +119,7 @@ def run_hook(
precommit_env: dict,
verbose: bool = False,
stdout: Optional[int] = subprocess.PIPE,
json_output_path: Optional[Path] = None
) -> int:
"""This function runs the pre-commit process and waits until finished.
We run this function in multithread.
Expand All @@ -127,17 +129,24 @@ def run_hook(
precommit_env (dict): The pre-commit environment variables
verbose (bool, optional): Whether print verbose output. Defaults to False.
stdout (Optional[int], optional): The way to handle stdout. Defaults to subprocess.PIPE.

json_output_path (Optional[Path]): Optional path to a JSON formatted output file/dir where pre-commit hooks
results are stored. None by deafult, and file is not created.
Returns:
int: return code - 0 if hook passed, 1 if failed
"""
logger.debug(f"Running hook {hook_id}")

# Otherwise it's None and file won't be created or already a file's path.
if json_output_path and json_output_path.is_dir():
json_output_path = json_output_path / f'{hook_id}.json'

process = PreCommitRunner._run_pre_commit_process(
PRECOMMIT_CONFIG_MAIN_PATH,
precommit_env,
verbose,
stdout,
command=["run", "-a", hook_id],
json_output_path=json_output_path,
)

if process.stdout:
Expand All @@ -153,6 +162,7 @@ def _run_pre_commit_process(
verbose: bool,
stdout=None,
command: Optional[List[str]] = None,
json_output_path: Optional[Path] = None
) -> subprocess.CompletedProcess:
"""Runs a process of pre-commit

Expand All @@ -162,13 +172,15 @@ def _run_pre_commit_process(
verbose (bool): whether to print verbose output
stdout (optional): use `subprocess.PIPE` to capture stdout. Use None to print it. Defaults to None.
command (Optional[List[str]], optional): The pre-commit command to run. Defaults to None.

json_output_path (Optional[Path]): Optional path to a JSON formatted output file where pre-commit hooks
results are stored. None by deafult, and file is not created.
Returns:
_type_: _description_
"""
if command is None:
command = ["run", "-a"]
return subprocess.run(
output = subprocess.PIPE if json_output_path else stdout
completed_process = subprocess.run(
list(
filter(
None,
Expand All @@ -185,17 +197,24 @@ def _run_pre_commit_process(
),
env=precommit_env,
cwd=CONTENT_PATH,
stdout=stdout,
stderr=stdout,
stdout=output,
stderr=output,
universal_newlines=True,
)

if json_output_path:
with open(json_output_path, "w") as json_file:
json.dump(completed_process.__dict__, json_file, indent=4)

return completed_process

@staticmethod
def run(
pre_commit_context: PreCommitContext,
precommit_env: dict,
verbose: bool,
show_diff_on_failure: bool,
json_output_path: Optional[Path] = None
) -> int:
"""Execute the pre-commit hooks on the files.

Expand All @@ -204,7 +223,8 @@ def run(
precommit_env (dict): The environment variables dict.
verbose (bool): Whether run pre-commit in verbose mode.
show_diff_on_failure (bool): Whether to show diff when a hook fail or not.

json_output_path (Optional[Path]): Optional path to a JSON formatted output file/dir where pre-commit hooks
results are stored. None by deafult, and file is not created.
Returns:
int: The exit code - 0 if everything is valid.
"""
Expand All @@ -224,6 +244,8 @@ def run(
precommit_env,
verbose,
command=["install-hooks"],
json_output_path=json_output_path if not json_output_path.is_dir()
else json_output_path / 'install-hooks.json',
)

num_processes = cpu_count()
Expand All @@ -236,13 +258,20 @@ def run(
logger.debug(f"Running hook {original_hook_id} with {generated_hooks}")
hook_ids = generated_hooks.hook_ids
if generated_hooks.parallel and len(hook_ids) > 1:
# We shall not write results to the same file if running hooks in parallel, therefore,
# writing the results to a parallel directory.
if json_output_path and not json_output_path.is_dir():
json_output_path = json_output_path.parent / json_output_path.stem
json_output_path.mkdir(exist_ok=True)

with ThreadPool(num_processes) as pool:
current_hooks_exit_codes = pool.map(
partial(
PreCommitRunner.run_hook,
precommit_env=precommit_env,
verbose=verbose,
stdout=subprocess.PIPE,
json_output_path=json_output_path,
),
hook_ids,
)
Expand All @@ -252,7 +281,7 @@ def run(
hook_id,
precommit_env=precommit_env,
verbose=verbose,
stdout=None,
json_output_path=json_output_path,
)
for hook_id in hook_ids
]
Expand Down Expand Up @@ -284,6 +313,7 @@ def prepare_and_run(
show_diff_on_failure: bool = False,
exclude_files: Optional[Set[Path]] = None,
dry_run: bool = False,
json_output_path: Optional[Path] = None
) -> int:
"""Trigger the relevant hooks.

Expand All @@ -293,7 +323,8 @@ def prepare_and_run(
show_diff_on_failure (bool, optional): Whether to show diff when a hook fail or not. Defaults to False.
exclude_files (Optional[Set[Path]], optional): Files to exclude when running. Defaults to None.
dry_run (bool, optional): Whether to run the pre-commit hooks in dry-run mode. Defaults to False.

json_output_path (Path, optional): Optional path to a JSON formatted output file/dir where pre-commit hooks
results are stored. None by default, and file is not created.
Returns:
int: The exit code, 0 if nothing failed.
"""
Expand Down Expand Up @@ -345,8 +376,9 @@ def prepare_and_run(
f"Dry run, skipping pre-commit.\nConfig file saved to {PRECOMMIT_CONFIG_MAIN_PATH}"
)
return ret_val

ret_val = PreCommitRunner.run(
pre_commit_context, precommit_env, verbose, show_diff_on_failure
pre_commit_context, precommit_env, verbose, show_diff_on_failure, json_output_path
)
return ret_val

Expand Down Expand Up @@ -399,21 +431,20 @@ def group_by_language(
for integration_script_paths in more_itertools.chunked_even(
integrations_scripts_mapping.keys(), INTEGRATIONS_BATCH
):
with multiprocessing.Pool(processes=cpu_count()) as pool:
content_items = pool.map(BaseContent.from_path, integration_script_paths)
for content_item in content_items:
if not content_item or not isinstance(content_item, IntegrationScript):
continue
# content-item is a script/integration
integrations_scripts.add(content_item)
# Process each path sequentially
content_items = [BaseContent.from_path(path) for path in integration_script_paths]
for content_item in content_items:
if not content_item or not isinstance(content_item, IntegrationScript):
continue
# content-item is a script/integration
integrations_scripts.add(content_item)
logger.debug("Pre-Commit: Finished parsing all integrations and scripts")
exclude_integration_script = set()
for integration_script in integrations_scripts:
if (pack := integration_script.in_pack) and pack.object_id == API_MODULES_PACK:
# add api modules to the api_modules list, we will handle them later
api_modules.append(integration_script)
continue

if api_modules:
logger.debug("Pre-Commit: Starting to handle API Modules")
with ContentGraphInterface() as graph:
Expand Down Expand Up @@ -509,6 +540,7 @@ def pre_commit_manager(
docker_image: Optional[str] = None,
run_hook: Optional[str] = None,
pre_commit_template_path: Optional[Path] = None,
json_output_path: Optional[Path] = None
) -> int:
"""Run pre-commit hooks .

Expand All @@ -523,12 +555,15 @@ def pre_commit_manager(
force_run_hooks (Optional[List[str]], optional): List for hooks to force run. Defaults to None.
verbose (bool, optional): Whether run pre-commit in verbose mode. Defaults to False.
show_diff_on_failure (bool, optional): Whether show git diff after pre-commit failure. Defaults to False.
dry_run (bool, optional): Whether to run the pre-commit hooks in dry-run mode, which will only create the config file.
dry_run (bool, optional): Whether to run the pre-commit hooks in dry-run mode, which will only create the
config file.
run_docker_hooks (bool, optional): Whether to run docker based hooks or not.
image_ref: (str, optional): Override the image from YAML / native config file with this image reference.
docker_image: (str, optional): Override the `docker_image` property in the template file. This is a comma separated list of: `from-yml`, `native:dev`, `native:ga`, `native:candidate`.
docker_image: (str, optional): Override the `docker_image` property in the template file. This is a comma
separated list of: `from-yml`, `native:dev`, `native:ga`, `native:candidate`.
pre_commit_template_path (Path, optional): Path to the template pre-commit file.

json_output_path (Path, optional): Optional path to a JSON formatted output file/dir where pre-commit hooks results
are stored. None by default, and file is not created.
Returns:
int: Return code of pre-commit.
"""
Expand Down Expand Up @@ -597,6 +632,7 @@ def pre_commit_manager(
show_diff_on_failure,
exclude_files,
dry_run,
json_output_path,
)


Expand Down Expand Up @@ -694,4 +730,4 @@ def preprocess_files(
for file in files_to_run
}
# filter out files that are not in the content git repo (e.g in .gitignore)
return relative_paths & all_git_files
return relative_paths & all_git_files
2 changes: 1 addition & 1 deletion demisto_sdk/commands/split/ymlsplitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def extract_to_package_format(
yaml_out = f"{output_path}/{base_name}.yml"
logger.debug(f"Creating yml file: {yaml_out} ...")
if self.yml_data:
yaml_obj = self.yml_data
yaml_obj = self.yml_data.copy()
else:
yaml_obj = get_file(self.input, raise_on_error=True)

Expand Down
Loading