Skip to content

Commit

Permalink
Merge dcec540 into d35f9d9
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelFain committed Jun 19, 2024
2 parents d35f9d9 + dcec540 commit c5c235c
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .changelog/4351.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
changes:
- description: Fixed an issue where **pre-commit** and **validate** commands ignored changed files when running on an external contribution PR.
type: fix
pr_number: 4351
15 changes: 15 additions & 0 deletions demisto_sdk/commands/pre_commit/pre_commit_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,12 +662,27 @@ def preprocess_files(
raw_files = git_util._get_all_changed_files(prev_version)
if not commited_only:
raw_files = raw_files.union(staged_files)
if os.getenv("CONTRIB_BRANCH"):
"""
If this command runs on a build triggered by an external contribution PR,
the relevant modified files would have an "untracked" status in git.
The following code segment retrieves all relevant untracked file paths that were changed in the external contribution PR
and adds them to `raw_files`. See CIAC-10490 for more info.
"""
# filter out a string list of untracked files with a path that starts with "Packs/"
untracked_files_list = filter(
lambda f: f.startswith("Packs/"), git_util.repo.untracked_files
)
# convert the string list of untracked files to a set of Path object
untracked_files_paths = set(map(Path, untracked_files_list))
raw_files = raw_files.union(untracked_files_paths)
elif all_files:
raw_files = all_git_files
else:
raise ValueError(
"No files were given to run pre-commit on, and no flags were given."
)

files_to_run: Set[Path] = set()
for file in raw_files:
if file.is_dir():
Expand Down
24 changes: 24 additions & 0 deletions demisto_sdk/commands/pre_commit/tests/pre_commit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,30 @@ def test_preprocess_files_with_all_files(self, mocker):
output = preprocess_files(all_files=True)
assert output == expected_output

def test_preprocess_files_in_external_pr_use_case(self, mocker):
"""
Given:
- `CONTRIB_BRANCH` environment variable exists.
When:
- pre commit command is running in context of an external contribution PR.
Then:
- Collect all files within "Packs/" path, which represent changes made in a external contribution PR
and run the pre-commit command on them as well.
"""
expected_output = set([Path("Packs/modified.txt"), Path("Packs/untracked.txt")])
mocker.patch.object(
GitUtil, "_get_all_changed_files", return_value=expected_output
)
mocker.patch.dict(os.environ, {"CONTRIB_BRANCH": "true"})
mocker.patch.object(GitUtil, "_get_staged_files", return_value=set())
mocker.patch.object(GitUtil, "get_all_files", return_value=expected_output)
repo = mocker.patch(
"git.repo.base.Repo._get_untracked_files",
return_value=["Packs/untracked.txt"],
)
output = preprocess_files(use_git=True)
assert output == expected_output


def test_exclude_hooks_by_version(mocker, repo: Repo):
"""
Expand Down
14 changes: 14 additions & 0 deletions demisto_sdk/commands/validate/initializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ def get_unfiltered_changed_files_from_git(self) -> Tuple[Set, Set, Set]:
debug=True,
get_only_current_file_names=False,
)
if os.getenv("CONTRIB_BRANCH"):
"""
If this command runs on a build triggered by an external contribution PR,
the relevant modified files would have an "untracked" status in git.
The following code segment retrieves all relevant untracked files that were changed in the external contribution PR
and adds them to `modified_files`. See CIAC-10490 for more info.
"""
# filter out a string list of untracked files with a path that starts with "Packs/"
untracked_files_list = filter(
lambda f: f.startswith("Packs/"), self.git_util.repo.untracked_files
)
# convert the string list of untracked files to a set of Path object
untracked_files_paths = set(map(Path, untracked_files_list))
modified_files = modified_files.union(untracked_files_paths)

return modified_files, added_files, renamed_files

Expand Down
31 changes: 31 additions & 0 deletions demisto_sdk/commands/validate/tests/validators_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import tempfile
from pathlib import Path
from typing import Set
Expand All @@ -10,6 +11,7 @@

from demisto_sdk.commands.common.constants import INTEGRATIONS_DIR, GitStatuses
from demisto_sdk.commands.common.content_constant_paths import CONTENT_PATH
from demisto_sdk.commands.common.git_util import GitUtil
from demisto_sdk.commands.common.handlers import DEFAULT_JSON_HANDLER as json
from demisto_sdk.commands.content_graph.common import ContentType
from demisto_sdk.commands.content_graph.tests.test_tools import load_yaml
Expand Down Expand Up @@ -655,3 +657,32 @@ def test_description():
assert not [
validator for validator in get_all_validators() if not validator.description
]


def test_get_unfiltered_changed_files_from_git_in_external_pr_use_case(mocker):
"""
Given:
- `CONTRIB_BRANCH` environment variable exists.
When:
- validate command is running in context of an external contribution PR.
Then:
- Collect all files within "Packs/" path, which represent changes made in a external contribution PR
and run the validate command on them as well.
"""
expected_output = set([Path("Packs/modified.txt"), Path("Packs/untracked.txt")])
# mocker.patch.object(
# GitUtil, "_get_all_changed_files", return_value=expected_output
# )
mocker.patch.dict(os.environ, {"CONTRIB_BRANCH": "true"})
initializer = Initializer()
initializer.validate_git_installed()
mocker.patch.object(
GitUtil, "modified_files", return_value={Path("Packs/modified.txt")}
)
mocker.patch.object(GitUtil, "added_files", return_value={})
mocker.patch.object(GitUtil, "renamed_files", return_value={})
repo = mocker.patch(
"git.repo.base.Repo._get_untracked_files", return_value=["Packs/untracked.txt"]
)
output = initializer.get_unfiltered_changed_files_from_git()
assert output[0] == expected_output

0 comments on commit c5c235c

Please sign in to comment.