| 1 |
from builtins import filter |
| 2 |
import re |
| 3 |
import six |
| 4 |
from urllib.parse import urlparse |
| 5 |
|
| 6 |
import requests |
| 7 |
from six.moves.urllib.parse import quote_plus |
| 8 |
from jinja2 import Template |
| 9 |
|
| 10 |
from bugwarrior.config import asbool, aslist, die |
| 11 |
from bugwarrior.services import IssueService, Issue, ServiceClient |
| 12 |
|
| 13 |
import logging |
| 14 |
log = logging.getLogger(__name__) |
| 15 |
|
| 16 |
class TeamworkClient(ServiceClient): |
| 17 |
|
| 18 |
def __init__(self, host, token): |
| 19 |
self.host = host |
| 20 |
self.token = token |
| 21 |
|
| 22 |
def authenticate(self): |
| 23 |
response = requests.get(self.host + "/authenticate.json", auth=(self.token, "")) |
| 24 |
return self.json_response(response) |
| 25 |
|
| 26 |
def call_api(self, method, endpoint, data=None): |
| 27 |
response = requests.get(self.host + endpoint, auth=(self.token, ""), params=data) |
| 28 |
return self.json_response(response) |
| 29 |
|
| 30 |
class TeamworkIssue(Issue): |
| 31 |
URL = 'teamwork_url' |
| 32 |
TITLE = 'teamwork_title' |
| 33 |
DESCRIPTION_LONG = 'teamwork_description_long' |
| 34 |
PROJECT_ID = 'teamwork_project_id' |
| 35 |
STATUS = 'teamwork_status' |
| 36 |
ID = 'teamwork_id' |
| 37 |
|
| 38 |
UDAS = { |
| 39 |
URL: { |
| 40 |
'type': 'string', |
| 41 |
'label': 'Teamwork Url', |
| 42 |
}, |
| 43 |
TITLE: { |
| 44 |
'type': 'string', |
| 45 |
'label': 'Teamwork Title', |
| 46 |
}, |
| 47 |
DESCRIPTION_LONG: { |
| 48 |
'type': 'string', |
| 49 |
'label': 'Teamwork Description Long', |
| 50 |
}, |
| 51 |
PROJECT_ID: { |
| 52 |
'type': 'numeric', |
| 53 |
'label': 'Teamwork Project ID', |
| 54 |
}, |
| 55 |
STATUS: { |
| 56 |
'type': 'string', |
| 57 |
'label': 'Teamwork Status', |
| 58 |
}, |
| 59 |
ID: { |
| 60 |
'type': 'numeric', |
| 61 |
'label': 'Teamwork Task ID', |
| 62 |
}, |
| 63 |
} |
| 64 |
|
| 65 |
UNIQUE_KEY = (URL, ) |
| 66 |
PRIORITY_MAP = { |
| 67 |
"low": "L", |
| 68 |
"medium": "M", |
| 69 |
"high": "H" |
| 70 |
} |
| 71 |
|
| 72 |
def get_owner(self, issue): |
| 73 |
if issue: |
| 74 |
if self.record.get("responsible-party-ids", ""): |
| 75 |
if self.user_id in self.record.get("responsible-party-ids", ""): |
| 76 |
return self.name |
| 77 |
|
| 78 |
def get_author(self, issue): |
| 79 |
if issue: |
| 80 |
author = self.record["creator-firstname"] + " " + self.record["creator-lastname"] |
| 81 |
return author |
| 82 |
|
| 83 |
def get_task_url(self): |
| 84 |
return self.extra["host"] + "/#/tasks/" + str(self.record["id"]) |
| 85 |
|
| 86 |
def get_default_description(self): |
| 87 |
return self.build_default_description( |
| 88 |
title=self.record["content"], |
| 89 |
url=self.get_task_url(), |
| 90 |
number=self.record["id"], |
| 91 |
) |
| 92 |
|
| 93 |
def to_taskwarrior(self): |
| 94 |
task_url = self.get_task_url() |
| 95 |
description = self.record.get("content", "") |
| 96 |
parent_id = self.record["parentTaskId"] |
| 97 |
status = self.record["status"] |
| 98 |
|
| 99 |
due = self.parse_date(self.record.get('due-date')) |
| 100 |
created = self.parse_date(self.record.get('created-on')) |
| 101 |
modified = self.parse_date(self.record.get('last-changed-on')) |
| 102 |
|
| 103 |
end = "" |
| 104 |
if str(status) in ["reopened", "new"]: |
| 105 |
status = "Open" |
| 106 |
else: |
| 107 |
end = modified |
| 108 |
status = "Closed" |
| 109 |
|
| 110 |
return { |
| 111 |
'project': self.record["project-name"], |
| 112 |
'priority': self.get_priority(), |
| 113 |
'due': due, |
| 114 |
'entry': created, |
| 115 |
'end': end, |
| 116 |
'modified': modified, |
| 117 |
'annotations': self.extra.get('annotations', []), |
| 118 |
self.URL: task_url, |
| 119 |
self.TITLE: self.record.get("content", ""), |
| 120 |
self.DESCRIPTION_LONG: self.record.get("description", ""), |
| 121 |
self.PROJECT_ID: int(self.record["project-id"]), |
| 122 |
self.STATUS: status, |
| 123 |
self.ID: int(self.record["id"]), |
| 124 |
} |
| 125 |
|
| 126 |
class TeamworkService(IssueService): |
| 127 |
ISSUE_CLASS = TeamworkIssue |
| 128 |
CONFIG_PREFIX = 'teamwork_projects' |
| 129 |
|
| 130 |
def __init__(self, *args, **kwargs): |
| 131 |
super(TeamworkService, self).__init__(*args, **kwargs) |
| 132 |
self.host = self.config.get('host', '') |
| 133 |
self.token = self.config.get('token', '') |
| 134 |
self.client = TeamworkClient(self.host, self.token) |
| 135 |
user = self.client.authenticate() |
| 136 |
self.user_id = user["account"]["userId"] |
| 137 |
self.name = user["account"]["firstname"] + " " + user["account"]["lastname"] |
| 138 |
|
| 139 |
def get_comments(self, issue): |
| 140 |
if self.annotation_comments: |
| 141 |
if issue.get("comments-count", 0) > 0: |
| 142 |
endpoint = "/tasks/{task_id}/comments.json".format(task_id=issue["id"]) |
| 143 |
comments = self.client.call_api("GET", endpoint) |
| 144 |
comment_list = [] |
| 145 |
for comment in comments["comments"]: |
| 146 |
url = self.host + "/#/tasks/" + str(issue["id"]) |
| 147 |
author = "{first} {last}".format( |
| 148 |
first=comment["author-firstname"], |
| 149 |
last=comment["author-lastname"], |
| 150 |
) |
| 151 |
text = comment["body"] |
| 152 |
comment_list.append((author, text)) |
| 153 |
return self.build_annotations(comment_list, None) |
| 154 |
return [] |
| 155 |
|
| 156 |
|
| 157 |
def issues(self): |
| 158 |
response = self.client.call_api("GET", "/tasks.json")#, data= { "responsible-party-ids": self.user_id }) |
| 159 |
for issue in response["todo-items"]: |
| 160 |
# Determine if issue is need by if following comments, changes or assigned |
| 161 |
if issue["userFollowingComments"] or issue["userFollowingChanges"]\ |
| 162 |
or (self.user_id in issue.get("responsible-party-ids", "")): |
| 163 |
issue_obj = self.get_issue_for_record(issue) |
| 164 |
extra = { |
| 165 |
"host": self.host, |
| 166 |
'annotations': self.get_comments(issue), |
| 167 |
} |
| 168 |
issue_obj.update_extra(extra) |
| 169 |
yield issue_obj |