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."""
20
branch as _mod_branch,
24
from ...commands import (
27
from ...errors import (
31
from ...i18n import gettext
32
from ...option import (
38
class cmd_launchpad_open(Command):
39
__doc__ = """Open a Launchpad branch page in your web browser."""
44
'Do not actually open the browser. Just say the URL we would '
47
takes_args = ['location?']
49
def _possible_locations(self, location):
50
"""Yield possible external locations for the branch at 'location'."""
53
branch = _mod_branch.Branch.open_containing(location)[0]
54
except NotBranchError:
56
branch_url = branch.get_public_branch()
57
if branch_url is not None:
59
branch_url = branch.get_push_location()
60
if branch_url is not None:
63
def _get_web_url(self, service, location):
64
from .lp_registration import (
67
for branch_url in self._possible_locations(location):
69
return service.get_web_url_from_branch_url(branch_url)
70
except (NotLaunchpadBranch, InvalidURL):
72
raise NotLaunchpadBranch(branch_url)
74
def run(self, location=None, dry_run=False):
75
from .lp_registration import (
79
web_url = self._get_web_url(LaunchpadService(), location)
80
trace.note(gettext('Opening %s in web browser') % web_url)
82
import webbrowser # this import should not be lazy
83
# otherwise brz.exe lacks this module
84
webbrowser.open(web_url)
87
class cmd_launchpad_login(Command):
88
__doc__ = """Show or set the Launchpad user ID.
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.
95
Show the Launchpad ID of the current user::
99
Set the Launchpad ID of the current user to 'bob'::
101
brz launchpad-login bob
103
aliases = ['lp-login']
104
takes_args = ['name?']
108
"Don't check that the user name is valid."),
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
117
username = account.get_lp_login()
120
account.check_lp_login(username)
122
self.outf.write(gettext(
123
"Launchpad user ID exists and has SSH keys.\n"))
124
self.outf.write(username + '\n')
126
self.outf.write(gettext('No Launchpad user ID configured.\n'))
131
account.check_lp_login(name)
133
self.outf.write(gettext(
134
"Launchpad user ID exists and has SSH keys.\n"))
135
account.set_lp_login(name)
137
self.outf.write(gettext("Launchpad user ID set to '%s'.\n") %
141
class cmd_launchpad_logout(Command):
142
__doc__ = """Unset the Launchpad user ID.
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.
149
aliases = ['lp-logout']
150
takes_options = ['verbose']
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'))
158
account.set_lp_login(None)
160
self.outf.write(gettext(
161
"Launchpad user ID %s logged out.\n") %
165
class cmd_lp_find_proposal(Command):
167
__doc__ = """Find the proposal to merge this revision.
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.
173
Only the revision specified is searched for. To find the mainline
174
revision that merged it into mainline, use the "mainline" revision spec.
176
So, to find the merge proposal that reviewed line 1 of README::
178
brz lp-find-proposal -r mainline:annotate:README:1
181
takes_options = ['revision']
183
def run(self, revision=None):
187
b = _mod_branch.Branch.open_containing('.')[0]
188
with ui.ui_factory.nested_progress_bar() as pb, b.lock_read():
190
revision_id = b.last_revision()
192
revision_id = revision[0].as_revision_id(b)
193
merged = self._find_proposals(revision_id, pb)
195
raise CommandError(gettext('No review found.'))
196
trace.note(gettext('%d proposals(s) found.') % len(merged))
198
webbrowser.open(lp_api.canonical_url(mp))
200
def _find_proposals(self, revision_id, pb):
201
from launchpadlib import uris
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')))