-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from umonkey/develop
Support for TeamLab and RedMine
- Loading branch information
Showing
4 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from twiggy import log | ||
|
||
from bugwarrior.services import IssueService | ||
from bugwarrior.config import die | ||
|
||
import datetime | ||
import urllib | ||
import urllib2 | ||
import json | ||
|
||
|
||
class Client(object): | ||
def __init__(self, url, key): | ||
self.url = url | ||
self.key = key | ||
|
||
def find_issues(self, user_id=None): | ||
args = {} | ||
if user_id is not None: | ||
args["assigned_to_id"] = user_id | ||
return self.call_api("/issues.json", args)["issues"] | ||
|
||
def call_api(self, uri, get=None): | ||
url = self.url.rstrip("/") + uri | ||
|
||
if get: | ||
url += "?" + urllib.urlencode(get) | ||
|
||
req = urllib2.Request(url) | ||
req.add_header("X-Redmine-API-Key", self.key) | ||
|
||
res = urllib2.urlopen(req) | ||
|
||
return json.loads(res.read()) | ||
|
||
|
||
class RedMineService(IssueService): | ||
def __init__(self, *args, **kw): | ||
super(RedMineService, self).__init__(*args, **kw) | ||
|
||
self.url = self.config.get(self.target, 'url').rstrip("/") | ||
self.key = self.config.get(self.target, 'key') | ||
self.user_id = self.config.get(self.target, 'user_id') | ||
|
||
self.client = Client(self.url, self.key) | ||
|
||
self.project_name = None | ||
if self.config.has_option(self.target, "project_name"): | ||
self.project_name = self.config.get(self.target, "project_name") | ||
|
||
@classmethod | ||
def validate_config(cls, config, target): | ||
for k in ('url', 'key', 'user_id'): | ||
if not config.has_option(target, k): | ||
die("[%s] has no '%s'" % (target, k)) | ||
|
||
IssueService.validate_config(config, target) | ||
|
||
def get_issue_url(self, issue): | ||
return self.url + "/issues/" + str(issue["id"]) | ||
|
||
def get_project_name(self, issue): | ||
if self.project_name: | ||
return self.project_name | ||
return issue["project"]["name"] | ||
|
||
def issues(self): | ||
issues = self.client.find_issues(self.user_id) | ||
log.debug(" Found {0} total.", len(issues)) | ||
|
||
return [dict( | ||
description=self.description( | ||
issue["subject"], | ||
self.get_issue_url(issue), | ||
issue["id"], cls="issue", | ||
), | ||
project=self.get_project_name(issue), | ||
priority=self.default_priority, | ||
) for issue in issues] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
from twiggy import log | ||
|
||
from bugwarrior.services import IssueService | ||
from bugwarrior.config import die | ||
|
||
import datetime | ||
import json | ||
import urllib | ||
import urllib2 | ||
|
||
|
||
class Client(object): | ||
def __init__(self, hostname, verbose=False): | ||
self.hostname = hostname | ||
self.verbose = verbose | ||
self.token = None | ||
|
||
def authenticate(self, login, password): | ||
resp = self.call_api("/api/1.0/authentication.json", post={ | ||
"userName": str(login), | ||
"password": str(password), | ||
}) | ||
|
||
self.token = str(resp["token"]) | ||
|
||
def get_task_list(self): | ||
resp = self.call_api("/api/1.0/project/task/@self.json") | ||
return resp | ||
|
||
def call_api(self, uri, post=None, get=None): | ||
uri = "http://" + self.hostname + uri | ||
|
||
if post is None: | ||
data = None | ||
else: | ||
data = urllib.urlencode(post) | ||
|
||
if get is not None: | ||
uri += "?" + urllib.urlencode(get) | ||
|
||
self.log("Fetching %s" % uri) | ||
|
||
req = urllib2.Request(uri, data) | ||
if self.token is not None: | ||
req.add_header("Authorization", self.token) | ||
req.add_header("Accept", "application/json") | ||
|
||
res = urllib2.urlopen(req) | ||
if res.getcode() >= 400: | ||
raise Exception("Error accessing the API: %s" % res.read()) | ||
|
||
response = res.read() | ||
|
||
return json.loads(response)["response"] | ||
|
||
def log(self, message): | ||
if self.verbose: | ||
print message | ||
|
||
|
||
class TeamLabService(IssueService): | ||
def __init__(self, *args, **kw): | ||
super(TeamLabService, self).__init__(*args, **kw) | ||
|
||
self.hostname = self.config.get(self.target, 'hostname') | ||
_login = self.config.get(self.target, 'login') | ||
_password = self.config.get(self.target, 'password') | ||
|
||
self.client = Client(self.hostname) | ||
self.client.authenticate(_login, _password) | ||
|
||
self.project_name = self.hostname | ||
if self.config.has_option(self.target, "project_name"): | ||
self.project_name = self.config.get(self.target, "project_name") | ||
|
||
@classmethod | ||
def validate_config(cls, config, target): | ||
for k in ('login', 'password', 'hostname'): | ||
if not config.has_option(target, k): | ||
die("[%s] has no '%s'" % (target, k)) | ||
|
||
IssueService.validate_config(config, target) | ||
|
||
def get_issue_url(self, issue): | ||
return "http://%s/products/projects/tasks.aspx?prjID=%d&id=%d" \ | ||
% (self.hostname, issue["projectOwner"]["id"], issue["id"]) | ||
|
||
def get_priority(self, issue): | ||
if issue["priority"] == 1: | ||
return "H" | ||
else: | ||
return "M" | ||
|
||
def issues(self): | ||
issues = self.client.get_task_list() | ||
log.debug(" Remote has {0} total issues.", len(issues)) | ||
if not issues: | ||
return [] | ||
|
||
# Filter out closed tasks. | ||
issues = filter(lambda i: i["status"] == 1, issues) | ||
log.debug(" Remote has {0} active issues.", len(issues)) | ||
|
||
return [dict( | ||
description=self.description( | ||
issue["title"], | ||
self.get_issue_url(issue), | ||
issue["id"], cls="issue", | ||
), | ||
project=self.project_name, | ||
priority=self.get_priority(issue), | ||
) for issue in issues] | ||
|