/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
7408.3.1 by Jelmer Vernooij
Move propose module into core.
1
# Copyright (C) 2018-2019 Breezy Developers
0.431.1 by Jelmer Vernooij
Start work on propose command.
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
7408.3.1 by Jelmer Vernooij
Move propose module into core.
21
from . import (
0.431.1 by Jelmer Vernooij
Start work on propose command.
22
    errors,
23
    hooks,
24
    registry,
7268.12.3 by Jelmer Vernooij
Add missing import.
25
    urlutils,
0.431.1 by Jelmer Vernooij
Start work on propose command.
26
    )
27
28
0.431.38 by Jelmer Vernooij
Add NoSuchProject.
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
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
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
0.432.2 by Jelmer Vernooij
Publish command sort of works.
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
7381.5.1 by Jelmer Vernooij
Several more fixes for merge proposals. Add functions for reopening merge proposals.
56
class ReopenFailed(errors.BzrError):
57
58
    _fmt = "Reopening the merge proposal failed: %(error)s."
59
60
0.431.1 by Jelmer Vernooij
Start work on propose command.
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")
0.431.57 by Jelmer Vernooij
Cleanups.
66
        self.add_hook(
67
            'get_prerequisite',
0.431.1 by Jelmer Vernooij
Start work on propose command.
68
            "Return the prerequisite branch for proposing as merge.", (3, 0))
0.431.57 by Jelmer Vernooij
Cleanups.
69
        self.add_hook(
70
            'merge_proposal_body',
0.431.1 by Jelmer Vernooij
Start work on propose command.
71
            "Return an initial body for the merge proposal message.", (3, 0))
72
73
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
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
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
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
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
92
class HosterLoginRequired(errors.BzrError):
93
    """Action requires hoster login credentials."""
94
7268.8.2 by Jelmer Vernooij
Handle GitHub errors.
95
    _fmt = "Action requires credentials for hosting site %(hoster)r."""
96
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
97
    def __init__(self, hoster):
98
        errors.BzrError.__init__(self)
99
        self.hoster = hoster
100
101
7490.117.3 by Jelmer Vernooij
Raise detailed error when source branch is not derived from target branch.
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
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
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
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
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
7296.8.2 by Jelmer Vernooij
Add feature flag for commit message.
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
0.431.64 by Jelmer Vernooij
Add get_source_branch_url/get_target_branch_url methods.
139
    def get_source_branch_url(self):
140
        """Return the source branch."""
141
        raise NotImplementedError(self.get_source_branch_url)
142
7490.65.1 by Jelmer Vernooij
Add functions fo retrieving merge proposal source revision.
143
    def get_source_revision(self):
144
        """Return the latest revision for the source branch."""
145
        raise NotImplementedError(self.get_source_revision)
146
0.431.64 by Jelmer Vernooij
Add get_source_branch_url/get_target_branch_url methods.
147
    def get_target_branch_url(self):
148
        """Return the target branch."""
149
        raise NotImplementedError(self.get_target_branch_url)
150
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
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
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
157
    def close(self):
158
        """Close the merge proposal (without merging it)."""
159
        raise NotImplementedError(self.close)
160
0.431.46 by Jelmer Vernooij
Add MergeProposal.is_merged.
161
    def is_merged(self):
162
        """Check whether this merge proposal has been merged."""
163
        raise NotImplementedError(self.is_merged)
164
7381.5.1 by Jelmer Vernooij
Several more fixes for merge proposals. Add functions for reopening merge proposals.
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
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
172
    def merge(self, commit_message=None):
173
        """Merge this merge proposal."""
174
        raise NotImplementedError(self.merge)
175
7380.1.1 by Jelmer Vernooij
Several more fixes for git merge proposals.
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
7414.4.1 by Jelmer Vernooij
Add a MergeProposal.get_merged_by method.
183
    def get_merged_by(self):
184
        """If this proposal was merged, who merged it.
185
        """
186
        raise NotImplementedError(self.get_merged_by)
187
7414.4.3 by Jelmer Vernooij
Add MergeProposal.get_merged_at.
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
7490.52.1 by Jelmer Vernooij
Add MergeProposal.post_comment.
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
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
201
0.432.2 by Jelmer Vernooij
Publish command sort of works.
202
class MergeProposalBuilder(object):
0.431.1 by Jelmer Vernooij
Start work on propose command.
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
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
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
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
227
    def create_proposal(self, description, reviewers=None, labels=None,
7467.3.1 by Jelmer Vernooij
Add a work_in_progress flag.
228
                        prerequisite_branch=None, commit_message=None,
7490.6.1 by Jelmer Vernooij
Add allow-collaboration flag.
229
                        work_in_progress=False, allow_collaboration=False):
