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