Simplify job to remove unnecessary loops

This commit is contained in:
Jake Howard 2019-02-17 16:50:57 +00:00
parent 78b7d545e2
commit 009ad478c9
Signed by: jake
GPG key ID: 57AFB45680EDD477
2 changed files with 28 additions and 28 deletions

View file

@ -7,7 +7,7 @@ from .todoist_assigned_issues import todoist_assigned_issues
def create_scheduler(): def create_scheduler():
scheduler = AsyncIOScheduler() scheduler = AsyncIOScheduler()
scheduler.add_job(todoist_assigned_issues) scheduler.add_job(todoist_assigned_issues, 'interval', minutes=15)
return scheduler return scheduler

View file

@ -1,4 +1,4 @@
import re import logging
from typing import Dict from typing import Dict
from github import Issue from github import Issue
@ -16,7 +16,7 @@ LABEL_TO_STATUS = {
'should have': 2 'should have': 2
} }
ISSUE_NUMBER_RE = re.compile(r"\[#(\d+?)\]") logger = logging.getLogger(__name__)
def get_status_for_issue(issue: Issue) -> int: def get_status_for_issue(issue: Issue) -> int:
@ -53,33 +53,33 @@ def todoist_assigned_issues():
for repo_name, project_id in REPOS.items(): 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} existing_tasks = {item['id']: item['content'] for item in todoist.state['items'] if item['project_id'] == project_id}
repo = github.get_repo(repo_name) repo = github.get_repo(repo_name)
for issue in repo.get_issues(assignee=me.login): for issue in repo.get_issues(assignee=me.login, state='all'):
me_assigned = me.login in {assignee.login for assignee in issue.assignees}
existing_task_id = get_existing_task(existing_tasks, issue) 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),
priority=get_status_for_issue(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'): if existing_task_id and not me_assigned:
existing_task_id = get_existing_task(existing_tasks, issue) logger.info("Deleting task for '{}'".format(issue.title))
if existing_task_id is not None:
todoist.items.complete([existing_task_id])
for existing_task_id, existing_task_content in existing_tasks.items():
if repo.html_url not in existing_task_content:
continue
issue_number = ISSUE_NUMBER_RE.match(existing_task_content).group(1)
issue = repo.get_issue(int(issue_number))
assignees = {assignee.login for assignee in issue.assignees}
if me.login not in assignees:
todoist.items.delete([existing_task_id]) todoist.items.delete([existing_task_id])
continue
elif issue.state == 'closed' and existing_task_id is not None:
logger.info("Completing task for '{}'".format(issue.title))
todoist.items.complete([existing_task_id])
continue
if issue.state == 'open':
if existing_task_id is None:
logger.info("Creating task for '{}'".format(issue.title))
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),
priority=get_status_for_issue(issue)
)
if issue.milestone and issue.milestone.due_on:
existing_task.update(date_string=issue.milestone.due_on.strftime("%d/%m/%Y"))
todoist.commit() todoist.commit()