/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/propose/launchpad.py

  • Committer: Jelmer Vernooij
  • Date: 2017-05-21 12:41:27 UTC
  • mto: This revision was merged to the branch mainline in revision 6623.
  • Revision ID: jelmer@jelmer.uk-20170521124127-iv8etg0vwymyai6y
s/bzr/brz/ in apport config.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2010, 2011 Canonical Ltd
2
 
# Copyright (C) 2018 Breezy Developers
3
 
#
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.
8
 
#
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.
13
 
#
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
17
 
 
18
 
"""Support for Launchpad."""
19
 
 
20
 
import re
21
 
import shutil
22
 
import tempfile
23
 
 
24
 
from ...propose import (
25
 
    Hoster,
26
 
    LabelsUnsupported,
27
 
    MergeProposal,
28
 
    MergeProposalBuilder,
29
 
    MergeProposalExists,
30
 
    UnsupportedHoster,
31
 
    )
32
 
 
33
 
from ... import (
34
 
    branch as _mod_branch,
35
 
    controldir,
36
 
    errors,
37
 
    hooks,
38
 
    urlutils,
39
 
    )
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 (
44
 
    lp_api,
45
 
    uris as lp_uris,
46
 
    )
47
 
 
48
 
from launchpadlib import uris
49
 
""")
50
 
from ...transport import get_transport
51
 
 
52
 
 
53
 
# TODO(jelmer): Make selection of launchpad staging a configuration option.
54
 
 
55
 
def status_to_lp_mp_statuses(status):
56
 
    statuses = []
57
 
    if status in ('open', 'all'):
58
 
        statuses.extend([
59
 
            'Work in progress',
60
 
            'Needs review',
61
 
            'Approved',
62
 
            'Code failed to merge',
63
 
            'Queued'])
64
 
    if status in ('closed', 'all'):
65
 
        statuses.extend(['Rejected', 'Superseded'])
66
 
    if status in ('merged', 'all'):
67
 
        statuses.append('Merged')
68
 
    return statuses
69
 
 
70
 
 
71
 
def plausible_launchpad_url(url):
72
 
    if url is None:
73
 
        return False
74
 
    if url.startswith('lp:'):
75
 
        return True
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))
79
 
 
80
 
 
81
 
class WebserviceFailure(Exception):
82
 
 
83
 
    def __init__(self, message):
84
 
        self.message = message
85
 
 
86
 
 
87
 
def _call_webservice(call, *args, **kwargs):
88
 
    """Make a call to the webservice, wrapping failures.
