/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/launchpad/cmds.py

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2020-08-23 01:15:41 UTC
  • mfrom: (7520.1.4 merge-3.1)
  • Revision ID: breezy.the.bot@gmail.com-20200823011541-nv0oh7nzaganx2qy
Merge lp:brz/3.1.

Merged from https://code.launchpad.net/~jelmer/brz/merge-3.1/+merge/389690

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006-2017 Canonical Ltd
 
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
"""Launchpad plugin commands."""
 
18
 
 
19
from ... import (
 
20
    branch as _mod_branch,
 
21
    controldir,
 
22
    trace,
 
23
    )
 
24
from ...commands import (
 
25
    Command,
 
26
    )
 
27
from ...errors import (
 
28
    CommandError,
 
29
    NotBranchError,
 
30
    )
 
31
from ...i18n import gettext
 
32
from ...option import (
 
33
    Option,
 
34
    ListOption,
 
35
    )
 
36
 
 
37
 
 
38
class cmd_launchpad_open(Command):
 
39
    __doc__ = """Open a Launchpad branch page in your web browser."""
 
40
 
 
41
    aliases = ['lp-open']
 
42
    takes_options = [
 
43
        Option('dry-run',
 
44
               'Do not actually open the browser. Just say the URL we would '
 
45
               'use.'),
 
46
        ]
 
47
    takes_args = ['location?']
 
48
 
 
49
    def _possible_locations(self, location):
 
50
        """Yield possible external locations for the branch at 'location'."""
 
51
        yield location
 
52
        try:
 
53
            branch = _mod_branch.Branch.open_containing(location)[0]
 
54
        except NotBranchError:
 
55
            return
 
56
        branch_url = branch.get_public_branch()
 
57
        if branch_url is not None:
 
58
            yield branch_url
 
59
        branch_url = branch.get_push_location()
 
60
        if branch_url is not None:
 
61
            yield branch_url
 
62
 
 
63
    def _get_web_url(self, service, location):
 
64
        from .lp_registration import (
 
65
            InvalidURL,
 
66
            NotLaunchpadBranch)
 
67
        for branch_url in self._possible_locations(location):
 
68
            try:
 
69
                return service.get_web_url_from_branch_url(branch_url)
 
70
            except (NotLaunchpadBranch, InvalidURL):
 
71
                pass
 
72
        raise NotLaunchpadBranch(branch_url)
 
73
 
 
74
    def run(self, location=None, dry_run=False):
 
75
        from .lp_registration import (
 
76
            LaunchpadService)
 
77
        if location is None:
 
78
            location = u'.'
 
79
        web_url = self._get_web_url(LaunchpadService(), location)
 
80
        trace.note(gettext('Opening %s in web browser') % web_url)
 
81
        if not dry_run:
 
82
            import webbrowser   # this import should not be lazy
 
83
            # otherwise brz.exe lacks this module
 
84
            webbrowser.open(web_url)
 
85
 
 
86
 
 
87
class cmd_launchpad_login(Command):
 
88
    __doc__ = """Show or set the Launchpad user ID.
 
89
 
 
90
    When communicating with Launchpad, some commands need to know your
 
91
    Launchpad user ID.  This command can be used to set or show the
 
92
    user ID that Bazaar will use for such communication.
 
93
 
 
94
    :Examples:
 
95
      Show the Launchpad ID of the current user::
 
96
 
 
97
          brz launchpad-login
 
98
 
 
99
      Set the Launchpad ID of the current user to 'bob'::
 
100
 
 
101
          brz launchpad-login bob
 
102
    """
 
103
    aliases = ['lp-login']
 
104
    takes_args = ['name?']
 
105
    takes_options = [
 
106
        'verbose',
 
107
        Option('no-check',
 
108
               "Don't check that the user name is valid."),
 
109
        ]
 
110
 
 
111
    def run(self, name=None, no_check=False, verbose=False):
 
112
        # This is totally separate from any launchpadlib login system.
 
113
        from . import account
 
114
        check_account = not no_check
 
115
 
 
116
        if name is None:
 
117
            username = account.get_lp_login()
 
