/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
6622.7.1 by Colin Watson
Remove `bzr register-branch`, since it has not worked for a long time.
1
# Copyright (C) 2006-2017 Canonical Ltd
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
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
6491.2.2 by Jelmer Vernooij
Use absolute imports in new file.
19
from __future__ import absolute_import
20
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
21
from ... import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
22
    branch as _mod_branch,
23
    controldir,
24
    trace,
25
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
26
from ...commands import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
27
    Command,
28
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
29
from ...errors import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
30
    BzrCommandError,
31
    NotBranchError,
32
    )
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
33
from ...i18n import gettext
34
from ...option import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
35
    Option,
36
    ListOption,
37
    )
6754.5.1 by Jelmer Vernooij
Fix some python3 compatibility issues that break 'make check-nodocs3' for me.
38
from ...sixish import (
39
    text_type,
40
    )
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
41
42
43
class cmd_launchpad_open(Command):
44
    __doc__ = """Open a Launchpad branch page in your web browser."""
45
46
    aliases = ['lp-open']
47
    takes_options = [
48
        Option('dry-run',
49
               'Do not actually open the browser. Just say the URL we would '
50
               'use.'),
51
        ]
52
    takes_args = ['location?']
53
54
    def _possible_locations(self, location):
55
        """Yield possible external locations for the branch at 'location'."""
56
        yield location
57
        try:
58
            branch = _mod_branch.Branch.open_containing(location)[0]
59
        except NotBranchError:
60
            return
61
        branch_url = branch.get_public_branch()
62
        if branch_url is not None:
63
            yield branch_url
64
        branch_url = branch.get_push_location()
65
        if branch_url is not None:
66
            yield branch_url
67
68
    def _get_web_url(self, service, location):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
69
        from .lp_registration import (
6729.6.1 by Jelmer Vernooij
Move urlutils errors.
70
            InvalidURL,
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
71
            NotLaunchpadBranch)
72
        for branch_url in self._possible_locations(location):
73
            try:
74
                return service.get_web_url_from_branch_url(branch_url)
75
            except (NotLaunchpadBranch, InvalidURL):
76
                pass
77
        raise NotLaunchpadBranch(branch_url)
78
79
    def run(self, location=None, dry_run=False):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
80
        from .lp_registration import (
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
81
            LaunchpadService)
82
        if location is None:
83
            location = u'.'
84
        web_url = self._get_web_url(LaunchpadService(), location)
85
        trace.note(gettext('Opening %s in web browser') % web_url)
86
        if not dry_run:
87
            import webbrowser   # this import should not be lazy
7143.15.2 by Jelmer Vernooij
Run autopep8.
88
            # otherwise brz.exe lacks this module
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
89
            webbrowser.open(web_url)
90
91
92
class cmd_launchpad_login(Command):
93
    __doc__ = """Show or set the Launchpad user ID.
94
95
    When communicating with Launchpad, some commands need to know your
96
    Launchpad user ID.  This command can be used to set or show the
97
    user ID that Bazaar will use for such communication.
98
99
    :Examples:
100
      Show the Launchpad ID of the current user::
101
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
102
          brz launchpad-login
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
103
104
      Set the Launchpad ID of the current user to 'bob'::
105
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
106
          brz launchpad-login bob
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
107
    """
108
    aliases = ['lp-login']
109
    takes_args = ['name?']
110
    takes_options = [
111
        'verbose',
112
        Option('no-check',
113
               "Don't check that the user name is valid."),
114
        ]
115
116
    def run(self, name=None, no_check=False, verbose=False):
117
        # This is totally separate from any launchpadlib login system.
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
118
        from . import account
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
119
        check_account = not no_check
120
121
        if name is None:
122
            username = account.get_lp_login()
123
            if username:
124
                if check_account:
125
                    account.check_lp_login(username)
126
                    if verbose:
127
                        self.outf.write(gettext(
128
                            "Launchpad user ID exists and has SSH keys.\n"))
129
                self.outf.write(username + '\n')
130
            else:
131
                self.outf.write(gettext('No Launchpad user ID configured.\n'))
132
                return 1
133
        else:
134
            name = name.lower()
135
            if check_account:
136
                account.check_lp_login(name)
137
                if verbose:
138
                    self.outf.write(gettext(
139
                        "Launchpad user ID exists and has SSH keys.\n"))
140
            account.set_lp_login(name)
141
            if verbose:
142
                self.outf.write(gettext("Launchpad user ID set to '%s'.\n") %
7143.15.2 by Jelmer Vernooij
Run autopep8.
143
                                (name,))
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
144
145
6852.1.1 by Jelmer Vernooij
Add lp-logout command.
146
class cmd_launchpad_logout(Command):
147
    __doc__ = """Unset the Launchpad user ID.
148
149
    When communicating with Launchpad, some commands need to know your
150
    Launchpad user ID.  This command will log you out from Launchpad.
151
    This means that communication with Launchpad will happen over
152
    HTTPS, and will not require one of your SSH keys to be available.
153
    """
154
    aliases = ['lp-logout']
155
    takes_options = ['verbose']
156
157
    def run(self, verbose=False):
158
        from . import account
159
        old_username = account.get_lp_login()
160
        if old_username is None:
161
            self.outf.write(gettext('Not logged into Launchpad.\n'))
162
            return 1
163
        account.set_lp_login(None)
164
        if verbose:
165
            self.outf.write(gettext(
166
                "Launchpad user ID %s logged out.\n") %
167
                old_username)
