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

  • Committer: Robert Collins
  • Date: 2010-05-11 08:36:16 UTC
  • mto: This revision was merged to the branch mainline in revision 5223.
  • Revision ID: robertc@robertcollins.net-20100511083616-b8fjb19zomwupid0
Make all lock methods return Result objects, rather than lock_read returning self, as per John's review.

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
 
from __future__ import absolute_import
21
 
 
22
 
import re
23
 
import shutil
24
 
import tempfile
25
 
 
26
 
from ...propose import (
27
 
    Hoster,
28
 
    LabelsUnsupported,
29
 
    MergeProposal,
30
 
    MergeProposalBuilder,
31
 
    MergeProposalExists,
32
 
    UnsupportedHoster,
33
 
    )
34
 
 
35
 
from ... import (
36
 
    branch as _mod_branch,
37
 
    controldir,
38
 
    errors,
39
 
    hooks,
40
 
    urlutils,
41
 
    )
42
 
from ...git.urls import git_url_to_bzr_url
43
 
from ...lazy_import import lazy_import
44
 
lazy_import(globals(), """
45
 
from breezy.plugins.launchpad import (
46
 
    lp_api,
47
 
    uris as lp_uris,
48
 
    )
49
 
 
50
 
from launchpadlib import uris
51
 
""")
52
 
from ...transport import get_transport
53
 
 
54
 
 
55
 
# TODO(jelmer): Make selection of launchpad staging a configuration option.
56
 
 
57
 
def status_to_lp_mp_statuses(status):
58
 
    statuses = []
59
 
    if status in ('open', 'all'):
60
 
        statuses.extend([
61
 
            'Work in progress',
62
 
            'Needs review',
63
 
            'Approved',
64
 
            'Code failed to merge',
65
 
            'Queued'])
66
 
    if status in ('closed', 'all'):
67
 
        statuses.extend(['Rejected', 'Superseded'])
68
 
    if status in ('merged', 'all'):
69
 
        statuses.append('Merged')
70
 
    return statuses
71
 
 
72
 
 
73
 
def plausible_launchpad_url(url):
74
 
    if url is None:
75
 
        return False
76
 
    if url.startswith('lp:'):
77
 
        return True
78
 
    regex = re.compile(r'([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
79
 
                       r'://(bazaar|git).*\.launchpad\.net')
80
 
    return bool(regex.match(url))
81
 
 
82
 
 
83
 
class WebserviceFailure(Exception):
84
 
 
85
 
    def __init__(self, message):
86
 
        self.message = message
87
 
 
88
 
 
89
 
def _call_webservice(call, *args, **kwargs):
90
 
    """Make a call to the webservice, wrapping failures.
91
 
 
92
 
    :param call: The call to make.
93
 
    :param *args: *args for the call.
94
 
    :param **kwargs: **kwargs for the call.
95
 
    :return: The result of calling call(*args, *kwargs).
96
 
    """
97
 
    from lazr.restfulclient import errors as restful_errors
98
 
    try:
99
 
        return call(*args, **kwargs)
100
 
    except restful_errors.HTTPError as e:
101
 
        error_lines = []
102
 
        for line in e.content.splitlines():
103
 
            if line.startswith(b'Traceback (most recent call last):'):
104
 
                break
105
 
            error_lines.append(line)
106
 
        raise WebserviceFailure(b''.join(error_lines))
107
 
 
108
 
 
109
 
class LaunchpadMergeProposal(MergeProposal):
110
 
 
111
 
    def __init__(self, mp):
112
 
        self._mp = mp
113
 
 
114
 
    def get_source_branch_url(self):
115
 
        if self._mp.source_branch:
116
 
            return self._mp.source_branch.bzr_identity
117
 
        else:
118
 
            return git_url_to_bzr_url(
119
 
                self._mp.source_git_repository.git_identity,
120
 
                ref=self._mp.source_git_path.encode('utf-8'))
121
 
 
122
 
    def get_source_revision(self):
123
 
        if self._mp.source_branch:
124
 
            last_scanned_id = self._mp.source_branch.last_scanned_id
125
 
            if last_scanned_id:
126
 
                return last_scanned_id.encode('utf-8')
127
 
            else:
128
 
                return None
129
 
        else:
130
 
            from breezy.git.mapping import default_mapping
