1. Mark Gemmill
  2. bitbucket-cli

Commits

Mark Gemmill  committed 30e881c

fixes to update and delete commands

  • Participants
  • Parent commits 1db56b7
  • Branches master

Comments (0)

Files changed (4)

File CHANGES

View file
+version 0.2.? (April 15, 2013)
+------------------------------
+
+- added --debug flag to cli which will involk a traceback to be printed on errors 
+
+- fixed bug with `clone` command (missing comma in a tuple of strings are not easy
+to find.
+
+- added better error trapping of HTTP errors thrown by requests. In particular, calls
+to create an already existing repository were erroring with just a '400 ClientError: None'
+message, which was not helpful. Now we parse the response content for info.
+
+- fixed error with a throw-away try-catch. Thanks to Florian for the patch.
+
+- updated README
+
+- trimmed down the `update` command. Options for changing the scm and protocol don't do anything,
+so stripped the options down just to changing the public/private.
+
+
 version 0.2.? (February 18, 2013)
 ---------------------------------
 

File README.md

View file
 
     ...  bb create_from_local --username <your-user-name> --public --protocol ssh <repo-owner> <repo-name>
 
-`update` a BitBucket repository:
+`update` a BitBucket repository. Currently the only option is to change whether a repository is
+*public* or *private*. This example will make the repository private:
 
-    ...  bb update <user-name> <repo-name>
+    ...  bb update --username <user-name> --private <repo-name>
 
 `delete` one of your BitBucket repository:
 

File bitbucket/cli.py

View file
 from .repositories import get_user_repos
 from .config import USERNAME, PASSWORD, SCM, PROTOCOL
 from requests.exceptions import HTTPError
+from requests.status_codes import _codes as status_codes
 
 
 def password(func):
 
 @password
 def update_command(args):
-    update_repository(args.username,
+    result = update_repository(args.username,
                       args.reponame,
                       args.password,
-                      scm=args.scm,
-                      private=args.private)
+                      is_private=args.private)
+    print ''
+    print 'Repository successfully updated.'
+    display_repo_info(result)
 
 
 @password
 def delete_command(args):
-    result = delete_repository(args.username,
+    delete_repository(args.username,
                                args.reponame,
                                args.password)
-    if result:
-        print '{0}/{1} was deleted.'.format(args.username, args.reponame)
-    else:
-        print 'repository deletion failed!'
+    print '{0}/{1} was deleted.'.format(args.username, args.reponame)
 
 
 @password
     #
     update_cmd_parser = subp.add_parser('update',
                             usage=('bitbucket update [-h] [--username USERNAME]\n'
-                                   '                        [--password PASSWORD] [--private | --public]\n'
-                                   '                        [--scm SCM] [--protocol PROTOCOL]\n'
+                                   '                        [--password PASSWORD]\n'
+                                   '                        [--private | --public]\n'
                                    '                        reponame'),
                             description='update an existing bitbucket repository')
     add_standard_args(update_cmd_parser,
                       ('username',
                        'password',
-                       'protocol',
                        'private',
-                       'scm',
-                       'ownername',
+                       'public',
                        'reponame'))
     update_cmd_parser.set_defaults(func=update_command)
 
             traceback.print_tb(exc_tb)
             print '-' * 60
 
+    def print_http_error(ex):
+        import re
+        http_err_code = ex.response.status_code
+        http_err = status_codes.get(http_err_code, [''])[0]
+        print '\nRequest Error {0}: {1}'.format(http_err_code, http_err.replace('_', ' '))
+        # Errros are being sent back as html, so let's strip
+        # out the markup to make it a bit more readable on the
+        # commandline.
+        msg = re.sub('\<[^\>]+\>', ' ', ex.response.content)
+        msg = re.sub(' +', ' ', msg)
+        msg = msg.strip()
+        if msg:
+            print msg
+
     args = None
     exit_code = 0
 
         pass
     except HTTPError as ex:
         # If we get this, then we know it's something with requests
-        print ex
+        print_http_error(ex)
         debug_print_error(args)
         exit_code = 1
     except Exception as ex:

File bitbucket/repositories.py

View file
 
 
 def _json_or_error(r):
-    if r.status_code != 200:
+    # Let's just assume that successful calls to bitbucket
+    # will be in the 200 range regardless of the exact number.
+    # XXX: alternatively, we could pass in the expected error.
+    if r.status_code not in range(200, 300):
         r.raise_for_status()
-    return json.loads(r.content)
+    try:
+        return json.loads(r.content)
+    except:
+        pass
 
 
 def get_user_repos(username, password=''):
 
 def create_repository(name, username, password, scm='hg', is_private=True):
     url = BASE_URL + 'repositories/'
-    r = requests.post(url, data={'name': name, 'scm': scm,
-            'is_private': str(bool(is_private))}, auth=(username, password))
+    payload = {'name': name,
+               'scm': scm,
+               'is_private': str(bool(is_private))}
+    r = requests.post(url, data=payload, auth=(username, password))
     return _json_or_error(r)
 
 
 def delete_repository(username, repo_slug, password):
     url = BASE_URL + 'repositories/%s/%s/' % (username, repo_slug)
     r = requests.delete(url, auth=(username, password))
-    return r.status_code == 204
+    # previously testing explicitly for 204 and handling errors
+    # differently from the other calls.
+    return _json_or_error(r)
 
 
 def download_file(repo_user, repo_slug, filename, username='', password=''):