118
            if username:
 
119
                if check_account:
 
120
                    account.check_lp_login(username)
 
121
                    if verbose:
 
122
                        self.outf.write(gettext(
 
123
                            "Launchpad user ID exists and has SSH keys.\n"))
 
124
                self.outf.write(username + '\n')
 
125
            else:
 
126
                self.outf.write(gettext('No Launchpad user ID configured.\n'))
 
127
                return 1
 
128
        else:
 
129
            name = name.lower()
 
130
            if check_account:
 
131
                account.check_lp_login(name)
 
132
                if verbose:
 
133
                    self.outf.write(gettext(
 
134
                        "Launchpad user ID exists and has SSH keys.\n"))
 
135
            account.set_lp_login(name)
 
136
            if verbose:
 
137
                self.outf.write(gettext("Launchpad user ID set to '%s'.\n") %
 
138
                                (name,))
 
139
 
 
140
 
 
141
class cmd_launchpad_logout(Command):
 
142
    __doc__ = """Unset the Launchpad user ID.
 
143
 
 
144
    When communicating with Launchpad, some commands need to know your
 
145
    Launchpad user ID.  This command will log you out from Launchpad.
 
146
    This means that communication with Launchpad will happen over
 
147
    HTTPS, and will not require one of your SSH keys to be available.
 
148
    """
 
149
    aliases = ['lp-logout']
 
150
    takes_options = ['verbose']
 
151
 
 
152
    def run(self, verbose=False):
 
153
        from . import account
 
154
        old_username = account.get_lp_login()
 
155
        if old_username is None:
 
156
            self.outf.write(gettext('Not logged into Launchpad.\n'))
 
157
            return 1
 
158
        account.set_lp_login(None)
 
159
        if verbose:
 
160
            self.outf.write(gettext(
 
161
                "Launchpad user ID %s logged out.\n") %
 
162
                old_username)
 
163
 
 
164
 
 
165
class cmd_lp_find_proposal(Command):
 
166
 
 
167
    __doc__ = """Find the proposal to merge this revision.
 
168
 
 
169
    Finds the merge proposal(s) that discussed landing the specified revision.
 
170
    This works only if the if the merged_revno was recorded for the merge
 
171
    proposal.  The proposal(s) are opened in a web browser.
 
172
 
 
173
    Only the revision specified is searched for.  To find the mainline
 
174
    revision that merged it into mainline, use the "mainline" revision spec.
 
175
 
 
176
    So, to find the merge proposal that reviewed line 1 of README::
 
177
 
 
178
      brz lp-find-proposal -r mainline:annotate:README:1
 
179
    """
 
180
 
 
181
    takes_options = ['revision']
 
182
 
 
183
    def run(self, revision=None):
 
184
        from ... import ui
 
185
        from . import lp_api
 
186
        import webbrowser
 
187
        b = _mod_branch.Branch.open_containing('.')[0]
 
188
        with ui.ui_factory.nested_progress_bar() as pb, b.lock_read():
 
189
            if revision is None:
 
190
                revision_id = b.last_revision()
 
191
            else:
 
192
                revision_id = revision[0].as_revision_id(b)
 
193
            merged = self._find_proposals(revision_id, pb)
 
194
            if len(merged) == 0:
 
195
                raise CommandError(gettext('No review found.'))
 
196
            trace.note(gettext('%d proposals(s) found.') % len(merged))
 
197
            for mp in merged:
 
198
                webbrowser.open(lp_api.canonical_url(mp))
 
199
 
 
200
    def _find_proposals(self, revision_id, pb):
 
201
        from launchpadlib import uris
 
202
        from . import lp_api
 
203
        # "devel" because branches.getMergeProposals is not part of 1.0 API.
 
204
        lp_base_url = uris.LPNET_SERVICE_ROOT
 
205
        launchpad = lp_api.connect_launchpad(lp_base_url, version='devel')
 
206
        pb.update(gettext('Finding proposals'))
 
207
        return list(launchpad.branches.getMergeProposals(
 
208
                    merged_revision=revision_id.decode('utf-8')))