/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: Jelmer Vernooij
  • Date: 2020-02-07 02:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 7492.
  • Revision ID: jelmer@jelmer.uk-20200207021430-m49iq3x4x8xlib6x
Drop python2 support.

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