month solution #9
180
main.py
180
main.py
@@ -1,4 +1,4 @@
|
|||||||
import requests
|
import pip._vendor.requests as requests
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
@@ -6,32 +6,39 @@ from os import getenv
|
|||||||
|
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
ACCESS_TOKEN = getenv("TOKEN")
|
ACCESS_TOKEN = getenv("TOKEN")
|
||||||
USERNAMES = ["kalinom6"] # Modify
|
USERNAMES = ["litneri"] # Modify
|
||||||
URL = "https://globaljira.roche.com/rest/api/2"
|
URL = "https://globaljira.roche.com/rest/api/2"
|
||||||
CERT = getenv("CERT_LOCATION")
|
CERT = getenv("CERT_LOCATION")
|
||||||
WEEKS_BACK = 1
|
WEEKS_BACK = 1
|
||||||
VERBOSE = True
|
VERBOSE = True
|
||||||
|
|
||||||
|
MONTH_VIEW = True
|
||||||
|
CHECK_MONTH_BACK = False
|
||||||
|
|
||||||
headers = {
|
headers = {
|
||||||
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
||||||
"Content-Type": "application/json"
|
"Content-Type": "application/json"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def send_request(url, method, payload=None):
|
def send_request(url, method, payload=None):
|
||||||
if method == "GET":
|
if method == "GET":
|
||||||
response = requests.get(url, headers=headers, data=json.dumps(payload), verify=CERT)
|
response = requests.get(url, headers=headers,
|
||||||
|
data=json.dumps(payload), verify=CERT)
|
||||||
if method == "POST":
|
if method == "POST":
|
||||||
response = requests.post(url, headers=headers, data=json.dumps(payload), verify=CERT)
|
response = requests.post(url, headers=headers,
|
||||||
|
data=json.dumps(payload), verify=CERT)
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
return response.json()
|
return response.json()
|
||||||
print(f"Error: {response.status_code}, {response.text}")
|
print(f"Error: {response.status_code}, {response.text}")
|
||||||
|
|
||||||
|
|
||||||
def get_issues(username):
|
def get_issues(username):
|
||||||
jql = f"assignee WAS '{username}'"
|
jql = f"assignee WAS '{username}'"
|
||||||
payload = {
|
payload = {
|
||||||
"jql": jql,
|
"jql": jql,
|
||||||
"startAt": 0,
|
"startAt": 0,
|
||||||
"maxResults": 50,
|
"maxResults": 15,
|
||||||
"fields": [
|
"fields": [
|
||||||
"key", "summary"
|
"key", "summary"
|
||||||
]
|
]
|
||||||
@@ -43,11 +50,12 @@ def get_issues(username):
|
|||||||
task = {
|
task = {
|
||||||
"task_id": issue["key"],
|
"task_id": issue["key"],
|
||||||
"summary": issue["fields"]["summary"],
|
"summary": issue["fields"]["summary"],
|
||||||
"task_link": f"https://globaljira.roche.com/browse/{issue['key']}"
|
"task_link": f"https://globaljira.roche.com/browse/{issue['key']}"
|
||||||
}
|
}
|
||||||
tasks.append(task)
|
tasks.append(task)
|
||||||
return tasks
|
return tasks
|
||||||
|
|
||||||
|
|
||||||
def get_worklogs(issues):
|
def get_worklogs(issues):
|
||||||
worklogs = []
|
worklogs = []
|
||||||
for issue in issues:
|
for issue in issues:
|
||||||
@@ -62,13 +70,20 @@ def get_worklogs(issues):
|
|||||||
"author": log["author"]["name"]
|
"author": log["author"]["name"]
|
||||||
}
|
}
|
||||||
if worklog["author"] in USERNAMES:
|
if worklog["author"] in USERNAMES:
|
||||||
worklogs.append(worklog)
|
worklogs.append(worklog)
|
||||||
|
|
||||||
|
worklogs.sort(key=date_sort)
|
||||||
return worklogs
|
return worklogs
|
||||||
|
|
||||||
|
|
||||||
|
def date_sort(worklog):
|
||||||
|
return worklog['date']
|
||||||
|
|
||||||
|
|
||||||
def get_days(day):
|
def get_days(day):
|
||||||
if WEEKS_BACK:
|
if WEEKS_BACK:
|
||||||
day = day - datetime.timedelta(weeks=WEEKS_BACK)
|
day = day - datetime.timedelta(weeks=WEEKS_BACK)
|
||||||
|
|
||||||
year, weeknum, day_of_week = day.isocalendar()
|
year, weeknum, day_of_week = day.isocalendar()
|
||||||
while day_of_week != 1:
|
while day_of_week != 1:
|
||||||
day = day - datetime.timedelta(days=1)
|
day = day - datetime.timedelta(days=1)
|
||||||
@@ -81,7 +96,7 @@ def get_days(day):
|
|||||||
days.append(str(day))
|
days.append(str(day))
|
||||||
day = day + datetime.timedelta(days=1)
|
day = day + datetime.timedelta(days=1)
|
||||||
day_of_week += 1
|
day_of_week += 1
|
||||||
|
|
||||||
days_second_part = []
|
days_second_part = []
|
||||||
if day_of_week <= 7: # This condition means the loop ended because the month changed
|
if day_of_week <= 7: # This condition means the loop ended because the month changed
|
||||||
next_month = day.month
|
next_month = day.month
|
||||||
@@ -89,55 +104,138 @@ def get_days(day):
|
|||||||
days_second_part.append(str(day))
|
days_second_part.append(str(day))
|
||||||
day = day + datetime.timedelta(days=1)
|
day = day + datetime.timedelta(days=1)
|
||||||
day_of_week += 1
|
day_of_week += 1
|
||||||
|
|
||||||
return days, days_second_part
|
return days, days_second_part
|
||||||
|
|
||||||
|
|
||||||
def get_week_tickets(worklogs, days):
|
def get_week_tickets(worklogs, days):
|
||||||
this_week_tickets = []
|
this_week_tickets = []
|
||||||
for worklog in worklogs:
|
for worklog in worklogs:
|
||||||
worklog_date = datetime.datetime.strptime(worklog["date"].split('T')[0], '%Y-%m-%d').date()
|
worklog_date = datetime.datetime.strptime(
|
||||||
|
worklog["date"].split('T')[0], '%Y-%m-%d').date()
|
||||||
if str(worklog_date) in days:
|
if str(worklog_date) in days:
|
||||||
this_week_tickets.append(worklog)
|
this_week_tickets.append(worklog)
|
||||||
return this_week_tickets
|
return this_week_tickets
|
||||||
|
|
||||||
|
#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)
|
||||||
|
days = []
|
||||||
|
|
||||||
|
# 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_omnimat_string(worklogs):
|
||||||
|
|
||||||
|
week = datetime.datetime.strptime(
|
||||||
|
worklogs[0]["date"][0:10], '%Y-%m-%d').date()
|
||||||
|
|
||||||
|
week_number_start = week
|
||||||
|
to_monday = week_number_start.weekday()
|
||||||
|
week_number_start -= datetime.timedelta(days=to_monday)
|
||||||
|
week_number_end = week_number_start+datetime.timedelta(days=6)
|
||||||
|
|
||||||
|
week_iso = week.isocalendar().week
|
||||||
|
omnimat_string = worklogs[0]["author"]
|
||||||
|
|
||||||
|
for worklog in worklogs:
|
||||||
|
ticket = worklog["ticket_id"]
|
||||||
|
date = worklog["date"][0:10]
|
||||||
|
timespent = worklog["time_spent"]
|
||||||
|
|
||||||
|
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__() + \
|
||||||
|
"\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'
|
||||||
|
|
||||||
|
return omnimat_string
|
||||||
|
#end of code month branch
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print('The script starts its work...')
|
print('The script starts its work...')
|
||||||
|
|
||||||
for username in USERNAMES:
|
if MONTH_VIEW:
|
||||||
issues = get_issues(username)
|
for username in USERNAMES:
|
||||||
worklogs = get_worklogs(issues)
|
issues = get_issues(username)
|
||||||
today = datetime.date.today()
|
worklogs = get_worklogs(issues)
|
||||||
days, days_second_part = get_days(today)
|
days = get_days_of_month(CHECK_MONTH_BACK)
|
||||||
week_worklogs = get_week_tickets(worklogs, days)
|
worklogs = get_month_tickets(worklogs, days, CHECK_MONTH_BACK)
|
||||||
omnimat_string = ""
|
omnimat_string = get_omnimat_string(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'
|
|
||||||
|
|
||||||
if days_second_part:
|
|
||||||
second_omnimat_string = ""
|
|
||||||
worklogs_second_part = get_week_tickets(worklogs, days_second_part)
|
|
||||||
for worklog in week_worklogs:
|
for worklog in week_worklogs:
|
||||||
if worklog["ticket_id"] not in second_omnimat_string:
|
if worklog["ticket_id"] not in omnimat_string:
|
||||||
second_omnimat_string += worklog["ticket_id"] + '\n'
|
omnimat_string += worklog["ticket_id"] + '\n'
|
||||||
|
|
||||||
|
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'
|
||||||
|
|
||||||
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("Days:")
|
|
||||||
print(days)
|
|
||||||
print(f"Omnimat string:")
|
|
||||||
print(omnimat_string)
|
|
||||||
if days_second_part:
|
|
||||||
if VERBOSE:
|
if VERBOSE:
|
||||||
print(f"Worklogs for the weeks 2nd part:")
|
print('Issues of the user:')
|
||||||
print(json.dumps(worklogs_second_part, indent=4))
|
print(json.dumps(issues, indent=4))
|
||||||
|
print(f"Worklogs for the week:")
|
||||||
|
print(json.dumps(week_worklogs, indent=4))
|
||||||
print("Days:")
|
print("Days:")
|
||||||
print(days_second_part)
|
print(days)
|
||||||
print(f"Omnimat string:")
|
print(f"Omnimat string:")
|
||||||
print(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)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user