Skip to content

Commit 2d31fc7

Browse files
authored
Merge branch 'ComplianceAsCode:main' into add-branch-configure
2 parents 1bbd4c9 + 190cbc2 commit 2d31fc7

File tree

4 files changed

+59
-2
lines changed

4 files changed

+59
-2
lines changed

harvest/cli.py

+7
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ def _init_arguments(self):
116116
metavar="YYYY-MM-DD or YYYYMMDD",
117117
default=False,
118118
)
119+
self.add_argument(
120+
"--include-file-path",
121+
help="Should the file path be included in the saved file names",
122+
action="store_true",
123+
dest="include_file_path",
124+
)
119125

120126
def _validate_arguments(self, args):
121127
if not args.end:
@@ -145,6 +151,7 @@ def _run(self, args):
145151
args.branch,
146152
args.repo_path,
147153
args.no_validate,
154+
include_file_path=args.include_file_path,
148155
)
149156

150157
for file in args.filepath:

harvest/collator.py

+16-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@
2727
class Collator(object):
2828
"""Harvest collator to retrieve Git repository content."""
2929

30-
def __init__(self, repo_url, creds, branch, repo_path=None, validate=True):
30+
def __init__(
31+
self,
32+
repo_url,
33+
creds,
34+
branch,
35+
repo_path=None,
36+
validate=True,
37+
include_file_path=False,
38+
):
3139
"""Construct the Collator object."""
3240
parsed = urlparse(repo_url)
3341
self.scheme = parsed.scheme
@@ -38,6 +46,7 @@ def __init__(self, repo_url, creds, branch, repo_path=None, validate=True):
3846
self.repo_path = repo_path
3947
self.git_repo = None
4048
self.validate = validate
49+
self.include_file_path = include_file_path
4150

4251
@property
4352
def local_path(self):
@@ -84,16 +93,21 @@ def read(self, filepath, from_dt, until_dt):
8493
raise FileMissingError(f"{filepath} not found between {since} and {until}")
8594
return commits
8695

87-
def write(self, filepath, commits):
96+
def write(self, filepath: str, commits):
8897
"""
8998
Create file artifacts.
9099
91100
:param str filepath: The relative path to the file within the repo
92101
:param list commits: A list of commits for a given file and date range
93102
"""
103+
file_path_include = ""
104+
if self.include_file_path:
105+
file_path_include = "_".join(filepath.rsplit("/")[:-1]) + "_"
106+
94107
for commit in commits:
95108
file_name = (
96109
f"./{self._ts_to_str(commit.committed_date)}_"
110+
f"{file_path_include}"
97111
f'{filepath.rsplit("/", 1).pop()}'
98112
)
99113
with open(file_name, "w+") as f:

test/test_cli_collate.py

+24
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,27 @@ def test_collate_local(self, mock_read, mock_write):
307307
datetime(today.year, today.month, today.day),
308308
)
309309
mock_write.assert_called_once_with("my/path/baz.json", ["commit-foo"])
310+
311+
@patch("harvest.collator.Collator.write")
312+
@patch("harvest.collator.Collator.read")
313+
def test_collate_include_file_path(self, mock_read, mock_write):
314+
"""Ensures collate sub-command works when '--include-file-path' is provided."""
315+
mock_read.return_value = ["commit-foo"]
316+
self.harvest.run(
317+
[
318+
"collate",
319+
"local",
320+
"my/path/baz.json",
321+
"--include-file-path",
322+
"--repo-path",
323+
"os/repo/path",
324+
]
325+
)
326+
today = datetime.today()
327+
328+
mock_read.assert_called_once_with(
329+
"my/path/baz.json",
330+
datetime(today.year, today.month, today.day),
331+
datetime(today.year, today.month, today.day),
332+
)
333+
mock_write.assert_called_once_with("my/path/baz.json", ["commit-foo"])

test/test_collator.py

+12
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,18 @@ def test_write_functionality(self):
153153
self.assertIn(call("./20191105_foo.json", "w+"), m.mock_calls)
154154
self.assertIn(call("./20191101_foo.json", "w+"), m.mock_calls)
155155

156+
def test_write_includes_file_path(self):
157+
m = mock_open()
158+
with patch("builtins.open", m):
159+
collator = Collator(*self.args, include_file_path=True)
160+
collator.write("raw/foo/foo.json", self.commits)
161+
handle = m()
162+
163+
self.assertEqual(handle.write.call_count, 3)
164+
self.assertIn(call("./20191106_raw_foo_foo.json", "w+"), m.mock_calls)
165+
self.assertIn(call("./20191105_raw_foo_foo.json", "w+"), m.mock_calls)
166+
self.assertIn(call("./20191101_raw_foo_foo.json", "w+"), m.mock_calls)
167+
156168
@patch("harvest.collator.git.Repo.clone_from")
157169
@patch("harvest.collator.os.path.isdir")
158170
def test_checkout_clone(self, is_dir_mock, clone_from_mock):

0 commit comments

Comments
 (0)