From a88c14b7233e08ab26d7cbd48fc1e823e9d9588e Mon Sep 17 00:00:00 2001 From: Jakub Trllo Date: Thu, 7 Mar 2019 15:53:33 +0100 Subject: [PATCH] added action to launcher that syncs to clockify --- pype/plugins/launcher/actions/ClockifySync.py | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 pype/plugins/launcher/actions/ClockifySync.py diff --git a/pype/plugins/launcher/actions/ClockifySync.py b/pype/plugins/launcher/actions/ClockifySync.py new file mode 100644 index 0000000000..31ae1f3424 --- /dev/null +++ b/pype/plugins/launcher/actions/ClockifySync.py @@ -0,0 +1,61 @@ +from avalon import api, io +from pype.clockify import ClockifyAPI +from pype.api import Logger +log = Logger.getLogger(__name__, "clockify_sync") + + +class ClockifySync(api.Action): + + name = "sync_to_clockify" + label = "Sync to Clockify" + icon = "clockify_icon" + order = 500 + clockapi = ClockifyAPI() + have_permissions = clockapi.validate_workspace_perm() + + def is_compatible(self, session): + """Return whether the action is compatible with the session""" + return self.have_permissions + + def process(self, session, **kwargs): + project_name = session.get('AVALON_PROJECT', None) + + projects_to_sync = [] + if project_name.strip() == '' or project_name is None: + for project in io.projects(): + projects_to_sync.append(project) + else: + project = io.find_one({'type': 'project'}) + projects_to_sync.append(project) + + projects_info = {} + for project in projects_to_sync: + task_types = [task['name'] for task in project['config']['tasks']] + projects_info[project['name']] = task_types + + clockify_projects = self.clockapi.get_projects() + for project_name, task_types in projects_info.items(): + if project_name not in clockify_projects: + response = self.clockapi.add_project(project_name) + if 'id' not in response: + self.log.error('Project {} can\'t be created'.format( + project_name + )) + continue + project_id = response['id'] + else: + project_id = clockify_projects[project_name] + + clockify_project_tasks = self.clockapi.get_tasks( + project_id=project_id + ) + for task_type in task_types: + if task_type not in clockify_project_tasks: + response = self.clockapi.add_task( + task_type, project_id=project_id + ) + if 'id' not in response: + self.log.error('Task {} can\'t be created'.format( + task_type + )) + continue