changelog_generator/changelog2/main.py
2024-09-20 16:00:11 +02:00

154 lines
4.5 KiB
Python

#!/usr/bin/env python
import argparse
import json
import os
import ssl
import urllib.request as request
from config.config import parse_config
from crew import ChangelogCrew
from langchain_ollama import ChatOllama
from langchain_openai import ChatOpenAI
config = parse_config()
os.environ["OPENAI_API_KEY"] = config["OPENAI_API_KEY"]
# os.environ["OPENAI_API_BASE"] = "http://ollama:11434"
# os.environ["OPENAI_MODEL_NAME"] = "llama3.1:8b" # Adjust based on available model
llmLocal = ChatOllama(
model="llama3.1:8b", base_url="http://ollama:11434", temperatur=0.4
)
llmOpenAI = ChatOpenAI(
model_name="gpt-4o",
temperature=0.4,
)
config["git_task"] = False
def makeRedmineRequest(url: str, APIKey: str):
context = ssl.create_default_context()
context.check_hostname = False
context.verify_mode = ssl.CERT_NONE
req = request.Request(url, method="GET")
req.add_header("Content-Type", "application/json")
req.add_header("X-Redmine-API-Key", APIKey)
return request.urlopen(req, context=context)
def makeProjectRequest(url: str, APIKey: str) -> dict:
res = makeRedmineRequest(url, APIKey)
json_data = json.load(res)
project_info = {project["name"]: project["id"] for project in json_data["projects"]}
return project_info
def makeVersionRequest(url: str, APIKey: str, project_id: int) -> dict:
res = makeRedmineRequest(url, APIKey)
json_data = json.load(res)
version_info = {
version["name"]: version["id"]
for version in json_data["versions"]
if version["project"]["id"] == project_id and version["status"] == "open"
}
return version_info
def main():
desc = "This is a tool that generates changelog messages from Redmine and Git using AI."
parser = argparse.ArgumentParser(description=desc, add_help=True)
parser.add_argument(
"-p",
"--project",
help="The project ID for which text is to be generated",
# type=int,
type=str,
)
parser.add_argument(
"-f",
"--fixed-version",
help="The version ID of the project for which text is to be generated",
# type=int,
type=str,
)
parser.add_argument(
"-r",
"--repo",
help="The path to the Git repository",
type=str,
)
parser.add_argument(
"-t",
"--type",
help="The type of text to be generated",
choices=["Changelog", "ReleaseNotes", "test"],
type=str,
)
parser.add_argument(
"-l",
"--local",
help="Execute the generator using a local llm",
default=False,
action="store_true",
)
args = parser.parse_args()
if not args.project:
project_infos = makeProjectRequest(
url=config["REDMINE_HOST"] + "/projects.json",
APIKey=config["REDMINE_API_KEY"],
)
print("Enter a project id when calling. There is a choice:")
for project_name, project_id in project_infos.items():
print(f"{project_name} (ID: {project_id})")
exit(0)
if args.project and not args.fixed_version:
print("Enter a version id when calling. There is a choice:")
fixed_version = makeVersionRequest(
url=config["REDMINE_HOST"]
+ "/projects/"
+ str(args.project)
+ "/versions.json",
APIKey=config["REDMINE_API_KEY"],
project_id=args.project,
)
for version_name, version_id in fixed_version.items():
print(f"{version_name}: {version_id}")
exit(0)
if args.project and args.fixed_version and not args.type:
args.type = input("Please choose a type (Changelog/ReleaseNotes)")
customField = ""
prompt = ""
if args.project and args.fixed_version:
if args.type == "Changelog":
customField = "&cf_21=Changelog"
elif args.type == "ReleaseNotes":
customField = "&cf_21=Release_notes"
else:
customField = ""
if args.repo != None:
os.environ["GIT_REPO_PATH"] = args.repo
config["git_task"] = True
else:
os.environ["GIT_REPO_PATH"] = ""
changelog_crew = ChangelogCrew(git_task=config["git_task"], local=args.local)
result = changelog_crew.crew().kickoff(
inputs={
"project_id": args.project,
"version_id": args.fixed_version,
"repo_path": args.repo,
"changelog_type": args.type,
}
)
print("######################")
print(result)
if __name__ == "__main__":
main()