158 lines
5.4 KiB
Python
158 lines
5.4 KiB
Python
from redminelib import Redmine
|
|
from redminelib.exceptions import AuthError
|
|
from dotenv import load_dotenv
|
|
from colorama import Fore, Style, init
|
|
from datetime import date, datetime
|
|
import os, getpass
|
|
|
|
WHOAMI = getpass.getuser()
|
|
AUTH_ENV_PATH = f'/home/{WHOAMI}/.config/redmine-cli/.auth.env'
|
|
SETTINGS_ENV_PATH = f'/home/{WHOAMI}/.config/redmine-cli/.settings.env'
|
|
LOG_PATH = f'/home/{WHOAMI}/.config/redmine-cli/log'
|
|
|
|
load_dotenv(dotenv_path=AUTH_ENV_PATH)
|
|
load_dotenv(dotenv_path=SETTINGS_ENV_PATH)
|
|
# REDMINE_URL= "https://192.168.10.231/redmine/"
|
|
# Auth
|
|
REDMINE_URL = os.getenv("REDMINE_URL")
|
|
REDMINE_USERNAME = os.getenv('REDMINE_USERNAME')
|
|
REDMINE_PASSWORD = os.getenv('REDMINE_PASSWORD')
|
|
REDMINE_USER_ID = os.getenv('REDMINE_USER_ID')
|
|
|
|
# Settings
|
|
REDMINE_PROJECT_ID = os.getenv('REDMINE_PROJECT_ID')
|
|
REDMINE_STATUS_ID = os.getenv('REDMINE_STATUS_ID')
|
|
REDMINE_ESTIMATED_HOURS = os.getenv('REDMINE_ESTIMATED_HOURS')
|
|
REDMINE_DONE_RATIO = os.getenv('REDMINE_DONE_RATIO')
|
|
SCRIPT_DEBUG = os.getenv('SCRIPT_DEBUG')
|
|
|
|
def debugging():
|
|
if SCRIPT_DEBUG == 0:
|
|
pass
|
|
else:
|
|
print('debug activated')
|
|
|
|
def custom_input(datatype, text, color):
|
|
if color == "magenta":
|
|
rtn = datatype(input(f" {Fore.MAGENTA}{text}{Fore.MAGENTA}: ")) or f"Default {text}"
|
|
return rtn
|
|
return None
|
|
|
|
|
|
def clear_console():
|
|
os.system('clear')
|
|
|
|
|
|
def write_to_env(auth_to_change):
|
|
if not os.path.exists(AUTH_ENV_PATH):
|
|
try:
|
|
with open(AUTH_ENV_PATH, 'w') as env_file:
|
|
for key, value in auth_to_change.items():
|
|
env_file.write(f"{key}={value}\n")
|
|
print(f"Credentials written to {AUTH_ENV_PATH}")
|
|
except Exception as e:
|
|
print(f"An error occurred: {e}")
|
|
else:
|
|
print(f"The file {AUTH_ENV_PATH} already exists.")
|
|
|
|
|
|
def get_redmine_user(temp_username, temp_password):
|
|
try:
|
|
redmine = Redmine(REDMINE_URL, username=temp_username, password=temp_password, requests={'verify': False})
|
|
user = redmine.user.get("current")
|
|
|
|
new_auth_credentials = {
|
|
'REDMINE_USERNAME': temp_username,
|
|
'REDMINE_PASSWORD': temp_password,
|
|
'REDMINE_USER_ID': user.id
|
|
}
|
|
write_to_env(new_auth_credentials)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
|
|
def get_credentials():
|
|
global redmine, user
|
|
|
|
if os.path.exists(AUTH_ENV_PATH):
|
|
try:
|
|
redmine = Redmine(REDMINE_URL, username=REDMINE_USERNAME, password=REDMINE_PASSWORD, requests={'verify': False})
|
|
# user = redmine.user.get("current")
|
|
# print(user.id)
|
|
# input("...")
|
|
create_new_ticket_assigned_to_myself_and_set_to_done() #!temp
|
|
except:
|
|
print("No connection to redmine...")
|
|
else:
|
|
if not os.path.exists(f"/home/{WHOAMI}/.config/redmine-cli"):
|
|
os.mkdir(f"/home/{WHOAMI}/.config/redmine-cli")
|
|
if not os.path.exists(f"/home/{WHOAMI}/.config/redmine-cli/log"):
|
|
os.mkdir(f"/home/{WHOAMI}/.config/redmine-cli/log")
|
|
|
|
temp_username = str(input('Please input redmine username: '))
|
|
temp_password = getpass.getpass('Please input redmine password: ')
|
|
try:
|
|
redmine = Redmine(REDMINE_URL, username=temp_username, password=temp_password, requests={'verify': False}).auth()
|
|
get_redmine_user(temp_username, temp_password)
|
|
except AuthError:
|
|
raise Exception('Invalid login or password provided')
|
|
|
|
|
|
def create_new_ticket_assigned_to_myself_and_set_to_done():
|
|
clear_console()
|
|
title = custom_input(str, "Subject q/exit", "magenta")
|
|
if title == "q":
|
|
os._exit(0)
|
|
# title = str(input(' Subject: ')) or 'Default Subject'
|
|
description = custom_input(str, "Description", "magenta")
|
|
try:
|
|
estimated_hours_input = int(custom_input(str, "Estimated hours (int)", "magenta"))
|
|
except ValueError:
|
|
estimated_hours_input = 0.5
|
|
try:
|
|
redmine.issue.create(
|
|
project_id = REDMINE_PROJECT_ID, #EDV 223/IT Support 425
|
|
tracker_id = 1,
|
|
status_id = REDMINE_STATUS_ID, #Erledigt 3/Abgeschlossen 5
|
|
assigned_to_id = REDMINE_USER_ID,
|
|
estimated_hours = REDMINE_ESTIMATED_HOURS,
|
|
subject = title,
|
|
done_ratio = REDMINE_DONE_RATIO,
|
|
description = description,
|
|
)
|
|
try:
|
|
log_for_current_day(title, description)
|
|
except Exception as e:
|
|
print(f"loggin not possible\n{e}")
|
|
print(" Ticket has been created")
|
|
input(" Please press enter to continue...")
|
|
|
|
except Exception as e:
|
|
print(REDMINE_PROJECT_ID)
|
|
print(f" An error occurred: {e}")
|
|
input(" Please press enter to continue...")
|
|
|
|
|
|
def log_for_current_day(ticket_title, ticket_desc):
|
|
|
|
if not ticket_desc:
|
|
ticket_desc = "No Description"
|
|
file_name = date.today()
|
|
file_name = file_name.strftime("%d-%m-%y")
|
|
|
|
current_time = datetime.now()
|
|
current_time = current_time.strftime("%H:%M")
|
|
|
|
if not os.path.exists(f"{LOG_PATH}/{file_name}"):
|
|
with open(f"{LOG_PATH}/{file_name}", "w+") as f:
|
|
f.close()
|
|
|
|
with open(f"{LOG_PATH}/{file_name}", "a") as f:
|
|
f.write(f"{current_time} | {ticket_title} > {ticket_desc}\n")
|
|
|
|
if __name__ == '__main__':
|
|
init(autoreset=True)
|
|
while True:
|
|
get_credentials()
|
|
# create_new_ticket_assigned_to_myself_and_set_to_done() |