89
 
 
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).
94
 
    """
95
 
    from lazr.restfulclient import errors as restful_errors
96
 
    try:
97
 
        return call(*args, **kwargs)
98
 
    except restful_errors.HTTPError as e:
99
 
        error_lines = []
100
 
        for line in e.content.splitlines():
101
 
            if line.startswith(b'Traceback (most recent call last):'):
102
 
                break
103
 
            error_lines.append(line)
104
 
        raise WebserviceFailure(b''.join(error_lines))
105
 
 
106
 
 
107
 
class LaunchpadMergeProposal(MergeProposal):
108
 
 
109
 
    def __init__(self, mp):
110
 
        self._mp = mp
111
 
 
112
 
    def get_source_branch_url(self):
113
 
        if self._mp.source_branch:
114
 
            return self._mp.source_branch.bzr_identity
115
 
        else:
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'))
119
 
 
120
 
    def get_target_branch_url(self):
121
 
        if self._mp.target_branch:
122
 
            return self._mp.target_branch.bzr_identity
123
 
        else:
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'))
127
 
 
128
 
    @property
129
 
    def url(self):
130
 
        return lp_api.canonical_url(self._mp)
131
 
 
132
 
    def is_merged(self):
133
 
        return (self._mp.queue_status == 'Merged')
134
 
 
135
 
    def is_closed(self):
136
 
        return (self._mp.queue_status in ('Rejected', 'Superseded'))
137
 
 
138
 
    def reopen(self):
139
 
        self._mp.setStatus(status='Needs review')
140
 
 
141
 
    def get_description(self):
142
 
        return self._mp.description
143
 
 
144
 
    def set_description(self, description):
145
 
        self._mp.description = description
146
 
        self._mp.lp_save()
147
 
 
148
 
    def get_commit_message(self):
149
 
        return self._mp.commit_message
150
 
 
151
 
    def set_commit_message(self, commit_message):
152
 
        self._mp.commit_message = commit_message
153
 
        self._mp.lp_save()
154
 
 
155
 
    def close(self):
156
 
        self._mp.setStatus(status='Rejected')
157
 
 
158
 
    def can_be_merged(self):
159
 
        if not self._mp.preview_diff:
160
 
            # Maybe?
161
 
            return True
162
 
        return not bool(self._mp.preview_diff.conflicts)
163
 
 
164
 
    def get_merged_by(self):
165
 
        merge_reporter = self._mp.merge_reporter
166
 
        if merge_reporter is None:
167
 
            return None
168
 
        return merge_reporter.name
169
 
 
170
 
    def get_merged_at(self):
171
 
        return self._mp.date_merged
172
 
 
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
179
 
        # support that yet.
180
 
        # tree = target_branch.create_memorytree()
181
 
        tmpdir = tempfile.mkdtemp()
182
 
        try:
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)
187
 
        finally:
188
 
            shutil.rmtree(tmpdir)
189
 
 
190
 
 
191
 
class Launchpad(Hoster):
192
 
    """The Launchpad hosting service."""
193
 
 
194
 
    name = 'launchpad'
195
 
 
196
 
    # https://bugs.launchpad.net/launchpad/+bug/397676
197
 
    supports_merge_proposal_labels = False
198
 
 
199
 
    supports_merge_proposal_commit_message = True
200
 
 
201
 
    merge_proposal_description_format = 'plain'
202
 
 
203
 
    def __init__(self, staging=False):
204
 
        self._staging = staging
205
 
        if staging:
206
 
            lp_base_url = uris.STAGING_SERVICE_ROOT
207
 
        else:
208
 
            lp_base_url = uris.LPNET_SERVICE_ROOT
209
 
        self.launchpad = lp_api.connect_launchpad(lp_base_url, version='devel')
210
 
 
211
 
    @property
212
 
    def base_url(self):
213
 
        return lp_api.uris.web_root_for_service_root(
214
 
            str(self.launchpad._root_uri))
215
 
 
216
 
    def __repr__(self):
217
 
        return "Launchpad(staging=%s)" % self._staging
218
 
 
219
 
    def hosts(self, branch):
220
 
        # TODO(jelmer): staging vs non-staging?
221
 
        return plausible_launchpad_url(branch.user_url)
222
 
 
223
 
    @classmethod
224
 
    def probe_from_url(cls, url, possible_transports=None):
225
 
        if plausible_launchpad_url(url):
226
 
            return Launchpad()
227
 
        raise UnsupportedHoster(url)
228
 
 
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(
232
 
            url)
233
 
        repo_lp = self.launchpad.git_repositories.getByPath(
234
 
            path=path.strip('/'))
235
 
        try:
236
 
            ref_path = params['ref']
237
 
        except KeyError:
238
 
            branch_name = params.get('branch', branch.name)
239
 
            if branch_name:
240
 
                ref_path = 'refs/heads/%s' % branch_name
241
 
            else:
242
 
                ref_path = repo_lp.default_branch
243
 
        ref_lp = repo_lp.getRefByPath(path=ref_path)
244
 
        return (repo_lp, ref_lp)
245
 
 
246
 
    def _get_lp_bzr_branch_from_branch(self, branch):
247
 
        return self.launchpad.branches.getByUrl(
248
 
            url=urlutils.unescape(branch.user_url))
249
 
 
250
 
    def _get_derived_git_path(self, base_path, owner, project):
251
 
        base_repo = self.launchpad.git_repositories.getByPath(path=base_path)
252
 
        if project is None:
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
257
 
        # URLs?
258
 
        return "~%s/%s" % (owner, project)
259
 
 
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)
264
 
        try:
265
 
            dir_to = controldir.ControlDir.open_from_transport(to_transport)
266
 
        except errors.NotBranchError:
267
 
            # Didn't find anything
268
 
            dir_to = None
269
 
 
270
 
        if dir_to is None:
271
 
            try:
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,
277
 
                    lossy=True)
278
 
        else:
279
 
            try:
280
 
                dir_to = dir_to.push_branch(
281
 
                    local_branch, revision_id, overwrite=overwrite, name=name)
282
 
            except errors.NoRoundtrippingSupport:
283
 
                if not allow_lossy:
284
 
                    raise
285
 
                dir_to = dir_to.push_branch(
286
 
                    local_branch, revision_id, overwrite=overwrite, name=name,
287
 
                    lossy=True)
288
 
            br_to = dir_to.target_branch
289
 
        return br_to, (
290
 
            "https://git.launchpad.net/%s/+ref/%s" % (to_path, name))
291
 
 
292
 
    def _get_derived_bzr_path(self, base_branch, name, owner, project):
293
 
        if project is None:
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
297
 
        # URLs?
298
 
        return "~%s/%s/%s" % (owner, project, name)
299
 
 
300
 
    def get_push_url(self, branch):
301
 
        (vcs, user, password, path, params) = self._split_url(branch.user_url)
302
 
        if vcs == 'bzr':
303
 
            branch_lp = self._get_lp_bzr_branch_from_branch(branch)
304
 
            return branch_lp.bzr_identity
305
 
        elif vcs == 'git':
306
 
            return urlutils.join_segment_parameters(
307
 
                "git+ssh://git.launchpad.net/" + path, params)
308
 
        else:
309
 
            raise AssertionError
310
 
 
311
 
    def _publish_bzr(self, local_branch, base_branch, name, owner,
312
 
                     project=None, revision_id=None, overwrite=False,
313
 
                     allow_lossy=True):
314
 
        to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
315
 
        to_transport = get_transport("lp:" + to_path)
316
 
        try:
317
 
            dir_to = controldir.ControlDir.open_from_transport(to_transport)
318
 
        except errors.NotBranchError:
319
 
            # Didn't find anything
320
 
            dir_to = None
321
 
 
322
 
        if dir_to is None:
323
 
            br_to = local_branch.create_clone_on_transport(
324
 
                to_transport, revision_id=revision_id)
325
 
        else:
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)
329
 
 
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.'):
335
 
            vcs = 'bzr'
336
 
        elif host.startswith('git.'):
337
 
            vcs = 'git'
338
 
        else:
339
 
            raise ValueError("unknown host %s" % host)
340
 
        return (vcs, user, password, path, params)
341
 
 
342
 
    def publish_derived(self, local_branch, base_branch, name, project=None,
343
 
                        owner=None, revision_id=None, overwrite=False,
344
 
                        allow_lossy=True):
345
 
        """Publish a branch to the site, derived from base_branch.
