0
This commit is contained in:
47
api.py
47
api.py
@@ -1,6 +1,7 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
from datetime import datetime
|
||||
import logging
|
||||
|
||||
from colorama import Fore
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
|
||||
@@ -34,20 +35,23 @@ class ClockifyAPI(ClockifyConfig):
|
||||
result = []
|
||||
for activity in tqdm(activities, desc="Парсинг задач"):
|
||||
for tag_id in activity["tagIds"]:
|
||||
start = datetime.strptime(activity["timeInterval"]["start"], config.DATETIME_FORMAT) + timedelta(hours=3)
|
||||
end = datetime.strptime(activity["timeInterval"]["end"] or str(datetime.now(timezone.utc).strftime(config.DATETIME_FORMAT)), config.DATETIME_FORMAT) + timedelta(hours=3)
|
||||
time_worked_out_total += time_to_hour(end - start)
|
||||
start = datetime.strptime(activity["timeInterval"]["start"], config.DATETIME_FORMAT)
|
||||
end = datetime.strptime(activity["timeInterval"]["end"], config.DATETIME_FORMAT) if activity["timeInterval"]["end"] else None
|
||||
time_worked_out_total += time_to_hour((end if end else datetime.now()) - start)
|
||||
tag = [tag for tag in self.get_tags(workspace_id=WORKSPACE_ID) if tag.id == tag_id][0]
|
||||
task_id, task_title = tag.title.split(" - ")
|
||||
result.append(
|
||||
Activity(
|
||||
id=activity["id"],
|
||||
tag_id=tag_id,
|
||||
task_id=task_id,
|
||||
task_title=task_title,
|
||||
project_id=activity["projectId"],
|
||||
description=activity["description"],
|
||||
time_spent=time_to_hour(end - start),
|
||||
time_spent=time_to_hour((end if end else datetime.now()) - start),
|
||||
date_start=start,
|
||||
date_end=end
|
||||
date_end=end,
|
||||
is_tracked=activity["billable"]
|
||||
)
|
||||
)
|
||||
logging.info(f"Сохранено задач: {len(result)}")
|
||||
@@ -70,16 +74,27 @@ class ClockifyAPI(ClockifyConfig):
|
||||
def mark_tracked(self, activities: list[Activity]):
|
||||
for activity in activities:
|
||||
try:
|
||||
resp = requests.put(self.base_url + self.update_time_entry.format(
|
||||
resp = requests.put(
|
||||
self.base_url + self.update_time_entry.format(
|
||||
workspace_id=WORKSPACE_ID,
|
||||
id=activity.id
|
||||
), data={
|
||||
"description": "*" + activity.description
|
||||
})
|
||||
), json={
|
||||
"billable": True,
|
||||
"start": activity.date_start.strftime(config.DATETIME_FORMAT),
|
||||
"end": activity.date_end.strftime(config.DATETIME_FORMAT) if activity.date_end else None,
|
||||
"description": activity.description,
|
||||
"tagIds": [activity.tag_id],
|
||||
"projectId": activity.project_id
|
||||
}, headers={
|
||||
"X-API-KEY": self.token
|
||||
}
|
||||
)
|
||||
if resp.ok:
|
||||
logging.debug(activity, "помечена как затреканная")
|
||||
logging.debug(resp.json())
|
||||
logging.info(f"{activity} {Fore.GREEN} помечена как затреканная{Fore.RESET}")
|
||||
else:
|
||||
logging.error(activity, "не помечена как затреканная")
|
||||
logging.debug(resp.json())
|
||||
logging.error(f"{activity} {Fore.RED} не помечена как затреканная{Fore.RESET}")
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
return
|
||||
@@ -111,13 +126,13 @@ class RedmineAPI(RedmineConfig):
|
||||
resp = requests.post(
|
||||
self.base_url + self.time_entries_url,
|
||||
headers={"X-Redmine-Api-Key": self.token},
|
||||
data={
|
||||
json={
|
||||
"time_entry": {
|
||||
"issue_id": activity.task_id,
|
||||
"hours": activity.time_spent,
|
||||
"activity_id": ActivityType.DEVELOPMENT,
|
||||
"comments": activity.description,
|
||||
"spent_on": activity.date.strftime(self.datetime_format)
|
||||
"spent_on": activity.date_start.strftime(self.datetime_format)
|
||||
}
|
||||
}
|
||||
)
|
||||
@@ -126,8 +141,8 @@ class RedmineAPI(RedmineConfig):
|
||||
tracked.append(activity)
|
||||
logging.debug(activity)
|
||||
else:
|
||||
logging.error(activity, "Не была затрекана")
|
||||
logging.error(resp.json(), resp)
|
||||
logging.error(f"{activity} Не была затрекана")
|
||||
logging.error(f"{resp.json()['errors']} {resp.status_code}")
|
||||
except Exception as e:
|
||||
logging.error(e)
|
||||
continue
|
||||
|
||||
11
classes.py
11
classes.py
@@ -7,17 +7,16 @@ class Activity:
|
||||
id: str
|
||||
task_id: int
|
||||
task_title: str
|
||||
tag_id: str
|
||||
description: str
|
||||
project_id: str
|
||||
time_spent: float
|
||||
date_start: datetime
|
||||
date_end: datetime
|
||||
|
||||
@property
|
||||
def is_tracked(self):
|
||||
return self.description.startswith("*")
|
||||
date_end: datetime = None
|
||||
is_tracked: bool = False
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.date_start}: {self.description.strip("*")} для задачи "{self.task_title}"'
|
||||
return f'{self.date_start}: {self.description} для задачи "{self.task_title}"'
|
||||
|
||||
|
||||
@dataclass
|
||||
|
||||
@@ -52,7 +52,7 @@ class ClockifyConfig:
|
||||
|
||||
@property
|
||||
def update_time_entry(self):
|
||||
return "/workspaces/{workspaceId}/time-entries/{id}"
|
||||
return "/workspaces/{workspace_id}/time-entries/{id}"
|
||||
|
||||
|
||||
config = Config()
|
||||
|
||||
10
enums.py
10
enums.py
@@ -1,9 +1,15 @@
|
||||
from enum import Enum
|
||||
from enum import IntEnum, StrEnum
|
||||
|
||||
|
||||
class ActivityType(Enum):
|
||||
class ActivityType(IntEnum):
|
||||
DEVELOPMENT = 9
|
||||
BUSINESS_ANALYZE = 8
|
||||
CODE_REVIEW = 57
|
||||
CONSULTATIONS = 58
|
||||
SUPPORT = 60
|
||||
|
||||
|
||||
class ClockifyProjects(StrEnum):
|
||||
VETRO3 = "67e266148411754017d0cee5"
|
||||
VETRO_RETAIL = "67e29b8e837a92506f5e86df"
|
||||
SPHERA = "67e5476b1b872e13361e007c"
|
||||
|
||||
5
main.py
5
main.py
@@ -9,10 +9,11 @@ from utils import today_start, today_end
|
||||
def main():
|
||||
clock = ClockifyAPI(config.CLOCKIFY_TOKEN)
|
||||
red = RedmineAPI(config.REDMINE_TOKEN)
|
||||
tracked_activities = red.track_activities(clock.get_activities(
|
||||
activities = clock.get_activities(
|
||||
start=today_start(),
|
||||
end=today_end()
|
||||
))
|
||||
)
|
||||
tracked_activities = red.track_activities(activities)
|
||||
clock.mark_tracked(tracked_activities)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user