| 1 |
from __future__ import absolute_import |
| 2 |
|
| 3 |
import megaplan |
| 4 |
|
| 5 |
from bugwarrior.config import die |
| 6 |
from bugwarrior.services import IssueService, Issue |
| 7 |
|
| 8 |
import logging |
| 9 |
log = logging.getLogger(__name__) |
| 10 |
|
| 11 |
|
| 12 |
class MegaplanIssue(Issue): |
| 13 |
URL = 'megaplanurl' |
| 14 |
FOREIGN_ID = 'megaplanid' |
| 15 |
TITLE = 'megaplantitle' |
| 16 |
|
| 17 |
UDAS = { |
| 18 |
TITLE: { |
| 19 |
'type': 'string', |
| 20 |
'label': 'Megaplan Title', |
| 21 |
}, |
| 22 |
URL: { |
| 23 |
'type': 'string', |
| 24 |
'label': 'Megaplan URL', |
| 25 |
}, |
| 26 |
FOREIGN_ID: { |
| 27 |
'type': 'string', |
| 28 |
'label': 'Megaplan Issue ID' |
| 29 |
} |
| 30 |
} |
| 31 |
UNIQUE_KEY = (URL, ) |
| 32 |
|
| 33 |
def to_taskwarrior(self): |
| 34 |
return { |
| 35 |
'project': self.get_project(), |
| 36 |
'priority': self.get_priority(), |
| 37 |
|
| 38 |
self.FOREIGN_ID: self.record['Id'], |
| 39 |
self.URL: self.get_issue_url(), |
| 40 |
self.TITLE: self.get_issue_title(), |
| 41 |
} |
| 42 |
|
| 43 |
def get_project(self): |
| 44 |
return self.origin['project_name'] |
| 45 |
|
| 46 |
def get_default_description(self): |
| 47 |
return self.build_default_description( |
| 48 |
title=self.get_issue_title(), |
| 49 |
url=self.get_processed_url(self.get_issue_url()), |
| 50 |
number=self.record['Id'], |
| 51 |
cls='issue', |
| 52 |
) |
| 53 |
|
| 54 |
def get_issue_url(self): |
| 55 |
return "https://%s/task/%d/card/" % ( |
| 56 |
self.origin['hostname'], self.record["Id"] |
| 57 |
) |
| 58 |
|
| 59 |
def get_issue_title(self): |
| 60 |
parts = self.record["Name"].split("|") |
| 61 |
return parts[-1].strip() |
| 62 |
|
| 63 |
def get_issue_id(self): |
| 64 |
if self.record["Id"] > 1000000: |
| 65 |
return self.record["Id"] - 1000000 |
| 66 |
return self.record["Id"] |
| 67 |
|
| 68 |
|
| 69 |
class MegaplanService(IssueService): |
| 70 |
ISSUE_CLASS = MegaplanIssue |
| 71 |
CONFIG_PREFIX = 'megaplan' |
| 72 |
|
| 73 |
def __init__(self, *args, **kw): |
| 74 |
super(MegaplanService, self).__init__(*args, **kw) |
| 75 |
|
| 76 |
self.hostname = self.config.get('hostname') |
| 77 |
_login = self.config.get('login') |
| 78 |
_password = self.get_password('password', _login) |
| 79 |
|
| 80 |
self.client = megaplan.Client(self.hostname) |
| 81 |
self.client.authenticate(_login, _password) |
| 82 |
|
| 83 |
self.project_name = self.config.get('project_name', self.hostname) |
| 84 |
|
| 85 |
@staticmethod |
| 86 |
def get_keyring_service(service_config): |
| 87 |
login = service_config.get('login') |
| 88 |
hostname = service_config.get('hostname') |
| 89 |
return "megaplan://%s@%s" % (login, hostname) |
| 90 |
|
| 91 |
def get_service_metadata(self): |
| 92 |
return { |
| 93 |
'project_name': self.project_name, |
| 94 |
'hostname': self.hostname, |
| 95 |
} |
| 96 |
|
| 97 |
@classmethod |
| 98 |
def validate_config(cls, service_config, target): |
| 99 |
for k in ('login', 'password', 'hostname'): |
| 100 |
if k not in service_config: |
| 101 |
die("[%s] has no 'mplan.%s'" % (target, k)) |
| 102 |
|
| 103 |
IssueService.validate_config(service_config, target) |
| 104 |
|
| 105 |
def issues(self): |
| 106 |
issues = self.client.get_actual_tasks() |
| 107 |
log.debug(" Found %i total.", len(issues)) |
| 108 |
|
| 109 |
for issue in issues: |
| 110 |
yield self.get_issue_for_record(issue) |