Skip to content

Commit

Permalink
Add pre-commit logs in CI (#4581)
Browse files Browse the repository at this point in the history
* Dynamically add log_file attribute to all hooks

* Change log_file path

* Add log

* Add log

* Create hook logs path in artifats folder

* Update log folder path

* Update artifacts path calcualtion

* Update log_file path logic

* Update hook log file path

* Update log_file creation if statement logic

* Add changlog

* ruff
  • Loading branch information
samuelFain authored Sep 26, 2024
1 parent 4f81228 commit 4591c25
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/4581.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Added support for `pre-commit` hooks to save logs as Gitlab CI artifacts.
type: internal
pr_number: 4581
6 changes: 6 additions & 0 deletions demisto_sdk/commands/pre_commit/pre_commit_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
PRECOMMIT_FOLDER = CACHE_DIR / "pre-commit"
PRECOMMIT_CONFIG = PRECOMMIT_FOLDER / "config"
PRECOMMIT_CONFIG_MAIN_PATH = PRECOMMIT_CONFIG / "pre-commit-config-main.yaml"
ARTIFACTS_FOLDER = os.getenv("ARTIFACTS_FOLDER")
HOOK_LOG_PATH = Path(ARTIFACTS_FOLDER) / "pre-commit" if ARTIFACTS_FOLDER else None

# This has to be relative to content path so the docker will be able to write to it
PRE_COMMIT_FOLDER_SHARED = CONTENT_PATH / ".pre-commit"
Expand Down Expand Up @@ -62,6 +64,8 @@ def __post_init__(self):
shutil.rmtree(PRE_COMMIT_FOLDER_SHARED, ignore_errors=True)
PRECOMMIT_FOLDER.mkdir(parents=True)
PRECOMMIT_CONFIG.mkdir()
if HOOK_LOG_PATH:
HOOK_LOG_PATH.mkdir(parents=True, exist_ok=True)
self.precommit_template: dict = get_file_or_remote(
self.pre_commit_template_path
)
Expand Down Expand Up @@ -126,6 +130,8 @@ def _get_hooks(self, pre_commit_config: dict) -> dict:
for repo in pre_commit_config.get("repos", []):
new_hooks = []
for hook in repo.get("hooks", []):
if (not hook.get("log_file")) and HOOK_LOG_PATH:
hook["log_file"] = f"{HOOK_LOG_PATH}/{hook['id']}.log"
if not self.run_docker_hooks and hook["id"].endswith("in-docker"):
continue
if (self.run_hook and self.run_hook in hook["id"]) or (
Expand Down

0 comments on commit 4591c25

Please sign in to comment.