1
# Copyright (C) 2006-2017 Canonical Ltd
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.
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.
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
17
"""Launchpad plugin commands."""
19
from __future__ import absolute_import
22
branch as _mod_branch,
26
from ...commands import (
29
from ...errors import (
33
from ...i18n import gettext
34
from ...option import (
38
from ...sixish import (
43
class cmd_launchpad_open(Command):
44
__doc__ = """Open a Launchpad branch page in your web browser."""
49
'Do not actually open the browser. Just say the URL we would '
52
takes_args = ['location?']
54
def _possible_locations(self, location):
55
"""Yield possible external locations for the branch at 'location'."""
58
branch = _mod_branch.Branch.open_containing(location)[0]
59
except NotBranchError:
61
branch_url = branch.get_public_branch()
62
if branch_url is not None:
64
branch_url = branch.get_push_location()
65
if branch_url is not None:
68
def _get_web_url(self, service, location):
69
from .lp_registration import (
72
for branch_url in self._possible_locations(location):
74
return service.get_web_url_from_branch_url(branch_url)
75
except (NotLaunchpadBranch, InvalidURL):
77
raise NotLaunchpadBranch(branch_url)
79
def run(self, location=None, dry_run=False):
80
from .lp_registration import (
84
web_url = self._get_web_url(LaunchpadService(), location)
85
trace.note(gettext('Opening %s in web browser') % web_url)
87
import webbrowser # this import should not be lazy
88
# otherwise brz.exe lacks this module
89
webbrowser.open(web_url)
92
class cmd_launchpad_login(Command):
93
__doc__ = """Show or set the Launchpad user ID.
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.
100
Show the Launchpad ID of the current user::
104
Set the Launchpad ID of the current user to 'bob'::
106
brz launchpad-login bob
108
aliases = ['lp-login']
109
takes_args = ['name?']
113
"Don't check that the user name is valid."),
116
def run(self, name=None, no_check=False, verbose=False):
117
# This is totally separate from any launchpadlib login system.
118
from . import account
119
check_account = not no_check
122
username = account.get_lp_login()
125
account.check_lp_login(username)
127
self.outf.write(gettext(
128
"Launchpad user ID exists and has SSH keys.\n"))
129
self.outf.write(username + '\n')
131
self.outf.write(gettext('No Launchpad user ID configured.\n'))
136
account.check_lp_login(name)
138
self.outf.write(gettext(
139
"Launchpad user ID exists and has SSH keys.\n"))
140
account.set_lp_login(name)
142
self.outf.write(gettext("Launchpad user ID set to '%s'.\n") %
146
# XXX: cmd_launchpad_mirror is untested
147
class cmd_launchpad_mirror(Command):
148
__doc__ = """Ask Launchpad to mirror a branch now."""
150
aliases = ['lp-mirror']
151
takes_args = ['location?']
153
def run(self, location='.'):
155
from .lp_registration import LaunchpadService
156
branch, _ = _mod_branch.Branch.open_containing(location)
157
service = LaunchpadService()
158
launchpad = lp_api.login(service)
159
lp_branch = lp_api.LaunchpadBranch.from_bzr(launchpad, branch,
160
create_missing=False)
161
lp_branch.lp.requestMirror()
164
class cmd_lp_propose_merge(Command):
165
__doc__ = """Propose merging a branch on Launchpad.
167
This will open your usual editor to provide the initial comment. When it
168
has created the proposal, it will open it in your default web browser.
170
The branch will be proposed to merge into SUBMIT_BRANCH. If SUBMIT_BRANCH
171
is not supplied, the remembered submit branch will be used. If no submit
172
branch is remembered, the development focus will be used.
174
By default, the SUBMIT_BRANCH's review team will be requested to review
175
the merge proposal. This can be overriden by specifying --review (-R).
176
The parameter the launchpad account name of the desired reviewer. This
177
may optionally be followed by '=' and the review type. For example:
179
brz lp-propose-merge --review jrandom --review review-team=qa
181
This will propose a merge, request "jrandom" to perform a review of
182
unspecified type, and request "review-team" to perform a "qa" review.
185
takes_options = [Option('staging',
186
help='Propose the merge on staging.'),
187
Option('message', short_name='m', type=text_type,
188
help='Commit message.'),
190
help=('Mark the proposal as approved immediately, '
191
'setting the approved revision to tip.')),
192
Option('fixes', 'The bug this proposal fixes.', str),
193
ListOption('review', short_name='R', type=text_type,
194
help='Requested reviewer and optional type.')]
196
takes_args = ['submit_branch?']
198
aliases = ['lp-submit', 'lp-propose']
200
def run(self, submit_branch=None, review=None, staging=False,
201
message=None, approve=False, fixes=None):
202
from . import lp_propose
203
tree, branch, relpath = controldir.ControlDir.open_containing_tree_or_branch(
209
for review in review:
211
reviews.append(review.split('=', 2))
213
reviews.append((review, ''))
214
if submit_branch is None:
215
submit_branch = branch.get_submit_branch()
216
if submit_branch is None:
219
target = _mod_branch.Branch.open(submit_branch)
220
proposer = lp_propose.Proposer(tree, branch, target, message,
221
reviews, staging, approve=approve,
223
proposer.check_proposal()
224
proposer.create_proposal()
227
class cmd_lp_find_proposal(Command):
229
__doc__ = """Find the proposal to merge this revision.
231
Finds the merge proposal(s) that discussed landing the specified revision.
232
This works only if the if the merged_revno was recorded for the merge
233
proposal. The proposal(s) are opened in a web browser.
235
Only the revision specified is searched for. To find the mainline
236
revision that merged it into mainline, use the "mainline" revision spec.
238
So, to find the merge proposal that reviewed line 1 of README::
240
brz lp-find-proposal -r mainline:annotate:README:1
243
takes_options = ['revision']
245
def run(self, revision=None):
249
b = _mod_branch.Branch.open_containing('.')[0]
250
pb = ui.ui_factory.nested_progress_bar()
254
revision_id = b.last_revision()
256
revision_id = revision[0].as_revision_id(b)
257
merged = self._find_proposals(revision_id, pb)
259
raise BzrCommandError(gettext('No review found.'))
260
trace.note(gettext('%d proposals(s) found.') % len(merged))
262
webbrowser.open(lp_api.canonical_url(mp))
267
def _find_proposals(self, revision_id, pb):
268
from . import (lp_api, lp_registration)
269
# "devel" because branches.getMergeProposals is not part of 1.0 API.
270
launchpad = lp_api.login(lp_registration.LaunchpadService(),
272
pb.update(gettext('Finding proposals'))
273
return list(launchpad.branches.getMergeProposals(
274
merged_revision=revision_id))