131
 
            git_repo = self._mp.source_git_repository
132
 
            git_ref = git_repo.getRefByPath(path=self._mp.source_git_path)
133
 
            sha = git_ref.commit_sha1
134
 
            if sha is None:
135
 
                return None
136
 
            return default_mapping.revision_id_foreign_to_bzr(
137
 
                sha.encode('ascii'))
138
 
 
139
 
    def get_target_branch_url(self):
140
 
        if self._mp.target_branch:
141
 
            return self._mp.target_branch.bzr_identity
142
 
        else:
143
 
            return git_url_to_bzr_url(
144
 
                self._mp.target_git_repository.git_identity,
145
 
                ref=self._mp.target_git_path.encode('utf-8'))
146
 
 
147
 
    @property
148
 
    def url(self):
149
 
        return lp_api.canonical_url(self._mp)
150
 
 
151
 
    def is_merged(self):
152
 
        return (self._mp.queue_status == 'Merged')
153
 
 
154
 
    def is_closed(self):
155
 
        return (self._mp.queue_status in ('Rejected', 'Superseded'))
156
 
 
157
 
    def reopen(self):
158
 
        self._mp.setStatus(status='Needs review')
159
 
 
160
 
    def get_description(self):
161
 
        return self._mp.description
162
 
 
163
 
    def set_description(self, description):
164
 
        self._mp.description = description
165
 
        self._mp.lp_save()
166
 
 
167
 
    def get_commit_message(self):
168
 
        return self._mp.commit_message
169
 
 
170
 
    def set_commit_message(self, commit_message):
171
 
        self._mp.commit_message = commit_message
172
 
        self._mp.lp_save()
173
 
 
174
 
    def close(self):
175
 
        self._mp.setStatus(status='Rejected')
176
 
 
177
 
    def can_be_merged(self):
178
 
        if not self._mp.preview_diff:
179
 
            # Maybe?
180
 
            return True
181
 
        return not bool(self._mp.preview_diff.conflicts)
182
 
 
183
 
    def get_merged_by(self):
184
 
        merge_reporter = self._mp.merge_reporter
185
 
        if merge_reporter is None:
186
 
            return None
187
 
        return merge_reporter.name
188
 
 
189
 
    def get_merged_at(self):
190
 
        return self._mp.date_merged
191
 
 
192
 
    def merge(self, commit_message=None):
193
 
        target_branch = _mod_branch.Branch.open(
194
 
            self.get_target_branch_url())
195
 
        source_branch = _mod_branch.Branch.open(
196
 
            self.get_source_branch_url())
197
 
        # TODO(jelmer): Ideally this would use a memorytree, but merge doesn't
198
 
        # support that yet.
199
 
        # tree = target_branch.create_memorytree()
200
 
        tmpdir = tempfile.mkdtemp()
201
 
        try:
202
 
            tree = target_branch.create_checkout(
203
 
                to_location=tmpdir, lightweight=True)
204
 
            tree.merge_from_branch(source_branch)
205
 
            tree.commit(commit_message or self._mp.commit_message)
206
 
        finally:
207
 
            shutil.rmtree(tmpdir)
208
 
 
209
 
    def post_comment(self, body):
210
 
        self._mp.createComment(content=body)
211
 
 
212
 
 
213
 
class Launchpad(Hoster):
214
 
    """The Launchpad hosting service."""
215
 
 
216
 
    name = 'launchpad'
217
 
 
218
 
    # https://bugs.launchpad.net/launchpad/+bug/397676
219
 
    supports_merge_proposal_labels = False
220
 
 
221
 
    supports_merge_proposal_commit_message = True
222
 
 
223
 
    supports_allow_collaboration = False
224
 
 
225
 
    merge_proposal_description_format = 'plain'
226
 
 
227
 
    def __init__(self, service_root):
228
 
        self._api_base_url = service_root
229
 
        self._launchpad = None
230
 
 
231
 
    @property
232
 
    def name(self):
233
 
        if self._api_base_url == uris.LPNET_SERVICE_ROOT:
234
 
            return 'Launchpad'
235
 
        return 'Launchpad at %s' % self.base_url
236
 
 
237
 
    @property
238
 
    def launchpad(self):
239
 
        if self._launchpad is None:
240
 
            self._launchpad = lp_api.connect_launchpad(self._api_base_url, version='devel')
