Skip to content

Commit 94efee6

Browse files
committed
Commit file deletion as well as archiving
1 parent 397e205 commit 94efee6

File tree

3 files changed

+71
-9
lines changed

3 files changed

+71
-9
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[metadata]
22
name = IntuneCD
3-
version = 2.4.1.beta1
3+
version = 2.4.1.beta2
44
author = Tobias Almén
55
author_email = almenscorner@outlook.com
66
description = Tool to backup and update configurations in Intune

src/IntuneCD/intunecdlib/archive.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,21 @@ def archive_file(self, file, root):
4242
shutil.move(src, dst)
4343

4444
if self.audit_data:
45-
self._handle_audit_commit(file, dst, archive_path)
45+
self._handle_audit_commit(file, dst, archive_path, src)
4646

4747
def _get_audit_delete_events(self) -> list:
4848
"""Gets all delete events from the audit log from the last 24h
4949
5050
Returns:
5151
list: A list of all delete events
5252
"""
53-
start_date = (datetime.now(timezone.utc) - timedelta(days=1)).isoformat()
53+
if not os.getenv("AUDIT_DAYS_BACK"):
54+
days_back = 1
55+
else:
56+
days_back = int(os.getenv("AUDIT_DAYS_BACK"))
57+
start_date = (
58+
datetime.now(timezone.utc) - timedelta(days=days_back)
59+
).isoformat()
5460
end_date = datetime.now(timezone.utc).isoformat()
5561

5662
q_params = {
@@ -69,7 +75,7 @@ def _get_audit_delete_events(self) -> list:
6975

7076
return audit_data
7177

72-
def _handle_audit_commit(self, filename, filepath, archive_path):
78+
def _handle_audit_commit(self, filename, filepath, archive_path, source_file):
7379
"""Handles the audit commit for the file
7480
7581
Args:
@@ -109,6 +115,17 @@ def _handle_audit_commit(self, filename, filepath, archive_path):
109115
}
110116

111117
self.filename = os.path.splitext(os.path.basename(filepath))[0]
118+
# process audit and check for deleted files
119+
self.process_audit_data.process_audit_data(
120+
audit_data_record,
121+
None,
122+
archive_path,
123+
filepath,
124+
get_record=False,
125+
record=audit_data_record,
126+
source_file=source_file,
127+
)
128+
# process audit and check for archived files
112129
self.process_audit_data.process_audit_data(
113130
audit_data_record,
114131
None,

src/IntuneCD/intunecdlib/process_audit_data.py

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# -*- coding: utf-8 -*-
22
import subprocess
3+
import os
34

45
from .IntuneCDBase import IntuneCDBase
56

@@ -128,7 +129,30 @@ def _git_check_new_file(self, path, file):
128129

129130
return False
130131

131-
def _git_commit_changes(self, audit_record, path, file):
132+
def _git_check_deleted_file(self, path, file):
133+
"""Checks if the file is deleted.
134+
135+
Args:
136+
path: The path to the git repo.
137+
file: The file to check.
138+
"""
139+
140+
# check if it is a deleted file
141+
cmd = ["git", "-C", path, "ls-files", "--deleted", f"{file}"]
142+
self.log(
143+
function="_git_check_deleted_file",
144+
msg=f"Running command {cmd} to check if {file} is a deleted file.",
145+
)
146+
deleted_file_result = subprocess.run(
147+
cmd, capture_output=True, text=True, check=False
148+
)
149+
# check if "deleted" is in the stdout
150+
if deleted_file_result.stdout:
151+
return True
152+
153+
return False
154+
155+
def _git_commit_changes(self, audit_record, path, file, deleted=False):
132156
"""
133157
Commits the changes to the git repo.
134158
@@ -137,7 +161,10 @@ def _git_commit_changes(self, audit_record, path, file):
137161
:param file: The file to commit.
138162
"""
139163
# commit the changes
140-
cmd = ["git", "-C", path, "add", f"{file}"]
164+
if deleted:
165+
cmd = ["git", "-C", path, "rm", f"{file}"]
166+
else:
167+
cmd = ["git", "-C", path, "add", f"{file}"]
141168
self.log(
142169
function="_git_commit_changes",
143170
msg=f"Running command {cmd} to add {file} to the git repo.",
@@ -195,7 +222,14 @@ def _get_payload_from_audit_data(self, audit_data, compare_data):
195222
return records
196223

197224
def process_audit_data(
198-
self, audit_data, compare_data, path, file, get_record=True, record=None
225+
self,
226+
audit_data,
227+
compare_data,
228+
path,
229+
file,
230+
get_record=True,
231+
record=None,
232+
source_file=None,
199233
):
200234
"""
201235
Processes the audit data from Intune.
@@ -233,10 +267,21 @@ def process_audit_data(
233267

234268
if not result:
235269
file_not_found = self._git_check_new_file(path, file)
270+
if source_file:
271+
path = os.path.dirname(source_file)
272+
file_deleted = self._git_check_deleted_file(path, source_file)
273+
else:
274+
file_deleted = self._git_check_deleted_file(path, file)
236275

237-
if result or file_not_found:
276+
if result or file_not_found or file_deleted:
238277
# commit the changes
239-
self._git_commit_changes(record, path, file)
278+
if file_deleted and source_file:
279+
path = os.path.dirname(source_file)
280+
self._git_commit_changes(record, path, source_file, deleted=True)
281+
elif file_deleted:
282+
self._git_commit_changes(record, path, file, deleted=True)
283+
else:
284+
self._git_commit_changes(record, path, file)
240285
else:
241286
self.log(
242287
function="process_audit_data",

0 commit comments

Comments
 (0)