From 81b3498f35fdec57a247bbb414c9522f3d32bb31 Mon Sep 17 00:00:00 2001 From: Andreas Zweili Date: Wed, 29 Dec 2021 11:10:56 +0100 Subject: [PATCH] refactor code --- tests/conftest.py | 1 - tests/test_gitlab.py | 16 +++++++--------- tests/test_mantishub.py | 14 ++++++-------- todoist_interface/gitlab.py | 19 ++++++++++--------- todoist_interface/mantishub.py | 25 +++++++++++++------------ todoist_interface/todoist.py | 4 ++-- 6 files changed, 38 insertions(+), 41 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index a947350..9ceb064 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,4 +1,3 @@ -import json import pytest diff --git a/tests/test_gitlab.py b/tests/test_gitlab.py index 675b394..da0c82a 100644 --- a/tests/test_gitlab.py +++ b/tests/test_gitlab.py @@ -1,21 +1,19 @@ import json import requests -from todoist_interface.gitlab import GitlabAPI +from todoist_interface import gitlab import mocks def test_init(): - gitlab = GitlabAPI("url", "token", "assignee") - assert (gitlab.url == "url" - and gitlab.token == "token" - and gitlab.assignee == "assignee") + api = gitlab.GitlabAPI("url", "token", "assignee") + assert (api.url == "url" + and api.token == "token" + and api.assignee == "assignee") def test_covert_to_todoist(example_issues): - gitlab = GitlabAPI("url", "token", "assignee") issues = json.loads(example_issues) - tasks = gitlab.convert_to_todoist(issues) assert tasks == [ {'content': '[Consequatur vero maxime deserunt laboriosam est voluptas dolorem.](http://gitlab.example.com/my-group/my-project/issues/6)', @@ -29,7 +27,7 @@ def test_gitlab_get_tickets(monkeypatch, example_issues): # apply the monkeypatch for requests.get to mock_get monkeypatch.setattr(requests, "get", mock_get) - gitlab = GitlabAPI("url", "token", "assignee") - issues = gitlab.get_issues() + api = gitlab.GitlabAPI("url", "token", "assignee") + issues = api.get_issues() assert issues[0][ 'content'] == "[Consequatur vero maxime deserunt laboriosam est voluptas dolorem.](http://gitlab.example.com/my-group/my-project/issues/6)" diff --git a/tests/test_mantishub.py b/tests/test_mantishub.py index a95d759..429d4ee 100644 --- a/tests/test_mantishub.py +++ b/tests/test_mantishub.py @@ -1,14 +1,14 @@ import json import requests -from todoist_interface.mantishub import MantishubAPI +from todoist_interface import mantishub import mocks def test_mantishub_init(): - mantishub = MantishubAPI("mantistoken") - assert mantishub.token == "mantistoken" + api = mantishub.MantishubAPI("mantistoken") + assert api.token == "mantistoken" def test_mantishub_get_tickets(monkeypatch, example_tickets): @@ -18,16 +18,14 @@ def test_mantishub_get_tickets(monkeypatch, example_tickets): # apply the monkeypatch for requests.get to mock_get monkeypatch.setattr(requests, "get", mock_get) - mantishub = MantishubAPI("mantistoken") - tickets = mantishub.get_tickets() + api = mantishub.MantishubAPI("mantistoken") + tickets = api.get_tickets() assert tickets[0]['content'] == "[Sample issue title](https://contria.mantishub.io/view.php?id=1)" def test_covert_to_todoist(example_tickets): - mantis = MantishubAPI("token") issues = json.loads(example_tickets) - - tasks = mantis.convert_to_todoist(issues["issues"]) + tasks = mantishub.convert_to_todoist(issues["issues"]) assert tasks == [ {'content': '[Sample issue title](https://contria.mantishub.io/view.php?id=1)', 'label_ids': [2158784659]} diff --git a/todoist_interface/gitlab.py b/todoist_interface/gitlab.py index f6e432c..ee13b1e 100644 --- a/todoist_interface/gitlab.py +++ b/todoist_interface/gitlab.py @@ -1,6 +1,15 @@ import requests +def convert_to_todoist(issues): + tasks = [] + for issue in issues: + content = "[{title}]({url})".format(title=issue["title"], + url=issue["web_url"]) + tasks.append({"content": content, "label_ids": [2158782094, ]}) + return tasks + + class GitlabAPI: def __init__(self, url: str, token: str, assignee: str) -> None: self.url = url @@ -24,14 +33,6 @@ class GitlabAPI: return issues - def convert_to_todoist(self, issues): - tasks = [] - for issue in issues: - content = "[{title}]({url})".format(title=issue["title"], - url=issue["web_url"]) - tasks.append({"content": content, "label_ids": [2158782094, ]}) - return tasks - def get_issues(self) -> list: issues = self.get_issues_by_assignee(self.assignee) - return self.convert_to_todoist(issues) + return convert_to_todoist(issues) diff --git a/todoist_interface/mantishub.py b/todoist_interface/mantishub.py index 6996530..3e3e73b 100644 --- a/todoist_interface/mantishub.py +++ b/todoist_interface/mantishub.py @@ -1,6 +1,18 @@ import requests +def convert_to_todoist(tickets): + # TODO: add a function to convert mantis priority to todoist priority + tasks = [] + for ticket in tickets: + url = ("https://contria.mantishub.io/view.php?id=" + + str(ticket["id"])) + content = "[{title}]({url})".format(title=ticket["summary"], + url=url) + tasks.append({"content": content, "label_ids": [2158784659, ]}) + return tasks + + class MantishubAPI: url = "https://contria.mantishub.io/api/rest/" @@ -12,15 +24,4 @@ class MantishubAPI: self.url + "issues?filter_id=assigned", headers={"Authorization": self.token}) tickets = response.json() - return self.convert_to_todoist(tickets["issues"]) - - def convert_to_todoist(self, tickets): - # TODO: add a function to convert mantis priority to todoist priority - tasks = [] - for ticket in tickets: - url = ("https://contria.mantishub.io/view.php?id=" - + str(ticket["id"])) - content = "[{title}]({url})".format(title=ticket["summary"], - url=url) - tasks.append({"content": content, "label_ids": [2158784659, ]}) - return tasks + return convert_to_todoist(tickets["issues"]) diff --git a/todoist_interface/todoist.py b/todoist_interface/todoist.py index 635f95e..694592d 100644 --- a/todoist_interface/todoist.py +++ b/todoist_interface/todoist.py @@ -9,14 +9,14 @@ class TodoistAPI: def __init__(self, token: str): self.token = token - def get_get_tasks_by_filter(self, filter: str): + def get_get_tasks_by_filter(self, todoist_filter: str): """ Returns all tasks from todoist """ response = requests.get( self.url + 'tasks', headers={'Authorization': 'Bearer ' + self.token}, - params={"filter": filter}) + params={"filter": todoist_filter}) return response.json() def create_tasks(self, tasks: list):