Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Introduce the status command

The status command will display the id and summary of each pending
review request a user has.
  • Loading branch information...
commit 8709d10a5c8dbb6bb5effc79038b96e800b305a8 1 parent fea88b9
Steven MacLeod smacleod authored
1  rbtools/api/client.py
@@ -9,6 +9,7 @@ class RBClient(object):
9 9 'transport_cls' parameter.
10 10 """
11 11 def __init__(self, url, transport_cls=SyncTransport, *args, **kwargs):
  12 + self.url = url
12 13 self._transport = transport_cls(url, *args, **kwargs)
13 14
14 15 def get_root(self, *args, **kwargs):
111 rbtools/commands/status.py
... ... @@ -0,0 +1,111 @@
  1 +import logging
  2 +
  3 +from rbtools.commands import Command, Option
  4 +from rbtools.utils.users import get_user
  5 +
  6 +
  7 +def get_repository_id(repository_info, api_root, repository_name=None):
  8 + """Get the repository ID from the server.
  9 +
  10 + This will compare the paths returned by the SCM client
  11 + with those on the server, and return the id of the first
  12 + match.
  13 + """
  14 + detected_paths = repository_info.path
  15 +
  16 + if not isinstance(detected_paths, list):
  17 + detected_paths = [detected_paths]
  18 +
  19 + repositories = api_root.get_repositories()
  20 +
  21 + try:
  22 + while True:
  23 + for repo in repositories:
  24 + if (repo.path in detected_paths or
  25 + repo.name == repository_name):
  26 + return repo.id
  27 +
  28 + repositories = repositories.get_next()
  29 + except StopIteration:
  30 + return None
  31 +
  32 +
  33 +class Status(Command):
  34 + """Display review requests for the current repository."""
  35 + name = "status"
  36 + author = "The Review Board Project"
  37 + description = "Output a list of your pending review requests."
  38 + args = ""
  39 + option_list = [
  40 + Option("--server",
  41 + dest="server",
  42 + metavar="SERVER",
  43 + config_key="REVIEWBOARD_URL",
  44 + default=None,
  45 + help="specify a different Review Board server to use"),
  46 + Option("--username",
  47 + dest="username",
  48 + metavar="USERNAME",
  49 + config_key="USERNAME",
  50 + default=None,
  51 + help="user name to be supplied to the Review Board server"),
  52 + Option("--password",
  53 + dest="password",
  54 + metavar="PASSWORD",
  55 + config_key="PASSWORD",
  56 + default=None,
  57 + help="password to be supplied to the Review Board server"),
  58 + Option("--all",
  59 + dest="all_repositories",
  60 + action="store_true",
  61 + default=False,
  62 + help="Show review requests for all repositories instead "
  63 + "of the detected repository."),
  64 + ]
  65 +
  66 + def output_request(self, request):
  67 + print "r/%s - %s" % (request.id, request.summary)
  68 +
  69 + def output_draft(self, request, draft):
  70 + print "* r/%s - %s" % (request.id, draft.summary)
  71 +
  72 + def main(self):
  73 + repository_info, tool = self.initialize_scm_tool()
  74 + server_url = self.get_server_url(repository_info, tool)
  75 + api_client, api_root = self.get_api(server_url)
  76 + self.setup_tool(tool, api_root=api_root)
  77 + user = get_user(api_client, api_root, auth_required=True)
  78 +
  79 + query_args = {
  80 + 'from_user': user.username,
  81 + 'status': 'pending',
  82 + 'expand': 'draft',
  83 + }
  84 +
  85 + if not self.options.all_repositories:
  86 + repo_id = get_repository_id(
  87 + repository_info,
  88 + api_root,
  89 + repository_name=self.config.get('REPOSITORY', None))
  90 +
  91 + if repo_id:
  92 + query_args['repository'] = repo_id
  93 + else:
  94 + logging.warning('The repository detected in the current '
  95 + 'directory was not found on\n'
  96 + 'the Review Board server. Displaying review '
  97 + 'requests from all repositories.')
  98 +
  99 + requests = api_root.get_review_requests(**query_args)
  100 +
  101 + try:
  102 + while True:
  103 + for request in requests:
  104 + if request.draft:
  105 + self.output_draft(request, request.draft[0])
  106 + else:
  107 + self.output_request(request)
  108 +
  109 + requests = requests.get_next(**query_args)
  110 + except StopIteration:
  111 + pass
34 rbtools/utils/users.py
... ... @@ -0,0 +1,34 @@
  1 +import getpass
  2 +import logging
  3 +import sys
  4 +
  5 +from rbtools.api.errors import AuthorizationError
  6 +from rbtools.commands import CommandError
  7 +
  8 +
  9 +def get_user(api_client, api_root, auth_required=False):
  10 + """Return the user resource for the current session
  11 +
  12 + None will be returned if the user is not authenticated, unless the
  13 + 'auth_required' parameter is True, in which case the user will be
  14 + prompted to login.
  15 + """
  16 + session = api_root.get_session()
  17 +
  18 + if not session.authenticated:
  19 + if not auth_required:
  20 + return None
  21 +
  22 + logging.warning('You are not authenticated with the Review Board'
  23 + 'server at %s, please login.' % api_client.url)
  24 + sys.stderr.write('Username: ')
  25 + username = raw_input()
  26 + password = getpass.getpass('Password: ')
  27 + api_client.login(username, password)
  28 +
  29 + try:
  30 + session = session.get_self()
  31 + except AuthorizationError:
  32 + raise CommandError('You are not authenticated.')
  33 +
  34 + return session.get_user()
1  setup.py
@@ -56,6 +56,7 @@
56 56 'patch = rbtools.commands.patch:Patch',
57 57 'post = rbtools.commands.post:Post',
58 58 'publish = rbtools.commands.publish:Publish',
  59 + 'status = rbtools.commands.status:Status',
59 60 ]
60 61
61 62 setup(name=PACKAGE_NAME,

0 comments on commit 8709d10

Please sign in to comment.
Something went wrong with that request. Please try again.