/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: 2017-06-10 01:35:53 UTC
  • mto: (6670.4.8 move-bzr)
  • mto: This revision was merged to the branch mainline in revision 6681.
  • Revision ID: jelmer@jelmer.uk-20170610013553-560y7mn3su4pp763
Fix remaining tests.

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
    InvalidURL,
 
32
    NotBranchError,
 
33
    )
 
34
from ...i18n import gettext
 
35
from ...option import (
 
36
    Option,
 
37
    ListOption,
 
38
    )
 
39
 
 
40
 
 
41
class cmd_launchpad_open(Command):
 
42
    __doc__ = """Open a Launchpad branch page in your web browser."""
 
43
 
 
44
    aliases = ['lp-open']
 
45
    takes_options = [
 
46
        Option('dry-run',
 
47
               'Do not actually open the browser. Just say the URL we would '
 
48
               'use.'),
 
49
        ]
 
50
    takes_args = ['location?']
 
51
 
 
52
    def _possible_locations(self, location):
 
53
        """Yield possible external locations for the branch at 'location'."""
 
54
        yield location
 
55
        try:
 
56
            branch = _mod_branch.Branch.open_containing(location)[0]
 
57
        except NotBranchError:
 
58
            return
 
59
        branch_url = branch.get_public_branch()
 
60
        if branch_url is not None:
 
61
            yield branch_url
 
62
        branch_url = branch.get_push_location()
 
63
        if branch_url is not None:
 
64
            yield branch_url
 
65
 
 
66
    def _get_web_url(self, service, location):
 
67
        from .lp_registration import (
 
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
# XXX: cmd_launchpad_mirror is untested
 
144
class cmd_launchpad_mirror(Command):
 
145
    __doc__ = """Ask Launchpad to mirror a branch now."""
 
146
 
 
147
    aliases = ['lp-mirror']
 
148
    takes_args = ['location?']
 
149
 
 
150
    def run(self, location='.'):
 
151
        from . import lp_api
 
152
        from .lp_registration import LaunchpadService
 
153
        branch, _ = _mod_branch.Branch.open_containing(location)
 
154
        service = LaunchpadService()
 
155
        launchpad = lp_api.login(service)
 
156
        lp_branch = lp_api.LaunchpadBranch.from_bzr(launchpad, branch,
 
157
                create_missing=False)
 
158
        lp_branch.lp.requestMirror()
 
159
 
 
160
 
 
161
class cmd_lp_propose_merge(Command):
 
162
    __doc__ = """Propose merging a branch on Launchpad.
 
163
 
 
164
    This will open your usual editor to provide the initial comment.  When it
 
165
    has created the proposal, it will open it in your default web browser.
 
166
 
 
167
    The branch will be proposed to merge into SUBMIT_BRANCH.  If SUBMIT_BRANCH
 
168
    is not supplied, the remembered submit branch will be used.  If no submit
 
169
    branch is remembered, the development focus will be used.
 
170
 
 
171
    By default, the SUBMIT_BRANCH's review team will be requested to review
 
172
    the merge proposal.  This can be overriden by specifying --review (-R).
 
173
    The parameter the launchpad account name of the desired reviewer.  This
 
174
    may optionally be followed by '=' and the review type.  For example:
 
175
 
 
176
      brz lp-propose-merge --review jrandom --review review-team=qa
 
177
 
 
178
    This will propose a merge,  request "jrandom" to perform a review of
 
179
    unspecified type, and request "review-team" to perform a "qa" review.
 
180
    """
 
181
 
 
182
    takes_options = [Option('staging',
 
183
                            help='Propose the merge on staging.'),
 
184
                     Option('message', short_name='m', type=unicode,
 
185
                            help='Commit message.'),
 
186
                     Option('approve',
 
187
                            help=('Mark the proposal as approved immediately, '
 
188
                                  'setting the approved revision to tip.')),
 
189
                     Option('fixes', 'The bug this proposal fixes.', str),
 
190
                     ListOption('review', short_name='R', type=unicode,
 
191
                            help='Requested reviewer and optional type.')]
 
192
 
 
193
    takes_args = ['submit_branch?']
 
194
 
 
195
    aliases = ['lp-submit', 'lp-propose']
 
196
 
 
197
    def run(self, submit_branch=None, review=None, staging=False,
 
198
            message=None, approve=False, fixes=None):
 
199
        from . import lp_propose
 
200
        tree, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch(
 
201
            '.')
 
202
        if review is None:
 
203
            reviews = None
 
204
        else:
 
205
            reviews = []
 
206
            for review in review:
 
207
                if '=' in review:
 
208
                    reviews.append(review.split('=', 2))
 
209
                else:
 
210
                    reviews.append((review, ''))
 
211
            if submit_branch is None:
 
212
                submit_branch = branch.get_submit_branch()
 
213
        if submit_branch is None:
 
214
            target = None
 
215
        else:
 
216
            target = _mod_branch.Branch.open(submit_branch)
 
217
        proposer = lp_propose.Proposer(tree, branch, target, message,
 
218
                                       reviews, staging, approve=approve,
 
219
                                       fixes=fixes)
 
220
        proposer.check_proposal()
 
221
        proposer.create_proposal()
 
222
 
 
223
 
 
224
class cmd_lp_find_proposal(Command):
 
225
 
 
226
    __doc__ = """Find the proposal to merge this revision.
 
227
 
 
228
    Finds the merge proposal(s) that discussed landing the specified revision.
 
229
    This works only if the if the merged_revno was recorded for the merge
 
230
    proposal.  The proposal(s) are opened in a web browser.
 
231
 
 
232
    Only the revision specified is searched for.  To find the mainline
 
233
    revision that merged it into mainline, use the "mainline" revision spec.
 
234
 
 
235
    So, to find the merge proposal that reviewed line 1 of README::
 
236
 
 
237
      brz lp-find-proposal -r mainline:annotate:README:1
 
238
    """
 
239
 
 
240
    takes_options = ['revision']
 
241
 
 
242
    def run(self, revision=None):
 
243
        from ... import ui
 
244
        from . import lp_api
 
245
        import webbrowser
 
246
        b = _mod_branch.Branch.open_containing('.')[0]
 
247
        pb = ui.ui_factory.nested_progress_bar()
 
248
        b.lock_read()
 
249
        try:
 
250
            if revision is None:
 
251
                revision_id = b.last_revision()
 
252
            else:
 
253
                revision_id = revision[0].as_revision_id(b)
 
254
            merged = self._find_proposals(revision_id, pb)
 
255
            if len(merged) == 0:
 
256
                raise BzrCommandError(gettext('No review found.'))
 
257
            trace.note(gettext('%d proposals(s) found.') % len(merged))
 
258
            for mp in merged:
 
259
                webbrowser.open(lp_api.canonical_url(mp))
 
260
        finally:
 
261
            b.unlock()
 
262
            pb.finished()
 
263
 
 
264
    def _find_proposals(self, revision_id, pb):
 
265
        from . import (lp_api, lp_registration)
 
266
        # "devel" because branches.getMergeProposals is not part of 1.0 API.
 
267
        launchpad = lp_api.login(lp_registration.LaunchpadService(),
 
268
                                 version='devel')
 
269
        pb.update(gettext('Finding proposals'))
 
270
        return list(launchpad.branches.getMergeProposals(
 
271
                    merged_revision=revision_id))