1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""GitHub command implementations."""
19
from __future__ import absolute_import
24
from ...commands import Command
27
class cmd_github_login(Command):
28
__doc__ = """Log into GitHub.
30
When communicating with GitHub, some commands need to authenticate to
34
takes_args = ['username?']
36
def run(self, username=None):
37
from github import Github, GithubException
38
from breezy.config import AuthenticationConfig
39
authconfig = AuthenticationConfig()
41
username = authconfig.get_user(
42
'https', 'github.com', prompt=u'GitHub username', ask=True)
43
password = authconfig.get_password('https', 'github.com', username)
44
client = Github(username, password)
45
user = client.get_user()
47
authorization = user.create_authorization(
48
scopes=['user', 'repo', 'delete_repo'], note='Breezy',
49
note_url='https://github.com/breezy-team/breezy')
50
except GithubException as e:
51
errs = e.data.get('errors', [])
53
err_code = errs[0].get('code')
54
if err_code == u'already_exists':
55
raise errors.BzrCommandError('token already exists')
56
raise errors.BzrCommandError(e.data['message'])
57
# TODO(jelmer): This should really use something in
58
# AuthenticationConfig
59
from .github import store_github_token
60
store_github_token(scheme='https', host='github.com',
61
token=authorization.token)