init commit
This commit is contained in:
+142
@@ -0,0 +1,142 @@
|
||||
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()
|
||||
ENV_PATH = f'/home/{WHOAMI}/.config/redmine-cli/.env'
|
||||
LOG_PATH = f'/home/{WHOAMI}/.config/redmine-cli/log'
|
||||
|
||||
load_dotenv(dotenv_path=ENV_PATH)
|
||||
|
||||
# REDMINE_URL= "https://192.168.10.231/redmine/"
|
||||
REDMINE_URL = "http://pc23/redmine"
|
||||
REDMINE_USERNAME = os.getenv('REDMINE_USERNAME')
|
||||
REDMINE_PASSWORD = os.getenv('REDMINE_PASSWORD')
|
||||
REDMINE_USER_ID = os.getenv('REDMINE_USER_ID')
|
||||
|
||||
|
||||
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(ENV_PATH):
|
||||
try:
|
||||
with open(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 {ENV_PATH}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {e}")
|
||||
else:
|
||||
print(f"The file {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(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():
|
||||
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 = 223,
|
||||
tracker_id = 1,
|
||||
status_id = 5, #Erledigt 3/Abgeschlossen 5
|
||||
assigned_to_id = REDMINE_USER_ID,
|
||||
estimated_hours = estimated_hours_input,
|
||||
subject = title,
|
||||
done_ratio = 100,
|
||||
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(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()
|
||||
Reference in New Issue
Block a user