346
 
 
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
353
 
        """
354
 
        if owner is None:
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)
369
 
        else:
370
 
            raise AssertionError('not a valid Launchpad URL')
371
 
 
372
 
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
373
 
        if owner is 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,
386
 
                {'branch': name})
387
 
            return _mod_branch.Branch.open(to_url)
388
 
        else:
389
 
            raise AssertionError('not a valid Launchpad URL')
390
 
 
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:
402
 
                    continue
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):
414
 
                    continue
415
 
                yield LaunchpadMergeProposal(mp)
416
 
        else:
417
 
            raise AssertionError('not a valid Launchpad URL')
418
 
 
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)
428
 
        else:
429
 
            raise AssertionError('not a valid Launchpad URL')
430
 
 
431
 
    @classmethod
432
 
    def iter_instances(cls):
433
 
        yield cls()
434
 
 
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)
439
 
 
440
 
    def iter_my_forks(self):
441
 
        # Launchpad doesn't really have the concept of "forks"
442
 
        return iter([])
443
 
 
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(
447
 
            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
453
 
        # UnsupportedHoster
454
 
        # See https://api.launchpad.net/devel/#branch_merge_proposal
455
 
        # the syntax is:
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)
460
 
 
461
 
 
462
 
class LaunchpadBazaarMergeProposalBuilder(MergeProposalBuilder):
463
 
 
464
 
    def __init__(self, lp_host, source_branch, target_branch,
465
 
                 staging=None, approve=None, fixes=None):
466
 
        """Constructor.
467
 
 
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
471
 
            production.
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
475
 
            branches).
476
 
        """
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)
486
 
        else:
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
491
 
        self.fixes = fixes
492
 
 
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)
497
 
        return ''.join(info)
498
 
 
499
 
    def get_initial_body(self):
500
 
        """Get a body for the proposal for the user to modify.
501
 
 
502
 
        :return: a str or None.
503
 
        """
504
 
        if not self.hooks['merge_proposal_body']:
505
 
            return None
506
 
 
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)
512
 
            return list(files)
513
 
        with self.target_branch.lock_read(), \
514
 
                self.source_branch.lock_read():
515
 
            body = None
516
 
            for hook in self.hooks['merge_proposal_body']:
517
 
                body = hook({
518
 
                    'target_branch': self.target_branch_lp.bzr_identity,
519
 
                    'modified_files_callback': list_modified_files,
520
 
                    'old_body': body,
521
 
                })
522
 
            return body
523
 
 
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'):
531
 
                continue
532
 
            if mp.target_branch.self_link == self.target_branch_lp.self_link:
533
 
                raise MergeProposalExists(lp_api.canonical_url(mp))
534
 
 
535
 
    def approve_proposal(self, mp):
536
 
        with self.source_branch.lock_read():
537
 
            _call_webservice(
538
 
                mp.createComment,
539
 
                vote=u'Approve',
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())
544
 
 
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."""
549
 
        if labels:
