Skip to content

Commit 4feb98c

Browse files
committed
Enhance JSON review processing script to handle GitHub connection failures
Updated the script to check for GitHub repository availability before creating issues. If the connection fails, files are moved to a 'failed' directory for manual review, improving error handling and user feedback. Adjusted function signatures to accept optional repository parameters.
1 parent 5f48b55 commit 4feb98c

File tree

1 file changed

+33
-16
lines changed

1 file changed

+33
-16
lines changed

Scripts/process_json_reviews.py

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,11 @@ def format_issue_body(review_data: Dict[str, Any]) -> str:
109109
return "\n".join(body_lines)
110110

111111

112-
def ensure_labels_exist(repo: Any) -> None:
112+
def ensure_labels_exist(repo: Optional[Any]) -> None:
113113
"""Ensure required labels exist in the repository."""
114+
if repo is None:
115+
return # Skip label check if repo is not available
116+
114117
required_labels = {
115118
"review": "1f76b4", # Blue
116119
"correct": "0e8a16", # Green
@@ -139,7 +142,7 @@ def ensure_labels_exist(repo: Any) -> None:
139142
print(f" ✓ Label already exists: {label_name}")
140143

141144

142-
def create_github_issue(repo: Any, review_data: Dict[str, Any]) -> Optional[int]:
145+
def create_github_issue(repo: Optional[Any], review_data: Dict[str, Any]) -> Optional[int]:
143146
"""Create a GitHub Issue from review data."""
144147
try:
145148
# Ensure labels exist
@@ -168,7 +171,7 @@ def create_github_issue(repo: Any, review_data: Dict[str, Any]) -> Optional[int]
168171
return None
169172

170173

171-
def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]:
174+
def process_json_file(file_path: Path, repo: Optional[Any]) -> Tuple[bool, Optional[str]]:
172175
"""Process a single JSON review file."""
173176
try:
174177
print(f" Reading file: {file_path}")
@@ -193,7 +196,18 @@ def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]:
193196
return False, error_msg
194197
print(f" ✓ Validation passed")
195198

196-
# Create GitHub Issue
199+
# Create GitHub Issue (if repo is available)
200+
if repo is None:
201+
print(f" ⚠ GitHub connection unavailable, moving file to failed/ for manual review")
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 to: {failed_path} (will retry when GitHub is available)")
207+
except Exception as move_error:
208+
print(f" ⚠ Could not move file: {move_error}", file=sys.stderr)
209+
return False, "GitHub connection unavailable"
210+
197211
print(f" Creating GitHub Issue...")
198212
issue_number = create_github_issue(repo, review_data)
199213
if issue_number is None:
@@ -266,18 +280,6 @@ def main() -> None:
266280
else:
267281
print(f"✓ GITHUB_TOKEN is set (length: {len(github_token)})")
268282

269-
# Initialize GitHub client
270-
try:
271-
print(f"\nConnecting to GitHub repository: {REPO_OWNER}/{REPO_NAME}")
272-
g = Github(github_token)
273-
repo = g.get_repo(f"{REPO_OWNER}/{REPO_NAME}")
274-
print(f"✓ Successfully connected to repository")
275-
except Exception as e:
276-
print(f"✗ Error: Failed to connect to GitHub repository: {e}", file=sys.stderr)
277-
import traceback
278-
traceback.print_exc()
279-
sys.exit(2)
280-
281283
# Ensure directories exist
282284
print(f"\nChecking directories...")
283285
os.makedirs(REVIEWS_DIR, exist_ok=True)
@@ -303,6 +305,21 @@ def main() -> None:
303305
print("Exiting successfully (no files to process)")
304306
return
305307

308+
# Only connect to GitHub if we have files to process
309+
repo = None
310+
print(f"\nConnecting to GitHub repository: {REPO_OWNER}/{REPO_NAME}")
311+
try:
312+
g = Github(github_token)
313+
repo = g.get_repo(f"{REPO_OWNER}/{REPO_NAME}")
314+
print(f"✓ Successfully connected to repository")
315+
except Exception as e:
316+
print(f"✗ Error: Failed to connect to GitHub repository: {e}", file=sys.stderr)
317+
print(f"⚠ Warning: Cannot connect to GitHub. Files will be moved to failed/ for manual review.")
318+
import traceback
319+
traceback.print_exc()
320+
# Don't exit - continue to handle files locally
321+
repo = None
322+
306323
# Process each file
307324
processed_count = 0
308325
failed_count = 0

0 commit comments

Comments
 (0)