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."""
24
from ...propose import (
34
branch as _mod_branch,
40
from ...git.urls import git_url_to_bzr_url
41
from ...lazy_import import lazy_import
42
lazy_import(globals(), """
43
from breezy.plugins.launchpad import (
48
from launchpadlib import uris
50
from ...transport import get_transport
53
# TODO(jelmer): Make selection of launchpad staging a configuration option.
55
def status_to_lp_mp_statuses(status):
57
if status in ('open', 'all'):
62
'Code failed to merge',
64
if status in ('closed', 'all'):
65
statuses.extend(['Rejected', 'Superseded'])
66
if status in ('merged', 'all'):
67
statuses.append('Merged')
71
def plausible_launchpad_url(url):
74
if url.startswith('lp:'):
76
regex = re.compile(r'([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
77
r'://(bazaar|git).*\.launchpad\.net')
78
return bool(regex.match(url))
81
class WebserviceFailure(Exception):
83
def __init__(self, message):
84
self.message = message
87
def _call_webservice(call, *args, **kwargs):
88
"""Make a call to the webservice, wrapping failures.
90
:param call: The call to make.
91
:param *args: *args for the call.
92
:param **kwargs: **kwargs for the call.
93
:return: The result of calling call(*args, *kwargs).
95
from lazr.restfulclient import errors as restful_errors
97
return call(*args, **kwargs)
98
except restful_errors.HTTPError as e:
100
for line in e.content.splitlines():
101
if line.startswith(b'Traceback (most recent call last):'):
103
error_lines.append(line)
104
raise WebserviceFailure(b''.join(error_lines))
107
class LaunchpadMergeProposal(MergeProposal):
109
def __init__(self, mp):
112
def get_source_branch_url(self):
113
if self._mp.source_branch:
114
return self._mp.source_branch.bzr_identity
116
return git_url_to_bzr_url(
117
self._mp.source_git_repository.git_identity,
118
ref=self._mp.source_git_path.encode('utf-8'))
120
def get_target_branch_url(self):
121
if self._mp.target_branch:
122
return self._mp.target_branch.bzr_identity
124
return git_url_to_bzr_url(
125
self._mp.target_git_repository.git_identity,
126
ref=self._mp.target_git_path.encode('utf-8'))
130
return lp_api.canonical_url(self._mp)
133
return (self._mp.queue_status == 'Merged')
136
return (self._mp.queue_status in ('Rejected', 'Superseded'))
139
self._mp.setStatus(status='Needs review')
141
def get_description(self):
142
return self._mp.description
144
def set_description(self, description):
145
self._mp.description = description
148
def get_commit_message(self):
149
return self._mp.commit_message
151
def set_commit_message(self, commit_message):
152
self._mp.commit_message = commit_message
156
self._mp.setStatus(status='Rejected')
158
def can_be_merged(self):
159
if not self._mp.preview_diff:
162
return not bool(self._mp.preview_diff.conflicts)
164
def get_merged_by(self):
165
merge_reporter = self._mp.merge_reporter
166
if merge_reporter is None:
168
return merge_reporter.name
170
def get_merged_at(self):
171
return self._mp.date_merged
173
def merge(self, commit_message=None):
174
target_branch = _mod_branch.Branch.open(
175
self.get_target_branch_url())
176
source_branch = _mod_branch.Branch.open(
177
self.get_source_branch_url())
178
# TODO(jelmer): Ideally this would use a memorytree, but merge doesn't
180
# tree = target_branch.create_memorytree()
181
tmpdir = tempfile.mkdtemp()
183
tree = target_branch.create_checkout(
184
to_location=tmpdir, lightweight=True)
185
tree.merge_from_branch(source_branch)
186
tree.commit(commit_message or self._mp.commit_message)
188
shutil.rmtree(tmpdir)
191
class Launchpad(Hoster):
192
"""The Launchpad hosting service."""
196
# https://bugs.launchpad.net/launchpad/+bug/397676
197
supports_merge_proposal_labels = False
199
supports_merge_proposal_commit_message = True
201
merge_proposal_description_format = 'plain'
203
def __init__(self, staging=False):
204
self._staging = staging
206
lp_base_url = uris.STAGING_SERVICE_ROOT
208
lp_base_url = uris.LPNET_SERVICE_ROOT
209
self.launchpad = lp_api.connect_launchpad(lp_base_url, version='devel')
213
return lp_api.uris.web_root_for_service_root(
214
str(self.launchpad._root_uri))
217
return "Launchpad(staging=%s)" % self._staging
219
def hosts(self, branch):
220
# TODO(jelmer): staging vs non-staging?
221
return plausible_launchpad_url(branch.user_url)
224
def probe_from_url(cls, url, possible_transports=None):
225
if plausible_launchpad_url(url):
227
raise UnsupportedHoster(url)
229
def _get_lp_git_ref_from_branch(self, branch):
230
url, params = urlutils.split_segment_parameters(branch.user_url)
231
(scheme, user, password, host, port, path) = urlutils.parse_url(
233
repo_lp = self.launchpad.git_repositories.getByPath(
234
path=path.strip('/'))
236
ref_path = params['ref']
238
branch_name = params.get('branch', branch.name)
240
ref_path = 'refs/heads/%s' % branch_name
242
ref_path = repo_lp.default_branch
243
ref_lp = repo_lp.getRefByPath(path=ref_path)
244
return (repo_lp, ref_lp)
246
def _get_lp_bzr_branch_from_branch(self, branch):
247
return self.launchpad.branches.getByUrl(
248
url=urlutils.unescape(branch.user_url))
250
def _get_derived_git_path(self, base_path, owner, project):
251
base_repo = self.launchpad.git_repositories.getByPath(path=base_path)
253
project = urlutils.parse_url(base_repo.git_ssh_url)[-1].strip('/')
254
if project.startswith('~'):
255
project = '/'.join(base_path.split('/')[1:])
256
# TODO(jelmer): Surely there is a better way of creating one of these
258
return "~%s/%s" % (owner, project)
260
def _publish_git(self, local_branch, base_path, name, owner, project=None,
261
revision_id=None, overwrite=False, allow_lossy=True):
262
to_path = self._get_derived_git_path(base_path, owner, project)
263
to_transport = get_transport("git+ssh://git.launchpad.net/" + to_path)
265
dir_to = controldir.ControlDir.open_from_transport(to_transport)
266
except errors.NotBranchError:
267
# Didn't find anything
272
br_to = local_branch.create_clone_on_transport(
273
to_transport, revision_id=revision_id, name=name)
274
except errors.NoRoundtrippingSupport:
275
br_to = local_branch.create_clone_on_transport(
276
to_transport, revision_id=revision_id, name=name,
280
dir_to = dir_to.push_branch(
281
local_branch, revision_id, overwrite=overwrite, name=name)
282
except errors.NoRoundtrippingSupport:
285
dir_to = dir_to.push_branch(
286
local_branch, revision_id, overwrite=overwrite, name=name,
288
br_to = dir_to.target_branch
290
"https://git.launchpad.net/%s/+ref/%s" % (to_path, name))
292
def _get_derived_bzr_path(self, base_branch, name, owner, project):
294
base_branch_lp = self._get_lp_bzr_branch_from_branch(base_branch)
295
project = '/'.join(base_branch_lp.unique_name.split('/')[1:-1])
296
# TODO(jelmer): Surely there is a better way of creating one of these
298
return "~%s/%s/%s" % (owner, project, name)
300
def get_push_url(self, branch):
301
(vcs, user, password, path, params) = self._split_url(branch.user_url)
303
branch_lp = self._get_lp_bzr_branch_from_branch(branch)
304
return branch_lp.bzr_identity
306
return urlutils.join_segment_parameters(
307
"git+ssh://git.launchpad.net/" + path, params)
311
def _publish_bzr(self, local_branch, base_branch, name, owner,
312
project=None, revision_id=None, overwrite=False,
314
to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
315
to_transport = get_transport("lp:" + to_path)
317
dir_to = controldir.ControlDir.open_from_transport(to_transport)
318
except errors.NotBranchError:
319
# Didn't find anything
323
br_to = local_branch.create_clone_on_transport(
324
to_transport, revision_id=revision_id)
326
br_to = dir_to.push_branch(
327
local_branch, revision_id, overwrite=overwrite).target_branch
328
return br_to, ("https://code.launchpad.net/" + to_path)
330
def _split_url(self, url):
331
url, params = urlutils.split_segment_parameters(url)
332
(scheme, user, password, host, port, path) = urlutils.parse_url(url)
333
path = path.strip('/')
334
if host.startswith('bazaar.'):
336
elif host.startswith('git.'):
339
raise ValueError("unknown host %s" % host)
340
return (vcs, user, password, path, params)
342
def publish_derived(self, local_branch, base_branch, name, project=None,
343
owner=None, revision_id=None, overwrite=False,
345
"""Publish a branch to the site, derived from base_branch.
347
:param base_branch: branch to derive the new branch from
348
:param new_branch: branch to publish
349
:param name: Name of the new branch on the remote host
350
:param project: Optional project name
351
:param owner: Optional owner
352
:return: resulting branch
355
owner = self.launchpad.me.name
356
(base_vcs, base_user, base_password, base_path,
357
base_params) = self._split_url(base_branch.user_url)
358
# TODO(jelmer): Prevent publishing to development focus
359
if base_vcs == 'bzr':
360
return self._publish_bzr(
361
local_branch, base_branch, name, project=project, owner=owner,
362
revision_id=revision_id, overwrite=overwrite,
363
allow_lossy=allow_lossy)
364
elif base_vcs == 'git':
365
return self._publish_git(
366
local_branch, base_path, name, project=project, owner=owner,
367
revision_id=revision_id, overwrite=overwrite,
368
allow_lossy=allow_lossy)
370
raise AssertionError('not a valid Launchpad URL')
372
def get_derived_branch(self, base_branch, name, project=None, owner=None):
374
owner = self.launchpad.me.name
375
(base_vcs, base_user, base_password, base_path,
376
base_params) = self._split_url(base_branch.user_url)
377
if base_vcs == 'bzr':
378
to_path = self._get_derived_bzr_path(
379
base_branch, name, owner, project)
380
return _mod_branch.Branch.open("lp:" + to_path)
381
elif base_vcs == 'git':
382
to_path = self._get_derived_git_path(
383
base_path.strip('/'), owner, project)
384
to_url = urlutils.join_segment_parameters(
385
"git+ssh://git.launchpad.net/" + to_path,
387
return _mod_branch.Branch.open(to_url)
389
raise AssertionError('not a valid Launchpad URL')
391
def iter_proposals(self, source_branch, target_branch, status='open'):
392
(base_vcs, base_user, base_password, base_path,
393
base_params) = self._split_url(target_branch.user_url)
394
statuses = status_to_lp_mp_statuses(status)
395
if base_vcs == 'bzr':
396
target_branch_lp = self.launchpad.branches.getByUrl(
397
url=target_branch.user_url)
398
source_branch_lp = self.launchpad.branches.getByUrl(
399
url=source_branch.user_url)
400
for mp in target_branch_lp.getMergeProposals(status=statuses):
401
if mp.source_branch_link != source_branch_lp.self_link:
403
yield LaunchpadMergeProposal(mp)
404
elif base_vcs == 'git':
405
(source_repo_lp, source_branch_lp) = (
406
self._get_lp_git_ref_from_branch(source_branch))
407
(target_repo_lp, target_branch_lp) = (
408
self._get_lp_git_ref_from_branch(target_branch))
409
for mp in target_branch_lp.getMergeProposals(status=statuses):
410
if (target_branch_lp.path != mp.target_git_path or
411
target_repo_lp != mp.target_git_repository or
412
source_branch_lp.path != mp.source_git_path or
413
source_repo_lp != mp.source_git_repository):
415
yield LaunchpadMergeProposal(mp)
417
raise AssertionError('not a valid Launchpad URL')
419
def get_proposer(self, source_branch, target_branch):
420
(base_vcs, base_user, base_password, base_path,
421
base_params) = self._split_url(target_branch.user_url)
422
if base_vcs == 'bzr':
423
return LaunchpadBazaarMergeProposalBuilder(
424
self, source_branch, target_branch)
425
elif base_vcs == 'git':
426
return LaunchpadGitMergeProposalBuilder(
427
self, source_branch, target_branch)
429
raise AssertionError('not a valid Launchpad URL')
432
def iter_instances(cls):
435
def iter_my_proposals(self, status='open'):
436
statuses = status_to_lp_mp_statuses(status)
437
for mp in self.launchpad.me.getMergeProposals(status=statuses):
438
yield LaunchpadMergeProposal(mp)
440
def iter_my_forks(self):
441
# Launchpad doesn't really have the concept of "forks"
444
def get_proposal_by_url(self, url):
445
# Launchpad doesn't have a way to find a merge proposal by URL.
446
(scheme, user, password, host, port, path) = urlutils.parse_url(
448
LAUNCHPAD_CODE_DOMAINS = [
449
('code.%s' % domain) for domain in lp_uris.LAUNCHPAD_DOMAINS.values()]
450
if host not in LAUNCHPAD_CODE_DOMAINS:
451
raise UnsupportedHoster(url)
452
# TODO(jelmer): Check if this is a launchpad URL. Otherwise, raise
454
# See https://api.launchpad.net/devel/#branch_merge_proposal
456
# https://api.launchpad.net/devel/~<author.name>/<project.name>/<branch.name>/+merge/<id>
457
api_url = str(self.launchpad._root_uri) + path
458
mp = self.launchpad.load(api_url)
459
return LaunchpadMergeProposal(mp)
462
class LaunchpadBazaarMergeProposalBuilder(MergeProposalBuilder):
464
def __init__(self, lp_host, source_branch, target_branch,
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 staging: If True, propose the merge against staging instead of
472
:param approve: If True, mark the new proposal as approved immediately.
473
This is useful when a project permits some things to be approved
474
by the submitter (e.g. merges between release and deployment
477
self.lp_host = lp_host
478
self.launchpad = lp_host.launchpad
479
self.source_branch = source_branch
480
self.source_branch_lp = self.launchpad.branches.getByUrl(
481
url=source_branch.user_url)
482
if target_branch is None:
483
self.target_branch_lp = self.source_branch_lp.get_target()
484
self.target_branch = _mod_branch.Branch.open(
485
self.target_branch_lp.bzr_identity)
487
self.target_branch = target_branch
488
self.target_branch_lp = self.launchpad.branches.getByUrl(
489
url=target_branch.user_url)
490
self.approve = approve
493
def get_infotext(self):
494
"""Determine the initial comment for the merge proposal."""
495
info = ["Source: %s\n" % self.source_branch_lp.bzr_identity]
496
info.append("Target: %s\n" % self.target_branch_lp.bzr_identity)
499
def get_initial_body(self):
500
"""Get a body for the proposal for the user to modify.
502
:return: a str or None.
504
if not self.hooks['merge_proposal_body']:
507
def list_modified_files():
508
lca_tree = self.source_branch_lp.find_lca_tree(
509
self.target_branch_lp)
510
source_tree = self.source_branch.basis_tree()
511
files = modified_files(lca_tree, source_tree)
513
with self.target_branch.lock_read(), \
514
self.source_branch.lock_read():
516
for hook in self.hooks['merge_proposal_body']:
518
'target_branch': self.target_branch_lp.bzr_identity,
519
'modified_files_callback': list_modified_files,
524
def check_proposal(self):
525
"""Check that the submission is sensible."""
526
if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
527
raise errors.BzrCommandError(
528
'Source and target branches must be different.')
529
for mp in self.source_branch_lp.landing_targets:
530
if mp.queue_status in ('Merged', 'Rejected'):
532
if mp.target_branch.self_link == self.target_branch_lp.self_link:
533
raise MergeProposalExists(lp_api.canonical_url(mp))
535
def approve_proposal(self, mp):
536
with self.source_branch.lock_read():
540
subject='', # Use the default subject.
541
content=u"Rubberstamp! Proposer approves of own proposal.")
542
_call_webservice(mp.setStatus, status=u'Approved',
543
revid=self.source_branch.last_revision())
545
def create_proposal(self, description, reviewers=None, labels=None,
546
prerequisite_branch=None, commit_message=None,
547
work_in_progress=False):
548
"""Perform the submission."""
550
raise LabelsUnsupported(self)
551
if prerequisite_branch is not None:
552
prereq = self.launchpad.branches.getByUrl(
553
url=prerequisite_branch.user_url)
556
if reviewers is None:
560
for reviewer in reviewers:
562
reviewer_obj = self.launchpad.people.getByEmail(email=reviewer)
564
reviewer_obj = self.launchpad.people[reviewer]
565
reviewer_objs.append(reviewer_obj)
567
mp = _call_webservice(
568
self.source_branch_lp.createMergeProposal,
569
target_branch=self.target_branch_lp,
570
prerequisite_branch=prereq,
571
initial_comment=description.strip(),
572
commit_message=commit_message,
573
needs_review=(not work_in_progress),
574
reviewers=[reviewer.self_link for reviewer in reviewer_objs],
575
review_types=['' for reviewer in reviewer_objs])
576
except WebserviceFailure as e:
578
if (b'There is already a branch merge proposal '
579
b'registered for branch ') in e.message:
580
raise MergeProposalExists(self.source_branch.user_url)
584
self.approve_proposal(mp)
586
if self.fixes.startswith('lp:'):
587
self.fixes = self.fixes[3:]
590
bug=self.launchpad.bugs[int(self.fixes)])
591
return LaunchpadMergeProposal(mp)
594
class LaunchpadGitMergeProposalBuilder(MergeProposalBuilder):
596
def __init__(self, lp_host, source_branch, target_branch,
597
staging=None, approve=None, fixes=None):
600
:param source_branch: The branch to propose for merging.
601
:param target_branch: The branch to merge into.
602
:param staging: If True, propose the merge against staging instead of
604
:param approve: If True, mark the new proposal as approved immediately.
605
This is useful when a project permits some things to be approved
606
by the submitter (e.g. merges between release and deployment
609
self.lp_host = lp_host
610
self.launchpad = lp_host.launchpad
611
self.source_branch = source_branch
612
(self.source_repo_lp,
613
self.source_branch_lp) = self.lp_host._get_lp_git_ref_from_branch(
615
if target_branch is None:
616
self.target_branch_lp = self.source_branch.get_target()
617
self.target_branch = _mod_branch.Branch.open(
618
self.target_branch_lp.git_https_url)
620
self.target_branch = target_branch
621
(self.target_repo_lp, self.target_branch_lp) = (
622
self.lp_host._get_lp_git_ref_from_branch(target_branch))
623
self.approve = approve
626
def get_infotext(self):
627
"""Determine the initial comment for the merge proposal."""
628
info = ["Source: %s\n" % self.source_branch.user_url]
629
info.append("Target: %s\n" % self.target_branch.user_url)
632
def get_initial_body(self):
633
"""Get a body for the proposal for the user to modify.
635
:return: a str or None.
637
if not self.hooks['merge_proposal_body']:
640
def list_modified_files():
641
lca_tree = self.source_branch_lp.find_lca_tree(
642
self.target_branch_lp)
643
source_tree = self.source_branch.basis_tree()
644
files = modified_files(lca_tree, source_tree)
646
with self.target_branch.lock_read(), \
647
self.source_branch.lock_read():
649
for hook in self.hooks['merge_proposal_body']:
651
'target_branch': self.target_branch,
652
'modified_files_callback': list_modified_files,
657
def check_proposal(self):
658
"""Check that the submission is sensible."""
659
if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
660
raise errors.BzrCommandError(
661
'Source and target branches must be different.')
662
for mp in self.source_branch_lp.landing_targets:
663
if mp.queue_status in ('Merged', 'Rejected'):
665
if mp.target_branch.self_link == self.target_branch_lp.self_link:
666
raise MergeProposalExists(lp_api.canonical_url(mp))
668
def approve_proposal(self, mp):
669
with self.source_branch.lock_read():
673
subject='', # Use the default subject.
674
content=u"Rubberstamp! Proposer approves of own proposal.")
676
mp.setStatus, status=u'Approved',
677
revid=self.source_branch.last_revision())
679
def create_proposal(self, description, reviewers=None, labels=None,
680
prerequisite_branch=None, commit_message=None):
681
"""Perform the submission."""
683
raise LabelsUnsupported(self)
684
if prerequisite_branch is not None:
685
(prereq_repo_lp, prereq_branch_lp) = (
686
self.lp_host._get_lp_git_ref_from_branch(prerequisite_branch))
688
prereq_branch_lp = None
689
if reviewers is None:
692
mp = _call_webservice(
693
self.source_branch_lp.createMergeProposal,
694
merge_target=self.target_branch_lp,
695
merge_prerequisite=prereq_branch_lp,
696
initial_comment=description.strip(),
697
commit_message=commit_message,
699
reviewers=[self.launchpad.people[reviewer].self_link
700
for reviewer in reviewers],
701
review_types=[None for reviewer in reviewers])
702
except WebserviceFailure as e:
704
if ('There is already a branch merge proposal '
705
'registered for branch ') in e.message:
706
raise MergeProposalExists(self.source_branch.user_url)
709
self.approve_proposal(mp)
711
if self.fixes.startswith('lp:'):
712
self.fixes = self.fixes[3:]
715
bug=self.launchpad.bugs[int(self.fixes)])
716
return LaunchpadMergeProposal(mp)
719
def modified_files(old_tree, new_tree):
720
"""Return a list of paths in the new tree with modified contents."""
721
for change in new_tree.iter_changes(old_tree):
722
if change.changed_content and change.kind[1] == 'file':