/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: 2019-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
 
18
18
"""Support for Launchpad."""
19
19
 
 
20
from __future__ import absolute_import
 
21
 
20
22
import re
21
 
import shutil
22
 
import tempfile
23
23
 
24
 
from ...propose import (
 
24
from .propose import (
25
25
    Hoster,
26
26
    LabelsUnsupported,
27
27
    MergeProposal,
37
37
    hooks,
38
38
    urlutils,
39
39
    )
40
 
from ...git.urls import git_url_to_bzr_url
 
40
from ...git.refs import ref_to_branch_name
41
41
from ...lazy_import import lazy_import
42
42
lazy_import(globals(), """
43
43
from breezy.plugins.launchpad import (
44
44
    lp_api,
45
 
    uris as lp_uris,
46
45
    )
47
46
 
48
47
from launchpadlib import uris
73
72
        return False
74
73
    if url.startswith('lp:'):
75
74
        return True
76
 
    regex = re.compile(r'([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
77
 
                       r'://(bazaar|git).*\.launchpad\.net')
 
75
    regex = re.compile('([a-z]*\+)*(bzr\+ssh|http|ssh|git|https)'
 
76
                       '://(bazaar|git).*.launchpad.net')
78
77
    return bool(regex.match(url))
79
78
 
80
79
 
113
112
        if self._mp.source_branch:
114
113
            return self._mp.source_branch.bzr_identity
115
114
        else:
116
 
            return git_url_to_bzr_url(
 
115
            branch_name = ref_to_branch_name(
 
116
                self._mp.source_git_path.encode('utf-8'))
 
117
            return urlutils.join_segment_parameters(
117
118
                self._mp.source_git_repository.git_identity,
118
 
                ref=self._mp.source_git_path.encode('utf-8'))
119
 
 
120
 
    def get_source_revision(self):
121
 
        if self._mp.source_branch:
122
 
            last_scanned_id = self._mp.source_branch.last_scanned_id
123
 
            if last_scanned_id:
124
 
                return last_scanned_id.encode('utf-8')
125
 
            else:
126
 
                return None
127
 
        else:
128
 
            from breezy.git.mapping import default_mapping
129
 
            git_repo = self._mp.source_git_repository
130
 
            git_ref = git_repo.getRefByPath(path=self._mp.source_git_path)
131
 
            sha = git_ref.commit_sha1
132
 
            if sha is None:
133
 
                return None
134
 
            return default_mapping.revision_id_foreign_to_bzr(
135
 
                sha.encode('ascii'))
 
119
                {"branch": branch_name})
136
120
 
137
121
    def get_target_branch_url(self):
138
122
        if self._mp.target_branch:
139
123
            return self._mp.target_branch.bzr_identity
140
124
        else:
141
 
            return git_url_to_bzr_url(
 
125
            branch_name = ref_to_branch_name(
 
126
                self._mp.target_git_path.encode('utf-8'))
 
127
            return urlutils.join_segment_parameters(
142
128
                self._mp.target_git_repository.git_identity,
143
 
                ref=self._mp.target_git_path.encode('utf-8'))
 
129
                {"branch": branch_name})
144
130
 
145
131
    @property
146
132
    def url(self):
149
135
    def is_merged(self):
150
136
        return (self._mp.queue_status == 'Merged')
151
137
 
152
 
    def is_closed(self):
153
 
        return (self._mp.queue_status in ('Rejected', 'Superseded'))
154
 
 
155
 
    def reopen(self):
156
 
        self._mp.setStatus(status='Needs review')
157
 
 
158
138
    def get_description(self):
159
139
        return self._mp.description
160
140
 
162
142
        self._mp.description = description
163
143
        self._mp.lp_save()
164
144
 
165
 
    def get_commit_message(self):
166
 
        return self._mp.commit_message
167
 
 
168
 
    def set_commit_message(self, commit_message):
169
 
        self._mp.commit_message = commit_message
170
 
        self._mp.lp_save()
171
 
 
172
145
    def close(self):
173
146
        self._mp.setStatus(status='Rejected')
174
147
 
175
 
    def can_be_merged(self):
176
 
        if not self._mp.preview_diff:
177
 
            # Maybe?
178
 
            return True
179
 
        return not bool(self._mp.preview_diff.conflicts)
180
 
 
181
 
    def get_merged_by(self):
182
 
        merge_reporter = self._mp.merge_reporter
183
 
        if merge_reporter is None:
184
 
            return None
185
 
        return merge_reporter.name
186
 
 
187
 
    def get_merged_at(self):
188
 
        return self._mp.date_merged
189
 
 
190
 
    def merge(self, commit_message=None):
191
 
        target_branch = _mod_branch.Branch.open(
192
 
            self.get_target_branch_url())
193
 
        source_branch = _mod_branch.Branch.open(
194
 
            self.get_source_branch_url())
195
 
        # TODO(jelmer): Ideally this would use a memorytree, but merge doesn't
196
 
        # support that yet.
197
 
        # tree = target_branch.create_memorytree()
198
 
        tmpdir = tempfile.mkdtemp()
199
 
        try:
200
 
            tree = target_branch.create_checkout(
201
 
                to_location=tmpdir, lightweight=True)
202
 
            tree.merge_from_branch(source_branch)
203
 
            tree.commit(commit_message or self._mp.commit_message)
204
 
        finally:
205
 
            shutil.rmtree(tmpdir)
206
 
 
207
 
    def post_comment(self, body):
208
 
        self._mp.createComment(content=body)
209
 
 
210
148
 
211
149
class Launchpad(Hoster):
212
150
    """The Launchpad hosting service."""
216
154
    # https://bugs.launchpad.net/launchpad/+bug/397676
217
155
    supports_merge_proposal_labels = False
218
156
 
219
 
    supports_merge_proposal_commit_message = True
220
 
 
221
 
    supports_allow_collaboration = False
222
 
 
223
 
    merge_proposal_description_format = 'plain'
224
 
 
225
 
    def __init__(self, service_root):
226
 
        self._api_base_url = service_root
227
 
        self._launchpad = None
228
 
 
229
 
    @property
230
 
    def name(self):
231
 
        if self._api_base_url == uris.LPNET_SERVICE_ROOT:
232
 
            return 'Launchpad'
233
 
        return 'Launchpad at %s' % self.base_url
234
 
 
235
 
    @property
236
 
    def launchpad(self):
237
 
        if self._launchpad is None:
238
 
            self._launchpad = lp_api.connect_launchpad(self._api_base_url, version='devel')
239
 
        return self._launchpad
 
157
    def __init__(self, staging=False):
 
158
        self._staging = staging
 
159
        if staging:
 
160
            lp_base_url = uris.STAGING_SERVICE_ROOT
 
161
        else:
 
162
            lp_base_url = uris.LPNET_SERVICE_ROOT
 
163
        self.launchpad = lp_api.connect_launchpad(lp_base_url)
240
164
 
241
165
    @property
242
166
    def base_url(self):
243
 
        return lp_api.uris.web_root_for_service_root(self._api_base_url)
 
167
        return lp_api.uris.web_root_for_service_root(
 
168
            str(self.launchpad._root_uri))
244
169
 
245
170
    def __repr__(self):
246
 
        return "Launchpad(service_root=%s)" % self._api_base_url
247
 
 
248
 
    def get_current_user(self):
249
 
        return self.launchpad.me.name
250
 
 
251
 
    def get_user_url(self, username):
252
 
        return self.launchpad.people[username].web_link
 
171
        return "Launchpad(staging=%s)" % self._staging
253
172
 
254
173
    def hosts(self, branch):
255
174
        # TODO(jelmer): staging vs non-staging?
256
175
        return plausible_launchpad_url(branch.user_url)
257
176
 
258
177
    @classmethod
259
 
    def probe_from_url(cls, url, possible_transports=None):
260
 
        if plausible_launchpad_url(url):
261
 
            return Launchpad(uris.LPNET_SERVICE_ROOT)
262
 
        raise UnsupportedHoster(url)
 
178
    def probe(cls, branch):
 
179
        if plausible_launchpad_url(branch.user_url):
 
180
            return Launchpad()
 
181
        raise UnsupportedHoster(branch)
263
182
 
264
183
    def _get_lp_git_ref_from_branch(self, branch):
265
184
        url, params = urlutils.split_segment_parameters(branch.user_url)
293
212
        return "~%s/%s" % (owner, project)
294
213
 
295
214
    def _publish_git(self, local_branch, base_path, name, owner, project=None,
296
 
                     revision_id=None, overwrite=False, allow_lossy=True,
297
 
                     tag_selector=None):
 
215
                     revision_id=None, overwrite=False, allow_lossy=True):
298
216
        to_path = self._get_derived_git_path(base_path, owner, project)
299
217
        to_transport = get_transport("git+ssh://git.launchpad.net/" + to_path)
300
218
        try:
306
224
        if dir_to is None:
307
225
            try:
308
226
                br_to = local_branch.create_clone_on_transport(
309
 
                    to_transport, revision_id=revision_id, name=name,
310
 
                    tag_selector=tag_selector)
 
227
                    to_transport, revision_id=revision_id, name=name)
311
228
            except errors.NoRoundtrippingSupport:
312
229
                br_to = local_branch.create_clone_on_transport(
313
230
                    to_transport, revision_id=revision_id, name=name,
314
 
                    lossy=True, tag_selector=tag_selector)
 
231
                    lossy=True)
315
232
        else:
316
233
            try:
317
234
                dir_to = dir_to.push_branch(
318
 
                    local_branch, revision_id, overwrite=overwrite, name=name,
319
 
                    tag_selector=tag_selector)
 
235
                    local_branch, revision_id, overwrite=overwrite, name=name)
320
236
            except errors.NoRoundtrippingSupport:
321
237
                if not allow_lossy:
322
238
                    raise
323
239
                dir_to = dir_to.push_branch(
324
240
                    local_branch, revision_id, overwrite=overwrite, name=name,
325
 
                    lossy=True, tag_selector=tag_selector)
 
241
                    lossy=True)
326
242
            br_to = dir_to.target_branch
327
243
        return br_to, (
328
244
            "https://git.launchpad.net/%s/+ref/%s" % (to_path, name))
348
264
 
349
265
    def _publish_bzr(self, local_branch, base_branch, name, owner,
350
266
                     project=None, revision_id=None, overwrite=False,
351
 
                     allow_lossy=True, tag_selector=None):
 
267
                     allow_lossy=True):
352
268
        to_path = self._get_derived_bzr_path(base_branch, name, owner, project)
353
269
        to_transport = get_transport("lp:" + to_path)
354
270
        try:
359
275
 
360
276
        if dir_to is None:
361
277
            br_to = local_branch.create_clone_on_transport(
362
 
                to_transport, revision_id=revision_id, tag_selector=tag_selector)
 
278
                to_transport, revision_id=revision_id)
363
279
        else:
364
280
            br_to = dir_to.push_branch(
365
 
                local_branch, revision_id, overwrite=overwrite,
366
 
                tag_selector=tag_selector).target_branch
 
281
                local_branch, revision_id, overwrite=overwrite).target_branch
367
282
        return br_to, ("https://code.launchpad.net/" + to_path)
368
283
 
369
284
    def _split_url(self, url):
380
295
 
381
296
    def publish_derived(self, local_branch, base_branch, name, project=None,
382
297
                        owner=None, revision_id=None, overwrite=False,
383
 
                        allow_lossy=True, tag_selector=None):
 
298
                        allow_lossy=True):
384
299
        """Publish a branch to the site, derived from base_branch.
385
300
 
386
301
        :param base_branch: branch to derive the new branch from
399
314
            return self._publish_bzr(
400
315
                local_branch, base_branch, name, project=project, owner=owner,
401
316
                revision_id=revision_id, overwrite=overwrite,
402
 
                allow_lossy=allow_lossy, tag_selector=tag_selector)
 
317
                allow_lossy=allow_lossy)
403
318
        elif base_vcs == 'git':
404
319
            return self._publish_git(
405
320
                local_branch, base_path, name, project=project, owner=owner,
406
321
                revision_id=revision_id, overwrite=overwrite,
407
 
                allow_lossy=allow_lossy, tag_selector=tag_selector)
 
322
                allow_lossy=allow_lossy)
