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

121 lines
4 KiB
Python

import urllib.request as request
import json
from dotenv import load_dotenv
import os
import ssl
import argparse
import textwrap
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():
try:
load_dotenv("./.env")
rdmnHost = os.getenv("REDMINE_HOST")
rdmnAPIKey = os.getenv("REDMINE_API_KEY")
openaiAPIKey = os.getenv("OPENAI_API_KEY")
except Exception as e:
print("No Env_variables set: ", e)
finally:
rdmnHost = "https://redmine.tixeltec.de/redmine"
if rdmnAPIKey is None or openaiAPIKey is None:
print(
"Please set environment variables for REDMINE_API_KEY and/or OPENAI_API_KEY"
)
exit(1)
desc = """This is a simple tool that allows you to generate changelog messages from Redmine using AI.
Usage:
1. Create a `.env` file . Fill in the environment variables as follows:
```
REDMINE_HOST=your_redmine_host
REDMINE_API_KEY=your_redmine_api_key
OPENAI_API_KEY=your_openai_api_key
```
2. Run the script with the following arguments:\n
- `-p, --project`: The project ID for which the changelog should be generated.
- `-f, --fixed-version`: The version ID for which the changelog should be generated.
- `-t, --type`: The type of text to be generated (Changelog or Release Notes).
3. If you don't provide a project ID or version ID, a list of available projects and versions will be displayed for you to choose from.
4. The generated changelog will be output in AsciiDoc format on the console.
Examples:
python3 changelog_generator.py -p 47 -f 755 -t Changelog
python3 changelog_generator.py -p 47 -f 755 -t ReleaseNotes
"""
clp = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=textwrap.dedent(desc),
add_help=True,
)
clp.add_argument(
"-p",
"--project",
help="The project for which text is to be generated",
type=int,
)
clp.add_argument(
"-f",
"--fixed-version",
help="The version of the project for which text is to be generated",
type=int,
)
clp.add_argument(
"-t",
"--type",
help="The type of text to be generated",
choices=["Changelog", "ReleaseNotes"],
type=str,
)
args = clp.parse_args()
if not args.project:
project_infos = makeProjectRequest(
url=rdmnHost + "/projects.json", APIKey=rdmnAPIKey
)
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})")
if args.project and not args.fixed_version:
print("Enter a version id when calling. There is a choice:")
fixed_version = makeVersionRequest(
url=rdmnHost + "/projects/" + str(args.project) + "/versions.json",
APIKey=rdmnAPIKey,
project_id=args.project,
)
for version_name, version_id in fixed_version.items():
print(f"{version_name}: {version_id}")
if __name__ == "__main__":
main()