168
169
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
170
class cmd_lp_propose_merge(Command):
171
    __doc__ = """Propose merging a branch on Launchpad.
172
173
    This will open your usual editor to provide the initial comment.  When it
174
    has created the proposal, it will open it in your default web browser.
175
176
    The branch will be proposed to merge into SUBMIT_BRANCH.  If SUBMIT_BRANCH
177
    is not supplied, the remembered submit branch will be used.  If no submit
178
    branch is remembered, the development focus will be used.
179
180
    By default, the SUBMIT_BRANCH's review team will be requested to review
181
    the merge proposal.  This can be overriden by specifying --review (-R).
182
    The parameter the launchpad account name of the desired reviewer.  This
183
    may optionally be followed by '=' and the review type.  For example:
184
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
185
      brz lp-propose-merge --review jrandom --review review-team=qa
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
186
187
    This will propose a merge,  request "jrandom" to perform a review of
188
    unspecified type, and request "review-team" to perform a "qa" review.
189
    """
190
7211.13.3 by Jelmer Vernooij
Hide the lp-propose command.
191
    hidden = True
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
192
    takes_options = [Option('staging',
193
                            help='Propose the merge on staging.'),
6754.5.1 by Jelmer Vernooij
Fix some python3 compatibility issues that break 'make check-nodocs3' for me.
194
                     Option('message', short_name='m', type=text_type,
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
195
                            help='Commit message.'),
196
                     Option('approve',
6570.1.3 by Jonathan Lange
Document the behaviour
197
                            help=('Mark the proposal as approved immediately, '
198
                                  'setting the approved revision to tip.')),
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
199
                     Option('fixes', 'The bug this proposal fixes.', str),
6754.5.1 by Jelmer Vernooij
Fix some python3 compatibility issues that break 'make check-nodocs3' for me.
200
                     ListOption('review', short_name='R', type=text_type,
7143.15.2 by Jelmer Vernooij
Run autopep8.
201
                                help='Requested reviewer and optional type.')]
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
202
203
    takes_args = ['submit_branch?']
204
205
    aliases = ['lp-submit', 'lp-propose']
206
207
    def run(self, submit_branch=None, review=None, staging=False,
208
            message=None, approve=False, fixes=None):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
209
        from . import lp_propose
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
210
        tree, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch(
211
            '.')
212
        if review is None:
213
            reviews = None
214
        else:
215
            reviews = []
216
            for review in review:
217
                if '=' in review:
218
                    reviews.append(review.split('=', 2))
219
                else:
220
                    reviews.append((review, ''))
221
            if submit_branch is None:
222
                submit_branch = branch.get_submit_branch()
223
        if submit_branch is None:
224
            target = None
225
        else:
226
            target = _mod_branch.Branch.open(submit_branch)
227
        proposer = lp_propose.Proposer(tree, branch, target, message,
228
                                       reviews, staging, approve=approve,
229
                                       fixes=fixes)
230
        proposer.check_proposal()
231
        proposer.create_proposal()
232
233
234
class cmd_lp_find_proposal(Command):
235
236
    __doc__ = """Find the proposal to merge this revision.
237
238
    Finds the merge proposal(s) that discussed landing the specified revision.
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
239
    This works only if the if the merged_revno was recorded for the merge
240
    proposal.  The proposal(s) are opened in a web browser.
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
241
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
242
    Only the revision specified is searched for.  To find the mainline
243
    revision that merged it into mainline, use the "mainline" revision spec.
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
244
245
    So, to find the merge proposal that reviewed line 1 of README::
246
6622.1.28 by Jelmer Vernooij
More renames; commands in output, environment variables.
247
      brz lp-find-proposal -r mainline:annotate:README:1
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
248
    """
249
250
    takes_options = ['revision']
251
252
    def run(self, revision=None):
6624 by Jelmer Vernooij
Merge Python3 porting work ('py3 pokes')
253
        from ... import ui
254
        from . import lp_api
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
255
        import webbrowser
256
        b = _mod_branch.Branch.open_containing('.')[0]
6861.4.1 by Jelmer Vernooij
Make progress bars context managers.
257
        with ui.ui_factory.nested_progress_bar() as pb, b.lock_read():
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
258
            if revision is None:
259
                revision_id = b.last_revision()
260
            else:
261
                revision_id = revision[0].as_revision_id(b)
262
            merged = self._find_proposals(revision_id, pb)
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
263
            if len(merged) == 0:
264
                raise BzrCommandError(gettext('No review found.'))
265
            trace.note(gettext('%d proposals(s) found.') % len(merged))
266
            for mp in merged:
267
                webbrowser.open(lp_api.canonical_url(mp))
268
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
269
    def _find_proposals(self, revision_id, pb):
7240.5.1 by Jelmer Vernooij
Avoid LaunchpadService when connecting to API.
270
        from . import lp_api
6538.2.5 by Aaron Bentley
Updates from review.
271
        # "devel" because branches.getMergeProposals is not part of 1.0 API.
7240.5.1 by Jelmer Vernooij
Avoid LaunchpadService when connecting to API.
272
        launchpad = lp_api.connect_launchpad(version='devel')
6491.2.1 by Jelmer Vernooij
lazily load launchpad plugin commands.
273
        pb.update(gettext('Finding proposals'))
6538.2.2 by Aaron Bentley
Look up merge proposals by exact revision-id.
274
        return list(launchpad.branches.getMergeProposals(
6538.2.3 by Aaron Bentley
Fix default version for login.
275
                    merged_revision=revision_id))