Skip to content

Commit 7911241

Browse files
committed
Add failed directory handling to JSON review processing script
Implemented functionality to move invalid and failed JSON files to a dedicated 'failed' directory. This enhancement improves error management by organizing problematic files, allowing for easier troubleshooting and review of processing issues.
1 parent 0f5deee commit 7911241

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Scripts/process_json_reviews.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
REPO_NAME = "Graph-Python-scripts"
2727
REVIEWS_DIR = Path("reviews")
2828
PROCESSED_DIR = Path("reviews/processed")
29+
FAILED_DIR = Path("reviews/failed")
2930

3031
# Method names to validate
3132
METHODS = [
@@ -181,6 +182,14 @@ def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]:
181182
is_valid, error_msg = validate_review(review_data)
182183
if not is_valid:
183184
print(f" ✗ Validation failed: {error_msg}")
185+
# Move invalid file to failed directory
186+
try:
187+
FAILED_DIR.mkdir(parents=True, exist_ok=True)
188+
failed_path = FAILED_DIR / file_path.name
189+
shutil.move(str(file_path), str(failed_path))
190+
print(f" → Moved invalid file to: {failed_path}")
191+
except Exception as move_error:
192+
print(f" ⚠ Could not move invalid file: {move_error}", file=sys.stderr)
184193
return False, error_msg
185194
print(f" ✓ Validation passed")
186195

@@ -189,6 +198,14 @@ def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]:
189198
issue_number = create_github_issue(repo, review_data)
190199
if issue_number is None:
191200
print(f" ✗ Failed to create GitHub Issue")
201+
# Move failed file to failed directory
202+
try:
203+
FAILED_DIR.mkdir(parents=True, exist_ok=True)
204+
failed_path = FAILED_DIR / file_path.name
205+
shutil.move(str(file_path), str(failed_path))
206+
print(f" → Moved failed file to: {failed_path}")
207+
except Exception as move_error:
208+
print(f" ⚠ Could not move failed file: {move_error}", file=sys.stderr)
192209
return False, "Failed to create GitHub Issue"
193210
print(f" ✓ Created GitHub Issue #{issue_number}")
194211

@@ -203,6 +220,15 @@ def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]:
203220

204221
except json.JSONDecodeError as e:
205222
print(f" ✗ JSON decode error: {e}")
223+
# Move invalid JSON file to failed directory
224+
try:
225+
FAILED_DIR.mkdir(parents=True, exist_ok=True)
226+
failed_path = FAILED_DIR / file_path.name
227+
if file_path.exists():
228+
shutil.move(str(file_path), str(failed_path))
229+
print(f" → Moved invalid JSON file to: {failed_path}")
230+
except Exception as move_error:
231+
print(f" ⚠ Could not move invalid file: {move_error}", file=sys.stderr)
206232
return False, f"Invalid JSON: {e}"
207233
except FileNotFoundError as e:
208234
print(f" ✗ File not found: {e}")
@@ -214,6 +240,14 @@ def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]:
214240
print(f" ✗ Unexpected error: {e}")
215241
import traceback
216242
traceback.print_exc()
243+
# Move failed file to failed directory
244+
try:
245+
FAILED_DIR.mkdir(parents=True, exist_ok=True)
246+
failed_path = FAILED_DIR / file_path.name
247+
shutil.move(str(file_path), str(failed_path))
248+
print(f" → Moved failed file to: {failed_path}")
249+
except Exception as move_error:
250+
print(f" ⚠ Could not move failed file: {move_error}", file=sys.stderr)
217251
return False, f"Error processing file: {e}"
218252

219253

@@ -248,8 +282,10 @@ def main() -> None:
248282
print(f"\nChecking directories...")
249283
os.makedirs(REVIEWS_DIR, exist_ok=True)
250284
os.makedirs(PROCESSED_DIR, exist_ok=True)
285+
os.makedirs(FAILED_DIR, exist_ok=True)
251286
print(f"✓ Reviews directory: {REVIEWS_DIR}")
252287
print(f"✓ Processed directory: {PROCESSED_DIR}")
288+
print(f"✓ Failed directory: {FAILED_DIR}")
253289

254290
# Find all JSON files in reviews directory (exclude processed directory)
255291
print(f"\nScanning for JSON files in {REVIEWS_DIR}...")

0 commit comments

Comments
 (0)