Redmine: Add support for --author and --assignee in @issues.

This commit is contained in:
Valentin Lorentz 2012-12-09 20:34:47 +01:00
parent 83896494b6
commit 00aad411b4
2 changed files with 57 additions and 20 deletions

View File

@ -46,10 +46,13 @@ def fetch(site, uri, **kwargs):
url += '?' + utils.web.urlencode(kwargs) url += '?' + utils.web.urlencode(kwargs)
return json.load(utils.web.getUrlFd(url)) return json.load(utils.web.getUrlFd(url))
class ProjectNotFound(Exception): class ResourceNotFound(Exception):
pass pass
class AmbiguousProject(Exception): class AmbiguousResource(Exception):
pass
class AccessDenied(Exception):
pass pass
def get_project(site, project): def get_project(site, project):
@ -60,12 +63,38 @@ def get_project(site, project):
if projects: if projects:
break break
if not projects: if not projects:
raise ProjectNotFound() raise ResourceNotFound()
elif len(projects) > 1: elif len(projects) > 1:
raise AmbiguousProject() raise AmbiguousResource()
else: else:
return projects[0] return projects[0]
def get_project_or_error(irc, site, project):
try:
return get_project(site, project)
except ResourceNotFound:
irc.error(_('Project not found.'), Raise=True)
except AmbiguousResource:
irc.error(_('Ambiguous project name.'), Raise=True)
def get_user(site, user):
if user.isdigit():
return fetch(site, 'users/%s' % user)
else:
# TODO: Find a way to get user data from their name...
# (authenticating as admin seems the only way)
raise AccessDenied()
def get_user_or_error(irc, site, user):
try:
return get_user(site, user)
except ResourceNotFound:
irc.error(_('User not found.'), Raise=True)
except AmbiguousResource:
irc.error(_('Ambiguous user name.'), Raise=True)
except AccessDenied:
irc.error(_('Cannot get a user id from their name.'), Raise=True)
def handle_site_arg(wrap_args): def handle_site_arg(wrap_args):
"""Decorator for handling the <site> argument of all commands, because """Decorator for handling the <site> argument of all commands, because
I am lazy.""" I am lazy."""
@ -89,15 +118,6 @@ def handle_site_arg(wrap_args):
irc.error(_('Invalid site name.'), Raise=True) irc.error(_('Invalid site name.'), Raise=True)
site = sites[site_name] site = sites[site_name]
# Handle project converter
if project:
try:
args2 = (get_project(site, args2[0]),) + tuple(args[1:])
except ProjectNotFound:
irc.error(_('Project not found.'), Raise=True)
except AmbiguousProject:
irc.error(_('Ambiguous project name.'), Raise=True)
return f(self, irc, msg, args, site, *args2) return f(self, irc, msg, args, site, *args2)
newf.__doc__ = """[<site>] %s newf.__doc__ = """[<site>] %s
@ -172,13 +192,28 @@ class Redmine(callbacks.Plugin):
irc.reply(format('%L', projects)) irc.reply(format('%L', projects))
@internationalizeDocstring @internationalizeDocstring
@handle_site_arg(['project']) @handle_site_arg([getopts({'project': 'something',
def issues(self, irc, msg, args, site, project): 'author': 'something',
"""<project> 'assignee': 'something',
})])
def issues(self, irc, msg, args, site, optlist):
"""[--project <project>] [--author <username>] \
[--assignee <username>]
Return the list of issues of the <project> on the Redmine <site>.""" Return a list of issues on the Redmine <site>, filtered with
issues = fetch(site, 'issues', project_id=project['id'], given parameters."""
sort='updated_on:desc')['issues'] fetch_args = {}
for (key, value) in optlist:
if key == 'project':
fetch_args['project_id'] = get_project_or_error(irc, site, value)['id']
elif key == 'author':
fetch_args['author_id'] = get_user_or_error(irc, site, value)['user']['id']
elif key == 'assignee':
fetch_args['assigned_to_id'] = get_user_or_error(irc, site, value)['user']['id']
else:
raise AssertionError((key, value))
issues = fetch(site, 'issues', sort='updated_on:desc', **fetch_args)
issues = issues['issues']
new_issues = [] new_issues = []
for issue in issues: for issue in issues:
new_issue = {} new_issue = {}

View File

@ -57,7 +57,9 @@ class RedmineTestCase(PluginTestCase):
@init @init
def testIssues(self): def testIssues(self):
self.assertRegexp('issues lqdn campagne', '^\x02.*\x02 \(last.*\)') self.assertRegexp('issues lqdn --project campagne', '^\x02.*\x02 \(last.*\)')
self.assertRegexp('issues lqdn --author 19', '^\x02.*\x02 \(last.*\)')
self.assertRegexp('issues lqdn --assignee 19', '^\x02.*\x02 \(last.*\)')
if not network: if not network:
del RedmineTestCase del RedmineTestCase