408
323
        else:
409
324
            raise AssertionError('not a valid Launchpad URL')
410
325
 
469
384
 
470
385
    @classmethod
471
386
    def iter_instances(cls):
472
 
        credential_store = lp_api.get_credential_store()
473
 
        for service_root in set(uris.service_roots.values()):
474
 
            auth_engine = lp_api.get_auth_engine(service_root)
475
 
            creds = credential_store.load(auth_engine.unique_consumer_id)
476
 
            if creds is not None:
477
 
                yield cls(service_root)
 
387
        yield cls()
478
388
 
479
389
    def iter_my_proposals(self, status='open'):
480
390
        statuses = status_to_lp_mp_statuses(status)
481
391
        for mp in self.launchpad.me.getMergeProposals(status=statuses):
482
392
            yield LaunchpadMergeProposal(mp)
483
393
 
484
 
    def iter_my_forks(self):
485
 
        # Launchpad doesn't really have the concept of "forks"
486
 
        return iter([])
487
 
 
488
 
    def get_proposal_by_url(self, url):
489
 
        # Launchpad doesn't have a way to find a merge proposal by URL.
490
 
        (scheme, user, password, host, port, path) = urlutils.parse_url(
491
 
            url)
492
 
        LAUNCHPAD_CODE_DOMAINS = [
493
 
            ('code.%s' % domain) for domain in lp_uris.LAUNCHPAD_DOMAINS.values()]
