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

  • Committer: Jelmer Vernooij
  • Date: 2020-09-02 16:35:18 UTC
  • mto: (7490.40.109 work)
  • mto: This revision was merged to the branch mainline in revision 7526.
  • Revision ID: jelmer@jelmer.uk-20200902163518-sy9f4unbboljphgu
Handle duplicate directories entries for git.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2018-2019 Breezy Developers
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
16
 
 
17
"""Helper functions for proposing merges."""
 
18
 
 
19
from __future__ import absolute_import
 
20
 
 
21
from . import (
 
22
    errors,
 
23
    hooks,
 
24
    registry,
 
25
    urlutils,
 
26
    )
 
27
 
 
28
 
 
29
class NoSuchProject(errors.BzrError):
 
30
 
 
31
    _fmt = "Project does not exist: %(project)s."
 
32
 
 
33
    def __init__(self, project):
 
34
        errors.BzrError.__init__(self)
 
35
        self.project = project
 
36
 
 
37
 
 
38
class MergeProposalExists(errors.BzrError):
 
39
 
 
40
    _fmt = "A merge proposal already exists: %(url)s."
 
41
 
 
42
    def __init__(self, url):
 
43
        errors.BzrError.__init__(self)
 
44
        self.url = url
 
45
 
 
46
 
 
47
class UnsupportedHoster(errors.BzrError):
 
48
 
 
49
    _fmt = "No supported hoster for %(branch)s."
 
50
 
 
51
    def __init__(self, branch):
 
52
        errors.BzrError.__init__(self)
 
53
        self.branch = branch
 
54
 
 
55
 
 
56
class ReopenFailed(errors.BzrError):
 
57
 
 
58
    _fmt = "Reopening the merge proposal failed: %(error)s."
 
59
 
 
60
 
 
61
class ProposeMergeHooks(hooks.Hooks):
 
62
    """Hooks for proposing a merge on Launchpad."""
 
63
 
 
64
    def __init__(self):
 
65
        hooks.Hooks.__init__(self, __name__, "Proposer.hooks")
 
66
        self.add_hook(
 
67
            'get_prerequisite',
 
68
            "Return the prerequisite branch for proposing as merge.", (3, 0))
 
69
        self.add_hook(
 
70
            'merge_proposal_body',
 
71
            "Return an initial body for the merge proposal message.", (3, 0))
 
72
 
 
73
 
 
74
class LabelsUnsupported(errors.BzrError):
 
75
    """Labels not supported by this hoster."""
 
76
 
 
77
    _fmt = "Labels are not supported by %(hoster)r."
 
78
 
 
79
    def __init__(self, hoster):
 
80
        errors.BzrError.__init__(self)
 
81
        self.hoster = hoster
 
82
 
 
83
 
 
84
class PrerequisiteBranchUnsupported(errors.BzrError):
 
85
    """Prerequisite branch not supported by this hoster."""
 
86
 
 
87
    def __init__(self, hoster):
 
88
        errors.BzrError.__init__(self)
 
89
        self.hoster = hoster
 
90
 
 
91
 
 
92
class HosterLoginRequired(errors.BzrError):
 
93
    """Action requires hoster login credentials."""
 
94
 
 
95
    _fmt = "Action requires credentials for hosting site %(hoster)r."""
 
96
 
 
97
    def __init__(self, hoster):
 
98
        errors.BzrError.__init__(self)
 
99
        self.hoster = hoster
 
100
 
 
101
 
 
102
class SourceNotDerivedFromTarget(errors.BzrError):
 
103
    """Source branch is not derived from target branch."""
 
104
 
 
105
    _fmt = ("Source %(source_branch)r not derived from "
 
106
            "target %(target_branch)r.")
 
107
 
 
108
    def __init__(self, source_branch, target_branch):
 
109
        errors.BzrError.__init__(
 
110
            self, source_branch=source_branch,
 
111
            target_branch=target_branch)
 
112
 
 
113
 
 
114
class MergeProposal(object):
 
115
    """A merge proposal.
 
116
 
 
117
    :ivar url: URL for the merge proposal
 
118
    """
 
119
 
 
120
    def __init__(self, url=None):
 
121
        self.url = url
 
122
 
 
123
    def get_description(self):
 