550
 
            raise LabelsUnsupported(self)
551
 
        if prerequisite_branch is not None:
552
 
            prereq = self.launchpad.branches.getByUrl(
553
 
                url=prerequisite_branch.user_url)
554
 
        else:
555
 
            prereq = None
556
 
        if reviewers is None:
557
 
            reviewer_objs = []
558
 
        else:
559
 
            reviewer_objs = []
560
 
            for reviewer in reviewers:
561
 
                if '@' in reviewer:
562
 
                    reviewer_obj = self.launchpad.people.getByEmail(email=reviewer)
563
 
                else:
564
 
                    reviewer_obj = self.launchpad.people[reviewer]
565
 
                reviewer_objs.append(reviewer_obj)
566
 
        try:
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:
577
 
            # Urgh.
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)
581
 
            raise
582
 
 
583
 
        if self.approve:
584
 
            self.approve_proposal(mp)
585
 
        if self.fixes:
586
 
            if self.fixes.startswith('lp:'):
587
 
                self.fixes = self.fixes[3:]
588
 
            _call_webservice(
589
 
                mp.linkBug,
590
 
                bug=self.launchpad.bugs[int(self.fixes)])
591
 
        return LaunchpadMergeProposal(mp)
592
 
 
593
 
 
594
 
class LaunchpadGitMergeProposalBuilder(MergeProposalBuilder):
595
 
 
596
 
    def __init__(self, lp_host, source_branch, target_branch,
597
 
                 staging=None, approve=None, fixes=None):
598
 
        """Constructor.
599
 
 
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
603
 
            production.
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
607
 
            branches).
608
 
        """
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(
614
 
                source_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)
619
 
        else:
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
624
 
        self.fixes = fixes
625
 
 
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)
630
 
        return ''.join(info)
631
 
 
632
 
    def get_initial_body(self):
633
 
        """Get a body for the proposal for the user to modify.
634
 
 
635
 
        :return: a str or None.
636
 
        """
637
 
        if not self.hooks['merge_proposal_body']:
638
 
            return None
639
 
 
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)
645
 
            return list(files)
646
 
        with self.target_branch.lock_read(), \
647
 
                self.source_branch.lock_read():
648
 
            body = None
649
 
            for hook in self.hooks['merge_proposal_body']:
650
 
                body = hook({
651
 
                    'target_branch': self.target_branch,
652
 
                    'modified_files_callback': list_modified_files,
653
 
                    'old_body': body,
654
 
                })
655
 
            return body
656
 
 
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'):
664
 
                continue
665
 
            if mp.target_branch.self_link == self.target_branch_lp.self_link:
666
 
                raise MergeProposalExists(lp_api.canonical_url(mp))
667
 
 
668
 
    def approve_proposal(self, mp):
669
 
        with self.source_branch.lock_read():
670
 
            _call_webservice(
671
 
                mp.createComment,
672
 
                vote=u'Approve',
673
 
                subject='',  # Use the default subject.
674
 
                content=u"Rubberstamp! Proposer approves of own proposal.")
675
 
            _call_webservice(
676
 
                mp.setStatus, status=u'Approved',
677
 
                revid=self.source_branch.last_revision())
678
 
 
679
 
    def create_proposal(self, description, reviewers=None, labels=None,
680
 
                        prerequisite_branch=None, commit_message=None):
681
 
        """Perform the submission."""
682
 
        if labels:
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))
687
 
        else:
688
 
            prereq_branch_lp = None
689
 
        if reviewers is None:
690
 
            reviewers = []
691
 
        try:
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,
698
 
                needs_review=True,
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:
703
 
            # Urgh.
704
 
            if ('There is already a branch merge proposal '
705
 
                    'registered for branch ') in e.message:
706
 
                raise MergeProposalExists(self.source_branch.user_url)
707
 
            raise
708
 
        if self.approve:
709
 
            self.approve_proposal(mp)
710
 
        if self.fixes:
711
 
            if self.fixes.startswith('lp:'):
712
 
                self.fixes = self.fixes[3:]
713
 
            _call_webservice(
714
 
                mp.linkBug,
715
 
                bug=self.launchpad.bugs[int(self.fixes)])
716
 
        return LaunchpadMergeProposal(mp)
717
 
 
718
 
 
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':
723
 
            yield str(path)