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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Michał Kalinowski
					Michał Kalinowski