136 lines
4.3 KiB
Python
Executable File
136 lines
4.3 KiB
Python
Executable File
import requests
|
|
import json
|
|
import datetime
|
|
from dotenv import load_dotenv
|
|
from os import getenv
|
|
|
|
load_dotenv()
|
|
ACCESS_TOKEN = getenv("TOKEN")
|
|
USERNAMES = ["kalinom6"] # Modify
|
|
URL = "https://globaljira.roche.com/rest/api/2"
|
|
CERT = getenv("CERT_LOCATION")
|
|
WEEKS_BACK = 1
|
|
VERBOSE = True
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {ACCESS_TOKEN}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
def send_request(url, method, payload=None):
|
|
if method == "GET":
|
|
response = requests.get(url, headers=headers, data=json.dumps(payload), verify=CERT)
|
|
if method == "POST":
|
|
response = requests.post(url, headers=headers, data=json.dumps(payload), verify=CERT)
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
print(f"Error: {response.status_code}, {response.text}")
|
|
|
|
def get_issues(username):
|
|
jql = f"assignee WAS '{username}'"
|
|
payload = {
|
|
"jql": jql,
|
|
"startAt": 0,
|
|
"maxResults": 50,
|
|
"fields": [
|
|
"key", "summary"
|
|
]
|
|
}
|
|
data = send_request(f'{URL}/search', "POST", payload)
|
|
if data:
|
|
tasks = []
|
|
for issue in data.get("issues", []):
|
|
task = {
|
|
"task_id": issue["key"],
|
|
"summary": issue["fields"]["summary"],
|
|
"task_link": f"https://globaljira.roche.com/browse/{issue['key']}"
|
|
}
|
|
tasks.append(task)
|
|
return tasks
|
|
|
|
def get_worklogs(issues):
|
|
worklogs = []
|
|
for issue in issues:
|
|
data = send_request(f'{URL}/issue/{issue["task_id"]}/worklog', "GET")
|
|
if data:
|
|
for log in data.get("worklogs", []):
|
|
worklog = {
|
|
"ticket_id": issue["task_id"],
|
|
"jira_link": issue["task_link"],
|
|
"time_spent": log["timeSpent"],
|
|
"date": log["started"],
|
|
"author": log["author"]["name"]
|
|
}
|
|
if worklog["author"] in USERNAMES:
|
|
worklogs.append(worklog)
|
|
return worklogs
|
|
|
|
def get_days(day, weeks_back=WEEKS_BACK):
|
|
if WEEKS_BACK:
|
|
day = day - datetime.timedelta(weeks=weeks_back)
|
|
year, weeknum, day_of_week = day.isocalendar()
|
|
days = []
|
|
days.append(str(day))
|
|
while day_of_week > 1:
|
|
day = day - datetime.timedelta(days=1)
|
|
day_of_week -= 1
|
|
days.append(str(day))
|
|
return days
|
|
|
|
# NOT FUNCTIONAL
|
|
def get_weeks(day, worklogs, weeks=list()):
|
|
year, weeknum, day_of_week = day.isocalendar()
|
|
days = get_days(day, weeks_back=0)
|
|
tickets = get_week_tickets(worklogs, days)
|
|
weeks.append(tickets)
|
|
if weeknum != 0:
|
|
days = get_days(day, weeks_back=1)
|
|
get_weeks(days[0], worklogs, weeks)
|
|
return weeks
|
|
|
|
# TODO: implement this
|
|
def get_days_range(date_from, date_to):
|
|
pass
|
|
|
|
def get_week_tickets(worklogs, days):
|
|
this_week_tickets = []
|
|
for worklog in worklogs:
|
|
worklog_date = datetime.datetime.strptime(worklog["date"].split('T')[0], '%Y-%m-%d').date()
|
|
current_month = get_max_month(days)
|
|
worklog_month = str(worklog_date).split('-')[1]
|
|
if str(worklog_date) in days and int(worklog_month) == current_month:
|
|
this_week_tickets.append(worklog)
|
|
return this_week_tickets
|
|
|
|
def get_max_month(days):
|
|
max = 0
|
|
for day in days:
|
|
month = int(day.split('-')[1])
|
|
if month > max:
|
|
max = month
|
|
return max
|
|
|
|
def main():
|
|
print('The script starts its work...')
|
|
|
|
for username in USERNAMES:
|
|
issues = get_issues(username)
|
|
worklogs = get_worklogs(issues)
|
|
today = datetime.date.today()
|
|
days = get_days(today)
|
|
worklogs = get_week_tickets(worklogs, days)
|
|
omnimat_string = ""
|
|
|
|
for worklog in worklogs:
|
|
if worklog["ticket_id"] not in omnimat_string:
|
|
omnimat_string += worklog["ticket_id"] + '\n'
|
|
|
|
if VERBOSE:
|
|
print('Issues of the user:')
|
|
print(json.dumps(issues, indent=4))
|
|
print(f"Worklogs for the current week:")
|
|
print(json.dumps(worklogs, indent=4))
|
|
print(f"Omnimat string:")
|
|
print(omnimat_string)
|
|
if __name__ == '__main__':
|
|
main() |