-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp1_json_creator_from_github.py
More file actions
583 lines (479 loc) · 23.5 KB
/
Copy pathp1_json_creator_from_github.py
File metadata and controls
583 lines (479 loc) · 23.5 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
#!/usr/bin/env python3
"""
Collect GitHub issues that are closed and related to logs from specified projects.
Currently configured for Apache Druid, but can be extended to other projects.
"""
import requests
import time
from pathlib import Path
from typing import Dict, List, Optional
from datetime import datetime
import re
import os
import csv
from src.utils import Config, save_json
class GitHubIssueCollector:
"""Collect GitHub issues related to logs from specified repositories."""
def __init__(self, github_token: Optional[str] = None):
self.session = requests.Session()
self.base_url = "https://api.github.com"
self.rate_limit_remaining = 5000
self.rate_limit_reset = 0
self.request_cache = {} # Simple cache for requests
# Set up authentication if token provided
if github_token:
self.session.headers.update({
'Authorization': f'token {github_token}',
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'LogFinder-Issue-Collector/1.0'
})
else:
self.session.headers.update({
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'LogFinder-Issue-Collector/1.0'
})
print("Warning: No GitHub token provided. Rate limits will be lower (60 requests/hour).")
def check_rate_limit(self):
"""Check and handle GitHub API rate limits."""
if self.rate_limit_remaining <= 1:
wait_time = max(self.rate_limit_reset - time.time(), 0) + 10
print(f"Rate limit reached. Waiting {wait_time:.0f} seconds...")
time.sleep(wait_time)
def make_request(self, url: str, params: Dict = None) -> Optional[Dict]:
"""Make a GitHub API request with rate limit handling and caching."""
# Create cache key
cache_key = f"{url}?{str(sorted((params or {}).items()))}"
# Check cache first
if cache_key in self.request_cache:
return self.request_cache[cache_key]
self.check_rate_limit()
try:
response = self.session.get(url, params=params)
# Update rate limit info
self.rate_limit_remaining = int(response.headers.get('X-RateLimit-Remaining', 0))
self.rate_limit_reset = int(response.headers.get('X-RateLimit-Reset', 0))
if response.status_code == 200:
result = response.json()
# Cache the result (but limit cache size)
if len(self.request_cache) < 1000: # Prevent memory issues
self.request_cache[cache_key] = result
return result
elif response.status_code == 403:
print(f"Rate limited or forbidden: {response.text}")
time.sleep(60)
return None
elif response.status_code == 404:
print(f"Not found: {url}")
return None
else:
print(f"Error {response.status_code}: {response.text}")
return None
except Exception as e:
print(f"Request failed: {e}")
return None
def get_paginated_results(self, url: str, params: Dict = None, max_results: Optional[int] = None) -> List[Dict]:
"""Get all pages of results from a paginated GitHub API endpoint."""
all_results = []
page = 1
per_page = 100
if params is None:
params = {}
params.update({'per_page': per_page, 'page': page})
while True:
print(f" Fetching page {page}...")
params['page'] = page
data = self.make_request(url, params)
if not data:
break
if isinstance(data, list):
results = data
else:
results = data.get('items', [])
if not results:
break
all_results.extend(results)
# Check if we've reached the limit
if max_results and len(all_results) >= max_results:
all_results = all_results[:max_results]
break
# Check if we got a full page (if not, we're done)
if len(results) < per_page:
break
page += 1
return all_results
def is_log_related(self, issue: Dict) -> bool:
"""Check if an issue is related to logs (has 'log' in description or log attachments)."""
body = issue.get('body', '') or ''
title = issue.get('title', '') or ''
# Check if 'log' appears in title or body
if 'log' in body.lower() or 'log' in title.lower():
return True
# Check for log file attachments
log_attachment_patterns = [
r'\[.*\]\(.*\.log\)', # Markdown links to .log files
r'https?://.*\.log', # Direct .log file links
r'\[.*log.*\]\(.*\)', # Links with 'log' in the text
r'log\.txt', # log.txt files
r'.*_log\.txt', # files ending with _log.txt
r'.*\.log\..*', # log files with additional extensions
]
for pattern in log_attachment_patterns:
if re.search(pattern, body, re.IGNORECASE):
return True
return False
def extract_attachments(self, issue: Dict) -> List[Dict]:
"""Extract attachment information from issue body."""
body = issue.get('body', '') or ''
attachments = []
# Extract markdown images
image_pattern = r'!\[(.*?)\]\((.*?)\)'
for match in re.finditer(image_pattern, body):
alt_text, url = match.groups()
attachments.append({
'type': 'image',
'filename': alt_text or 'image',
'url': url
})
# Extract file links
file_pattern = r'\[(.*?)\]\((.*\.(log|txt|pdf|zip|tar\.gz|csv|json))\)'
for match in re.finditer(file_pattern, body, re.IGNORECASE):
filename, url, ext = match.groups()
attachments.append({
'type': 'file',
'filename': filename,
'url': url,
'extension': ext
})
# Extract direct file URLs
direct_file_pattern = r'(https?://.*\.(png|jpg|jpeg|gif|pdf|log|txt|zip|tar\.gz|csv|json))'
for match in re.finditer(direct_file_pattern, body, re.IGNORECASE):
url, ext = match.groups()
filename = url.split('/')[-1]
attachments.append({
'type': 'file',
'filename': filename,
'url': url,
'extension': ext
})
# Extract GitHub user uploads
upload_pattern = r'(https://user-images\.githubusercontent\.com/[^\s)]+)'
for match in re.finditer(upload_pattern, body):
url = match.group(1)
filename = url.split('/')[-1]
attachments.append({
'type': 'upload',
'filename': filename,
'url': url
})
return attachments
def get_issue_details(self, owner: str, repo: str, issue_number: int) -> Optional[Dict]:
"""Get detailed information for a specific issue."""
url = f"{self.base_url}/repos/{owner}/{repo}/issues/{issue_number}"
return self.make_request(url)
def find_closing_pr_optimized(self, owner: str, repo: str, issue_number: int) -> tuple[Optional[int], int]:
"""Find the pull request that closed the issue and return (pr_number, commit_count)."""
# First try: search for PRs that mention this issue (most reliable and fewer calls)
search_url = f"{self.base_url}/search/issues"
search_params = {
'q': f'repo:{owner}/{repo} is:pr is:merged #{issue_number}',
'sort': 'updated',
'order': 'desc'
}
result = self.make_request(search_url, search_params)
if result and result.get('items'):
# Return the first (most recent) PR that mentions this issue
pr_data = result['items'][0]
pr_number = pr_data['number']
# Get commit count directly from the search result if available
commit_count = pr_data.get('commits', 1) # Default to 1 if not available
return pr_number, commit_count
# Fallback: check issue events (only if PR search failed)
url = f"{self.base_url}/repos/{owner}/{repo}/issues/{issue_number}/events"
events = self.make_request(url)
if events:
# Look for 'closed' event with a commit_id
for event in events:
if event.get('event') == 'closed' and event.get('commit_id'):
# Try to find the PR that contains this commit with a single search
search_params = {
'q': f'repo:{owner}/{repo} is:pr is:merged {event["commit_id"]}',
'sort': 'updated',
'order': 'desc'
}
result = self.make_request(search_url, search_params)
if result and result.get('items'):
pr_data = result['items'][0]
pr_number = pr_data['number']
commit_count = pr_data.get('commits', 1)
return pr_number, commit_count
return None, 0
def get_issues_batch(self, owner: str, repo: str, issue_numbers: List[int]) -> Dict[int, Dict]:
"""Get details for multiple issues more efficiently."""
issues_details = {}
# Process in smaller batches to avoid rate limits
batch_size = 10
for i in range(0, len(issue_numbers), batch_size):
batch = issue_numbers[i:i + batch_size]
print(f" Getting details for issues batch {i//batch_size + 1}")
for issue_num in batch:
issue_details = self.get_issue_details(owner, repo, issue_num)
if issue_details:
issues_details[issue_num] = issue_details
# Small delay to be respectful of rate limits
time.sleep(0.1)
return issues_details
def collect_issues_from_repo(self, owner: str, repo: str, max_issues: Optional[int] = None) -> List[Dict]:
"""Collect closed issues related to logs with state_reason 'completed' from a GitHub repository."""
print(f"Collecting log-related issues from {owner}/{repo}...")
# Search for closed issues with 'log' keyword
search_url = f"{self.base_url}/search/issues"
search_params = {
'q': f'repo:{owner}/{repo} is:issue is:closed log',
'sort': 'updated',
'order': 'desc'
}
print(" Searching for closed issues...")
issues = self.get_paginated_results(search_url, search_params, max_results=max_issues)
# Limit issues if requested
if max_issues and max_issues < len(issues):
issues = issues[:max_issues]
print(f" Limited to first {max_issues} closed issues")
else:
print(f" Found {len(issues)} closed issues")
# First pass: filter log-related issues
log_related_issue_numbers = []
for i, issue in enumerate(issues):
if i % 50 == 0:
print(f" Filtering issues: {i}/{len(issues)} (found {len(log_related_issue_numbers)} log-related so far)")
# Check if it's log-related (cheap operation)
if self.is_log_related(issue):
log_related_issue_numbers.append(issue['number'])
print(f" Found {len(log_related_issue_numbers)} log-related issues")
# Second pass: get details for log-related issues in batches
log_related_issues = []
issues_with_completed_state = 0
if log_related_issue_numbers:
print(f" Getting detailed info for {len(log_related_issue_numbers)} log-related issues...")
issues_details = self.get_issues_batch(owner, repo, log_related_issue_numbers)
# Third pass: process issues with completed state and find PRs
for issue_number, issue_details in issues_details.items():
# Check if state_reason is 'completed' (not 'not_planned')
state_reason = issue_details.get('state_reason')
if state_reason == 'completed':
issues_with_completed_state += 1
# Extract attachment details
attachments = self.extract_attachments(issue_details)
# Find the PR that closed this issue (optimized - fewer API calls)
print(f" Finding PR for issue #{issue_details['number']}...")
pr_number, commit_count = self.find_closing_pr_optimized(owner, repo, issue_details['number'])
pr_url = f"https://github.com/{owner}/{repo}/pull/{pr_number}" if pr_number else None
if pr_number:
print(f" Found PR #{pr_number} with {commit_count} commits")
else:
print(f" No PR found for issue #{issue_details['number']}")
issue_data = {
'key': f"{repo.upper()}-{issue_details['number']}", # Create a consistent key format
'number': issue_details['number'],
'title': issue_details['title'],
'state': issue_details['state'],
'state_reason': state_reason,
'created_at': issue_details['created_at'],
'updated_at': issue_details['updated_at'],
'closed_at': issue_details['closed_at'],
'body': issue_details['body'],
'user': issue_details['user']['login'],
'labels': [label['name'] for label in issue_details['labels']],
'url': issue_details['html_url'],
'api_url': issue_details['url'],
'attachments': attachments,
'pr_number': pr_number,
'pr_url': pr_url,
'commit_count': commit_count
}
log_related_issues.append(issue_data)
print(f" Checked {len(log_related_issue_numbers)} log-related issues")
print(f" Found {issues_with_completed_state} issues with state_reason='completed'")
print(f" Total matching issues: {len(log_related_issues)}")
return log_related_issues
def collect_issues(self, repositories: List[Dict[str, str]], max_issues_per_repo: Optional[int] = None) -> Dict[str, List[Dict]]:
"""Collect issues from multiple repositories."""
all_issues = {}
for repo_info in repositories:
owner = repo_info['owner']
repo = repo_info['repo']
project_name = repo_info.get('project_name', repo)
issues = self.collect_issues_from_repo(owner, repo, max_issues_per_repo)
if issues:
all_issues[project_name] = issues
return all_issues
def get_github_token(interactive: bool = True) -> Optional[str]:
"""Get GitHub token from environment or prompt user."""
# Try to get from environment variable
token = os.getenv('GITHUB_TOKEN')
if token:
return token
# Try to get from config file
config_file = Path.home() / '.github_token'
if config_file.exists():
return config_file.read_text().strip()
if not interactive:
print("GitHub token not found. Running without authentication (lower rate limits).")
return None
# Prompt user
print("GitHub token not found in environment (GITHUB_TOKEN) or ~/.github_token")
print("You can continue without a token, but rate limits will be much lower.")
try:
use_token = input("Do you have a GitHub token? (y/n): ").lower().startswith('y')
if use_token:
token = input("Enter your GitHub token: ").strip()
save_token = input("Save token to ~/.github_token? (y/n): ").lower().startswith('y')
if save_token and token:
config_file.write_text(token)
config_file.chmod(0o600) # Secure permissions
return token
except (EOFError, KeyboardInterrupt):
print("\nNo token provided. Running without authentication.")
return None
def save_as_csv(formatted_issues: Dict[str, Dict], output_file: Path) -> None:
"""Save formatted issues to a CSV file."""
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
# Define CSV headers
fieldnames = [
'project', 'issue_key', 'issue_number', 'title', 'status', 'state_reason',
'priority', 'created', 'updated', 'closed', 'url', 'labels',
'user', 'body_preview', 'attachments_count', 'attachment_types',
'pr_number', 'pr_url', 'commit_count'
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
# Write issues to CSV
for project_name, project_issues in formatted_issues.items():
for issue_key, issue_data in project_issues.items():
issue = issue_data['issue']
# Get unique attachment types
attachment_types = set()
for attachment in issue.get('attachments', []):
if 'extension' in attachment:
attachment_types.add(attachment['extension'])
elif 'type' in attachment:
attachment_types.add(attachment['type'])
# Truncate body for CSV (to avoid huge cells)
body = issue.get('body', '') or ''
body_preview = body[:500] + '...' if len(body) > 500 else body
# Remove newlines for CSV format
body_preview = ' '.join(body_preview.split())
row = {
'project': project_name,
'issue_key': issue['key'],
'issue_number': issue['key'].split('-')[-1],
'title': issue['summary'],
'status': issue['status'],
'state_reason': issue['state_reason'],
'priority': issue['priority'],
'created': issue['created'],
'updated': issue['updated'],
'closed': issue['closed'],
'url': issue['url'],
'labels': ', '.join(issue.get('labels', [])),
'user': issue.get('user', 'N/A'),
'body_preview': body_preview,
'attachments_count': len(issue.get('attachments', [])),
'attachment_types': ', '.join(sorted(attachment_types)),
'pr_number': issue.get('pr_number', ''),
'pr_url': issue.get('pr_url', ''),
'commit_count': issue.get('commit_count', 0)
}
writer.writerow(row)
def main():
"""Main function."""
import argparse
# Parse command line arguments
parser = argparse.ArgumentParser(description='Collect GitHub issues related to logs')
parser.add_argument('--no-interactive', action='store_true', help='Run without prompts')
parser.add_argument('--max-issues', type=int, help='Maximum issues to process per repository')
args = parser.parse_args()
config = Config()
# Get GitHub token
github_token = get_github_token(interactive=not args.no_interactive)
# Initialize collector
collector = GitHubIssueCollector(github_token)
# Define repositories to collect from
repositories = [
{
'owner': 'apache',
'repo': 'druid',
'project_name': 'Druid'
}
# Add more repositories here as needed
# {
# 'owner': 'apache',
# 'repo': 'kafka',
# 'project_name': 'Kafka'
# }
]
print(f"Collecting issues from {len(repositories)} repositories...")
if args.max_issues:
print(f"Limited to {args.max_issues} issues per repository")
# Collect issues
all_issues = collector.collect_issues(repositories, max_issues_per_repo=args.max_issues)
# Convert to the expected format
formatted_issues = {}
total_issues = 0
for project_name, issues in all_issues.items():
project_issues = {}
for issue in issues:
issue_key = issue['key']
# Format to match existing structure
formatted_issue = {
'issue': {
'key': issue_key,
'summary': issue['title'],
'status': 'Closed',
'state_reason': issue['state_reason'],
'priority': 'Unknown', # GitHub doesn't have priority field
'created': issue['created_at'],
'updated': issue['updated_at'],
'closed': issue['closed_at'],
'url': issue['url'],
'labels': issue['labels'],
'attachments': issue['attachments'],
# Include body and user for CSV export
'body': issue['body'],
'user': issue['user'],
# Include PR and commit information
'pr_number': issue.get('pr_number'),
'pr_url': issue.get('pr_url'),
'commit_count': issue.get('commit_count', 0)
}
}
project_issues[issue_key] = formatted_issue
total_issues += 1
if project_issues:
formatted_issues[project_name] = project_issues
# Save results as JSON
output_file = config.outputs_dir / "p1_github_issues.json"
save_json(formatted_issues, output_file)
# Save results as CSV
csv_output_file = config.outputs_dir / "p1_github_issues.csv"
save_as_csv(formatted_issues, csv_output_file)
# Print summary
print(f"\nCollected {total_issues} log-related issues")
for project_name, issues in formatted_issues.items():
print(f" {project_name}: {len(issues)} issues")
print(f"\nResults saved to:")
print(f" JSON: {output_file}")
print(f" CSV: {csv_output_file}")
# Also create a summary file
summary = {
'collection_date': datetime.now().isoformat(),
'total_issues': total_issues,
'projects': {
project: len(issues) for project, issues in formatted_issues.items()
},
'repositories': repositories
}
summary_file = config.outputs_dir / "p1_github_collection_summary.json"
save_json(summary, summary_file)
print(f"Summary saved to: {summary_file}")
if __name__ == "__main__":
main()