-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
122 lines (106 loc) · 3.65 KB
/
main.py
File metadata and controls
122 lines (106 loc) · 3.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
121
122
import argparse
import logging
import os
import subprocess
import sys
from dotenv import load_dotenv
import helpers
from git import GitHelper
from parser import OverleafParser
# load config from .env file
load_dotenv()
OVERLEAF_URL = os.getenv("OVERLEAF_URL")
GIT_REPO_PATH = os.getenv("GIT_REPO_PATH")
TMP_ZIP_FOLDER = os.getenv("TMP_ZIP_FOLDER", "/tmp/")
LOGS_FOLDER = os.getenv("LOGS_FOLDER", "/tmp/overleaf_logs")
OPENWEBUI_URL = os.getenv("OPENWEBUI_URL")
API_KEY = os.getenv("API_KEY")
def main(args):
# logging
logger = logging.getLogger(__name__)
logging.basicConfig(filename=f'{LOGS_FOLDER}/{helpers.get_hour()}.log', level=args.log_level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.info(f"started, args: {args}")
# check constraints
if not args.overleaf_url.startswith("https://www.overleaf.com/read/"):
logger.error("invalid overleaf URL: need read (`https://www.overleaf.com/read/...`)")
return 1
if not os.path.isdir(os.path.join(os.path.expanduser(GIT_REPO_PATH), ".git")):
logger.error(f"git repo path {GIT_REPO_PATH} does not contain a .git folder")
return 1
# download overleaf project
logger.debug(f"downloading overleaf project from {args.overleaf_url} to {os.path.expanduser(args.git_path)}")
parser = OverleafParser(args.overleaf_url)
try:
parser.download(download_dir=TMP_ZIP_FOLDER)
except Exception as e:
logger.error(f"failed to download overleaf project: {e}")
return 1
# move downloaded files to git repo
logger.debug(f"moving downloaded files from {TMP_ZIP_FOLDER} to {os.path.expanduser(args.git_path)}")
proces_unzip = subprocess.run(
["unzip", "-o", os.path.join(TMP_ZIP_FOLDER, parser.get_filename()), "-d", os.path.expanduser(args.git_path)],
capture_output=True,
text=True
)
if proces_unzip.returncode != 0:
logger.error(f"failed to unzip downloaded files: {proces_unzip.stderr}")
return 1
logger.debug("unzip completed successfully")
# get git changes
git = GitHelper(args.git_path)
if not (git_diff := git.get_diff()):
logger.info("no changes detected, exiting")
return 0
logger.info("changes detected, proceeding with git operations")
# analyze changes and prepare commit message
openwebui = helpers.OpenWebUIHelper(
openwebui_url=args.openwebui_url,
api_key=args.api_key,
)
commit_message = openwebui.chat_with_model(
git_diff
)
logger.info(f"commit message: {commit_message}")
# git add, commit, and push
if not git.add_all():
return 1
if not git.commit(commit_message):
return 0
if not git.push():
return 1
return 0
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--overleaf_url",
type=str,
help="The URL of the Overleaf project to clone.",
default=OVERLEAF_URL
)
parser.add_argument(
"--git_path",
type=str,
default=GIT_REPO_PATH
)
parser.add_argument(
"--log_level",
type=str,
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
default="INFO",
help="Set the logging level."
)
parser.add_argument(
"--openwebui_url",
type=str,
help="URL of the OpenWebUI instance to use for commit message generation.",
default=OPENWEBUI_URL
)
parser.add_argument(
"--api_key",
type=str,
default=API_KEY,
help="API key for the OpenWebUI instance."
)
args = parser.parse_args()
sys.exit(main(args))