124
        """Get the description of the merge proposal."""
 
125
        raise NotImplementedError(self.get_description)
 
126
 
 
127
    def set_description(self, description):
 
128
        """Set the description of the merge proposal."""
 
129
        raise NotImplementedError(self.set_description)
 
130
 
 
131
    def get_commit_message(self):
 
132
        """Get the proposed commit message."""
 
133
        raise NotImplementedError(self.get_commit_message)
 
134
 
 
135
    def set_commit_message(self, commit_message):
 
136
        """Set the propose commit message."""
 
137
        raise NotImplementedError(self.set_commit_message)
 
138
 
 
139
    def get_source_branch_url(self):
 
140
        """Return the source branch."""
 
141
        raise NotImplementedError(self.get_source_branch_url)
 
142
 
 
143
    def get_source_revision(self):
 
144
        """Return the latest revision for the source branch."""
 
145
        raise NotImplementedError(self.get_source_revision)
 
146
 
 
147
    def get_target_branch_url(self):
 
148
        """Return the target branch."""
 
149
        raise NotImplementedError(self.get_target_branch_url)
 
150
 
 
151
    def get_source_project(self):
 
152
        raise NotImplementedError(self.get_source_project)
 
153
 
 
154
    def get_target_project(self):
 
155
        raise NotImplementedError(self.get_target_project)
 
156
 
 
157
    def close(self):
 
158
        """Close the merge proposal (without merging it)."""
 
159
        raise NotImplementedError(self.close)
 
160
 
 
161
    def is_merged(self):
 
162
        """Check whether this merge proposal has been merged."""
 
163
        raise NotImplementedError(self.is_merged)
 
164
 
 
165
    def is_closed(self):
 
166
        """Check whether this merge proposal is closed
 
167
 
 
168
        This can either mean that it is merged or rejected.
 
169
        """
 
170
        raise NotImplementedError(self.is_closed)
 
171
 
 
172
    def merge(self, commit_message=None):
 
173
        """Merge this merge proposal."""
 
174
        raise NotImplementedError(self.merge)
 
175
 
 
176
    def can_be_merged(self):
 
177
        """Can this merge proposal be merged?
 
178
 
 
179
        The answer to this can be no if e.g. it has conflics.
 
180
        """
 
181
        raise NotImplementedError(self.can_be_merged)
 
182
 
 
183
    def get_merged_by(self):
 
184
        """If this proposal was merged, who merged it.
 
185
        """
 
186
        raise NotImplementedError(self.get_merged_by)
 
187
 
 
188
    def get_merged_at(self):
 
189
        """If this proposal was merged, when it was merged.
 
190
        """
 
191
        raise NotImplementedError(self.get_merged_at)
 
192
 
 
193
    def post_comment(self, body):
 
194
        """Post a comment on the merge proposal.
 
195
 
 
196
        Args:
 
197
          body: Body of the comment
 
198
        """
 
199
        raise NotImplementedError(self.post_comment)
 
200
 
 
201
 
 
202
class MergeProposalBuilder(object):
 
203
    """Merge proposal creator.
 
204
 
 
205
    :param source_branch: Branch to propose for merging
 
206
    :param target_branch: Target branch
 
207
    """
 
208
 
 
209
    hooks = ProposeMergeHooks()
 
210
 
 
211
    def __init__(self, source_branch, target_branch):
 
212
        self.source_branch = source_branch
 
213
        self.target_branch = target_branch
 
214
 
 
215
    def get_initial_body(self):
 
216
        """Get a body for the proposal for the user to modify.
 
217
 
 
218
        :return: a str or None.
 
219
        """
 
220
        raise NotImplementedError(self.get_initial_body)
 
221
 
 
222
    def get_infotext(self):
 
223
        """Determine the initial comment for the merge proposal.
 
224
        """
 
225
        raise NotImplementedError(self.get_infotext)
 
226
 
 
227
    def create_proposal(self, description, reviewers=None, labels=None,
 
228
                        prerequisite_branch=None, commit_message=None,
 
229
                        work_in_progress=False, allow_collaboration=False):
 
