diff options
author | Leandro Lucarella <leandro.lucarella@sociomantic.com> | 2014-10-16 13:40:31 +0200 |
---|---|---|
committer | Mihails Strasuns <mihails.strasuns@sociomantic.com> | 2014-10-23 15:11:17 +0200 |
commit | eccdb7d13920f1fbb0e846863f9e3e3443e31fc5 (patch) | |
tree | 552439b61f209379158b22d0e7c1afd0f322fd24 | |
parent | a6ecb7be0366f01e98699e89f9c81dea16673495 (diff) |
setup: Accept e-mail as username too
This is not a trivial change, as GitHub doesn't support this for the
API, so we need to search for the possible username first. Only if one
(and only one) username is found the results of the search is used as
the username. The search is only triggered if there is a `@` symbol in
the username.
Fixes #102.
-rwxr-xr-x | git-hub | 24 | ||||
-rw-r--r-- | man.rst | 4 |
2 files changed, 26 insertions, 2 deletions
@@ -626,7 +626,11 @@ class SetupCmd (object): @classmethod def setup_parser(cls, parser): parser.add_argument('-u', '--username', - help="GitHub's username (login name)") + help="GitHub's username (login name). If an e-mail is " + "provided instead, a username matching that e-mail " + "will be searched and used instead, if found (for " + "this to work the e-mail must be part of the public " + "profile)") parser.add_argument('-p', '--password', help="GitHub's password (will not be stored)") parser.add_argument('-b', '--baseurl', metavar='URL', @@ -666,6 +670,12 @@ class SetupCmd (object): if password is None: password = getpass.getpass( 'GitHub password (will not be stored): ') + if '@' in username: + infof("E-mail used to authenticate, trying to " + "retrieve the GitHub username...") + username = cls.find_username(username) + infof("Found: {}", username) + req.set_basic_auth(username, password) note = 'git-hub' @@ -692,6 +702,18 @@ class SetupCmd (object): if args.baseurl is not None: set_config('baseurl', args.baseurl) + @classmethod + def find_username(cls, name): + users = req.get('/search/users', q=name)['items'] + users = [u['login'] for u in users] + if not users: + die("No users found when searching for '{}'", name) + if len(users) > 1: + die("More than one username found ({}), please try " + "again using your username instead", + ', '.join(users)) + return users[0].encode('UTF8') + # `git hub clone` command implementation class CloneCmd (object): @@ -47,7 +47,9 @@ COMMANDS \-u USERNAME, --username=USERNAME GitHub's username (login name), will be stored in the configuration - variable `hub.username`. + variable `hub.username`. If an e-mail is provided, then a username matching + that e-mail will be searched and used instead, if found (for this to work + the e-mail must be part of the public profile). \-p PASSWORD, --password=PASSWORD GitHub's password (will not be stored). |