11# -*- coding: utf-8 -*-
22import subprocess
3+ import os
34
45from .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