241
 
        return self._launchpad
242
 
 
243
 
    @property
244
 
    def base_url(self):
245
 
        return lp_api.uris.web_root_for_service_root(self._api_base_url)
246
 
 
247
 
    def __repr__(self):
248
 
        return "Launchpad(service_root=%s)" % self._api_base_url
249
 
 
250
 
    def get_current_user(self):
251
 
        return self.launchpad.me.name
252
 
 
253
 
    def get_user_url(self, username):
254
 
        return self.launchpad.people[username].web_link
255
 
 
256
 
    def hosts(self, branch):
257
 
        # TODO(jelmer): staging vs non-staging?
258
 
        return plausible_launchpad_url(branch.user_url)
259
 
 
260
 
    @classmethod
261
 
    def probe_from_url(cls, url, possible_transports=None):
262
 
        if plausible_launchpad_url(url):
263
 
            return Launchpad(uris.LPNET_SERVICE_ROOT)
264
 
        raise UnsupportedHoster(url)
265
 
 
266
 
    def _get_lp_git_ref_from_branch(self, branch):
267
 
        url, params = urlutils.split_segment_parameters(branch.user_url)
268
 
        (scheme, user, password, host, port, path) = urlutils.parse_url(
269
 
            url)
270
 
        repo_lp = self.launchpad.git_repositories.getByPath(
271
 
            path=path.strip('/'))
272
 
        try:
273
 
            ref_path = params['ref']
274
 
        except KeyError:
275
 
            branch_name = params.get('branch', branch.name)
276
 
            if branch_name:
277
 
                ref_path = 'refs/heads/%s' % branch_name
278
 
            else:
279
 
                ref_path = repo_lp.default_branch
280
 
        ref_lp = repo_lp.getRefByPath(path=ref_path)
281
 
        return (repo_lp, ref_lp)
282
 
 
283
 
    def _get_lp_bzr_branch_from_branch(self, branch):
284
 
        return self.launchpad.branches.getByUrl(
285
 
            url=urlutils.unescape(branch.user_url))
286
 
 
287
 
    def _get_derived_git_path(self, base_path, owner, project):
288
 
        base_repo = self.launchpad.git_repositories.getByPath(path=base_path)
289
 
        if project is None:
290
 
            project = urlutils.parse_url(base_repo.git_ssh_url)[-1].strip('/')
291
 
        if project.startswith('~'):
292
 
            project = '/'.join(base_path.split('/')[1:])
293
 
        # TODO(jelmer): Surely there is a better way of creating one of these
294
 
        # URLs?
295
 
        return "~%s/%s" % (owner, project)
296
 
 
297
 
    def _publish_git(self, local_branch, base_path, name, owner, project=None,
298
 
                     revision_id=None, overwrite=False, allow_lossy=True,
299
 
                     tag_selector=None):
300
 
        to_path = self._get_derived_git_path(base_path, owner, project)
301
 
        to_transport = get_transport("git+ssh://git.launchpad.net/" + to_path)
302
 
        try:
303
 
            dir_to = controldir.ControlDir.open_from_transport(to_transport)
304
 
        except errors.NotBranchError:
305
 
            # Didn't find anything
306
 
            dir_to = None
307
 
 
308
 
        if dir_to is None:
309
 
            try:
310
 
                br_to = local_branch.create_clone_on_transport(
311
 
                    to_transport, revision_id=revision_id, name=name,
312
 
                    tag_selector=tag_selector)
313
 
            except errors.NoRoundtrippingSupport:
314
 
                br_to = local_branch.create_clone_on_transport(
315
 
                    to_transport, revision_id=revision_id, name=name,
316
 
                    lossy=True, tag_selector=tag_selector)
317
 
        else:
318
 
            try:
319
 
                dir_to = dir_to.push_branch(
320
 
                    local_branch, revision_id, overwrite=overwrite, name=name,
321
 
                    tag_selector=tag_selector)
322
 
            except errors.NoRoundtrippingSupport:
323
 
                if not allow_lossy:
324
 
                    raise
325
 
                dir_to = dir_to.push_branch(
326
 
                    local_branch, revision_id, overwrite=overwrite, name=name,
327
 
                    lossy=True, tag_selector=tag_selector)
328
 
            br_to = dir_to.target_branch
