From 3e712c8132a986c813e7a6c3a80c3709facca12a Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Sat, 6 Nov 2021 20:07:57 +0100 Subject: [PATCH] add lots of things --- todoist_interface/__main__.py | 11 +++++++++++ todoist_interface/gitlab.py | 20 ++++++++++++++++++++ todoist_interface/todoist.py | 12 ++++++++++++ todoist_interface/utils.py | 14 ++++++++++++++ 4 files changed, 57 insertions(+) create mode 100644 todoist_interface/utils.py diff --git a/todoist_interface/__main__.py b/todoist_interface/__main__.py index f086f99..3ceda58 100644 --- a/todoist_interface/__main__.py +++ b/todoist_interface/__main__.py @@ -1,13 +1,24 @@ import settings from todoist import TodoistAPI from gitlab import GitlabAPI +import utils if __name__ == '__main__': # initialise the settings config = settings.read_config("todoist_interface.yml") + # initialise Todoist and Gitlab todoist = TodoistAPI(config['todoist']['token']) gitlab = GitlabAPI(config["gitlab"]["url"], config["gitlab"]["token"]) + + # Get the Todoist tasks tasks = todoist.get_get_tasks_by_filter("@gitlab") + + # Get the Gitlab issues + issues = gitlab.get_issues_by_assignee("zweili") + + # Create a list of issues that are not in Todoist + missing_tasks = utils.get_missing_tasks(tasks, issues) + todoist.create_tasks(missing_tasks) print(tasks) diff --git a/todoist_interface/gitlab.py b/todoist_interface/gitlab.py index ef55959..6c73ddb 100644 --- a/todoist_interface/gitlab.py +++ b/todoist_interface/gitlab.py @@ -1,4 +1,24 @@ +import requests + + class GitlabAPI: def __init__(self, url: str, token: str) -> None: self.url = url self.token = token + + def get_issues_by_assignee(self, assignee: str) -> list: + """ + Get all issues assigned to a specific user + :param assignee: The username of the assignee + :return: A list of issues assigned to the user + """ + + # Get all issues assigned to the user + url = (self.url + + '/api/v4/issues?assignee_username=' + + assignee + + '&state=opened') + response = requests.get(url, headers={'PRIVATE-TOKEN': self.token}) + issues = response.json() + + return issues diff --git a/todoist_interface/todoist.py b/todoist_interface/todoist.py index 3a4cc68..2e97fcd 100644 --- a/todoist_interface/todoist.py +++ b/todoist_interface/todoist.py @@ -1,4 +1,5 @@ import requests +import json class TodoistAPI: @@ -16,3 +17,14 @@ class TodoistAPI: headers={'Authorization': 'Bearer ' + self.token}, params={"filter": filter}) return response.json() + + def add_tasks(self, tasks: list): + """ + Adds tasks to todoist + """ + for task in tasks: + requests.post(self.url + '/tasks', + headers={'Authorization': 'Bearer ' + self.token}, + data=json.dumps({ + "content": task["content"], + })) diff --git a/todoist_interface/utils.py b/todoist_interface/utils.py new file mode 100644 index 0000000..cb85a25 --- /dev/null +++ b/todoist_interface/utils.py @@ -0,0 +1,14 @@ +def get_missing_tasks(tasks, issues_to_check): + """ + Check if tasks exists in tasks_to_check + :param tasks: list of tasks + :param issues_to_check: list of issues to check + :return: A list of tasks not in Todoist + """ + missing_tasks = [] + for issue in issues_to_check: + if issue["title"] not in [t["content"] for t in tasks]: + missing_tasks.append({"content": issue["title"], + "url": issue["web_url"]}) + + return missing_tasks