From cd04686ae572caa38fca366cadadf8be9aa62d99 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Fri, 30 Aug 2019 20:25:34 +0100 Subject: [PATCH] Get all relevant PRs --- actioner/scheduler/todoist_assigned_issues.py | 10 +- actioner/scheduler/todoist_repo_prs.py | 95 ++++++++----------- actioner/utils/github.py | 11 +++ actioner/utils/todoist.py | 8 ++ 4 files changed, 62 insertions(+), 62 deletions(-) diff --git a/actioner/scheduler/todoist_assigned_issues.py b/actioner/scheduler/todoist_assigned_issues.py index 4200c1f..3c6888f 100644 --- a/actioner/scheduler/todoist_assigned_issues.py +++ b/actioner/scheduler/todoist_assigned_issues.py @@ -5,7 +5,7 @@ from github import Issue from actioner.clients import get_todoist_client, github from actioner.utils import get_todoist_project_from_repo from actioner.utils.github import get_existing_task, get_issue_link, get_relevant_issues -from actioner.utils.todoist import is_task_completed +from actioner.utils.todoist import get_existing_tasks, is_task_completed LABEL_TO_STATUS = {"must have": 4, "critical": 4, "should have": 2} @@ -21,14 +21,6 @@ def issue_to_task_name(issue: Issue) -> str: return get_issue_link(issue) + ": " + issue.title -def get_existing_tasks(project_id, todoist): - return { - item["id"]: item["content"] - for item in todoist.state["items"] - if item["project_id"] == project_id - } - - def todoist_assigned_issues(): todoist = get_todoist_client() me = github.get_user() diff --git a/actioner/scheduler/todoist_repo_prs.py b/actioner/scheduler/todoist_repo_prs.py index 2e4cdef..b247ea9 100644 --- a/actioner/scheduler/todoist_repo_prs.py +++ b/actioner/scheduler/todoist_repo_prs.py @@ -4,15 +4,12 @@ from github import PullRequest from actioner.clients import get_todoist_client, github from actioner.utils import get_todoist_project_from_repo -from actioner.utils.github import get_existing_task, get_issue_link -from actioner.utils.todoist import is_task_completed +from actioner.utils.github import get_existing_task, get_issue_link, get_relevant_prs +from actioner.utils.todoist import get_existing_tasks, is_task_completed logger = logging.getLogger(__name__) -REPOS = ["srobo/competition-team-minutes"] - - def pr_to_task_name(pr: PullRequest) -> str: return "Review " + get_issue_link(pr) + ": " + pr.title @@ -28,60 +25,52 @@ def todoist_repo_prs(): me = github.get_user() todoist.projects.sync() todoist.items.sync() - for repo_name in REPOS: - project_id = get_todoist_project_from_repo(repo_name) + for pr in get_relevant_prs(): + project_id = get_todoist_project_from_repo(pr.base.repo.full_name) if not project_id: continue - 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 pr in repo.get_pulls(state="all"): - existing_task_id = get_existing_task(existing_tasks, pr) + existing_tasks = get_existing_tasks(project_id, todoist) + existing_task_id = get_existing_task(existing_tasks, pr) - if pr.state == "closed" and existing_task_id: - my_review = get_my_review(me, pr) - if pr.merged and my_review and my_review.state == "APPROVED": - if not is_task_completed(todoist.items.get_by_id(existing_task_id)): - logger.info("Completing task to review '{}'".format(pr.title)) - todoist.items.complete([existing_task_id]) - else: - logger.info("Deleting task to review '{}'".format(pr.title)) - todoist.items.delete([existing_task_id]) + if pr.state == "closed" and existing_task_id: + my_review = get_my_review(me, pr) + if pr.merged and my_review and my_review.state == "APPROVED": + if not is_task_completed(todoist.items.get_by_id(existing_task_id)): + logger.info("Completing task to review '{}'".format(pr.title)) + todoist.items.complete([existing_task_id]) + else: + logger.info("Deleting task to review '{}'".format(pr.title)) + todoist.items.delete([existing_task_id]) - elif pr.state == "open": - my_review = get_my_review(me, pr) + elif pr.state == "open": + my_review = get_my_review(me, pr) - if existing_task_id: - existing_task = todoist.items.get_by_id(existing_task_id) - task_completed = is_task_completed(existing_task) - if my_review: - if my_review.commit_id == pr.head.sha and not task_completed: - logger.info( - "Completing task to review '{}', because I already did it".format( - pr.title - ) + if existing_task_id: + existing_task = todoist.items.get_by_id(existing_task_id) + task_completed = is_task_completed(existing_task) + if my_review: + if my_review.commit_id == pr.head.sha and not task_completed: + logger.info( + "Completing task to review '{}', because I already did it".format( + pr.title ) - todoist.items.complete([existing_task_id]) - elif task_completed: - logger.info( - "Re-opening task to review '{}'".format(pr.title) - ) - todoist.items.uncomplete([existing_task_id]) - continue - elif my_review and my_review.commit_id != pr.head.sha: - logger.info("Creating task to review '{}'".format(pr.title)) - existing_task_id = todoist.items.add( - pr_to_task_name(pr), project_id - )["id"] - if existing_task_id is not None: - existing_task = todoist.items.get_by_id(existing_task_id) - existing_task.update(content=pr_to_task_name(pr)) - if pr.milestone and pr.milestone.due_on: - existing_task.update( - date_string=pr.milestone.due_on.strftime("%d/%m/%Y") ) + todoist.items.complete([existing_task_id]) + elif task_completed: + logger.info("Re-opening task to review '{}'".format(pr.title)) + todoist.items.uncomplete([existing_task_id]) + continue + elif my_review and my_review.commit_id != pr.head.sha: + logger.info("Creating task to review '{}'".format(pr.title)) + existing_task_id = todoist.items.add( + pr_to_task_name(pr), project_id=project_id + )["id"] + if existing_task_id is not None: + existing_task = todoist.items.get_by_id(existing_task_id) + existing_task.update(content=pr_to_task_name(pr)) + if pr.milestone and pr.milestone.due_on: + existing_task.update( + date_string=pr.milestone.due_on.strftime("%d/%m/%Y") + ) todoist.commit() diff --git a/actioner/utils/github.py b/actioner/utils/github.py index 36b7f21..b3b58dd 100644 --- a/actioner/utils/github.py +++ b/actioner/utils/github.py @@ -26,3 +26,14 @@ def get_relevant_issues(): for issue in repo.get_issues(since=since, state="all"): if issue.pull_request is None: yield issue + + +def get_relevant_prs(): + since = datetime.datetime.now() - relativedelta(weeks=1) + for repo in github.get_user().get_repos(): + if repo.updated_at < since: + continue + for pull in repo.get_pulls(state="all", sort="updated", direction="desc"): + if pull.updated_at < since: + break + yield pull diff --git a/actioner/utils/todoist.py b/actioner/utils/todoist.py index c4abeee..3107249 100644 --- a/actioner/utils/todoist.py +++ b/actioner/utils/todoist.py @@ -9,3 +9,11 @@ def is_task_completed(task: Item): return task["checked"] except KeyError: return False + + +def get_existing_tasks(project_id, todoist): + return { + item["id"]: item["content"] + for item in todoist.state["items"] + if item["project_id"] == project_id + }