494
 
        if host not in LAUNCHPAD_CODE_DOMAINS:
495
 
            raise UnsupportedHoster(url)
496
 
        # TODO(jelmer): Check if this is a launchpad URL. Otherwise, raise
497
 
        # UnsupportedHoster
498
 
        # See https://api.launchpad.net/devel/#branch_merge_proposal
499
 
        # the syntax is:
500
 
        # https://api.launchpad.net/devel/~<author.name>/<project.name>/<branch.name>/+merge/<id>
501
 
        api_url = str(self.launchpad._root_uri) + path
502
 
        mp = self.launchpad.load(api_url)
503
 
        return LaunchpadMergeProposal(mp)
504
 
 
505
394
 
506
395
class LaunchpadBazaarMergeProposalBuilder(MergeProposalBuilder):
507
396
 
508
 
    def __init__(self, lp_host, source_branch, target_branch,
 
397
    def __init__(self, lp_host, source_branch, target_branch, message=None,
509
398
                 staging=None, approve=None, fixes=None):
510
399
        """Constructor.
511
400
 
512
401
        :param source_branch: The branch to propose for merging.
513
402
        :param target_branch: The branch to merge into.
 
403
        :param message: The commit message to use.  (May be None.)
514
404
        :param staging: If True, propose the merge against staging instead of
515
405
            production.
516
406
        :param approve: If True, mark the new proposal as approved immediately.
531
421
            self.target_branch = target_branch
532
422
            self.target_branch_lp = self.launchpad.branches.getByUrl(
533
423
                url=target_branch.user_url)
 
424
        self.commit_message = message
534
425
        self.approve = approve
535
426
        self.fixes = fixes
536
427
 
537
428
    def get_infotext(self):
538
429
        """Determine the initial comment for the merge proposal."""
 
430
        if self.commit_message is not None:
 
431
            return self.commit_message.strip().encode('utf-8')
539
432
        info = ["Source: %s\n" % self.source_branch_lp.bzr_identity]
540
433
        info.append("Target: %s\n" % self.target_branch_lp.bzr_identity)
541
434
        return ''.join(info)
568
461
    def check_proposal(self):
569
462
        """Check that the submission is sensible."""
570
463
        if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
571
 
            raise errors.CommandError(
 
464
            raise errors.BzrCommandError(
572
465
                'Source and target branches must be different.')
573
466
        for mp in self.source_branch_lp.landing_targets:
574
467
            if mp.queue_status in ('Merged', 'Rejected'):
587
480
                             revid=self.source_branch.last_revision())
588
481
 
589
482
    def create_proposal(self, description, reviewers=None, labels=None,
590
 
                        prerequisite_branch=None, commit_message=None,
591
 
                        work_in_progress=False, allow_collaboration=False):
 
483
                        prerequisite_branch=None):
592
484
        """Perform the submission."""
593
485
        if labels:
594
 
            raise LabelsUnsupported(self)
 
486
            raise LabelsUnsupported()
595
487
        if prerequisite_branch is not None:
596
488
            prereq = self.launchpad.branches.getByUrl(
597
489
                url=prerequisite_branch.user_url)
598
490
        else:
599
491
            prereq = None
600
492
        if reviewers is None:
601
 
            reviewer_objs = []
602
 
        else:
603
 
            reviewer_objs = []
604
 
            for reviewer in reviewers:
605
 
                if '@' in reviewer:
606
 
                    reviewer_obj = self.launchpad.people.getByEmail(email=reviewer)
607
 
                else:
608
 
                    reviewer_obj = self.launchpad.people[reviewer]
609
 
                reviewer_objs.append(reviewer_obj)
 
493
            reviewers = []
610
494
        try:
611
495
            mp = _call_webservice(
612
496
                self.source_branch_lp.createMergeProposal,
613
497
                target_branch=self.target_branch_lp,
614
498
                prerequisite_branch=prereq,
615
499
                initial_comment=description.strip(),
616
 
                commit_message=commit_message,
617
 
                needs_review=(not work_in_progress),
618
 
                reviewers=[reviewer.self_link for reviewer in reviewer_objs],
619
 
                review_types=['' for reviewer in reviewer_objs])
 
500
                commit_message=self.commit_message,
 
501
                reviewers=[self.launchpad.people[reviewer].self_link
 
502
                           for reviewer in reviewers],
 
503
                review_types=[None for reviewer in reviewers])
620
504
        except WebserviceFailure as e:
621
505
            # Urgh.
622
506
            if (b'There is already a branch merge proposal '
637
521
 
638
522
class LaunchpadGitMergeProposalBuilder(MergeProposalBuilder):
639
523
 
640
 
    def __init__(self, lp_host, source_branch, target_branch,
 
524
    def __init__(self, lp_host, source_branch, target_branch, message=None,
641
525
                 staging=None, approve=None, fixes=None):
642
526
        """Constructor.
643
527
 
644
528
        :param source_branch: The branch to propose for merging.
645
529
        :param target_branch: The branch to merge into.
 
530
        :param message: The commit message to use.  (May be None.)
646
531
        :param staging: If True, propose the merge against staging instead of
647
532
            production.
648
533
        :param approve: If True, mark the new proposal as approved immediately.
664
549
            self.target_branch = target_branch
665
550
            (self.target_repo_lp, self.target_branch_lp) = (
666
551
                self.lp_host._get_lp_git_ref_from_branch(target_branch))
 
552
        self.commit_message = message
667
553
        self.approve = approve
668
554
        self.fixes = fixes
669
555
 
670
556
    def get_infotext(self):
671
557
        """Determine the initial comment for the merge proposal."""
 
558
        if self.commit_message is not None:
 
559
            return self.commit_message.strip().encode('utf-8')
672
560
        info = ["Source: %s\n" % self.source_branch.user_url]
673
561
        info.append("Target: %s\n" % self.target_branch.user_url)
674
562
        return ''.join(info)
701
589
    def check_proposal(self):
702
590
        """Check that the submission is sensible."""
703
591
        if self.source_branch_lp.self_link == self.target_branch_lp.self_link:
704
 
            raise errors.CommandError(
 
592
            raise errors.BzrCommandError(
705
593
                'Source and target branches must be different.')
706
594
        for mp in self.source_branch_lp.landing_targets:
707
595
            if mp.queue_status in ('Merged', 'Rejected'):
721
609
                revid=self.source_branch.last_revision())
722
610
 
723
611
    def create_proposal(self, description, reviewers=None, labels=None,
724
 
                        prerequisite_branch=None, commit_message=None,
725
 
                        work_in_progress=False, allow_collaboration=False):
 
612
                        prerequisite_branch=None):
