This commit is contained in:
2025-05-19 14:23:07 +03:00
parent 41d49072f8
commit fe0ea5faac
12 changed files with 229 additions and 164 deletions

65
api/redmine.py Normal file
View File

@@ -0,0 +1,65 @@
import logging
import requests
from tqdm import tqdm
from config import RedmineConfig, config
from classes import Activity
from enums import ActivityType
from exceptions import InvalidToken
WORKSPACE_ID = config.CLOCKIFY_WORKSPACE_ID
USER_ID = config.CLOCKIFY_USER_ID
class RedmineAPI(RedmineConfig):
def __init__(self, token):
self.token = self._check_token(token)
def track_activities(self, activities: list[Activity]) -> list[Activity]:
if len(activities) == 0:
logging.info("Нет задач для занесения трудочасов")
return
tracked = []
for activity in tqdm(activities, desc="Занесение трудочасов"):
if not activity.is_tracked:
try:
resp = requests.post(
self.base_url + self.time_entries_url,
headers={"X-Redmine-Api-Key": self.token},
json={
"time_entry": {
"issue_id": activity.task_id,
"hours": activity.time_spent,
"activity_id": ActivityType.DEVELOPMENT,
"comments": activity.description,
"spent_on": activity.date_start.strftime(self.datetime_format)
}
}
)
if resp.ok:
activity.is_tracked = True
tracked.append(activity)
logging.debug(activity)
else:
logging.error(f"{activity} Не была затрекана")
logging.error(f"{resp.json()['errors']} {resp.status_code}")
except Exception as e:
logging.error(e)
continue
if len(activities) == len(tracked):
logging.info("Все задачи были успешно занесены в Redmine")
else:
logging.info(f"Не все задачи были занесены в Redmine. Занесено {len(tracked)}")
return tracked
def _check_token(self, token):
resp = requests.get(self.base_url + self.my_account_url, headers={"X-Redmine-Api-Key": token})
if not resp.ok:
logging.error("Invalid Token for RedmineAPI")
raise InvalidToken
resp = resp.json()['user']
logging.debug({"login": resp["login"], "email": resp["mail"], "full_name": f"{resp["firstname"]} {resp["lastname"]}"})
return token