329
 
        return br_to, (
330
 
            "https://git.launchpad.net/%s/+ref/%s" % (to_path, name))
331
 
 
332
 
    def _get_derived_bzr_path(self, base_branch, name, owner, project):
333
 
        if project is None:
334
 
            base_branch_lp = self._get_lp_bzr_branch_from_branch(base_branch)
335
 
            project = '/'.join(base_branch_lp.unique_name.split('/')[1:-1])
336
 
        # TODO(jelmer): Surely there is a better way of creating one of these
337
 
        # URLs?
338
 
        return "~%s/%s/%s" % (owner, project, name)
339
 
 
340
 
    def get_push_url(self, branch):
341
 
        (vcs, user, password, path, params) = self._split_url(branch.user_url)
342
 
        if vcs == 'bzr':
343
 
            branch_lp = self._get_lp_bzr_branch_from_branch(branch)
344
 
            return branch_lp.bzr_identity
345
 
        elif vcs == 'git':
346
 
            return urlutils.join_segment_parameters(
347
 
                "git+ssh://git.launchpad.net/" + path, params)
348
 
        else:
349
 
            raise AssertionError
350
 
 
351
 
    def _publish_bzr(self, local_branch, base_branch, name, owner,
352
 
                     project=None, revision_id=None, overwrite=False,
353
 
                     allow_lossy=True, tag_selector=None):
354
 
        to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
355
 
        to_transport = get_transport("lp:" + to_path)
356
 
        try:
357
 
            dir_to = controldir.ControlDir.open_from_transport(to_transport)
358
 
        except errors.NotBranchError:
359
 
            # Didn't find anything
360
 
            dir_to = None
361
 
 
362
 
        if dir_to is None:
363
 
            br_to = local_branch.create_clone_on_transport(
364
 
                to_transport, revision_id=revision_id, tag_selector=tag_selector)
365
 
        else:
366
 
            br_to = dir_to.push_branch(
367
 
                local_branch, revision_id, overwrite=overwrite,
368
 
                tag_selector=tag_selector).target_branch
369
 
        return br_to, ("https://code.launchpad.net/" + to_path)
370
 
 
371
 
    def _split_url(self, url):
372
 
        url, params = urlutils.split_segment_parameters(url)
373
 
        (scheme, user, password, host, port, path) = urlutils.parse_url(url)
374
 
        path = path.strip('/')
375
 
        if host.startswith('bazaar.'):
376
 
            vcs = 'bzr'
377
 
        elif host.startswith('git.'):
378
 
            vcs = 'git'
379
 
        else:
380
 
            raise ValueError("unknown host %s" % host)
381
 
        return (vcs, user, password, path, params)
382
 
 
383
 
    def publish_derived(self, local_branch, base_branch, name, project=None,
384
 
                        owner=None, revision_id=None, overwrite=False,
385
 
                        allow_lossy=True, tag_selector=None):
386
 
        """Publish a branch to the site, derived from base_branch.
387
 
 
388
 
        :param base_branch: branch to derive the new branch from
389
 
        :param new_branch: branch to publish
390
 
        :param name: Name of the new branch on the remote host
391
 
        :param project: Optional project name
392
 
        :param owner: Optional owner
393
 
        :return: resulting branch
394
 
        """
395
 
        if owner is None:
396
 
            owner = self.launchpad.me.name
397
 
        (base_vcs, base_user, base_password, base_path,
398
 
            base_params) = self._split_url(base_branch.user_url)
399
 
        # TODO(jelmer): Prevent publishing to development focus
400
 
        if base_vcs == 'bzr':
401
 
            return self._publish_bzr(
402
 
                local_branch, base_branch, name, project=project, owner=owner,
403
 
                revision_id=revision_id, overwrite=overwrite,
404
 
                allow_lossy=allow_lossy, tag_selector=tag_selector)
405
 
        elif base_vcs == 'git':
406
 
            return self._publish_git(
407
 
                local_branch, base_path, name, project=project, owner=owner,
408
 
                revision_id=revision_id, overwrite=overwrite,
409
 
                allow_lossy=allow_lossy, tag_selector=tag_selector)
410
 
        else:
411
 
            raise AssertionError('not a valid Launchpad URL')
412
 
 
413
 
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
414
 
        if owner is None:
415
 
            owner = self.launchpad.me.name
416
 
        (base_vcs, base_user, base_password, base_path,
417
 
            base_params) = self._split_url(base_branch.user_url)
418
 
        if base_vcs == 'bzr':
419
 
            to_path = self._get_derived_bzr_path(
420
 
                base_branch, name, owner, project)
421
 
            return _mod_branch.Branch.open("lp:" + to_path)
422
 
        elif base_vcs == 'git':
423
 
            to_path = self._get_derived_git_path(
424
 
                base_path.strip('/'), owner, project)
425
 
            to_url = urlutils.join_segment_parameters(
426
 
                "git+ssh://git.launchpad.net/" + to_path,
427
 
                {'branch': name})
428
 
            return _mod_branch.Branch.open(to_url)
429
 
        else:
430
 
            raise AssertionError('not a valid Launchpad URL')
431
 
 
432
 
    def iter_proposals(self, source_branch, target_branch, status='open'):
433
 
        (base_vcs, base_user, base_password, base_path,
434
 
            base_params) = self._split_url(target_branch.user_url)
435
 
        statuses = status_to_lp_mp_statuses(status)
436
 
        if base_vcs == 'bzr':
437
 
            target_branch_lp = self.launchpad.branches.getByUrl(
438
 
                url=target_branch.user_url)
439
 
            source_branch_lp = self.launchpad.branches.getByUrl(
440
 
                url=source_branch.user_url)
441
 
            for mp in target_branch_lp.getMergeProposals(status=statuses):
442
 
                if mp.source_branch_link != source_branch_lp.self_link:
443
 
                    continue
444
 
                yield LaunchpadMergeProposal(mp)
445
 
        elif base_vcs == 'git':
446
 
            (source_repo_lp, source_branch_lp) = (
447
 
                self._get_lp_git_ref_from_branch(source_branch))
448
 
            (target_repo_lp, target_branch_lp) = (
449
 
                self._get_lp_git_ref_from_branch(target_branch))
450
 
            for mp in target_branch_lp.getMergeProposals(status=statuses):
451
 
                if (target_branch_lp.path != mp.target_git_path or
452
 
                        target_repo_lp != mp.target_git_repository or
453
 
                        source_branch_lp.path != mp.source_git_path or
454
 
                        source_repo_lp != mp.source_git_repository):
455
 
                    continue
456
 
                yield LaunchpadMergeProposal(mp)
457
 
        else:
458
 
            raise AssertionError('not a valid Launchpad URL')
459
 
 
460
 
    def get_proposer(self, source_branch, target_branch):
461
 
        (base_vcs, base_user, base_password, base_path,
462
 
            base_params) = self._split_url(target_branch.user_url)
463
 
        if base_vcs == 'bzr':
464
 
            return LaunchpadBazaarMergeProposalBuilder(
465
 
                self, source_branch, target_branch)
466
 
        elif base_vcs == 'git':
467
 
            return LaunchpadGitMergeProposalBuilder(
468
 
                self, source_branch, target_branch)
469
 
        else:
470
 
            raise AssertionError('not a valid Launchpad URL')
471
 
 
472
 
    @classmethod
473
 
    def iter_instances(cls):
474
 
        credential_store = lp_api.get_credential_store()
475
 
        for service_root in set(uris.service_roots.values()):
476
 
            auth_engine = lp_api.get_auth_engine(service_root)
477
 
            creds = credential_store.load(auth_engine.unique_consumer_id)
478
 
            if creds is not None:
479
 
                yield cls(service_root)
480
 
 
481
 
    def iter_my_proposals(self, status='open'):
482
 
        statuses = status_to_lp_mp_statuses(status)
483
 
        for mp in self.launchpad.me.getMergeProposals(status=statuses):
484
 
            yield LaunchpadMergeProposal(mp)
485
 
 
486
 
    def iter_my_forks(self):
487
 
        # Launchpad doesn't really have the concept of "forks"
488
 
        return iter([])
489
 
 
490
 
    def get_proposal_by_url(self, url):
491
 
        # Launchpad doesn't have a way to find a merge proposal by URL.
492
 
        (scheme, user, password, host, port, path) = urlutils.parse_url(
493
 
            url)