230
        """Create a proposal to merge a branch for merging.
 
231
 
 
232
        :param description: Description for the merge proposal
 
233
        :param reviewers: Optional list of people to ask reviews from
 
234
        :param labels: Labels to attach to the proposal
 
235
        :param prerequisite_branch: Optional prerequisite branch
 
236
        :param commit_message: Optional commit message
 
237
        :param work_in_progress:
 
238
            Whether this merge proposal is still a work-in-progress
 
239
        :param allow_collaboration:
 
240
            Whether to allow changes to the branch from the target branch
 
241
            maintainer(s)
 
242
        :return: A `MergeProposal` object
 
243
        """
 
244
        raise NotImplementedError(self.create_proposal)
 
245
 
 
246
 
 
247
class Hoster(object):
 
248
    """A hosting site manager.
 
249
    """
 
250
 
 
251
    # Does this hoster support arbitrary labels being attached to merge
 
252
    # proposals?
 
253
    supports_merge_proposal_labels = None
 
254
 
 
255
    @property
 
256
    def name(self):
 
257
        """Name of this instance."""
 
258
        return "%s at %s" % (type(self).__name__, self.base_url)
 
259
 
 
260
    # Does this hoster support suggesting a commit message in the
 
261
    # merge proposal?
 
262
    supports_merge_proposal_commit_message = None
 
263
 
 
264
    # The base_url that would be visible to users. I.e. https://github.com/
 
265
    # rather than https://api.github.com/
 
266
    base_url = None
 
267
 
 
268
    # The syntax to use for formatting merge proposal descriptions.
 
269
    # Common values: 'plain', 'markdown'
 
270
    merge_proposal_description_format = None
 
271
 
 
272
    # Does this hoster support the allow_collaboration flag?
 
273
    supports_allow_collaboration = False
 
274
 
 
275
    def publish_derived(self, new_branch, base_branch, name, project=None,
 
276
                        owner=None, revision_id=None, overwrite=False,
 
277
                        allow_lossy=True, tag_selector=None):
 
278
        """Publish a branch to the site, derived from base_branch.
 
279
 
 
280
        :param base_branch: branch to derive the new branch from
 
281
        :param new_branch: branch to publish
 
282
        :return: resulting branch, public URL
 
283
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
284
            known.
 
285
        """
 
286
        raise NotImplementedError(self.publish_derived)
 
287
 
 
288
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
289
        """Get a derived branch ('a fork').
 
290
        """
 
291
        raise NotImplementedError(self.get_derived_branch)
 
292
 
 
293
    def get_push_url(self, branch):
 
294
        """Get the push URL for a branch."""
 
295
        raise NotImplementedError(self.get_push_url)
 
296
 
 
297
    def get_proposer(self, source_branch, target_branch):
 
298
        """Get a merge proposal creator.
 
299
 
 
300
        :note: source_branch does not have to be hosted by the hoster.
 
301
 
 
302
        :param source_branch: Source branch
 
303
        :param target_branch: Target branch
 
304
        :return: A MergeProposalBuilder object
 
305
        """
 
306
        raise NotImplementedError(self.get_proposer)
 
307
 
 
308
    def iter_proposals(self, source_branch, target_branch, status='open'):
 
309
        """Get the merge proposals for a specified branch tuple.
 
310
 
 
311
        :param source_branch: Source branch
 
312
        :param target_branch: Target branch
 
313
        :param status: Status of proposals to iterate over
 
314
        :return: Iterate over MergeProposal object
 
315
        """
 
316
        raise NotImplementedError(self.iter_proposals)
 
317
 
 
318
    def get_proposal_by_url(self, url):
 
319
        """Retrieve a branch proposal by URL.
 
320
 
 
321
        :param url: Merge proposal URL.
 
322
        :return: MergeProposal object
 
323
        :raise UnsupportedHoster: Hoster does not support this URL
 
324
        """
 
325
        raise NotImplementedError(self.get_proposal_by_url)
 
326
 
 
327
    def hosts(self, branch):
 
328
        """Return true if this hoster hosts given branch."""
 
329
        raise NotImplementedError(self.hosts)
 
330
 
 
331
    @classmethod
 
332
    def probe_from_branch(cls, branch):
 
333
        """Create a Hoster object if this hoster knows about a branch."""
 
334
        url = urlutils.strip_segment_parameters(branch.user_url)
 
