forked from CodeBoarding/CodeBoarding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_action.py
More file actions
120 lines (103 loc) · 5.65 KB
/
github_action.py
File metadata and controls
120 lines (103 loc) · 5.65 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
import logging
import os
from pathlib import Path
from typing import List
from dotenv import load_dotenv
from agents.agent_responses import AnalysisInsights
from diagram_analysis import DiagramGenerator
from output_generators.markdown import generate_markdown_file
from output_generators.html import generate_html_file
from output_generators.mdx import generate_mdx_file
from output_generators.sphinx import generate_rst_file
from repo_utils import clone_repository, checkout_repo
from utils import create_temp_repo_folder
logger = logging.getLogger(__name__)
def generate_markdown(analysis_files: List[str], repo_name: str, repo_url: str, target_branch: str,
temp_repo_folder: Path, output_dir):
for file in analysis_files:
if str(file).endswith(".json") and "codeboarding_version.json" not in str(file):
print(f"Processing analysis file: {file}")
with open(file, 'r') as f:
analysis = AnalysisInsights.model_validate_json(f.read())
logger.info(f"Generated analysis file: {file}")
fname = Path(file).stem
if fname.endswith("analysis"):
fname = "overview"
generate_markdown_file(fname, analysis, repo_name,
repo_ref=f"{repo_url}/blob/{target_branch}/{output_dir}",
linked_files=analysis_files,
temp_dir=temp_repo_folder)
def generate_html(analysis_files: List[str], repo_name: str, repo_url: str, target_branch: str,
temp_repo_folder: Path, output_dir):
for file in analysis_files:
if str(file).endswith(".json") and "codeboarding_version.json" not in str(file):
print(f"Processing analysis file: {file}")
with open(file, 'r') as f:
analysis = AnalysisInsights.model_validate_json(f.read())
logger.info(f"Generated analysis file: {file}")
fname = Path(file).stem
if fname.endswith("analysis"):
fname = "overview"
generate_html_file(fname, analysis, repo_name,
repo_ref=f"{repo_url}/blob/{target_branch}",
linked_files=analysis_files,
temp_dir=temp_repo_folder)
def generate_mdx(analysis_files: List[str], repo_name: str, repo_url: str, target_branch: str, temp_repo_folder: Path,
output_dir):
for file in analysis_files:
if str(file).endswith(".json") and "codeboarding_version.json" not in str(file):
print(f"Processing analysis file: {file}")
with open(file, 'r') as f:
analysis = AnalysisInsights.model_validate_json(f.read())
logger.info(f"Generated analysis file: {file}")
fname = Path(file).stem
if fname.endswith("analysis"):
fname = "overview"
generate_mdx_file(fname, analysis, repo_name,
repo_ref=f"{repo_url}/blob/{target_branch}/{output_dir}",
linked_files=analysis_files,
temp_dir=temp_repo_folder)
def generate_rst(analysis_files: List[str], repo_name: str, repo_url: str, target_branch: str, temp_repo_folder: Path,
output_dir):
for file in analysis_files:
if str(file).endswith(".json") and "codeboarding_version.json" not in str(file):
print(f"Processing analysis file: {file}")
with open(file, 'r') as f:
analysis = AnalysisInsights.model_validate_json(f.read())
logger.info(f"Generated analysis file: {file}")
fname = Path(file).stem
if fname.endswith("analysis"):
fname = "overview"
generate_rst_file(fname, analysis, repo_name,
repo_ref=f"{repo_url}/blob/{target_branch}/{output_dir}",
linked_files=analysis_files,
temp_dir=temp_repo_folder)
def generate_analysis(repo_url: str, source_branch: str, target_branch: str, extension: str,
output_dir: str = ".codeboarding"):
"""
Generate analysis for a GitHub repository URL.
This function is intended to be used in a GitHub Action context.
"""
repo_root = Path(os.getenv("REPO_ROOT"))
repo_name = clone_repository(repo_url, repo_root)
repo_dir = repo_root / repo_name
checkout_repo(repo_dir, source_branch)
temp_repo_folder = create_temp_repo_folder()
generator = DiagramGenerator(repo_location=repo_dir,
temp_folder=temp_repo_folder,
repo_name=repo_name,
output_dir=temp_repo_folder,
depth_level=int(os.getenv("DIAGRAM_DEPTH_LEVEL")))
analysis_files = generator.generate_analysis()
# Now generated the markdowns:
if extension == ".md":
generate_markdown(analysis_files, repo_name, repo_url, target_branch, temp_repo_folder, output_dir)
elif extension == ".html":
generate_html(analysis_files, repo_name, repo_url, target_branch, temp_repo_folder, output_dir)
elif extension == ".mdx":
generate_mdx(analysis_files, repo_name, repo_url, target_branch, temp_repo_folder, output_dir)
elif extension == ".rst":
generate_rst(analysis_files, repo_name, repo_url, target_branch, temp_repo_folder, output_dir)
else:
raise ValueError(f"Unsupported extension: {extension}")
return temp_repo_folder