refactor code
This commit is contained in:
parent
b0dd5c241e
commit
81b3498f35
|
@ -1,4 +1,3 @@
|
||||||
import json
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,21 +1,19 @@
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from todoist_interface.gitlab import GitlabAPI
|
from todoist_interface import gitlab
|
||||||
import mocks
|
import mocks
|
||||||
|
|
||||||
|
|
||||||
def test_init():
|
def test_init():
|
||||||
gitlab = GitlabAPI("url", "token", "assignee")
|
api = gitlab.GitlabAPI("url", "token", "assignee")
|
||||||
assert (gitlab.url == "url"
|
assert (api.url == "url"
|
||||||
and gitlab.token == "token"
|
and api.token == "token"
|
||||||
and gitlab.assignee == "assignee")
|
and api.assignee == "assignee")
|
||||||
|
|
||||||
|
|
||||||
def test_covert_to_todoist(example_issues):
|
def test_covert_to_todoist(example_issues):
|
||||||
gitlab = GitlabAPI("url", "token", "assignee")
|
|
||||||
issues = json.loads(example_issues)
|
issues = json.loads(example_issues)
|
||||||
|
|
||||||
tasks = gitlab.convert_to_todoist(issues)
|
tasks = gitlab.convert_to_todoist(issues)
|
||||||
assert tasks == [
|
assert tasks == [
|
||||||
{'content': '[Consequatur vero maxime deserunt laboriosam est voluptas dolorem.](http://gitlab.example.com/my-group/my-project/issues/6)',
|
{'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
|
# apply the monkeypatch for requests.get to mock_get
|
||||||
monkeypatch.setattr(requests, "get", mock_get)
|
monkeypatch.setattr(requests, "get", mock_get)
|
||||||
gitlab = GitlabAPI("url", "token", "assignee")
|
api = gitlab.GitlabAPI("url", "token", "assignee")
|
||||||
issues = gitlab.get_issues()
|
issues = api.get_issues()
|
||||||
assert issues[0][
|
assert issues[0][
|
||||||
'content'] == "[Consequatur vero maxime deserunt laboriosam est voluptas dolorem.](http://gitlab.example.com/my-group/my-project/issues/6)"
|
'content'] == "[Consequatur vero maxime deserunt laboriosam est voluptas dolorem.](http://gitlab.example.com/my-group/my-project/issues/6)"
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
import json
|
import json
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
from todoist_interface.mantishub import MantishubAPI
|
from todoist_interface import mantishub
|
||||||
|
|
||||||
import mocks
|
import mocks
|
||||||
|
|
||||||
|
|
||||||
def test_mantishub_init():
|
def test_mantishub_init():
|
||||||
mantishub = MantishubAPI("mantistoken")
|
api = mantishub.MantishubAPI("mantistoken")
|
||||||
assert mantishub.token == "mantistoken"
|
assert api.token == "mantistoken"
|
||||||
|
|
||||||
|
|
||||||
def test_mantishub_get_tickets(monkeypatch, example_tickets):
|
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
|
# apply the monkeypatch for requests.get to mock_get
|
||||||
monkeypatch.setattr(requests, "get", mock_get)
|
monkeypatch.setattr(requests, "get", mock_get)
|
||||||
mantishub = MantishubAPI("mantistoken")
|
api = mantishub.MantishubAPI("mantistoken")
|
||||||
tickets = mantishub.get_tickets()
|
tickets = api.get_tickets()
|
||||||
assert tickets[0]['content'] == "[Sample issue title](https://contria.mantishub.io/view.php?id=1)"
|
assert tickets[0]['content'] == "[Sample issue title](https://contria.mantishub.io/view.php?id=1)"
|
||||||
|
|
||||||
|
|
||||||
def test_covert_to_todoist(example_tickets):
|
def test_covert_to_todoist(example_tickets):
|
||||||
mantis = MantishubAPI("token")
|
|
||||||
issues = json.loads(example_tickets)
|
issues = json.loads(example_tickets)
|
||||||
|
tasks = mantishub.convert_to_todoist(issues["issues"])
|
||||||
tasks = mantis.convert_to_todoist(issues["issues"])
|
|
||||||
assert tasks == [
|
assert tasks == [
|
||||||
{'content': '[Sample issue title](https://contria.mantishub.io/view.php?id=1)',
|
{'content': '[Sample issue title](https://contria.mantishub.io/view.php?id=1)',
|
||||||
'label_ids': [2158784659]}
|
'label_ids': [2158784659]}
|
||||||
|
|
|
@ -1,6 +1,15 @@
|
||||||
import requests
|
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:
|
class GitlabAPI:
|
||||||
def __init__(self, url: str, token: str, assignee: str) -> None:
|
def __init__(self, url: str, token: str, assignee: str) -> None:
|
||||||
self.url = url
|
self.url = url
|
||||||
|
@ -24,14 +33,6 @@ class GitlabAPI:
|
||||||
|
|
||||||
return issues
|
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:
|
def get_issues(self) -> list:
|
||||||
issues = self.get_issues_by_assignee(self.assignee)
|
issues = self.get_issues_by_assignee(self.assignee)
|
||||||
return self.convert_to_todoist(issues)
|
return convert_to_todoist(issues)
|
||||||
|
|
|
@ -1,6 +1,18 @@
|
||||||
import requests
|
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:
|
class MantishubAPI:
|
||||||
url = "https://contria.mantishub.io/api/rest/"
|
url = "https://contria.mantishub.io/api/rest/"
|
||||||
|
|
||||||
|
@ -12,15 +24,4 @@ class MantishubAPI:
|
||||||
self.url + "issues?filter_id=assigned",
|
self.url + "issues?filter_id=assigned",
|
||||||
headers={"Authorization": self.token})
|
headers={"Authorization": self.token})
|
||||||
tickets = response.json()
|
tickets = response.json()
|
||||||
return self.convert_to_todoist(tickets["issues"])
|
return 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
|
|
||||||
|
|
|
@ -9,14 +9,14 @@ class TodoistAPI:
|
||||||
def __init__(self, token: str):
|
def __init__(self, token: str):
|
||||||
self.token = token
|
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
|
Returns all tasks from todoist
|
||||||
"""
|
"""
|
||||||
response = requests.get(
|
response = requests.get(
|
||||||
self.url + 'tasks',
|
self.url + 'tasks',
|
||||||
headers={'Authorization': 'Bearer ' + self.token},
|
headers={'Authorization': 'Bearer ' + self.token},
|
||||||
params={"filter": filter})
|
params={"filter": todoist_filter})
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def create_tasks(self, tasks: list):
|
def create_tasks(self, tasks: list):
|
||||||
|
|
Loading…
Reference in New Issue