|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Process JSON review files and create GitHub Issues. |
| 4 | +
|
| 5 | +Reads JSON review files from /reviews/ directory, validates them, |
| 6 | +creates GitHub Issues, and moves processed files to /reviews/processed/. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | +import shutil |
| 12 | +import sys |
| 13 | +from pathlib import Path |
| 14 | +from typing import Dict, Any, Optional, List, Tuple |
| 15 | +from datetime import datetime |
| 16 | + |
| 17 | +try: |
| 18 | + from github import Github |
| 19 | +except ImportError: |
| 20 | + print("Error: PyGithub not installed. Run: pip install PyGithub", file=sys.stderr) |
| 21 | + sys.exit(1) |
| 22 | + |
| 23 | + |
| 24 | +# Repository configuration |
| 25 | +REPO_OWNER = "SingularityNET-Archive" |
| 26 | +REPO_NAME = "Graph-Python-scripts" |
| 27 | +REVIEWS_DIR = Path("reviews") |
| 28 | +PROCESSED_DIR = Path("reviews/processed") |
| 29 | + |
| 30 | +# Method names to validate |
| 31 | +METHODS = [ |
| 32 | + "coattendance", |
| 33 | + "field-degree", |
| 34 | + "path-structure", |
| 35 | + "centrality", |
| 36 | + "clustering", |
| 37 | + "components", |
| 38 | +] |
| 39 | + |
| 40 | +# Valid ratings |
| 41 | +VALID_RATINGS = ["correct", "needs-review", "incorrect"] |
| 42 | + |
| 43 | +# Required fields in review JSON |
| 44 | +REQUIRED_FIELDS = ["method", "rating", "comment", "timestamp"] |
| 45 | + |
| 46 | + |
| 47 | +def validate_review(review_data: Dict[str, Any]) -> Tuple[bool, Optional[str]]: |
| 48 | + """Validate review JSON structure.""" |
| 49 | + # Check required fields |
| 50 | + for field in REQUIRED_FIELDS: |
| 51 | + if field not in review_data: |
| 52 | + return False, f"Missing required field: {field}" |
| 53 | + |
| 54 | + # Validate method |
| 55 | + if review_data["method"] not in METHODS: |
| 56 | + return False, f"Invalid method: {review_data['method']}. Must be one of {METHODS}" |
| 57 | + |
| 58 | + # Validate rating |
| 59 | + if review_data["rating"] not in VALID_RATINGS: |
| 60 | + return False, f"Invalid rating: {review_data['rating']}. Must be one of {VALID_RATINGS}" |
| 61 | + |
| 62 | + # Validate comment is not empty |
| 63 | + if not review_data.get("comment", "").strip(): |
| 64 | + return False, "Comment field cannot be empty" |
| 65 | + |
| 66 | + return True, None |
| 67 | + |
| 68 | + |
| 69 | +def format_issue_body(review_data: Dict[str, Any]) -> str: |
| 70 | + """Format review data as GitHub Issue body.""" |
| 71 | + body_lines = [ |
| 72 | + "## Analysis Method", |
| 73 | + f"**Method:** {review_data['method']}", |
| 74 | + "", |
| 75 | + f"**File:** {review_data.get('file', 'docs/index.html')}", |
| 76 | + "", |
| 77 | + "---", |
| 78 | + "", |
| 79 | + "## Rating", |
| 80 | + "", |
| 81 | + f"- [x] {review_data['rating'].replace('-', ' ').title()}", |
| 82 | + "", |
| 83 | + "## Comments", |
| 84 | + "", |
| 85 | + review_data.get("comment", ""), |
| 86 | + ] |
| 87 | + |
| 88 | + if review_data.get("suggestions"): |
| 89 | + body_lines.extend([ |
| 90 | + "", |
| 91 | + "---", |
| 92 | + "", |
| 93 | + "## Suggestions", |
| 94 | + "", |
| 95 | + review_data["suggestions"], |
| 96 | + ]) |
| 97 | + |
| 98 | + if review_data.get("reviewer"): |
| 99 | + body_lines.extend([ |
| 100 | + "", |
| 101 | + "---", |
| 102 | + "", |
| 103 | + f"**Reviewed by:** {review_data['reviewer']}", |
| 104 | + f"**Review ID:** {review_data.get('id', 'unknown')}", |
| 105 | + f"**Submitted:** {review_data.get('timestamp', 'unknown')}", |
| 106 | + ]) |
| 107 | + |
| 108 | + return "\n".join(body_lines) |
| 109 | + |
| 110 | + |
| 111 | +def create_github_issue(repo: Any, review_data: Dict[str, Any]) -> Optional[int]: |
| 112 | + """Create a GitHub Issue from review data.""" |
| 113 | + try: |
| 114 | + title = f"[Review] {review_data['method'].replace('-', ' ').title()}" |
| 115 | + body = format_issue_body(review_data) |
| 116 | + |
| 117 | + # Labels to apply |
| 118 | + labels = ["review", review_data["rating"]] |
| 119 | + |
| 120 | + # Create issue |
| 121 | + issue = repo.create_issue( |
| 122 | + title=title, |
| 123 | + body=body, |
| 124 | + labels=labels |
| 125 | + ) |
| 126 | + |
| 127 | + print(f" ✓ Created issue #{issue.number}: {title}") |
| 128 | + return issue.number |
| 129 | + |
| 130 | + except Exception as e: |
| 131 | + print(f" ✗ Error creating issue: {e}", file=sys.stderr) |
| 132 | + return None |
| 133 | + |
| 134 | + |
| 135 | +def process_json_file(file_path: Path, repo: Any) -> Tuple[bool, Optional[str]]: |
| 136 | + """Process a single JSON review file.""" |
| 137 | + try: |
| 138 | + # Read and parse JSON |
| 139 | + with open(file_path, 'r') as f: |
| 140 | + review_data = json.load(f) |
| 141 | + |
| 142 | + # Validate review |
| 143 | + is_valid, error_msg = validate_review(review_data) |
| 144 | + if not is_valid: |
| 145 | + return False, error_msg |
| 146 | + |
| 147 | + # Create GitHub Issue |
| 148 | + issue_number = create_github_issue(repo, review_data) |
| 149 | + if issue_number is None: |
| 150 | + return False, "Failed to create GitHub Issue" |
| 151 | + |
| 152 | + # Move file to processed directory |
| 153 | + processed_path = PROCESSED_DIR / file_path.name |
| 154 | + os.makedirs(PROCESSED_DIR, exist_ok=True) |
| 155 | + shutil.move(str(file_path), str(processed_path)) |
| 156 | + |
| 157 | + return True, None |
| 158 | + |
| 159 | + except json.JSONDecodeError as e: |
| 160 | + return False, f"Invalid JSON: {e}" |
| 161 | + except Exception as e: |
| 162 | + return False, f"Error processing file: {e}" |
| 163 | + |
| 164 | + |
| 165 | +def main() -> None: |
| 166 | + """Main function to process JSON review files.""" |
| 167 | + # Get GitHub token from environment |
| 168 | + github_token = os.environ.get("GITHUB_TOKEN") |
| 169 | + if not github_token: |
| 170 | + print("Error: GITHUB_TOKEN environment variable not set", file=sys.stderr) |
| 171 | + print("Note: For GitHub Actions, this is automatically set as GITHUB_TOKEN", file=sys.stderr) |
| 172 | + sys.exit(1) |
| 173 | + |
| 174 | + # Initialize GitHub client |
| 175 | + g = Github(github_token) |
| 176 | + repo = g.get_repo(f"{REPO_OWNER}/{REPO_NAME}") |
| 177 | + |
| 178 | + # Ensure directories exist |
| 179 | + os.makedirs(REVIEWS_DIR, exist_ok=True) |
| 180 | + os.makedirs(PROCESSED_DIR, exist_ok=True) |
| 181 | + |
| 182 | + # Find all JSON files in reviews directory (not in processed) |
| 183 | + json_files = list(REVIEWS_DIR.glob("*.json")) |
| 184 | + |
| 185 | + if not json_files: |
| 186 | + print("No JSON review files found in reviews/ directory") |
| 187 | + return |
| 188 | + |
| 189 | + print(f"Found {len(json_files)} JSON review file(s) to process") |
| 190 | + |
| 191 | + # Process each file |
| 192 | + processed_count = 0 |
| 193 | + failed_count = 0 |
| 194 | + failed_files = [] |
| 195 | + |
| 196 | + for json_file in json_files: |
| 197 | + print(f"\nProcessing: {json_file.name}") |
| 198 | + success, error_msg = process_json_file(json_file, repo) |
| 199 | + |
| 200 | + if success: |
| 201 | + processed_count += 1 |
| 202 | + else: |
| 203 | + failed_count += 1 |
| 204 | + failed_files.append((json_file.name, error_msg)) |
| 205 | + print(f" ✗ Failed: {error_msg}") |
| 206 | + |
| 207 | + # Summary |
| 208 | + print(f"\n{'='*50}") |
| 209 | + print(f"Processing Summary:") |
| 210 | + print(f" ✓ Successfully processed: {processed_count}") |
| 211 | + print(f" ✗ Failed: {failed_count}") |
| 212 | + |
| 213 | + if failed_files: |
| 214 | + print(f"\nFailed files:") |
| 215 | + for filename, error in failed_files: |
| 216 | + print(f" - {filename}: {error}") |
| 217 | + |
| 218 | + if failed_count > 0: |
| 219 | + sys.exit(1) |
| 220 | + |
| 221 | + |
| 222 | +if __name__ == "__main__": |
| 223 | + main() |
| 224 | + |
0 commit comments