726
613
        """Perform the submission."""
727
614
        if labels:
728
 
            raise LabelsUnsupported(self)
 
615
            raise LabelsUnsupported()
729
616
        if prerequisite_branch is not None:
730
617
            (prereq_repo_lp, prereq_branch_lp) = (
731
618
                self.lp_host._get_lp_git_ref_from_branch(prerequisite_branch))
739
626
                merge_target=self.target_branch_lp,
740
627
                merge_prerequisite=prereq_branch_lp,
741
628
                initial_comment=description.strip(),
742
 
                commit_message=commit_message,
743
 
                needs_review=(not work_in_progress),
 
629
                commit_message=self.commit_message,
 
630
                needs_review=True,
744
631
                reviewers=[self.launchpad.people[reviewer].self_link
745
632
                           for reviewer in reviewers],
746
633
                review_types=[None for reviewer in reviewers])
763
650
 
764
651
def modified_files(old_tree, new_tree):
765
652
    """Return a list of paths in the new tree with modified contents."""
766
 
    for change in new_tree.iter_changes(old_tree):
767
 
        if change.changed_content and change.kind[1] == 'file':
 
653
    for f, (op, path), c, v, p, n, (ok, k), e in new_tree.iter_changes(
 
654
            old_tree):
 
655
        if c and k == 'file':
768
656
            yield str(path)