1
# Copyright (C) 2010, 2011 Canonical Ltd
2
# Copyright (C) 2018 Breezy Developers
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
"""Support for Launchpad."""
20
from __future__ import absolute_import
24
from .propose import (
35
branch as _mod_branch,
41
from ...lazy_import import lazy_import
42
lazy_import(globals(), """
43
from breezy.plugins.launchpad import (
48
from ...transport import get_transport
51
# TODO(jelmer): Make selection of launchpad staging a configuration option.
53
MERGE_PROPOSAL_STATUSES = [
59
'Code failed to merge',
65
def plausible_launchpad_url(url):
68
if url.startswith('lp:'):
70
regex = re.compile('([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
71
'://(bazaar|git).*.launchpad.net')
72
return bool(regex.match(url))
75
class WebserviceFailure(Exception):
77
def __init__(self, message):
78
self.message = message
81
def _call_webservice(call, *args, **kwargs):
82
"""Make a call to the webservice, wrapping failures.
84
:param call: The call to make.
85
:param *args: *args for the call.
86
:param **kwargs: **kwargs for the call.
87
:return: The result of calling call(*args, *kwargs).
89
from lazr.restfulclient import errors as restful_errors
91
return call(*args, **kwargs)
92
except restful_errors.HTTPError as e:
94
for line in e.content.splitlines():
95
if line.startswith(b'Traceback (most recent call last):'):
97
error_lines.append(line)
98
raise WebserviceFailure(b''.join(error_lines))
101
class LaunchpadMergeProposal(MergeProposal):
103
def __init__(self, mp):
108
return lp_api.canonical_url(self._mp)
111
return (self._mp.queue_status == 'Merged')
114
class Launchpad(Hoster):
115
"""The Launchpad hosting service."""
117
# https://bugs.launchpad.net/launchpad/+bug/397676
118
supports_merge_proposal_labels = False
120
def __init__(self, staging=False):
121
self._staging = staging
123
lp_instance = 'staging'
125
lp_instance = 'production'
126
self.launchpad = connect_launchpad(lp_instance)
129
return "Launchpad(staging=%s)" % self._staging
131
def hosts(self, branch):
132
# TODO(jelmer): staging vs non-staging?
133
return plausible_launchpad_url(branch.user_url)
136
def probe(cls, branch):
137
if plausible_launchpad_url(branch.user_url):
139
raise UnsupportedHoster(branch)
141
def _get_lp_git_ref_from_branch(self, branch):
142
url, params = urlutils.split_segment_parameters(branch.user_url)
143
(scheme, user, password, host, port, path) = urlutils.parse_url(
145
repo_lp = self.launchpad.git_repositories.getByPath(path=path.strip('/'))
147
ref_path = params['ref']
149
branch_name = params.get('branch', branch.name)
151
ref_path = 'refs/heads/%s' % branch_name
153
ref_path = repo_lp.default_branch
154
ref_lp = repo_lp.getRefByPath(path=ref_path)
155
return (repo_lp, ref_lp)
157
def _get_lp_bzr_branch_from_branch(self, branch):
158
return self.launchpad.branches.getByUrl(url=urlutils.unescape(branch.user_url))
160
def _get_derived_git_path(self, base_path, owner, project):
161
base_repo = self.launchpad.git_repositories.getByPath(path=base_path)
163
project = '/'.join(base_repo.unique_name.split('/')[1:])
164
# TODO(jelmer): Surely there is a better way of creating one of these URLs?
165
return "~%s/%s" % (owner, project)
167
def _publish_git(self, local_branch, base_path, name, owner, project=None,
168
revision_id=None, overwrite=False, allow_lossy=True):
169
to_path = self._get_derived_git_path(base_path, owner, project)
170
to_transport = get_transport("git+ssh://git.launchpad.net/" + to_path)
172
dir_to = controldir.ControlDir.open_from_transport(to_transport)
173
except errors.NotBranchError:
174
# Didn't find anything
179
br_to = local_branch.create_clone_on_transport(
180
to_transport, revision_id=revision_id, name=name,
181
stacked_on=main_branch.user_url)
182
except errors.NoRoundtrippingSupport:
183
br_to = local_branch.create_clone_on_transport(
184
to_transport, revision_id=revision_id, name=name,
185
stacked_on=main_branch.user_url, lossy=True)
188
dir_to = dir_to.push_branch(local_branch, revision_id, overwrite=overwrite, name=name)
189
except errors.NoRoundtrippingSupport:
192
dir_to = dir_to.push_branch(local_branch, revision_id, overwrite=overwrite, name=name, lossy=True)
193
br_to = dir_to.target_branch
194
return br_to, ("https://git.launchpad.net/%s/+ref/%s" % (to_path, name))
196
def _get_derived_bzr_path(self, base_branch, name, owner, project):
198
base_branch_lp = self._get_lp_bzr_branch_from_branch(base_branch)
199
project = '/'.join(base_branch_lp.unique_name.split('/')[1:-1])
200
# TODO(jelmer): Surely there is a better way of creating one of these URLs?
201
return "~%s/%s/%s" % (owner, project, name)
203
def get_push_url(self, branch):
204
(vcs, user, password, path, params) = self._split_url(branch.user_url)
206
branch_lp = self._get_lp_bzr_branch_from_branch(branch)
207
return branch_lp.bzr_identity
209
return urlutils.join_segment_parameters(
210
"git+ssh://git.launchpad.net/" + path, params)
214
def _publish_bzr(self, local_branch, base_branch, name, owner, project=None,
215
revision_id=None, overwrite=False, allow_lossy=True):
216
to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
217
to_transport = get_transport("lp:" + to_path)
219
dir_to = controldir.ControlDir.open_from_transport(to_transport)
220
except errors.NotBranchError:
221
# Didn't find anything
225
br_to = local_branch.create_clone_on_transport(to_transport, revision_id=revision_id)
227
br_to = dir_to.push_branch(local_branch, revision_id, overwrite=overwrite).target_branch
228
return br_to, ("https://code.launchpad.net/" + to_path)
230
def _split_url(self, url):
231
url, params = urlutils.split_segment_parameters(url)
232
(scheme, user, password, host, port, path) = urlutils.parse_url(url)
233
path = path.strip('/')
234
if host.startswith('bazaar.'):
236
elif host.startswith('git.'):
239
raise ValueError("unknown host %s" % host)
240
return (vcs, user, password, path, params)
242
def publish_derived(self, local_branch, base_branch, name, project=None, owner=None,
243
revision_id=None, overwrite=False, allow_lossy=True):
244
"""Publish a branch to the site, derived from base_branch.
246
:param base_branch: branch to derive the new branch from
247
:param new_branch: branch to publish
248
:param name: Name of the new branch on the remote host
249
:param project: Optional project name
250
:param owner: Optional owner
251
:return: resulting branch
254
owner = self.launchpad.me.name
255
(base_vcs, base_user, base_password, base_path, base_params) = self._split_url(
256
base_branch.user_url)
257
# TODO(jelmer): Prevent publishing to development focus
258
if base_vcs == 'bzr':
259
return self._publish_bzr(
260
local_branch, base_branch, name, project=project, owner=owner,
261
revision_id=revision_id, overwrite=overwrite,
262
allow_lossy=allow_lossy)
263
elif base_vcs == 'git':
264
return self._publish_git(
265
local_branch, base_path, name, project=project, owner=owner,
266
revision_id=revision_id, overwrite=overwrite,
267
allow_lossy=allow_lossy)
269
raise AssertionError('not a valid Launchpad URL')
271
def get_derived_branch(self, base_branch, name, project=None, owner=None):
273
owner = self.launchpad.me.name
274
(base_vcs, base_user, base_password, base_path, base_params) = self._split_url(
275
base_branch.user_url)
276
if base_vcs == 'bzr':
277
to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
278
return _mod_branch.Branch.open("lp:" + to_path)
279
elif base_vcs == 'git':
280
to_path = self._get_derived_git_path(
281
base_path.strip('/'), owner, project)
282
return _mod_branch.Branch.open(
283
"git+ssh://git.launchpad.net/" + to_path, name)
285
raise AssertionError('not a valid Launchpad URL')
287
def get_proposal(self, source_branch, target_branch):
288
(base_vcs, base_user, base_password, base_path, base_params) = (
289
self._split_url(target_branch.user_url))
290
if base_vcs == 'bzr':
291
target_branch_lp = self.launchpad.branches.getByUrl(
292
url=target_branch.user_url)
293
source_branch_lp = self.launchpad.branches.getByUrl(
294
url=source_branch.user_url)
295
for mp in target_branch_lp.getMergeProposals(
296
status=MERGE_PROPOSAL_STATUSES):
297
if mp.target_branch != target_branch_lp:
299
if mp.source_branch != source_branch_lp:
301
return LaunchpadMergeProposal(mp)
302
raise NoMergeProposal()
303
elif base_vcs == 'git':
304
(source_repo_lp, source_branch_lp) = (
305
self.lp_host._get_lp_git_ref_from_branch(source_branch))
306
(target_repo_lp, target_branch_lp) = (
307
self.lp_host._get_lp_git_ref_from_branch(target_branch))
308
for mp in target_branch_lp.getMergeProposals(
309
status=MERGE_PROPOSAL_STATUSES):
310
if (target_branch_lp.path != mp.target_git_path or
311
target_repo_lp != mp.target_git_repository or
312
source_branch_lp.path != mp.source_git_path or
313
source_repo_lp != mp.source_git_repository):
315
return LaunchpadMergeProposal(mp)
316
raise NoMergeProposal()
318
raise AssertionError('not a valid Launchpad URL')
320
def get_proposer(self, source_branch, target_branch):
321
(base_vcs, base_user, base_password, base_path, base_params) = (
322
self._split_url(target_branch.user_url))
323
if base_vcs == 'bzr':
324
return LaunchpadBazaarMergeProposalBuilder(
325
self, source_branch, target_branch)
326
elif base_vcs == 'git':
327
return LaunchpadGitMergeProposalBuilder(
328
self, source_branch, target_branch)
330
raise AssertionError('not a valid Launchpad URL')
333
def connect_launchpad(lp_instance='production'):
334
service = lp_registration.LaunchpadService(lp_instance=lp_instance)
335
return lp_api.login(service, version='devel')
338
class LaunchpadBazaarMergeProposalBuilder(MergeProposalBuilder):
340
def __init__(self, lp_host, source_branch, target_branch, message=None,
341
staging=None, approve=None, fixes=None):
344
:param source_branch: The branch to propose for merging.
345
:param target_branch: The branch to merge into.
346
:param message: The commit message to use. (May be None.)
347
:param staging: If True, propose the merge against staging instead of
349
:param approve: If True, mark the new proposal as approved immediately.
350
This is useful when a project permits some things to be approved
351
by the submitter (e.g. merges between release and deployment
354
self.lp_host = lp_host
355
self.launchpad = lp_host.launchpad
356
self.source_branch = source_branch
357
self.source_branch_lp = self.launchpad.branches.getByUrl(url=source_branch.user_url)
358
if target_branch is None:
359
self.target_branch_lp = self.source_branch_lp.get_target()
360
self.target_branch = _mod_branch.Branch.open(self.target_branch_lp.bzr_identity)
362
self.target_branch = target_branch
363
self.target_branch_lp = self.launchpad.branches.getByUrl(url=target_branch.user_url)
364
self.commit_message = message
365
self.approve = approve
368
def get_infotext(self):
369
"""Determine the initial comment for the merge proposal."""
370
if self.commit_message is not None:
371
return self.commit_message.strip().encode('utf-8')
372
info = ["Source: %s\n" % self.source_branch_lp.bzr_identity]
373
info.append("Target: %s\n" % self.target_branch_lp.bzr_identity)
376
def get_initial_body(self):
377
"""Get a body for the proposal for the user to modify.
379
:return: a str or None.
381
if not self.hooks['merge_proposal_body']:
384
def list_modified_files():
385
lca_tree = self.source_branch_lp.find_lca_tree(
386
self.target_branch_lp)
387
source_tree = self.source_branch.basis_tree()
388
files = modified_files(lca_tree, source_tree)
390
with self.target_branch.lock_read(), \
391
self.source_branch.lock_read():
393
for hook in self.hooks['merge_proposal_body']:
395
'target_branch': self.target_branch_lp.bzr_identity,
396
'modified_files_callback': list_modified_files,
401
def check_proposal(self):
402
"""Check that the submission is sensible."""
403
if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
404
raise errors.BzrCommandError(
405
'Source and target branches must be different.')
406
for mp in self.source_branch_lp.landing_targets:
407
if mp.queue_status in ('Merged', 'Rejected'):
409
if mp.target_branch.self_link == self.target_branch_lp.self_link:
410
raise MergeProposalExists(lp_api.canonical_url(mp))
412
def approve_proposal(self, mp):
413
with self.source_branch.lock_read():
417
subject='', # Use the default subject.
418
content=u"Rubberstamp! Proposer approves of own proposal.")
419
_call_webservice(mp.setStatus, status=u'Approved',
420
revid=self.source_branch.last_revision())
422
def create_proposal(self, description, reviewers=None, labels=None,
423
prerequisite_branch=None):
424
"""Perform the submission."""
426
raise LabelsUnsupported()
427
if prerequisite_branch is not None:
428
prereq = self.launchpad.branches.getByUrl(
429
url=prerequisite_branch.user_url)
432
if reviewers is None:
435
mp = _call_webservice(
436
self.source_branch_lp.createMergeProposal,
437
target_branch=self.target_branch_lp,
438
prerequisite_branch=prereq,
439
initial_comment=description.strip(),
440
commit_message=self.commit_message,
441
reviewers=[self.launchpad.people[reviewer].self_link
442
for reviewer in reviewers],
443
review_types=[None for reviewer in reviewers])
444
except WebserviceFailure as e:
446
if (b'There is already a branch merge proposal '
447
b'registered for branch ') in e.message:
448
raise MergeProposalExists(self.source_branch.user_url)
452
self.approve_proposal(mp)
454
if self.fixes.startswith('lp:'):
455
self.fixes = self.fixes[3:]
458
bug=self.launchpad.bugs[int(self.fixes)])
459
return LaunchpadMergeProposal(mp)
462
class LaunchpadGitMergeProposalBuilder(MergeProposalBuilder):
464
def __init__(self, lp_host, source_branch, target_branch, message=None,
465
staging=None, approve=None, fixes=None):
468
:param source_branch: The branch to propose for merging.
469
:param target_branch: The branch to merge into.
470
:param message: The commit message to use. (May be None.)
471
:param staging: If True, propose the merge against staging instead of
473
:param approve: If True, mark the new proposal as approved immediately.
474
This is useful when a project permits some things to be approved
475
by the submitter (e.g. merges between release and deployment
478
self.lp_host = lp_host
479
self.launchpad = lp_host.launchpad
480
self.source_branch = source_branch
481
(self.source_repo_lp, self.source_branch_lp) = self.lp_host._get_lp_git_ref_from_branch(source_branch)
482
if target_branch is None:
483
self.target_branch_lp = self.source_branch.get_target()
484
self.target_branch = _mod_branch.Branch.open(self.target_branch_lp.git_https_url)
486
self.target_branch = target_branch
487
(self.target_repo_lp, self.target_branch_lp) = self.lp_host._get_lp_git_ref_from_branch(target_branch)
488
self.commit_message = message
489
self.approve = approve
492
def get_infotext(self):
493
"""Determine the initial comment for the merge proposal."""
494
if self.commit_message is not None:
495
return self.commit_message.strip().encode('utf-8')
496
info = ["Source: %s\n" % self.source_branch.user_url]
497
info.append("Target: %s\n" % self.target_branch.user_url)
500
def get_initial_body(self):
501
"""Get a body for the proposal for the user to modify.
503
:return: a str or None.
505
if not self.hooks['merge_proposal_body']:
508
def list_modified_files():
509
lca_tree = self.source_branch_lp.find_lca_tree(
510
self.target_branch_lp)
511
source_tree = self.source_branch.basis_tree()
512
files = modified_files(lca_tree, source_tree)
514
with self.target_branch.lock_read(), \
515
self.source_branch.lock_read():
517
for hook in self.hooks['merge_proposal_body']:
519
'target_branch': self.target_branch,
520
'modified_files_callback': list_modified_files,
525
def check_proposal(self):
526
"""Check that the submission is sensible."""
527
if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
528
raise errors.BzrCommandError(
529
'Source and target branches must be different.')
530
for mp in self.source_branch_lp.landing_targets:
531
if mp.queue_status in ('Merged', 'Rejected'):
533
if mp.target_branch.self_link == self.target_branch_lp.self_link:
534
raise MergeProposalExists(lp_api.canonical_url(mp))
536
def approve_proposal(self, mp):
537
with self.source_branch.lock_read():
541
subject='', # Use the default subject.
542
content=u"Rubberstamp! Proposer approves of own proposal.")
544
mp.setStatus, status=u'Approved',
545
revid=self.source_branch.last_revision())
547
def create_proposal(self, description, reviewers=None, labels=None,
548
prerequisite_branch=None):
549
"""Perform the submission."""
551
raise LabelsUnsupported()
552
if prerequisite_branch is not None:
553
(prereq_repo_lp, prereq_branch_lp) = self.lp_host._get_lp_git_ref_from_branch(prerequisite_branch)
555
prereq_branch_lp = None
556
if reviewers is None:
559
mp = _call_webservice(
560
self.source_branch_lp.createMergeProposal,
561
merge_target=self.target_branch_lp,
562
merge_prerequisite=prereq_branch_lp,
563
initial_comment=description.strip().encode('utf-8'),
564
commit_message=self.commit_message,
566
reviewers=[self.launchpad.people[reviewer].self_link
567
for reviewer in reviewers],
568
review_types=[None for reviewer in reviewers])
569
except WebserviceFailure as e:
571
if ('There is already a branch merge proposal '
572
'registered for branch ') in e.message:
573
raise MergeProposalExists(self.source_branch.user_url)
576
self.approve_proposal(mp)
578
if self.fixes.startswith('lp:'):
579
self.fixes = self.fixes[3:]
582
bug=self.launchpad.bugs[int(self.fixes)])
583
return LaunchpadMergeProposal(mp)
586
def modified_files(old_tree, new_tree):
587
"""Return a list of paths in the new tree with modified contents."""
588
for f, (op, path), c, v, p, n, (ok, k), e in new_tree.iter_changes(
590
if c and k == 'file':