Move files around so the entrypoint is cleaner
This commit is contained in:
parent
c69cf3416e
commit
b56d7cdb10
5 changed files with 80 additions and 66 deletions
|
@ -1,74 +1,11 @@
|
|||
#!/usr/bin/env python3
|
||||
import datetime
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from .clients import github, todoist
|
||||
from .utils import get_github_issue_details, get_github_task, get_issue
|
||||
|
||||
|
||||
def get_issue_link(issue_or_pr) -> str:
|
||||
return "[#{id}]({url})".format(id=issue_or_pr.number, url=issue_or_pr.html_url)
|
||||
|
||||
|
||||
def issue_to_task_name(issue) -> str:
|
||||
return get_issue_link(issue) + ": " + issue.title
|
||||
|
||||
|
||||
def get_relevant_todoist_tasks():
|
||||
todoist.items.sync()
|
||||
tasks = {}
|
||||
for task in todoist.items.all():
|
||||
github_task = get_github_task(task["content"])
|
||||
if github_task:
|
||||
tasks[github_task] = task
|
||||
return tasks
|
||||
|
||||
|
||||
def is_task_completed(task):
|
||||
return task.data.get("checked", 0)
|
||||
from .tasks import ALL_TASKS
|
||||
|
||||
|
||||
def main():
|
||||
todoist_tasks = get_relevant_todoist_tasks()
|
||||
relevant_since = datetime.datetime.now() - relativedelta(
|
||||
weeks=30
|
||||
) # TODO: Make this a sane number
|
||||
tasks_actioned = []
|
||||
me = github.get_user()
|
||||
for assigned_issue in me.get_issues(state="all", since=relevant_since):
|
||||
task = todoist_tasks.get(assigned_issue.html_url)
|
||||
if not task and assigned_issue.state == "open":
|
||||
task = todoist.items.add(issue_to_task_name(assigned_issue))
|
||||
if not task:
|
||||
continue
|
||||
tasks_actioned.append(task["id"])
|
||||
if assigned_issue == "closed" and not is_task_completed(task):
|
||||
print("completing", assigned_issue)
|
||||
task.complete()
|
||||
if is_task_completed(task):
|
||||
print("uncompleting task", assigned_issue)
|
||||
task.uncomplete()
|
||||
if task["content"] != issue_to_task_name(assigned_issue):
|
||||
print("updating issue name for", assigned_issue)
|
||||
task.update(content=issue_to_task_name(assigned_issue))
|
||||
if assigned_issue.milestone and assigned_issue.milestone.due_on:
|
||||
task.update(
|
||||
date_string=assigned_issue.milestone.due_on.strftime("%d/%m/%Y")
|
||||
)
|
||||
|
||||
for task in todoist_tasks.values():
|
||||
if not is_task_completed(task) or task["id"] in tasks_actioned:
|
||||
continue
|
||||
issue_details = get_github_issue_details(task["content"])
|
||||
if not issue_details:
|
||||
continue
|
||||
org, repo, issue_number = issue_details
|
||||
issue = get_issue(me, org, repo, issue_number)
|
||||
me_assigned = me.login in {assignee.login for assignee in issue.assignees}
|
||||
if not me_assigned:
|
||||
print("Deleting", issue)
|
||||
task.delete()
|
||||
for task in ALL_TASKS:
|
||||
print("Executing", task.__name__)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
|
5
todoist_github/tasks/__init__.py
Normal file
5
todoist_github/tasks/__init__.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
from .assigned_issues import assigned_issues
|
||||
|
||||
ALL_TASKS = [assigned_issues]
|
||||
|
||||
__all__ = ["ALL_TASKS"]
|
59
todoist_github/tasks/assigned_issues.py
Normal file
59
todoist_github/tasks/assigned_issues.py
Normal file
|
@ -0,0 +1,59 @@
|
|||
import datetime
|
||||
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
from todoist_github.clients import github, todoist
|
||||
from todoist_github.utils import get_github_issue_details, get_github_task, get_issue
|
||||
from todoist_github.utils.todoist import is_task_completed, issue_to_task_name
|
||||
|
||||
|
||||
def get_relevant_todoist_tasks():
|
||||
todoist.items.sync()
|
||||
tasks = {}
|
||||
for task in todoist.items.all():
|
||||
github_task = get_github_task(task["content"])
|
||||
if github_task:
|
||||
tasks[github_task] = task
|
||||
return tasks
|
||||
|
||||
|
||||
def assigned_issues():
|
||||
todoist_tasks = get_relevant_todoist_tasks()
|
||||
relevant_since = datetime.datetime.now() - relativedelta(
|
||||
weeks=30
|
||||
) # TODO: Make this a sane number
|
||||
tasks_actioned = []
|
||||
me = github.get_user()
|
||||
for assigned_issue in me.get_issues(state="all", since=relevant_since):
|
||||
task = todoist_tasks.get(assigned_issue.html_url)
|
||||
if not task and assigned_issue.state == "open":
|
||||
task = todoist.items.add(issue_to_task_name(assigned_issue))
|
||||
if not task:
|
||||
continue
|
||||
tasks_actioned.append(task["id"])
|
||||
if assigned_issue == "closed" and not is_task_completed(task):
|
||||
print("completing", assigned_issue)
|
||||
task.complete()
|
||||
if is_task_completed(task):
|
||||
print("uncompleting task", assigned_issue)
|
||||
task.uncomplete()
|
||||
if task["content"] != issue_to_task_name(assigned_issue):
|
||||
print("updating issue name for", assigned_issue)
|
||||
task.update(content=issue_to_task_name(assigned_issue))
|
||||
if assigned_issue.milestone and assigned_issue.milestone.due_on:
|
||||
task.update(
|
||||
date_string=assigned_issue.milestone.due_on.strftime("%d/%m/%Y")
|
||||
)
|
||||
|
||||
for task in todoist_tasks.values():
|
||||
if not is_task_completed(task) or task["id"] in tasks_actioned:
|
||||
continue
|
||||
issue_details = get_github_issue_details(task["content"])
|
||||
if not issue_details:
|
||||
continue
|
||||
org, repo, issue_number = issue_details
|
||||
issue = get_issue(me, org, repo, issue_number)
|
||||
me_assigned = me.login in {assignee.login for assignee in issue.assignees}
|
||||
if not me_assigned:
|
||||
print("Deleting", issue)
|
||||
task.delete()
|
13
todoist_github/utils/todoist.py
Normal file
13
todoist_github/utils/todoist.py
Normal file
|
@ -0,0 +1,13 @@
|
|||
from github.Issue import Issue
|
||||
|
||||
|
||||
def get_issue_link(issue_or_pr) -> str:
|
||||
return "[#{id}]({url})".format(id=issue_or_pr.number, url=issue_or_pr.html_url)
|
||||
|
||||
|
||||
def issue_to_task_name(issue: Issue) -> str:
|
||||
return get_issue_link(issue) + ": " + issue.title
|
||||
|
||||
|
||||
def is_task_completed(task):
|
||||
return task.data.get("checked", 0)
|
Reference in a new issue