-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub_integration.py
More file actions
651 lines (510 loc) · 23.3 KB
/
github_integration.py
File metadata and controls
651 lines (510 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"""
GitHub Commit Integration for AI Impact Prediction
===================================================
This script:
1. Analyzes a GitHub commit to extract changed components
2. Checks which components have tests
3. Fetches real-time context (failures, deployment window)
4. Triggers the ML model to predict cascade risk
5. Posts results back to GitHub (PR comment, status check)
Author: Testing & AI Bootcamp
"""
import os
import sys
import json
import subprocess
import re
from pathlib import Path
from datetime import datetime
from typing import Dict, List, Set, Tuple
import requests
# Import our existing prediction model
sys.path.append(str(Path(__file__).parent))
from ecommerce_impact_prediction import (
EcommerceComponentGraph,
HistoricalDataGenerator,
ImpactPredictor
)
class ComponentAnnotationParser:
"""Parse component annotations from source code"""
ANNOTATION_PATTERN = r'@components:\s*([A-Za-z_]+)'
TEST_ANNOTATION_PATTERN = r'@tests:\s*([A-Za-z_,\s]+)'
@staticmethod
def extract_components_from_file(filepath: str) -> Set[str]:
"""Extract all component annotations from a file"""
components = set()
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
matches = re.findall(ComponentAnnotationParser.ANNOTATION_PATTERN, content)
components.update(matches)
except Exception as e:
print(f"Warning: Could not parse {filepath}: {e}")
return components
@staticmethod
def extract_tested_components_from_file(filepath: str) -> Set[str]:
"""Extract components tested by a test file"""
tested_components = set()
try:
with open(filepath, 'r', encoding='utf-8') as f:
content = f.read()
matches = re.findall(ComponentAnnotationParser.TEST_ANNOTATION_PATTERN, content)
for match in matches:
# Split by comma and clean up whitespace
components = [c.strip() for c in match.split(',')]
tested_components.update(components)
except Exception as e:
print(f"Warning: Could not parse test file {filepath}: {e}")
return tested_components
class GitCommitAnalyzer:
"""Analyze Git commits to extract component changes"""
def __init__(self, repo_path: str = "."):
self.repo_path = repo_path
def get_changed_files(self, commit_sha: str = "HEAD") -> List[str]:
"""Get list of files changed in a commit"""
try:
cmd = ["git", "diff-tree", "--no-commit-id", "--name-only", "-r", commit_sha]
result = subprocess.run(
cmd,
cwd=self.repo_path,
capture_output=True,
text=True,
check=True
)
files = result.stdout.strip().split('\n')
return [f for f in files if f] # Filter empty strings
except subprocess.CalledProcessError as e:
print(f"Error getting changed files: {e}")
return []
def get_file_changes(self, filepath: str, commit_sha: str = "HEAD") -> Dict[str, int]:
"""Get additions and deletions for a file"""
try:
cmd = ["git", "diff", f"{commit_sha}^", commit_sha, "--numstat", "--", filepath]
result = subprocess.run(
cmd,
cwd=self.repo_path,
capture_output=True,
text=True,
check=True
)
output = result.stdout.strip()
if output:
parts = output.split('\t')
additions = int(parts[0]) if parts[0] != '-' else 0
deletions = int(parts[1]) if parts[1] != '-' else 0
return {"additions": additions, "deletions": deletions}
except Exception as e:
print(f"Error getting file changes for {filepath}: {e}")
return {"additions": 0, "deletions": 0}
def get_commit_message(self, commit_sha: str = "HEAD") -> str:
"""Get commit message"""
try:
cmd = ["git", "log", "-1", "--pretty=%B", commit_sha]
result = subprocess.run(
cmd,
cwd=self.repo_path,
capture_output=True,
text=True,
check=True
)
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"Error getting commit message: {e}")
return ""
def analyze_commit(self, commit_sha: str = "HEAD") -> Dict:
"""Analyze a commit and extract component information"""
changed_files = self.get_changed_files(commit_sha)
commit_message = self.get_commit_message(commit_sha)
components_changed = set()
total_additions = 0
total_deletions = 0
file_details = []
for filepath in changed_files:
full_path = os.path.join(self.repo_path, filepath)
# Extract components from this file
if os.path.exists(full_path):
file_components = ComponentAnnotationParser.extract_components_from_file(full_path)
components_changed.update(file_components)
# Get change size
changes = self.get_file_changes(filepath, commit_sha)
total_additions += changes['additions']
total_deletions += changes['deletions']
file_details.append({
'filepath': filepath,
'components': list(file_components),
'additions': changes['additions'],
'deletions': changes['deletions']
})
# Calculate change size category
total_lines_changed = total_additions + total_deletions
if total_lines_changed < 20:
change_size = 1 # Small
elif total_lines_changed < 100:
change_size = 2 # Medium
else:
change_size = 3 # Large
return {
'commit_sha': commit_sha,
'commit_message': commit_message,
'components_changed': list(components_changed),
'num_components': len(components_changed),
'total_additions': total_additions,
'total_deletions': total_deletions,
'total_lines_changed': total_lines_changed,
'change_size': change_size,
'file_details': file_details
}
class TestCoverageAnalyzer:
"""Analyze test coverage for components"""
def __init__(self, repo_path: str = ".", test_dir: str = "tests"):
self.repo_path = repo_path
self.test_dir = os.path.join(repo_path, test_dir)
def get_tested_components(self) -> Set[str]:
"""Get all components that have tests"""
tested_components = set()
if not os.path.exists(self.test_dir):
print(f"Warning: Test directory {self.test_dir} does not exist")
return tested_components
# Find all test files
for root, dirs, files in os.walk(self.test_dir):
for filename in files:
if filename.startswith('test_') and filename.endswith('.py'):
filepath = os.path.join(root, filename)
components = ComponentAnnotationParser.extract_tested_components_from_file(filepath)
tested_components.update(components)
return tested_components
def check_component_has_tests(self, component: str) -> bool:
"""Check if a specific component has tests"""
tested_components = self.get_tested_components()
return component in tested_components
class FailureTracker:
"""Track recent system failures"""
def __init__(self, log_file: str = "failures.json"):
self.log_file = log_file
def get_recent_failures(self, hours: int = 24) -> List[str]:
"""Get components that failed in the last N hours"""
if not os.path.exists(self.log_file):
return []
try:
with open(self.log_file, 'r') as f:
failures = json.load(f)
# Filter by time
recent = []
cutoff_time = datetime.now().timestamp() - (hours * 3600)
for failure in failures:
if failure.get('timestamp', 0) > cutoff_time:
recent.append(failure['component'])
return recent
except Exception as e:
print(f"Error reading failure log: {e}")
return []
def log_failure(self, component: str, error_message: str = ""):
"""Log a component failure"""
failure = {
'component': component,
'timestamp': datetime.now().timestamp(),
'error': error_message
}
# Load existing failures
failures = []
if os.path.exists(self.log_file):
try:
with open(self.log_file, 'r') as f:
failures = json.load(f)
except:
pass
# Add new failure
failures.append(failure)
# Keep only last 100 failures
failures = failures[-100:]
# Save
with open(self.log_file, 'w') as f:
json.dump(failures, f, indent=2)
class DeploymentWindowManager:
"""Manage deployment windows"""
def __init__(self, config_file: str = "deployment_windows.json"):
self.config_file = config_file
def is_deployment_allowed(self) -> Tuple[bool, str]:
"""Check if deployment is currently allowed"""
if not os.path.exists(self.config_file):
return True, "open" # Default: always open
try:
with open(self.config_file, 'r') as f:
config = json.load(f)
now = datetime.now()
current_hour = now.hour
current_day = now.strftime('%A')
# Check if current time is in a restricted window
for window in config.get('restricted_windows', []):
if 'days' in window and current_day not in window['days']:
continue
if 'hours' in window:
start_hour, end_hour = window['hours']
if start_hour <= current_hour < end_hour:
return False, "restricted"
return True, "open"
except Exception as e:
print(f"Error reading deployment window config: {e}")
return True, "open"
def get_current_load(self) -> str:
"""Get current system load (could integrate with monitoring system)"""
# In real implementation, this would call your monitoring API
# For now, return based on time of day
hour = datetime.now().hour
# Simulate peak hours (9-5 PM)
if 9 <= hour < 17:
if 12 <= hour < 14: # Lunch time is extra busy
return "extreme"
return "high"
else:
return "normal"
class GitHubIntegration:
"""Post results back to GitHub"""
def __init__(self, github_token: str = None):
self.github_token = github_token or os.getenv('GITHUB_TOKEN')
self.repo = os.getenv('GITHUB_REPOSITORY')
def post_pr_comment(self, pr_number: int, comment: str):
"""Post a comment on a pull request"""
if not self.github_token or not self.repo:
print("GitHub integration not configured, printing comment instead:")
print(comment)
return
url = f"https://api.github.com/repos/{self.repo}/issues/{pr_number}/comments"
headers = {
'Authorization': f'token {self.github_token}',
'Accept': 'application/vnd.github.v3+json'
}
data = {'body': comment}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
print(f"✅ Posted comment to PR #{pr_number}")
except Exception as e:
print(f"Error posting GitHub comment: {e}")
def set_commit_status(self, commit_sha: str, state: str, description: str, context: str = "ai-impact-prediction"):
"""Set commit status check"""
if not self.github_token or not self.repo:
print(f"Status check: {state} - {description}")
return
url = f"https://api.github.com/repos/{self.repo}/statuses/{commit_sha}"
headers = {
'Authorization': f'token {self.github_token}',
'Accept': 'application/vnd.github.v3+json'
}
data = {
'state': state, # 'success', 'failure', 'pending', 'error'
'description': description,
'context': context
}
try:
response = requests.post(url, headers=headers, json=data)
response.raise_for_status()
print(f"✅ Set commit status: {state}")
except Exception as e:
print(f"Error setting commit status: {e}")
class RealWorldImpactPredictor:
"""Main orchestrator that brings everything together"""
def __init__(self, repo_path: str = "."):
self.repo_path = repo_path
# Initialize all components
self.git_analyzer = GitCommitAnalyzer(repo_path)
self.test_analyzer = TestCoverageAnalyzer(repo_path)
self.failure_tracker = FailureTracker()
self.deployment_manager = DeploymentWindowManager()
self.github = GitHubIntegration()
# Initialize ML model
print("🤖 Initializing ML model...")
self.graph = EcommerceComponentGraph()
data_gen = HistoricalDataGenerator(self.graph)
training_data = data_gen.generate_training_data(n_samples=500)
self.predictor = ImpactPredictor(self.graph)
self.predictor.train(training_data)
print("✅ ML model ready!\n")
def analyze_and_predict(self, commit_sha: str = "HEAD", pr_number: int = None):
"""
Main method: Analyze commit and predict impact
This is what gets called when a commit is pushed!
"""
print("=" * 80)
print(f"🔍 ANALYZING COMMIT: {commit_sha}")
print("=" * 80)
# Step 1: Analyze the commit
print("\n📝 Step 1: Analyzing Git commit...")
commit_info = self.git_analyzer.analyze_commit(commit_sha)
print(f" Commit message: {commit_info['commit_message'][:60]}...")
print(f" Components changed: {commit_info['num_components']}")
print(f" Lines changed: {commit_info['total_lines_changed']}")
print(f" Change size: {['Small', 'Medium', 'Large'][commit_info['change_size']-1]}")
if not commit_info['components_changed']:
print("\n⚠️ No annotated components found in this commit.")
print(" Make sure your code has @component:annotations!")
return
# Step 2: Check test coverage
print("\n🧪 Step 2: Checking test coverage...")
tested_components = self.test_analyzer.get_tested_components()
components_with_tests = [c for c in commit_info['components_changed']
if c in tested_components]
has_tests = len(components_with_tests) > 0
print(f" Tested components: {len(tested_components)} total")
print(f" Changed components with tests: {len(components_with_tests)}/{commit_info['num_components']}")
# Step 3: Get real-time context
print("\n🌐 Step 3: Gathering real-time context...")
recent_failures = self.failure_tracker.get_recent_failures(hours=24)
deployment_allowed, deployment_window = self.deployment_manager.is_deployment_allowed()
current_load = self.deployment_manager.get_current_load()
print(f" Current load: {current_load}")
print(f" Deployment window: {deployment_window}")
print(f" Recent failures (24h): {len(recent_failures)}")
if recent_failures:
print(f" Failed components: {', '.join(recent_failures[:5])}")
# Build MCP context
mcp_context = {
'current_load': current_load,
'recent_failures': recent_failures,
'active_users': self._estimate_active_users(),
'time_of_day': 'peak' if current_load in ['high', 'extreme'] else 'normal',
'day_of_week': datetime.now().strftime('%A'),
'deployment_window': deployment_window
}
# Step 4: Run predictions for each changed component
print("\n🤖 Step 4: Running AI impact predictions...")
all_results = []
highest_risk = 0.0
highest_risk_component = None
for component in commit_info['components_changed']:
# Check if component exists in our graph
if component not in self.graph.graph.nodes():
print(f" ⚠️ Component '{component}' not in dependency graph, skipping...")
continue
# Run prediction
result = self.predictor.predict_impact(
component=component,
change_size=commit_info['change_size'],
has_tests=(component in tested_components),
mcp_context=mcp_context,
verbose=False # Don't print details for each component
)
all_results.append(result)
if result['adjusted_risk'] > highest_risk:
highest_risk = result['adjusted_risk']
highest_risk_component = component
# Step 5: Generate report
print("\n" + "=" * 80)
print("📊 IMPACT PREDICTION REPORT")
print("=" * 80)
report = self._generate_report(
commit_info, all_results, highest_risk,
highest_risk_component, mcp_context
)
print(report)
# Step 6: Post to GitHub (if configured)
if pr_number:
self.github.post_pr_comment(pr_number, report)
# Set commit status
if highest_risk > 0.7:
self.github.set_commit_status(
commit_sha, 'failure',
f'🚨 HIGH RISK ({highest_risk*100:.0f}%) - Review required'
)
elif highest_risk > 0.4:
self.github.set_commit_status(
commit_sha, 'success',
f'⚠️ MEDIUM RISK ({highest_risk*100:.0f}%) - Proceed with caution'
)
else:
self.github.set_commit_status(
commit_sha, 'success',
f'✅ LOW RISK ({highest_risk*100:.0f}%) - Safe to deploy'
)
return {
'commit_info': commit_info,
'predictions': all_results,
'highest_risk': highest_risk,
'recommendation': 'BLOCK' if highest_risk > 0.7 else 'CAUTION' if highest_risk > 0.4 else 'PROCEED'
}
def _estimate_active_users(self) -> int:
"""Estimate active users based on time of day"""
hour = datetime.now().hour
# Simulate realistic traffic patterns
if 9 <= hour < 17: # Business hours
if 12 <= hour < 14: # Lunch peak
return 25000
return 15000
elif 17 <= hour < 22: # Evening
return 10000
else: # Night/early morning
return 3000
def _generate_report(self, commit_info, results, highest_risk,
highest_risk_component, mcp_context) -> str:
"""Generate a markdown report for GitHub"""
report = f"""
## 🤖 AI Impact Prediction Report
**Commit:** `{commit_info['commit_sha'][:8]}`
**Message:** {commit_info['commit_message'][:100]}
---
### 📊 Overall Risk Assessment
**Highest Risk: {highest_risk*100:.1f}%** ({['LOW ✅', 'MEDIUM ⚠️', 'HIGH 🚨'][0 if highest_risk < 0.4 else 1 if highest_risk < 0.7 else 2]})
"""
if highest_risk_component:
report += f"**Most Risky Component:** `{highest_risk_component}`\n\n"
# Recommendation
if highest_risk > 0.7:
report += """
### 🚨 RECOMMENDATION: BLOCK DEPLOYMENT
**This deployment carries HIGH RISK of cascade failures.**
**Actions Required:**
- ⏰ Delay until off-peak hours
- 🧪 Run complete end-to-end test suite
- 📋 Prepare rollback plan
- 👨💻 Have senior engineer on standby
"""
elif highest_risk > 0.4:
report += """
### ⚠️ RECOMMENDATION: PROCEED WITH CAUTION
**This deployment carries MEDIUM RISK.**
**Actions Required:**
- 🧪 Run targeted regression tests
- 👀 Monitor closely for 30 minutes post-deploy
- 📋 Have rollback plan ready
"""
else:
report += """
### ✅ RECOMMENDATION: SAFE TO PROCEED
**This deployment carries LOW RISK.**
**Actions:**
- 🚀 Standard deployment process
- 🧪 Run smoke tests post-deploy
"""
# Component details
report += "\n---\n\n### 📦 Component Analysis\n\n"
report += "| Component | Risk | Tests | Downstream |\n"
report += "|-----------|------|-------|------------|\n"
for result in results:
risk_pct = result['adjusted_risk'] * 100
risk_emoji = '🚨' if risk_pct > 70 else '⚠️' if risk_pct > 40 else '✅'
tests_emoji = '✅' if result.get('has_tests') else '❌'
report += f"| `{result['component']}` | {risk_emoji} {risk_pct:.0f}% | {tests_emoji} | "
report += f"{len(result['affected_components'])} |\n"
# Context
report += "\n---\n\n### 🌐 Context\n\n"
report += f"- **Load:** {mcp_context['current_load']}\n"
report += f"- **Active Users:** {mcp_context['active_users']:,}\n"
report += f"- **Time:** {mcp_context['day_of_week']}, {mcp_context['time_of_day']}\n"
report += f"- **Deployment Window:** {mcp_context['deployment_window']}\n"
if mcp_context['recent_failures']:
report += f"- **Recent Failures (24h):** {', '.join([f'`{c}`' for c in mcp_context['recent_failures'][:3]])}\n"
report += "\n---\n\n"
report += "*🤖 Generated by AI Impact Prediction System*\n"
return report
def main():
"""Main entry point for CLI usage"""
import argparse
parser = argparse.ArgumentParser(description='Analyze Git commit for impact prediction')
parser.add_argument('--commit', default='HEAD', help='Commit SHA to analyze')
parser.add_argument('--pr', type=int, help='Pull request number (for GitHub integration)')
parser.add_argument('--repo', default='.', help='Path to Git repository')
args = parser.parse_args()
predictor = RealWorldImpactPredictor(repo_path=args.repo)
predictor.analyze_and_predict(commit_sha=args.commit, pr_number=args.pr)
if __name__ == "__main__":
main()