from logging import getLogger
from time import sleep
from clickup_to_jira.comment import Comment
from clickup_to_jira.utils import get_item_from_user_input
from pyclickup import ClickUp
logger = getLogger(__name__)
SLEEP_PER_REQUEST = 1.1 * 60 / 100 # Rate is 100req / sec. Add 10% margin
[docs]class ClickUpHandler(ClickUp):
"""
Class responsible for retrieving info from ClickUp
"""
[docs] def get_click_up_tickets(self):
"""
Get all ClickUp tickets.
:return: The list of tickets
:rtype: list(Ticket)
"""
logger.info("get clickup tickets")
tasks = self.get_tasks_from_click_up()
tasks_with_comments = self.add_comments_to_tasks(tasks)
tasks_with_parent = self.add_parent_to_tasks(tasks_with_comments)
return self.get_sorted_tasks(tasks_with_parent)
[docs] def get_tasks_from_click_up(self):
"""
Get ClickUp tasks.
:return: The list of ClickUp tasks
:rtype: ClickUp(Task)
"""
cur_team = get_item_from_user_input("team", self.teams)
cur_space = get_item_from_user_input("space", cur_team.spaces)
cur_project = get_item_from_user_input("project", cur_space.projects)
lst = get_item_from_user_input(
"list", cur_project.lists, allow_none=True
)
if not lst:
return cur_project.get_all_tasks(
include_closed=True, subtasks=True
)
else:
return lst.get_all_tasks(include_closed=True, subtasks=True)
[docs] @staticmethod
def add_parent_to_tasks(tasks):
"""
Add parent to tasks.
:param list(Task) tasks: The tasks on which parent is added
:return: The updated tasks
:rtype: list(Task)
"""
for task in tasks:
task.parent = None
for task in tasks:
logger.info(f"Retrieving father for task {task.name}")
try:
task.parent = list(
filter(lambda x: x.id == task.parent, tasks)
)[0].name
except IndexError:
logger.info("Issue has no parent")
task.parent = None
return tasks
[docs] @staticmethod
def get_sorted_tasks(tasks):
"""
Get ordered tasks with tasks before subtasks.
:param list(Task) tasks: The tasks list
:return: The ordered tasks list
:rtype: list(Task)
"""
sorted_tasks = list()
for task in tasks:
if task.parent:
sorted_tasks.append(task)
else:
sorted_tasks.insert(0, task)
return sorted_tasks