494
 
        LAUNCHPAD_CODE_DOMAINS = [
495
 
            ('code.%s' % domain) for domain in lp_uris.LAUNCHPAD_DOMAINS.values()]
496
 
        if host not in LAUNCHPAD_CODE_DOMAINS:
497
 
            raise UnsupportedHoster(url)
498
 
        # TODO(jelmer): Check if this is a launchpad URL. Otherwise, raise
499
 
        # UnsupportedHoster
500
 
        # See https://api.launchpad.net/devel/#branch_merge_proposal
501
 
        # the syntax is:
502
 
        # https://api.launchpad.net/devel/~<author.name>/<project.name>/<branch.name>/+merge/<id>
503
 
        api_url = str(self.launchpad._root_uri) + path
504
 
        mp = self.launchpad.load(api_url)
505
 
        return LaunchpadMergeProposal(mp)
506
 
 
507
 
 
508
 
class LaunchpadBazaarMergeProposalBuilder(MergeProposalBuilder):
509
 
 
510
 
    def __init__(self, lp_host, source_branch, target_branch,
511
 
                 staging=None, approve=None, fixes=None):
512
 
        """Constructor.
513
 
 
514
 
        :param source_branch: The branch to propose for merging.
515
 
        :param target_branch: The branch to merge into.
516
 
        :param staging: If True, propose the merge against staging instead of
517
 
            production.
518
 
        :param approve: If True, mark the new proposal as approved immediately.
519
 
            This is useful when a project permits some things to be approved
520
 
            by the submitter (e.g. merges between release and deployment
521
 
            branches).
522
 
        """
523
 
        self.lp_host = lp_host
524
 
        self.launchpad = lp_host.launchpad
525
 
        self.source_branch = source_branch
526
 
        self.source_branch_lp = self.launchpad.branches.getByUrl(
527
 
            url=source_branch.user_url)
528
 
        if target_branch is None:
529
 
            self.target_branch_lp = self.source_branch_lp.get_target()
530
 
            self.target_branch = _mod_branch.Branch.open(
531
 
                self.target_branch_lp.bzr_identity)
532
 
        else:
533
 
            self.target_branch = target_branch
534
 
            self.target_branch_lp = self.launchpad.branches.getByUrl(
535
 
                url=target_branch.user_url)
536
 
        self.approve = approve
537
 
        self.fixes = fixes
538
 
 
539
 
    def get_infotext(self):
540
 
        """Determine the initial comment for the merge proposal."""
541
 
        info = ["Source: %s\n" % self.source_branch_lp.bzr_identity]
542
 
        info.append("Target: %s\n" % self.target_branch_lp.bzr_identity)
543
 
        return ''.join(info)
544
 
 
545
 
    def get_initial_body(self):
546
 
        """Get a body for the proposal for the user to modify.
547
 
 
548
 
        :return: a str or None.
549
 
        """
550
 
        if not self.hooks['merge_proposal_body']:
551
 
            return None
552
 
 
553
 
        def list_modified_files():
554
 
            lca_tree = self.source_branch_lp.find_lca_tree(
555
 
                self.target_branch_lp)
556
 
            source_tree = self.source_branch.basis_tree()
557
 
            files = modified_files(lca_tree, source_tree)
558
 
            return list(files)
559
 
        with self.target_branch.lock_read(), \
560
 
                self.source_branch.lock_read():
561
 
            body = None
562
 
            for hook in self.hooks['merge_proposal_body']:
563
 
                body = hook({
564
 
                    'target_branch': self.target_branch_lp.bzr_identity,
565
 
                    'modified_files_callback': list_modified_files,
566
 
                    'old_body': body,
567
 
                })
568
 
            return body
569
 
 
570
 
    def check_proposal(self):
571
 
        """Check that the submission is sensible."""
572
 
        if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
573
 
            raise errors.CommandError(
574
 
                'Source and target branches must be different.')
575
 
        for mp in self.source_branch_lp.landing_targets:
576
 
            if mp.queue_status in ('Merged', 'Rejected'):
577
 
                continue
578
 
            if mp.target_branch.self_link == self.target_branch_lp.self_link:
579
 
                raise MergeProposalExists(lp_api.canonical_url(mp))
580
 
 
581
 
    def approve_proposal(self, mp):
582
 
        with self.source_branch.lock_read():