0.431.1 by Jelmer Vernooij
Start work on propose command.
230
        """Create a proposal to merge a branch for merging.
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
231
232
        :param description: Description for the merge proposal
0.431.5 by Jelmer Vernooij
Initial work on gitlab support.
233
        :param reviewers: Optional list of people to ask reviews from
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
234
        :param labels: Labels to attach to the proposal
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
235
        :param prerequisite_branch: Optional prerequisite branch
7296.8.1 by Jelmer Vernooij
Add commit-message option to 'brz propose'.
236
        :param commit_message: Optional commit message
7467.3.1 by Jelmer Vernooij
Add a work_in_progress flag.
237
        :param work_in_progress:
238
            Whether this merge proposal is still a work-in-progress
7490.6.1 by Jelmer Vernooij
Add allow-collaboration flag.
239
        :param allow_collaboration:
240
            Whether to allow changes to the branch from the target branch
241
            maintainer(s)
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
242
        :return: A `MergeProposal` object
0.431.1 by Jelmer Vernooij
Start work on propose command.
243
        """
244
        raise NotImplementedError(self.create_proposal)
245
246
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
247
class Hoster(object):
248
    """A hosting site manager.
249
    """
250
7260.1.1 by Jelmer Vernooij
Add .base_url property to Hoster.
251
    # Does this hoster support arbitrary labels being attached to merge
252
    # proposals?
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
253
    supports_merge_proposal_labels = None
254
7490.105.1 by Jelmer Vernooij
Add some hoster metadata fields.
255
    @property
256
    def name(self):
257
        """Name of this instance."""
258
        return "%s at %s" % (type(self).__name__, self.base_url)
259
7296.8.2 by Jelmer Vernooij
Add feature flag for commit message.
260
    # Does this hoster support suggesting a commit message in the
261
    # merge proposal?
262
    supports_merge_proposal_commit_message = None
263
7260.1.1 by Jelmer Vernooij
Add .base_url property to Hoster.
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
7445.1.1 by Jelmer Vernooij
Add Hoster.merge_proposal_description_format and common function for determining title.
268
    # The syntax to use for formatting merge proposal descriptions.
269
    # Common values: 'plain', 'markdown'
270
    merge_proposal_description_format = None
271
7490.3.9 by Jelmer Vernooij
Add supports_allow_collaboration flag.
272
    # Does this hoster support the allow_collaboration flag?
273
    supports_allow_collaboration = False
274
0.431.20 by Jelmer Vernooij
publish -> publish_derived.
275
    def publish_derived(self, new_branch, base_branch, name, project=None,
0.431.51 by Jelmer Vernooij
Allow fallback to lossy by default.
276
                        owner=None, revision_id=None, overwrite=False,
7489.4.2 by Jelmer Vernooij
Plumb through tag_selector.
277
                        allow_lossy=True, tag_selector=None):
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
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
0.432.3 by Jelmer Vernooij
Publish command works for github.
282
        :return: resulting branch, public URL
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
283
        :raise HosterLoginRequired: Action requires a hoster login, but none is
284
            known.
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
285
        """
7490.105.1 by Jelmer Vernooij
Add some hoster metadata fields.
286
        raise NotImplementedError(self.publish_derived)
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
287
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
288
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
0.431.31 by Jelmer Vernooij
Drop autopropose command.
289
        """Get a derived branch ('a fork').
290
        """
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
291
        raise NotImplementedError(self.get_derived_branch)
292
0.431.28 by Jelmer Vernooij
Implement Hoster.get_push_url.
293
    def get_push_url(self, branch):
294
        """Get the push URL for a branch."""
295
        raise NotImplementedError(self.get_push_url)
296
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
297
    def get_proposer(self, source_branch, target_branch):
298
        """Get a merge proposal creator.
299
0.431.31 by Jelmer Vernooij
Drop autopropose command.
300
        :note: source_branch does not have to be hosted by the hoster.
301
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
302
        :param source_branch: Source branch
303
        :param target_branch: Target branch
0.432.2 by Jelmer Vernooij
Publish command sort of works.
304
        :return: A MergeProposalBuilder object
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
305
        """
306
        raise NotImplementedError(self.get_proposer)
307
0.431.68 by Jelmer Vernooij
Add status to other Hosters.
308
    def iter_proposals(self, source_branch, target_branch, status='open'):
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
309
        """Get the merge proposals for a specified branch tuple.
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
310
311
        :param source_branch: Source branch
312
        :param target_branch: Target branch
0.431.68 by Jelmer Vernooij
Add status to other Hosters.
313
        :param status: Status of proposals to iterate over
