From fec37dd2941ed9659c645eb9722041fa4c11d1c5 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 16 Feb 2019 16:53:23 +0000 Subject: [PATCH] Add assigned github issues to todoist --- actioner/scheduler/__init__.py | 7 ++- actioner/scheduler/todoist_assigned_issues.py | 58 +++++++++++++++++++ 2 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 actioner/scheduler/todoist_assigned_issues.py diff --git a/actioner/scheduler/__init__.py b/actioner/scheduler/__init__.py index 9ca99dd..15bff66 100644 --- a/actioner/scheduler/__init__.py +++ b/actioner/scheduler/__init__.py @@ -1,10 +1,13 @@ import asyncio -from apscheduler.schedulers.blocking import BlockingScheduler +from apscheduler.schedulers.asyncio import AsyncIOScheduler + +from .todoist_assigned_issues import todoist_assigned_issues def create_scheduler(): - scheduler = BlockingScheduler() + scheduler = AsyncIOScheduler() + scheduler.add_job(todoist_assigned_issues, 'interval', minutes=15) return scheduler diff --git a/actioner/scheduler/todoist_assigned_issues.py b/actioner/scheduler/todoist_assigned_issues.py new file mode 100644 index 0000000..37c9b0c --- /dev/null +++ b/actioner/scheduler/todoist_assigned_issues.py @@ -0,0 +1,58 @@ +from typing import Dict + +from github import Issue + +from actioner.clients import github, todoist + +REPOS = { + 'srobo/tasks': 2190856871, + 'srobo/core-team-minutes': 2190856871 +} + + +def get_issue_link(issue: Issue) -> str: + return "[#{id}]({url})".format( + id=issue.number, + url=issue.html_url + ) + + +def issue_to_task_name(issue: Issue) -> str: + return get_issue_link(issue) + ": " + issue.title + + +def get_existing_task(tasks: Dict[int, str], issue: Issue): + issue_link = get_issue_link(issue) + for task_id, task_title in tasks.items(): + if issue_link in task_title: + return task_id + return None + + +def todoist_assigned_issues(): + me = github.get_user() + todoist.projects.sync() + todoist.items.sync() + for repo_name, project_id in REPOS.items(): + existing_tasks = {item['id']: item['content'] for item in todoist.state['items'] if item['project_id'] == project_id} + repo = github.get_repo(repo_name) + for issue in repo.get_issues(assignee=me.login): + existing_task_id = get_existing_task(existing_tasks, issue) + if existing_task_id is None: + existing_task_id = todoist.items.add( + issue_to_task_name(issue), + project_id + )['id'] + existing_task = todoist.items.get_by_id(existing_task_id) + existing_task.update( + content=issue_to_task_name(issue) + ) + if issue.milestone and issue.milestone.due_on: + existing_task.update(date_string=issue.milestone.due_on.strftime("%d/%m/%Y")) + + for issue in repo.get_issues(assignee=me.login, state='closed'): + existing_task_id = get_existing_task(existing_tasks, issue) + if existing_task_id is not None: + todoist.items.complete([existing_task_id]) + + todoist.commit()