583
 
            _call_webservice(
584
 
                mp.createComment,
585
 
                vote=u'Approve',
586
 
                subject='',  # Use the default subject.
587
 
                content=u"Rubberstamp! Proposer approves of own proposal.")
588
 
            _call_webservice(mp.setStatus, status=u'Approved',
589
 
                             revid=self.source_branch.last_revision())
590
 
 
591
 
    def create_proposal(self, description, reviewers=None, labels=None,
592
 
                        prerequisite_branch=None, commit_message=None,
593
 
                        work_in_progress=False, allow_collaboration=False):
594
 
        """Perform the submission."""
595
 
        if labels:
596
 
            raise LabelsUnsupported(self)
597
 
        if prerequisite_branch is not None:
598
 
            prereq = self.launchpad.branches.getByUrl(
599
 
                url=prerequisite_branch.user_url)
600
 
        else:
601
 
            prereq = None
602
 
        if reviewers is None:
603
 
            reviewer_objs = []
604
 
        else:
605
 
            reviewer_objs = []
606
 
            for reviewer in reviewers:
607
 
                if '@' in reviewer:
608
 
                    reviewer_obj = self.launchpad.people.getByEmail(email=reviewer)
609
 
                else:
610
 
                    reviewer_obj = self.launchpad.people[reviewer]
611
 
                reviewer_objs.append(reviewer_obj)
612
 
        try:
613
 
            mp = _call_webservice(
614
 
                self.source_branch_lp.createMergeProposal,
615
 
                target_branch=self.target_branch_lp,
616
 
                prerequisite_branch=prereq,
617
 
                initial_comment=description.strip(),
618
 
                commit_message=commit_message,
619
 
                needs_review=(not work_in_progress),
620
 
                reviewers=[reviewer.self_link for reviewer in reviewer_objs],
621
 
                review_types=['' for reviewer in reviewer_objs])
622
 
        except WebserviceFailure as e:
623
 
            # Urgh.
624
 
            if (b'There is already a branch merge proposal '
625
 
                    b'registered for branch ') in e.message:
626
 
                raise MergeProposalExists(self.source_branch.user_url)
627
 
            raise
628
 
 
629
 
        if self.approve:
630
 
            self.approve_proposal(mp)
631
 
        if self.fixes:
632
 
            if self.fixes.startswith('lp:'):
633
 
                self.fixes = self.fixes[3:]
634
 
            _call_webservice(
635
 
                mp.linkBug,
636
 
                bug=self.launchpad.bugs[int(self.fixes)])
637
 
        return LaunchpadMergeProposal(mp)
638
 
 
639
 
 
640
 
class LaunchpadGitMergeProposalBuilder(MergeProposalBuilder):
641
 
 
642
 
    def __init__(self, lp_host, source_branch, target_branch,
643
 
                 staging=None, approve=None, fixes=None):
644
 
        """Constructor.
645
 
 
646
 
        :param source_branch: The branch to propose for merging.
647
 
        :param target_branch: The branch to merge into.
648
 
        :param staging: If True, propose the merge against staging instead of
649
 
            production.
650
 
        :param approve: If True, mark the new proposal as approved immediately.
651
 
            This is useful when a project permits some things to be approved
652
 
            by the submitter (e.g. merges between release and deployment
653
 
            branches).
654
 
        """
655
 
        self.lp_host = lp_host
656
 
        self.launchpad = lp_host.launchpad
657
 
        self.source_branch = source_branch
658
 
        (self.source_repo_lp,
659
 
            self.source_branch_lp) = self.lp_host._get_lp_git_ref_from_branch(
660
 
                source_branch)
661
 
        if target_branch is None:
662
 
            self.target_branch_lp = self.source_branch.get_target()
663
 
            self.target_branch = _mod_branch.Branch.open(
664
 
                self.target_branch_lp.git_https_url)
665
 
        else:
666
 
            self.target_branch = target_branch
667
 
            (self.target_repo_lp, self.target_branch_lp) = (
668
 
                self.lp_host._get_lp_git_ref_from_branch(target_branch))
669
 
        self.approve = approve
670
 
        self.fixes = fixes
671
 
 
672
 
    def get_infotext(self):
673
 
        """Determine the initial comment for the merge proposal."""
674
 
        info = ["Source: %s\n" % self.source_branch.user_url]
