add lots of things

This commit is contained in:
Andreas Zweili 2021-11-06 20:07:57 +01:00
parent dce2ed8282
commit 3e712c8132
4 changed files with 57 additions and 0 deletions

View File

@ -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)

View File

@ -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

View File

@ -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"],
}))

View File

@ -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