-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetch_raw_content.py
More file actions
executable file
·384 lines (309 loc) · 13.8 KB
/
fetch_raw_content.py
File metadata and controls
executable file
·384 lines (309 loc) · 13.8 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
#!/usr/bin/env python3
"""
Script to fetch raw content from GitHub URLs and analyze PR sections.
"""
import argparse
import json
import os
import re
import requests
from collections import defaultdict
from datetime import datetime
def get_label_file_patterns():
"""Get mapping of labels to file patterns based on config.ts"""
return {
"教育": ["11_ステップ1教育", "21_ステップ2教育", "32_ステップ3教育"],
"子育て": ["12_ステップ1子育て", "31_ステップ3子育て"],
"行政改革": ["13_ステップ1行政改革", "22_ステップ2行政改革"],
"産業政策": ["14_ステップ1産業", "34_ステップ3産業"],
"科学技術": ["15_ステップ1科学技術", "33_ステップ3科学技術"],
"デジタル民主主義": ["16_ステップ1デジタル民主主義"],
"医療": ["17_ステップ1医療", "24_ステップ2医療"],
"経済財政": ["23_ステップ2経済財政", "36_ステップ3経済財政"],
"エネルギー": ["35_ステップ3エネルギー"],
"ビジョン": ["01_チームみらいのビジョン"],
"その他政策": ["50_国政のその他重要分野"]
}
def load_pr_data(file_path='all_pr_data.json'):
"""Load PR data from JSON file."""
print(f"Loading PR data from {file_path}...")
try:
with open(file_path, 'r', encoding='utf-8') as f:
all_prs = json.load(f)
print(f"Loaded {len(all_prs)} PRs")
return all_prs
except Exception as e:
print(f"Error loading PR data: {e}")
return []
def get_labeled_prs(all_prs, label):
"""Get all PRs with the specified label."""
labeled_prs = []
for pr in all_prs:
pr_labels = [l.get('name') for l in pr.get('labels', [])]
if label in pr_labels:
labeled_prs.append(pr)
print(f"Found {len(labeled_prs)} PRs with label '{label}'")
return labeled_prs
def fetch_raw_content(url):
"""Fetch raw content from GitHub URL."""
try:
response = requests.get(url)
if response.status_code == 200:
return response.text
else:
print(f"Failed to fetch content from {url}: {response.status_code}")
return None
except Exception as e:
print(f"Error fetching content from {url}: {e}")
return None
def extract_markdown_sections(content):
"""Extract markdown sections from content."""
if not content:
return {}
headings = []
section_content = {}
current_section_start = None
for i, line in enumerate(content.split('\n')):
heading_match = re.match(r'^(#+)\s+(.+)$', line)
jp_section_match = None
if not heading_match:
jp_section_match = re.match(r'^([0-9]+)|\d+))\s*(.+)$', line)
jp_policy_match = None
if not heading_match and not jp_section_match:
jp_policy_match = re.match(r'^###\s+(現状認識・課題分析|政策概要)$', line)
if heading_match or jp_section_match or jp_policy_match:
if heading_match:
level = len(heading_match.group(1))
title = heading_match.group(2).strip()
elif jp_section_match:
level = 2
title = jp_section_match.group(0).strip()
else:
level = 3
title = jp_policy_match.group(1).strip()
headings.append((i+1, level, title))
if current_section_start is not None:
section_content[current_section_start] = (current_section_start, i)
current_section_start = i+1
if current_section_start is not None:
section_content[current_section_start] = (current_section_start, len(content.split('\n')))
section_hierarchy = {}
section_path = []
for i, (line_num, level, title) in enumerate(headings):
while section_path and section_path[-1][1] >= level:
section_path.pop()
section_path.append((title, level))
path_str = " > ".join([t for t, _ in section_path])
section_hierarchy[line_num] = path_str
return section_hierarchy
def find_section_for_line(section_hierarchy, line_num):
"""Find the section for a given line number."""
section_starts = sorted(section_hierarchy.keys())
for i in range(len(section_starts) - 1):
if section_starts[i] <= line_num < section_starts[i + 1]:
return section_hierarchy[section_starts[i]]
if section_starts and line_num >= section_starts[-1]:
return section_hierarchy[section_starts[-1]]
return "Unknown section"
def extract_line_numbers_from_patch(patch):
"""Extract line numbers from patch."""
if not patch:
return []
line_numbers = []
current_line = None
for line in patch.split('\n'):
hunk_match = re.match(r'^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@', line)
if hunk_match:
current_line = int(hunk_match.group(1))
continue
if current_line is not None:
if line.startswith('+') and not line.startswith('+++'):
line_numbers.append(current_line)
if not line.startswith('-'):
current_line += 1
return line_numbers
def analyze_pr(pr, label, content_cache=None):
"""Analyze a PR and extract the sections it modifies for the given label."""
if content_cache is None:
content_cache = {}
basic_info = pr.get('basic_info', {})
pr_number = basic_info.get('number')
if not pr_number:
print("PR number not found")
return None
pr_files = pr.get('files', [])
if not pr_files:
print(f"No files found for PR #{pr_number}")
return None
file_patterns = get_label_file_patterns().get(label, [])
if not file_patterns:
print(f"No file patterns found for label: {label}")
return None
label_files = []
for file_info in pr_files:
filename = file_info.get('filename', '')
if filename.endswith('.md') and any(filename.startswith(pattern) for pattern in file_patterns):
label_files.append(file_info)
if not label_files:
print(f"No {label} files found for PR #{pr_number}")
return None
results = []
for file_info in label_files:
filename = file_info.get('filename', '')
print(f"Processing file: {filename}")
if filename in content_cache:
file_content = content_cache[filename]
else:
raw_url = file_info.get('raw_url')
if raw_url:
file_content = fetch_raw_content(raw_url)
if file_content:
content_cache[filename] = file_content
else:
file_content = None
if not file_content:
print(f"No content found for file: {filename}")
continue
section_hierarchy = extract_markdown_sections(file_content)
if not section_hierarchy:
print(f"No sections found in file: {filename}")
continue
patch = file_info.get('patch')
if not patch:
print(f"No patch found for file: {filename}")
continue
modified_lines = extract_line_numbers_from_patch(patch)
if not modified_lines:
print(f"No modified lines found for file: {filename}")
continue
affected_sections = set()
for line_num in modified_lines:
section = find_section_for_line(section_hierarchy, line_num)
affected_sections.add(section)
for section in affected_sections:
results.append({
'pr_number': pr_number,
'pr_title': basic_info.get('title', ''),
'pr_url': basic_info.get('html_url', ''),
'file': filename,
'section_path': section
})
print(f"Found {len(results)} affected sections")
return results
def generate_markdown_report(pr_analyses, label):
"""Generate a Markdown report from the PR analyses for the specified label."""
if not pr_analyses:
return f"# {label}関連PRのセクション分析\n\n分析対象のPRが見つかりませんでした。"
sections_to_prs = defaultdict(list)
for pr in pr_analyses:
for result in pr.get("results", []):
section_key = f"{result['file']}: {result['section_path']}"
sections_to_prs[section_key].append({
"number": result['pr_number'],
"title": result['pr_title'],
"url": result['pr_url']
})
report = f"# {label}関連PRのセクション分析\n\n"
report += f"分析日時: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
report += "## セクション別PR一覧\n\n"
for section_key, prs in sorted(sections_to_prs.items()):
file_path, section_path = section_key.split(': ', 1)
report += f"### {file_path}\n"
report += f"#### {section_path}\n"
report += "このセクションを変更するPR:\n"
for pr in prs:
report += f"- PR #{pr['number']}: [{pr['title']}]({pr['url']})\n"
report += "\n"
report += "## PR別変更セクション一覧\n\n"
pr_to_sections = defaultdict(list)
for pr in pr_analyses:
for result in pr.get("results", []):
pr_key = f"PR #{result['pr_number']}: {result['pr_title']}"
pr_url = result['pr_url']
section_key = f"{result['file']}: {result['section_path']}"
pr_to_sections[(pr_key, pr_url)].append(section_key)
for (pr_key, pr_url), sections in sorted(pr_to_sections.items()):
report += f"### [{pr_key}]({pr_url})\n"
for section in sorted(sections):
file_path, section_path = section.split(': ', 1)
report += f"- {file_path}: {section_path}\n"
report += "\n"
return report
def main():
parser = argparse.ArgumentParser(description="Fetch raw content and analyze PR sections")
parser.add_argument("--label", default="教育", help="Label to analyze (default: 教育)")
parser.add_argument("--output-dir", default="section_reports", help="Output directory for reports")
parser.add_argument("--limit", type=int, default=5, help="Limit the number of PRs to analyze")
parser.add_argument("--all-labels", action="store_true", help="Analyze all labels")
parser.add_argument("--summary", action="store_true", help="Generate a summary report")
args = parser.parse_args()
os.makedirs(args.output_dir, exist_ok=True)
all_prs = load_pr_data()
if not all_prs:
print("No PR data found")
return
if args.all_labels:
labels = get_label_file_patterns().keys()
else:
labels = [args.label]
label_summaries = {}
content_cache = {} # Cache for file content to avoid duplicate fetches
for label in labels:
print(f"\n{'='*60}")
print(f"Processing label: {label}")
print(f"{'='*60}")
labeled_prs = get_labeled_prs(all_prs, label)
if not labeled_prs:
label_summaries[label] = {"pr_count": 0, "section_count": 0}
continue
labeled_prs = labeled_prs[:args.limit]
pr_analyses = []
total_sections = 0
for pr in labeled_prs:
basic_info = pr.get('basic_info', {})
pr_number = basic_info.get('number')
print(f"\nAnalyzing PR #{pr_number}: {basic_info.get('title')}")
results = analyze_pr(pr, label, content_cache)
if results:
total_sections += len(results)
pr_analyses.append({
"pr_number": pr_number,
"pr_title": basic_info.get('title'),
"pr_url": basic_info.get('html_url'),
"results": results
})
label_summaries[label] = {
"pr_count": len(pr_analyses),
"section_count": total_sections
}
report = generate_markdown_report(pr_analyses, label)
output_file = os.path.join(args.output_dir, f"{label}_section_report.md")
with open(output_file, "w", encoding="utf-8") as f:
f.write(report)
print(f"Report for '{label}' written to {output_file}")
if args.summary or args.all_labels:
generate_summary_report(label_summaries, args.output_dir)
def generate_summary_report(label_summaries, output_dir):
"""Generate a summary report of all labels."""
report = "# マニフェスト政策ラベル別セクション分析サマリー\n\n"
report += f"分析日時: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n\n"
report += "| ラベル | 分析PR数 | 変更セクション数 |\n"
report += "|--------|----------|----------------|\n"
total_prs = 0
total_sections = 0
for label, stats in sorted(label_summaries.items()):
pr_count = stats["pr_count"]
section_count = stats["section_count"]
total_prs += pr_count
total_sections += section_count
report += f"| {label} | {pr_count} | {section_count} |\n"
report += f"| **合計** | **{total_prs}** | **{total_sections}** |\n\n"
report += "## 詳細レポートへのリンク\n\n"
for label in sorted(label_summaries.keys()):
report += f"- [{label}](./{label}_section_report.md)\n"
output_file = os.path.join(output_dir, "section_analysis_summary.md")
with open(output_file, "w", encoding="utf-8") as f:
f.write(report)
print(f"Summary report written to {output_file}")
if __name__ == "__main__":
main()