Permalink
Browse files

Introduce the status command

The status command will display the id and summary of each pending
review request a user has.
  • Loading branch information...
1 parent fea88b9 commit 8709d10a5c8dbb6bb5effc79038b96e800b305a8 @smacleod smacleod committed Jan 18, 2013
Showing with 147 additions and 0 deletions.
  1. +1 −0 rbtools/api/client.py
  2. +111 −0 rbtools/commands/status.py
  3. +34 −0 rbtools/utils/users.py
  4. +1 −0 setup.py
View
@@ -9,6 +9,7 @@ class RBClient(object):
'transport_cls' parameter.
"""
def __init__(self, url, transport_cls=SyncTransport, *args, **kwargs):
+ self.url = url
self._transport = transport_cls(url, *args, **kwargs)
def get_root(self, *args, **kwargs):
View
@@ -0,0 +1,111 @@
+import logging
+
+from rbtools.commands import Command, Option
+from rbtools.utils.users import get_user
+
+
+def get_repository_id(repository_info, api_root, repository_name=None):
+ """Get the repository ID from the server.
+
+ This will compare the paths returned by the SCM client
+ with those on the server, and return the id of the first
+ match.
+ """
+ detected_paths = repository_info.path
+
+ if not isinstance(detected_paths, list):
+ detected_paths = [detected_paths]
+
+ repositories = api_root.get_repositories()
+
+ try:
+ while True:
+ for repo in repositories:
+ if (repo.path in detected_paths or
+ repo.name == repository_name):
+ return repo.id
+
+ repositories = repositories.get_next()
+ except StopIteration:
+ return None
+
+
+class Status(Command):
+ """Display review requests for the current repository."""
+ name = "status"
+ author = "The Review Board Project"
+ description = "Output a list of your pending review requests."
+ args = ""
+ option_list = [
+ Option("--server",
+ dest="server",
+ metavar="SERVER",
+ config_key="REVIEWBOARD_URL",
+ default=None,
+ help="specify a different Review Board server to use"),
+ Option("--username",
+ dest="username",
+ metavar="USERNAME",
+ config_key="USERNAME",
+ default=None,
+ help="user name to be supplied to the Review Board server"),
+ Option("--password",
+ dest="password",
+ metavar="PASSWORD",
+ config_key="PASSWORD",
+ default=None,
+ help="password to be supplied to the Review Board server"),
+ Option("--all",
+ dest="all_repositories",
+ action="store_true",
+ default=False,
+ help="Show review requests for all repositories instead "
+ "of the detected repository."),
+ ]
+
+ def output_request(self, request):
+ print "r/%s - %s" % (request.id, request.summary)
+
+ def output_draft(self, request, draft):
+ print "* r/%s - %s" % (request.id, draft.summary)
+
+ def main(self):
+ repository_info, tool = self.initialize_scm_tool()
+ server_url = self.get_server_url(repository_info, tool)
+ api_client, api_root = self.get_api(server_url)
+ self.setup_tool(tool, api_root=api_root)
+ user = get_user(api_client, api_root, auth_required=True)
+
+ query_args = {
+ 'from_user': user.username,
+ 'status': 'pending',
+ 'expand': 'draft',
+ }
+
+ if not self.options.all_repositories:
+ repo_id = get_repository_id(
+ repository_info,
+ api_root,
+ repository_name=self.config.get('REPOSITORY', None))
+
+ if repo_id:
+ query_args['repository'] = repo_id
+ else:
+ logging.warning('The repository detected in the current '
+ 'directory was not found on\n'
+ 'the Review Board server. Displaying review '
+ 'requests from all repositories.')
+
+ requests = api_root.get_review_requests(**query_args)
+
+ try:
+ while True:
+ for request in requests:
+ if request.draft:
+ self.output_draft(request, request.draft[0])
+ else:
+ self.output_request(request)
+
+ requests = requests.get_next(**query_args)
+ except StopIteration:
+ pass
View
@@ -0,0 +1,34 @@
+import getpass
+import logging
+import sys
+
+from rbtools.api.errors import AuthorizationError
+from rbtools.commands import CommandError
+
+
+def get_user(api_client, api_root, auth_required=False):
+ """Return the user resource for the current session
+
+ None will be returned if the user is not authenticated, unless the
+ 'auth_required' parameter is True, in which case the user will be
+ prompted to login.
+ """
+ session = api_root.get_session()
+
+ if not session.authenticated:
+ if not auth_required:
+ return None
+
+ logging.warning('You are not authenticated with the Review Board'
+ 'server at %s, please login.' % api_client.url)
+ sys.stderr.write('Username: ')
+ username = raw_input()
+ password = getpass.getpass('Password: ')
+ api_client.login(username, password)
+
+ try:
+ session = session.get_self()
+ except AuthorizationError:
+ raise CommandError('You are not authenticated.')
+
+ return session.get_user()
View
@@ -56,6 +56,7 @@
'patch = rbtools.commands.patch:Patch',
'post = rbtools.commands.post:Post',
'publish = rbtools.commands.publish:Publish',
+ 'status = rbtools.commands.status:Status',
]
setup(name=PACKAGE_NAME,

0 comments on commit 8709d10

Please sign in to comment.