/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to breezy/plugins/github/cmds.py

  • Committer: Jelmer Vernooij
  • Date: 2020-06-15 01:29:36 UTC
  • mfrom: (7490.40.4 work)
  • mto: (7490.40.19 work)
  • mto: This revision was merged to the branch mainline in revision 7516.
  • Revision ID: jelmer@jelmer.uk-20200615012936-1adqbu592y7lzmy8
Merge upstream.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2018 Jelmer Vernooij <jelmer@jelmer.uk>
 
2
#
 
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.
 
7
#
 
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.
 
12
#
 
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
 
16
 
 
17
"""GitHub command implementations."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from ... import (
 
22
    errors,
 
23
    )
 
24
from ...commands import Command
 
25
 
 
26
 
 
27
class cmd_github_login(Command):
 
28
    __doc__ = """Log into GitHub.
 
29
 
 
30
    When communicating with GitHub, some commands need to authenticate to
 
31
    GitHub.
 
32
    """
 
33
 
 
34
    takes_args = ['username?']
 
35
 
 
36
    def run(self, username=None):
 
37
        from github import Github, GithubException
 
38
        from breezy.config import AuthenticationConfig
 
39
        authconfig = AuthenticationConfig()
 
40
        if username is None:
 
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()
 
46
        try:
 
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', [])
 
52
            if errs:
 
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)