0.431.67 by Jelmer Vernooij
Support multiple merge proposals per branch.
314
        :return: Iterate over MergeProposal object
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
315
        """
0.431.67 by Jelmer Vernooij
Support multiple merge proposals per branch.
316
        raise NotImplementedError(self.iter_proposals)
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
317
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
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
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
327
    def hosts(self, branch):
328
        """Return true if this hoster hosts given branch."""
329
        raise NotImplementedError(self.hosts)
330
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
331
    @classmethod
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
332
    def probe_from_branch(cls, branch):
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
333
        """Create a Hoster object if this hoster knows about a branch."""
7441.1.1 by Jelmer Vernooij
Add strip_segment_parameters function.
334
        url = urlutils.strip_segment_parameters(branch.user_url)
7296.10.1 by Jelmer Vernooij
Initial work making gitlab just directly use ReST.
335
        return cls.probe_from_url(
7296.10.2 by Jelmer Vernooij
More fixes.
336
            url, possible_transports=[branch.control_transport])
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
337
338
    @classmethod
7296.10.1 by Jelmer Vernooij
Initial work making gitlab just directly use ReST.
339
    def probe_from_url(cls, url, possible_hosters=None):
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
340
        """Create a Hoster object if this hoster knows about a URL."""
341
        raise NotImplementedError(cls.probe_from_url)
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
342
0.431.66 by Jelmer Vernooij
Add support for status argument.
343
    def iter_my_proposals(self, status='open'):
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
344
        """Iterate over the proposals created by the currently logged in user.
345
0.431.66 by Jelmer Vernooij
Add support for status argument.
346
        :param status: Only yield proposals with this status
347
            (one of: 'open', 'closed', 'merged', 'all')
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
348
        :return: Iterator over MergeProposal objects
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
349
        :raise HosterLoginRequired: Action requires a hoster login, but none is
350
            known.
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
351
        """
352
        raise NotImplementedError(self.iter_my_proposals)
353
7414.5.2 by Jelmer Vernooij
Change iter_my_projects to iter_my_forks.
354
    def iter_my_forks(self):
355
        """Iterate over the currently logged in users' forks.
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
356
7414.5.2 by Jelmer Vernooij
Change iter_my_projects to iter_my_forks.
357
        :return: Iterator over project_name
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
358
        """
7414.5.2 by Jelmer Vernooij
Change iter_my_projects to iter_my_forks.
359
        raise NotImplementedError(self.iter_my_forks)
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
360
361
    def delete_project(self, name):
362
        """Delete a project.
363
        """
364
        raise NotImplementedError(self.delete_project)
365
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
366
    @classmethod
367
    def iter_instances(cls):
368
        """Iterate instances.
369
370
        :return: Hoster instances
371
        """
372
        raise NotImplementedError(cls.iter_instances)
373
7490.105.1 by Jelmer Vernooij
Add some hoster metadata fields.
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
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
385
7445.1.1 by Jelmer Vernooij
Add Hoster.merge_proposal_description_format and common function for determining title.
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
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
391
def get_hoster(branch, possible_hosters=None):
7408.3.2 by Jelmer Vernooij
Add some tests.
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
    """
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
399
    if possible_hosters:
400
        for hoster in possible_hosters:
401
            if hoster.hosts(branch):
402
                return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
403
    for name, hoster_cls in hosters.items():
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
404
        try:
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
405
            hoster = hoster_cls.probe_from_branch(branch)
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
406
        except UnsupportedHoster:
407
            pass
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
408
        else:
409
            if possible_hosters is not None:
410
                possible_hosters.append(hoster)
411
            return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
412
    raise UnsupportedHoster(branch)
413
414
7490.138.2 by Jelmer Vernooij
Add Proposal.update.
415
def iter_hoster_instances(hoster=None):
7490.105.1 by Jelmer Vernooij
Add some hoster metadata fields.
416
    """Iterate over all known hoster instances.
417
418
    :return: Iterator over Hoster instances
419
    """
7490.138.2 by Jelmer Vernooij
Add Proposal.update.
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:
7490.105.1 by Jelmer Vernooij
Add some hoster metadata fields.
425
        for instance in hoster_cls.iter_instances():
426
            yield instance
427
428
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
429
def get_proposal_by_url(url):
7408.3.2 by Jelmer Vernooij
Add some tests.
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
    """
7490.105.1 by Jelmer Vernooij
Add some hoster metadata fields.
436
    for instance in iter_hoster_instances():
437
        try:
438
            return instance.get_proposal_by_url(url)
439
        except UnsupportedHoster:
440
            pass
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
441
    raise UnsupportedHoster(url)
442
443
0.432.2 by Jelmer Vernooij
Publish command sort of works.
444
hosters = registry.Registry()