Simplify job to remove unnecessary loops
This commit is contained in:
parent
78b7d545e2
commit
009ad478c9
2 changed files with 28 additions and 28 deletions
|
@ -7,7 +7,7 @@ from .todoist_assigned_issues import todoist_assigned_issues
|
|||
|
||||
def create_scheduler():
|
||||
scheduler = AsyncIOScheduler()
|
||||
scheduler.add_job(todoist_assigned_issues)
|
||||
scheduler.add_job(todoist_assigned_issues, 'interval', minutes=15)
|
||||
return scheduler
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import re
|
||||
import logging
|
||||
from typing import Dict
|
||||
|
||||
from github import Issue
|
||||
|
@ -16,7 +16,7 @@ LABEL_TO_STATUS = {
|
|||
'should have': 2
|
||||
}
|
||||
|
||||
ISSUE_NUMBER_RE = re.compile(r"\[#(\d+?)\]")
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_status_for_issue(issue: Issue) -> int:
|
||||
|
@ -53,9 +53,23 @@ def todoist_assigned_issues():
|
|||
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):
|
||||
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)
|
||||
|
||||
if existing_task_id and not me_assigned:
|
||||
logger.info("Deleting task for '{}'".format(issue.title))
|
||||
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
|
||||
|
@ -68,18 +82,4 @@ def todoist_assigned_issues():
|
|||
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])
|
||||
|
||||
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.commit()
|
||||
|
|
Reference in a new issue