general refactoring
This commit is contained in:
124
main.py
124
main.py
@@ -10,21 +10,27 @@ load_dotenv()
|
||||
|
||||
ACCESS_TOKEN = getenv("TOKEN")
|
||||
URL = getenv("URL")
|
||||
USERNAMES = ["kalinom6"] # Modify
|
||||
USERNAMES = ["kalinom6"]
|
||||
CERT = getenv("CERT_LOCATION")
|
||||
WEEKS_BACK = -1
|
||||
CHECK_MONTH_BACK = False
|
||||
WEEKS_BACK = 0
|
||||
MONTH_VIEW = False
|
||||
VERBOSE = False
|
||||
MONTHS_BACK = 0
|
||||
VERBOSE = True
|
||||
|
||||
def get_flag_value(flag):
|
||||
return flag.split("=")[1]
|
||||
|
||||
if "--monthview" in sys.argv:
|
||||
MONTH_VIEW = True
|
||||
if "--monthback" in sys.argv:
|
||||
CHECK_MONTH_BACK = True
|
||||
else:
|
||||
for arg in sys.argv:
|
||||
if "--weeksback" in arg:
|
||||
WEEKS_BACK = int(arg.split("=")[1])
|
||||
for arg in sys.argv:
|
||||
if "--weeksback" in arg and not MONTH_VIEW:
|
||||
WEEKS_BACK = int(get_flag_value(arg))
|
||||
if "--monthsback" in arg and MONTH_VIEW:
|
||||
MONTHS_BACK = int(get_flag_value(arg))
|
||||
if "--username" in arg:
|
||||
USERNAMES[0] = get_flag_value(arg)
|
||||
if ',' in USERNAMES[0]:
|
||||
USERNAMES = USERNAMES[0].split(',')
|
||||
if "--verbose" in sys.argv:
|
||||
VERBOSE = True
|
||||
|
||||
@@ -50,7 +56,7 @@ def get_issues(username):
|
||||
payload = {
|
||||
"jql": jql,
|
||||
"startAt": 0,
|
||||
"maxResults": 15,
|
||||
"maxResults": 999,
|
||||
"fields": [
|
||||
"key", "summary"
|
||||
]
|
||||
@@ -68,7 +74,7 @@ def get_issues(username):
|
||||
return tasks
|
||||
|
||||
|
||||
def get_worklogs(issues):
|
||||
def get_all_worklogs(issues):
|
||||
worklogs = []
|
||||
for issue in issues:
|
||||
data = send_request(f'{URL}/issue/{issue["task_id"]}/worklog', "GET")
|
||||
@@ -84,11 +90,11 @@ def get_worklogs(issues):
|
||||
if worklog["author"] in USERNAMES:
|
||||
worklogs.append(worklog)
|
||||
|
||||
worklogs.sort(key=date_sort)
|
||||
worklogs.sort(key=date_getter)
|
||||
return worklogs
|
||||
|
||||
|
||||
def date_sort(worklog):
|
||||
def date_getter(worklog):
|
||||
return worklog['date']
|
||||
|
||||
|
||||
@@ -120,65 +126,65 @@ def get_days(day):
|
||||
return days, days_second_part
|
||||
|
||||
|
||||
def get_week_tickets(worklogs, days):
|
||||
this_week_tickets = []
|
||||
def get_worklogs_for_days(worklogs, days):
|
||||
timeperiod_tickets = []
|
||||
for worklog in worklogs:
|
||||
worklog_date = datetime.datetime.strptime(
|
||||
worklog["date"].split('T')[0], '%Y-%m-%d').date()
|
||||
if str(worklog_date) in days:
|
||||
this_week_tickets.append(worklog)
|
||||
return this_week_tickets
|
||||
timeperiod_tickets.append(worklog)
|
||||
return timeperiod_tickets
|
||||
|
||||
def main():
|
||||
print('The script starts its work...')
|
||||
|
||||
issues = []
|
||||
worklogs = []
|
||||
if MONTH_VIEW:
|
||||
for username in USERNAMES:
|
||||
issues = get_issues(username)
|
||||
worklogs = get_worklogs(issues)
|
||||
days = get_days_of_month(CHECK_MONTH_BACK)
|
||||
worklogs = get_month_tickets(worklogs, days, CHECK_MONTH_BACK)
|
||||
omnimat_string = get_month_view(worklogs)
|
||||
print(omnimat_string)
|
||||
else:
|
||||
for username in USERNAMES:
|
||||
issues = get_issues(username)
|
||||
worklogs = get_worklogs(issues)
|
||||
today = datetime.date.today()
|
||||
days, days_second_part = get_days(today)
|
||||
week_worklogs = get_week_tickets(worklogs, days)
|
||||
omnimat_string = ""
|
||||
|
||||
for worklog in week_worklogs:
|
||||
if worklog["ticket_id"] not in omnimat_string:
|
||||
omnimat_string += worklog["ticket_id"] + '\n'
|
||||
|
||||
omnimat_string = ""
|
||||
if days_second_part:
|
||||
second_omnimat_string = ""
|
||||
worklogs_second_part = get_week_tickets(worklogs, days_second_part)
|
||||
for worklog in week_worklogs:
|
||||
if worklog["ticket_id"] not in second_omnimat_string:
|
||||
second_omnimat_string += worklog["ticket_id"] + '\n'
|
||||
all_worklogs = get_all_worklogs(issues)
|
||||
days = get_days_of_month(MONTHS_BACK)
|
||||
print(all_worklogs)
|
||||
worklogs = get_worklogs_for_days(all_worklogs, days)
|
||||
print(worklogs)
|
||||
month_view = get_month_view(worklogs)
|
||||
print(month_view)
|
||||
return
|
||||
for username in USERNAMES:
|
||||
issues = get_issues(username)
|
||||
all_worklogs = get_all_worklogs(issues)
|
||||
today = datetime.date.today()
|
||||
days, days_second_part = get_days(today)
|
||||
worklogs = get_worklogs_for_days(all_worklogs, days)
|
||||
omnimat_string = ""
|
||||
|
||||
print(worklogs)
|
||||
for worklog in worklogs:
|
||||
if worklog["ticket_id"] not in omnimat_string:
|
||||
omnimat_string += worklog["ticket_id"] + '\n'
|
||||
if days_second_part:
|
||||
second_omnimat_string = ""
|
||||
worklogs_second_part = get_worklogs_for_days(all_worklogs, days_second_part)
|
||||
for worklog in worklogs:
|
||||
if worklog["ticket_id"] not in second_omnimat_string:
|
||||
second_omnimat_string += worklog["ticket_id"] + '\n'
|
||||
if VERBOSE:
|
||||
print('Issues of the user:')
|
||||
print(json.dumps(issues, indent=4))
|
||||
print(f"Worklogs for the time period:")
|
||||
print(json.dumps(worklogs, indent=4))
|
||||
print("Days:")
|
||||
print(days)
|
||||
print(f"Omnimat string:")
|
||||
print(omnimat_string)
|
||||
if days_second_part:
|
||||
if VERBOSE:
|
||||
print('Issues of the user:')
|
||||
print(json.dumps(issues, indent=4))
|
||||
print(f"Worklogs for the week:")
|
||||
print(json.dumps(week_worklogs, indent=4))
|
||||
print(f"Worklogs for the weeks 2nd part:")
|
||||
print(json.dumps(worklogs_second_part, indent=4))
|
||||
print("Days:")
|
||||
print(days)
|
||||
print(days_second_part)
|
||||
print(f"Omnimat string:")
|
||||
print(omnimat_string)
|
||||
if days_second_part:
|
||||
if VERBOSE:
|
||||
print(f"Worklogs for the weeks 2nd part:")
|
||||
print(json.dumps(worklogs_second_part, indent=4))
|
||||
print("Days:")
|
||||
print(days_second_part)
|
||||
print(f"Omnimat string:")
|
||||
print(omnimat_string)
|
||||
|
||||
print(second_omnimat_string)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -1,43 +1,23 @@
|
||||
import datetime
|
||||
|
||||
#code of month branch
|
||||
def get_days_of_month(previous_month=False):
|
||||
today = datetime.date.today()
|
||||
start_of_month = today.replace(day=1)
|
||||
end_of_month = today.replace(day=28)+datetime.timedelta(days=4)
|
||||
end_of_month -= datetime.timedelta(days=end_of_month.day)
|
||||
def get_days_of_month(months_back=0):
|
||||
# set the current day to the first day of the current month
|
||||
# and loop for the days until the month changes
|
||||
current_day = datetime.datetime.today().replace(day=1)
|
||||
current_month = current_day.month
|
||||
days = []
|
||||
while months_back:
|
||||
current_day = datetime.datetime.today().replace(day=1)
|
||||
current_day -= datetime.timedelta(days=1)
|
||||
current_day = current_day.replace(day=1)
|
||||
current_month = current_day.month
|
||||
months_back -= 1
|
||||
while current_day.month == current_month:
|
||||
days.append(str(current_day).split(' ')[0])
|
||||
current_day += datetime.timedelta(days=1)
|
||||
|
||||
# minus one month
|
||||
if previous_month == True:
|
||||
start_of_month -= datetime.timedelta(days=1)
|
||||
start_of_month = start_of_month.replace(day=1)
|
||||
end_of_month = start_of_month
|
||||
end_of_month = today.replace(day=28)+datetime.timedelta(days=4)
|
||||
end_of_month -= datetime.timedelta(days=end_of_month.day)
|
||||
|
||||
days.append(str(start_of_month))
|
||||
while start_of_month != end_of_month:
|
||||
start_of_month += datetime.timedelta(days=1)
|
||||
days.append(str(start_of_month))
|
||||
|
||||
# returns all days of current month
|
||||
return days
|
||||
|
||||
def get_month_tickets(worklogs, days, previous_month=False):
|
||||
this_month_tickets = []
|
||||
for worklog in worklogs:
|
||||
worklog_date = datetime.datetime.strptime(
|
||||
worklog["date"].split('T')[0], '%Y-%m-%d').date()
|
||||
current_month = datetime.date.today().month
|
||||
# minus one month
|
||||
if previous_month == True:
|
||||
current_month -= 1
|
||||
worklog_month = str(worklog_date).split('-')[1]
|
||||
if str(worklog_date) in days and int(worklog_month) == current_month:
|
||||
this_month_tickets.append(worklog)
|
||||
return this_month_tickets
|
||||
|
||||
|
||||
def get_month_view(worklogs):
|
||||
|
||||
@@ -50,7 +30,7 @@ def get_month_view(worklogs):
|
||||
week_number_end = week_number_start+datetime.timedelta(days=6)
|
||||
|
||||
week_iso = week.isocalendar().week
|
||||
omnimat_string = worklogs[0]["author"]
|
||||
month_view = worklogs[0]["author"]
|
||||
|
||||
for worklog in worklogs:
|
||||
ticket = worklog["ticket_id"]
|
||||
@@ -59,13 +39,12 @@ def get_month_view(worklogs):
|
||||
|
||||
week_day = datetime.datetime.strptime(date, '%Y-%m-%d').date()
|
||||
if (week_day.isocalendar().week == week_iso):
|
||||
omnimat_string += "\nWeek " + str(week_iso)+"\tfrom "+week_number_start.__str__() + \
|
||||
month_view += "\nWeek " + str(week_iso)+"\tfrom "+week_number_start.__str__() + \
|
||||
"\tto "+week_number_end.__str__()+"\n\n"
|
||||
week_iso += 1
|
||||
week_number_start += datetime.timedelta(days=7)
|
||||
week_number_end += datetime.timedelta(days=7)
|
||||
|
||||
omnimat_string += ticket+'\tdate '+date+'\ttime '+timespent + '\n'
|
||||
month_view += ticket+'\tdate '+date+'\ttime '+timespent + '\n'
|
||||
|
||||
return omnimat_string
|
||||
#end of code month branch
|
||||
return month_view
|
||||
|
||||
Reference in New Issue
Block a user