-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmain.py
More file actions
199 lines (172 loc) · 6.42 KB
/
main.py
File metadata and controls
199 lines (172 loc) · 6.42 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
import argparse
from src.article_generator import generate_article_content
from src.config_parser import load_config_from_ini
# Import functions from the new modules
from src.git_utils import analyze_real_git_commits
def main():
"""Main function to run the Git commit analysis and article generation tool.
Supports optional configuration from an INI file and command-line arguments.
"""
parser = argparse.ArgumentParser(
description="Generate a blog article summarizing Git commits from repositories.",
)
parser.add_argument(
"-r",
"--repo-urls",
help="Comma-separated list of Git repository URLs.",
type=str,
)
parser.add_argument(
"-c",
"--company-identifier",
help='String to identify company commits (e.g., email domain or "My Company Name").',
type=str,
)
parser.add_argument(
"-m",
"--months-back",
help="Number of months back to analyze commits.",
type=int,
)
parser.add_argument(
"-f",
"--config-file",
help="Path to an INI configuration file. If provided and successfully loaded, "
"it will override other core parameters (-r, -c, -m).",
type=str,
)
parser.add_argument(
"-s",
"--save-to-file",
help="Automatically save the generated article to a file (provide filename).",
type=str,
nargs="?", # Allows the argument to be optional, if present without value, it's None
const="git_report.md", # Default value if -s is present without an argument
)
parser.add_argument(
"-d",
"--deploy-dir",
help="Name of the directory to clone repositories into (default: 'deploy').",
type=str,
default="deploy",
)
parser.add_argument(
"-k",
"--ai-key",
help="Pass the ai key for create commit summary by Author and repo.",
type=str,
default=None,
)
args = parser.parse_args()
repo_urls = []
company_identifier = ""
months_back = None
save_file_name = None
deploy_dir = None
ai_key = None
config_loaded_successfully = False
# Attempt to load from INI file if specified
if args.config_file:
config_data = load_config_from_ini(args.config_file)
if config_data:
repo_urls = config_data.get("repo_urls", [])
company_identifier = config_data.get("company_identifier", "")
months_back = config_data.get("months_back", None)
deploy_dir = config_data.get("deploy_dir", None)
ai_key = config_data.get("ai_apikey", None)
ai_model = config_data.get("ai_model", None)
config_loaded_successfully = True
print(f"Configuration loaded from {args.config_file}.")
else:
print(
f"Warning: Failed to load configuration from {args.config_file}."
" Proceeding with command-line arguments or prompts.",
)
# If config file was NOT successfully loaded, or not provided, then use CLI args
if not config_loaded_successfully:
if args.repo_urls:
repo_urls = [
url.strip() for url in args.repo_urls.split(",") if url.strip()
]
if args.company_identifier:
company_identifier = args.company_identifier.strip()
if args.months_back is not None:
months_back = args.months_back
if args.deploy_dir:
deploy_dir = args.deploy_dir
if args.ai_key:
ai_key = args.ai_key
if args.ai_model:
ai_model = args.ai_model
if args.deploy_dir:
deploy_dir = args.deploy_dir
if deploy_dir is None:
deploy_dir = "deploy"
if args.save_to_file is not None:
save_file_name = args.save_to_file
if not repo_urls:
repo_urls_input = input(
"Enter Git repository URLs (comma-separated, e.g.,"
"https://github.com/org/repo1.git,https://github.com/org/repo2.git): ",
).strip()
repo_urls = [url.strip() for url in repo_urls_input.split(",") if url.strip()]
if not repo_urls:
print("No repository URLs provided. Exiting.")
return
if not company_identifier:
company_identifier = input(
"Enter your company identifier (e.g., @mycompany.com or 'My Company Name'): ",
).strip()
if not company_identifier:
print("Company identifier cannot be empty. Exiting.")
return
if months_back is None:
while True:
try:
months_back = int(
input("Enter number of months back to analyze (e.g., 3): ").strip(),
)
if months_back <= 0:
raise ValueError
break
except ValueError:
print("Invalid input. Please enter a positive integer for months.")
if ai_key and not ai_model:
ai_model = "gpt-3.5-turbo"
print("\nStarting real Git analysis...")
analysis_result = analyze_real_git_commits(
repo_urls, company_identifier, months_back, deploy_dir,
)
if "error" in analysis_result:
print(f"\nError during Git analysis: {analysis_result['error']}")
return
commit_data = analysis_result.get("commit_data", [])
article = generate_article_content(commit_data, months_back, ai_key, ai_model)
print("\n--- Generated Article ---")
print(article)
print("\n--- End of Article ---")
if save_file_name:
try:
with open(save_file_name, "w", encoding="utf-8") as f:
f.write(article)
print(f"Article automatically saved to {save_file_name}")
except Exception as e:
print(f"Error automatically saving file {save_file_name}: {e}")
else:
save_option = (
input("\nDo you want to save the article to a file? (yes/no): ")
.lower()
.strip()
)
if save_option == "yes":
file_name = input("Enter desired filename (e.g., git_report.md): ").strip()
if not file_name:
file_name = "git_report.md"
try:
with open(file_name, "w", encoding="utf-8") as f:
f.write(article)
print(f"Article saved to {file_name}")
except Exception as e:
print(f"Error saving file: {e}")
if __name__ == "__main__":
main()