335
        return cls.probe_from_url(
 
336
            url, possible_transports=[branch.control_transport])
 
337
 
 
338
    @classmethod
 
339
    def probe_from_url(cls, url, possible_hosters=None):
 
340
        """Create a Hoster object if this hoster knows about a URL."""
 
341
        raise NotImplementedError(cls.probe_from_url)
 
342
 
 
343
    def iter_my_proposals(self, status='open'):
 
344
        """Iterate over the proposals created by the currently logged in user.
 
345
 
 
346
        :param status: Only yield proposals with this status
 
347
            (one of: 'open', 'closed', 'merged', 'all')
 
348
        :return: Iterator over MergeProposal objects
 
349
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
350
            known.
 
351
        """
 
352
        raise NotImplementedError(self.iter_my_proposals)
 
353
 
 
354
    def iter_my_forks(self):
 
355
        """Iterate over the currently logged in users' forks.
 
356
 
 
357
        :return: Iterator over project_name
 
358
        """
 
359
        raise NotImplementedError(self.iter_my_forks)
 
360
 
 
361
    def delete_project(self, name):
 
362
        """Delete a project.
 
363
        """
 
364
        raise NotImplementedError(self.delete_project)
 
365
 
 
366
    @classmethod
 
367
    def iter_instances(cls):
 
368
        """Iterate instances.
 
369
 
 
370
        :return: Hoster instances
 
371
        """
 
372
        raise NotImplementedError(cls.iter_instances)
 
373
 
 
374
    def get_current_user(self):
 
375
        """Retrieve the name of the currently logged in user.
 
376
 
 
377
        :return: Username or None if not logged in
 
378
        """
 
379
        raise NotImplementedError(self.get_current_user)
 
380
 
 
381
    def get_user_url(self, user):
 
382
        """Rerieve the web URL for a user."""
 
383
        raise NotImplementedError(self.get_user_url)
 
384
 
 
385
 
 
386
def determine_title(description):
 
387
    """Determine the title for a merge proposal based on full description."""
 
388
    return description.splitlines()[0].split('.')[0]
 
389
 
 
390
 
 
391
def get_hoster(branch, possible_hosters=None):
 
392
    """Find the hoster for a branch.
 
393
 
 
394
    :param branch: Branch to find hoster for
 
395
    :param possible_hosters: Optional list of hosters to reuse
 
396
    :raise UnsupportedHoster: if there is no hoster that supports `branch`
 
397
    :return: A `Hoster` object
 
398
    """
 
399
    if possible_hosters:
 
400
        for hoster in possible_hosters:
 
401
            if hoster.hosts(branch):
 
402
                return hoster
 
403
    for name, hoster_cls in hosters.items():
 
404
        try:
 
405
            hoster = hoster_cls.probe_from_branch(branch)
 
406
        except UnsupportedHoster:
 
407
            pass
 
408
        else:
 
409
            if possible_hosters is not None:
 
410
                possible_hosters.append(hoster)
 
411
            return hoster
 
412
    raise UnsupportedHoster(branch)
 
413
 
 
414
 
 
415
def iter_hoster_instances(hoster=None):
 
416
    """Iterate over all known hoster instances.
 
417
 
 
418
    :return: Iterator over Hoster instances
 
419
    """
 
420
    if hoster is None:
 
421
        hoster_clses = [hoster_cls for name, hoster_cls in hosters.items()]
 
422
    else:
 
423
        hoster_clses = [hoster]
 
424
    for hoster_cls in hoster_clses:
 
425
        for instance in hoster_cls.iter_instances():
 
426
            yield instance
 
427
 
 
428
 
 
429
def get_proposal_by_url(url):
 
430
    """Get the proposal object associated with a URL.
 
431
 
 
432
    :param url: URL of the proposal
 
433
    :raise UnsupportedHoster: if there is no hoster that supports the URL
 
434
    :return: A `MergeProposal` object
 
435
    """
 
436
    for instance in iter_hoster_instances():
 
437
        try:
 
438
            return instance.get_proposal_by_url(url)
 
439
        except UnsupportedHoster:
 
440
            pass
 
441
    raise UnsupportedHoster(url)
 
442
 
 
443
 
 
444
hosters = registry.Registry()