675
 
        info.append("Target: %s\n" % self.target_branch.user_url)
676
 
        return ''.join(info)
677
 
 
678
 
    def get_initial_body(self):
679
 
        """Get a body for the proposal for the user to modify.
680
 
 
681
 
        :return: a str or None.
682
 
        """
683
 
        if not self.hooks['merge_proposal_body']:
684
 
            return None
685
 
 
686
 
        def list_modified_files():
687
 
            lca_tree = self.source_branch_lp.find_lca_tree(
688
 
                self.target_branch_lp)
689
 
            source_tree = self.source_branch.basis_tree()
690
 
            files = modified_files(lca_tree, source_tree)
691
 
            return list(files)
692
 
        with self.target_branch.lock_read(), \
693
 
                self.source_branch.lock_read():
694
 
            body = None
695
 
            for hook in self.hooks['merge_proposal_body']:
696
 
                body = hook({
697
 
                    'target_branch': self.target_branch,
698
 
                    'modified_files_callback': list_modified_files,
699
 
                    'old_body': body,
700
 
                })
701
 
            return body
702
 
 
703
 
    def check_proposal(self):
704
 
        """Check that the submission is sensible."""
705
 
        if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
706
 
            raise errors.CommandError(
707
 
                'Source and target branches must be different.')
708
 
        for mp in self.source_branch_lp.landing_targets:
709
 
            if mp.queue_status in ('Merged', 'Rejected'):
710
 
                continue
711
 
            if mp.target_branch.self_link == self.target_branch_lp.self_link:
712
 
                raise MergeProposalExists(lp_api.canonical_url(mp))
713
 
 
714
 
    def approve_proposal(self, mp):
715
 
        with self.source_branch.lock_read():
716
 
            _call_webservice(
717
 
                mp.createComment,
718
 
                vote=u'Approve',
719
 
                subject='',  # Use the default subject.
720
 
                content=u"Rubberstamp! Proposer approves of own proposal.")
721
 
            _call_webservice(
722
 
                mp.setStatus, status=u'Approved',
723
 
                revid=self.source_branch.last_revision())
724
 
 
725
 
    def create_proposal(self, description, reviewers=None, labels=None,
726
 
                        prerequisite_branch=None, commit_message=None,
727
 
                        work_in_progress=False, allow_collaboration=False):
728
 
        """Perform the submission."""
729
 
        if labels:
730
 
            raise LabelsUnsupported(self)
731
 
        if prerequisite_branch is not None:
732
 
            (prereq_repo_lp, prereq_branch_lp) = (
733
 
                self.lp_host._get_lp_git_ref_from_branch(prerequisite_branch))
734
 
        else:
735
 
            prereq_branch_lp = None
736
 
        if reviewers is None:
737
 
            reviewers = []
738
 
        try:
739
 
            mp = _call_webservice(
740
 
                self.source_branch_lp.createMergeProposal,
741
 
                merge_target=self.target_branch_lp,
742
 
                merge_prerequisite=prereq_branch_lp,
743
 
                initial_comment=description.strip(),
744
 
                commit_message=commit_message,
745
 
                needs_review=(not work_in_progress),
746
 
                reviewers=[self.launchpad.people[reviewer].self_link
747
 
                           for reviewer in reviewers],
748
 
                review_types=[None for reviewer in reviewers])
749
 
        except WebserviceFailure as e:
750
 
            # Urgh.
751
 
            if ('There is already a branch merge proposal '
752
 
                    'registered for branch ') in e.message:
753
 
                raise MergeProposalExists(self.source_branch.user_url)
754
 
            raise
755
 
        if self.approve:
756
 
            self.approve_proposal(mp)
757
 
        if self.fixes:
758
 
            if self.fixes.startswith('lp:'):
759
 
                self.fixes = self.fixes[3:]
760
 
            _call_webservice(
761
 
                mp.linkBug,
762
 
                bug=self.launchpad.bugs[int(self.fixes)])
763
 
        return LaunchpadMergeProposal(mp)
764
 
 
765
 
 
766
 
def modified_files(old_tree, new_tree):
767
 
    """Return a list of paths in the new tree with modified contents."""
768
 
    for change in new_tree.iter_changes(old_tree):
769
 
        if change.changed_content and change.kind[1] == 'file':
770
 
            yield str(path)