/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
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
102
class MergeProposal(object):
103
    """A merge proposal.
104
105
    :ivar url: URL for the merge proposal
106
    """
107
108
    def __init__(self, url=None):
109
        self.url = url
110
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
111
    def get_description(self):
112
        """Get the description of the merge proposal."""
113
        raise NotImplementedError(self.get_description)
114
115
    def set_description(self, description):
116
        """Set the description of the merge proposal."""
117
        raise NotImplementedError(self.set_description)
118
7296.8.2 by Jelmer Vernooij
Add feature flag for commit message.
119
    def get_commit_message(self):
120
        """Get the proposed commit message."""
121
        raise NotImplementedError(self.get_commit_message)
122
123
    def set_commit_message(self, commit_message):
124
        """Set the propose commit message."""
125
        raise NotImplementedError(self.set_commit_message)
126
0.431.64 by Jelmer Vernooij
Add get_source_branch_url/get_target_branch_url methods.
127
    def get_source_branch_url(self):
128
        """Return the source branch."""
129
        raise NotImplementedError(self.get_source_branch_url)
130
7490.65.1 by Jelmer Vernooij
Add functions fo retrieving merge proposal source revision.
131
    def get_source_revision(self):
132
        """Return the latest revision for the source branch."""
133
        raise NotImplementedError(self.get_source_revision)
134
0.431.64 by Jelmer Vernooij
Add get_source_branch_url/get_target_branch_url methods.
135
    def get_target_branch_url(self):
136
        """Return the target branch."""
137
        raise NotImplementedError(self.get_target_branch_url)
138
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
139
    def get_source_project(self):
140
        raise NotImplementedError(self.get_source_project)
141
142
    def get_target_project(self):
143
        raise NotImplementedError(self.get_target_project)
144
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
145
    def close(self):
146
        """Close the merge proposal (without merging it)."""
147
        raise NotImplementedError(self.close)
148
0.431.46 by Jelmer Vernooij
Add MergeProposal.is_merged.
149
    def is_merged(self):
150
        """Check whether this merge proposal has been merged."""
151
        raise NotImplementedError(self.is_merged)
152
7381.5.1 by Jelmer Vernooij
Several more fixes for merge proposals. Add functions for reopening merge proposals.
153
    def is_closed(self):
154
        """Check whether this merge proposal is closed
155
156
        This can either mean that it is merged or rejected.
157
        """
158
        raise NotImplementedError(self.is_closed)
159
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
160
    def merge(self, commit_message=None):
161
        """Merge this merge proposal."""
162
        raise NotImplementedError(self.merge)
163
7380.1.1 by Jelmer Vernooij
Several more fixes for git merge proposals.
164
    def can_be_merged(self):
165
        """Can this merge proposal be merged?
166
167
        The answer to this can be no if e.g. it has conflics.
168
        """
169
        raise NotImplementedError(self.can_be_merged)
170
7414.4.1 by Jelmer Vernooij
Add a MergeProposal.get_merged_by method.
171
    def get_merged_by(self):
172
        """If this proposal was merged, who merged it.
173
        """
174
        raise NotImplementedError(self.get_merged_by)
175
7414.4.3 by Jelmer Vernooij
Add MergeProposal.get_merged_at.
176
    def get_merged_at(self):
177
        """If this proposal was merged, when it was merged.
178
        """
179
        raise NotImplementedError(self.get_merged_at)
180
7490.52.1 by Jelmer Vernooij
Add MergeProposal.post_comment.
181
    def post_comment(self, body):
182
        """Post a comment on the merge proposal.
183
184
        Args:
185
          body: Body of the comment
186
        """
187
        raise NotImplementedError(self.post_comment)
188
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
189
0.432.2 by Jelmer Vernooij
Publish command sort of works.
190
class MergeProposalBuilder(object):
0.431.1 by Jelmer Vernooij
Start work on propose command.
191
    """Merge proposal creator.
192
193
    :param source_branch: Branch to propose for merging
194
    :param target_branch: Target branch
195
    """
196
197
    hooks = ProposeMergeHooks()
198
199
    def __init__(self, source_branch, target_branch):
200
        self.source_branch = source_branch
201
        self.target_branch = target_branch
202
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
203
    def get_initial_body(self):
204
        """Get a body for the proposal for the user to modify.
205
206
        :return: a str or None.
207
        """
208
        raise NotImplementedError(self.get_initial_body)
209
210
    def get_infotext(self):
211
        """Determine the initial comment for the merge proposal.
212
        """
213
        raise NotImplementedError(self.get_infotext)
214
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
215
    def create_proposal(self, description, reviewers=None, labels=None,
7467.3.1 by Jelmer Vernooij
Add a work_in_progress flag.
216
                        prerequisite_branch=None, commit_message=None,
7490.6.1 by Jelmer Vernooij
Add allow-collaboration flag.
217
                        work_in_progress=False, allow_collaboration=False):
0.431.1 by Jelmer Vernooij
Start work on propose command.
218
        """Create a proposal to merge a branch for merging.
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
219
220
        :param description: Description for the merge proposal
0.431.5 by Jelmer Vernooij
Initial work on gitlab support.
221
        :param reviewers: Optional list of people to ask reviews from
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
222
        :param labels: Labels to attach to the proposal
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
223
        :param prerequisite_branch: Optional prerequisite branch
7296.8.1 by Jelmer Vernooij
Add commit-message option to 'brz propose'.
224
        :param commit_message: Optional commit message
7467.3.1 by Jelmer Vernooij
Add a work_in_progress flag.
225
        :param work_in_progress:
226
            Whether this merge proposal is still a work-in-progress
7490.6.1 by Jelmer Vernooij
Add allow-collaboration flag.
227
        :param allow_collaboration:
228
            Whether to allow changes to the branch from the target branch
229
            maintainer(s)
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
230
        :return: A `MergeProposal` object
0.431.1 by Jelmer Vernooij
Start work on propose command.
231
        """
232
        raise NotImplementedError(self.create_proposal)
233
234
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
235
class Hoster(object):
236
    """A hosting site manager.
237
    """
238
7260.1.1 by Jelmer Vernooij
Add .base_url property to Hoster.
239
    # Does this hoster support arbitrary labels being attached to merge
240
    # proposals?
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
241
    supports_merge_proposal_labels = None
242
7296.8.2 by Jelmer Vernooij
Add feature flag for commit message.
243
    # Does this hoster support suggesting a commit message in the
244
    # merge proposal?
245
    supports_merge_proposal_commit_message = None
246
7260.1.1 by Jelmer Vernooij
Add .base_url property to Hoster.
247
    # The base_url that would be visible to users. I.e. https://github.com/
248
    # rather than https://api.github.com/
249
    base_url = None
250
7445.1.1 by Jelmer Vernooij
Add Hoster.merge_proposal_description_format and common function for determining title.
251
    # The syntax to use for formatting merge proposal descriptions.
252
    # Common values: 'plain', 'markdown'
253
    merge_proposal_description_format = None
254
7490.3.9 by Jelmer Vernooij
Add supports_allow_collaboration flag.
255
    # Does this hoster support the allow_collaboration flag?
256
    supports_allow_collaboration = False
257
0.431.20 by Jelmer Vernooij
publish -> publish_derived.
258
    def publish_derived(self, new_branch, base_branch, name, project=None,
0.431.51 by Jelmer Vernooij
Allow fallback to lossy by default.
259
                        owner=None, revision_id=None, overwrite=False,
7489.4.2 by Jelmer Vernooij
Plumb through tag_selector.
260
                        allow_lossy=True, tag_selector=None):
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
261
        """Publish a branch to the site, derived from base_branch.
262
263
        :param base_branch: branch to derive the new branch from
264
        :param new_branch: branch to publish
0.432.3 by Jelmer Vernooij
Publish command works for github.
265
        :return: resulting branch, public URL
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
266
        :raise HosterLoginRequired: Action requires a hoster login, but none is
267
            known.
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
268
        """
269
        raise NotImplementedError(self.publish)
270
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
271
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
0.431.31 by Jelmer Vernooij
Drop autopropose command.
272
        """Get a derived branch ('a fork').
273
        """
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
274
        raise NotImplementedError(self.get_derived_branch)
275
0.431.28 by Jelmer Vernooij
Implement Hoster.get_push_url.
276
    def get_push_url(self, branch):
277
        """Get the push URL for a branch."""
278
        raise NotImplementedError(self.get_push_url)
279
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
280
    def get_proposer(self, source_branch, target_branch):
281
        """Get a merge proposal creator.
282
0.431.31 by Jelmer Vernooij
Drop autopropose command.
283
        :note: source_branch does not have to be hosted by the hoster.
284
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
285
        :param source_branch: Source branch
286
        :param target_branch: Target branch
0.432.2 by Jelmer Vernooij
Publish command sort of works.
287
        :return: A MergeProposalBuilder object
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
288
        """
289
        raise NotImplementedError(self.get_proposer)
290
0.431.68 by Jelmer Vernooij
Add status to other Hosters.
291
    def iter_proposals(self, source_branch, target_branch, status='open'):
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
292
        """Get the merge proposals for a specified branch tuple.
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
293
294
        :param source_branch: Source branch
295
        :param target_branch: Target branch
0.431.68 by Jelmer Vernooij
Add status to other Hosters.
296
        :param status: Status of proposals to iterate over
0.431.67 by Jelmer Vernooij
Support multiple merge proposals per branch.
297
        :return: Iterate over MergeProposal object
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
298
        """
0.431.67 by Jelmer Vernooij
Support multiple merge proposals per branch.
299
        raise NotImplementedError(self.iter_proposals)
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
300
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
301
    def get_proposal_by_url(self, url):
302
        """Retrieve a branch proposal by URL.
303
304
        :param url: Merge proposal URL.
305
        :return: MergeProposal object
306
        :raise UnsupportedHoster: Hoster does not support this URL
307
        """
308
        raise NotImplementedError(self.get_proposal_by_url)
309
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
310
    def hosts(self, branch):
311
        """Return true if this hoster hosts given branch."""
312
        raise NotImplementedError(self.hosts)
313
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
314
    @classmethod
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
315
    def probe_from_branch(cls, branch):
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
316
        """Create a Hoster object if this hoster knows about a branch."""
7441.1.1 by Jelmer Vernooij
Add strip_segment_parameters function.
317
        url = urlutils.strip_segment_parameters(branch.user_url)
7296.10.1 by Jelmer Vernooij
Initial work making gitlab just directly use ReST.
318
        return cls.probe_from_url(
7296.10.2 by Jelmer Vernooij
More fixes.
319
            url, possible_transports=[branch.control_transport])
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
320
321
    @classmethod
7296.10.1 by Jelmer Vernooij
Initial work making gitlab just directly use ReST.
322
    def probe_from_url(cls, url, possible_hosters=None):
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
323
        """Create a Hoster object if this hoster knows about a URL."""
324
        raise NotImplementedError(cls.probe_from_url)
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
325
0.431.66 by Jelmer Vernooij
Add support for status argument.
326
    def iter_my_proposals(self, status='open'):
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
327
        """Iterate over the proposals created by the currently logged in user.
328
0.431.66 by Jelmer Vernooij
Add support for status argument.
329
        :param status: Only yield proposals with this status
330
            (one of: 'open', 'closed', 'merged', 'all')
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
331
        :return: Iterator over MergeProposal objects
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
332
        :raise HosterLoginRequired: Action requires a hoster login, but none is
333
            known.
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
334
        """
335
        raise NotImplementedError(self.iter_my_proposals)
336
7414.5.2 by Jelmer Vernooij
Change iter_my_projects to iter_my_forks.
337
    def iter_my_forks(self):
338
        """Iterate over the currently logged in users' forks.
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
339
7414.5.2 by Jelmer Vernooij
Change iter_my_projects to iter_my_forks.
340
        :return: Iterator over project_name
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
341
        """
7414.5.2 by Jelmer Vernooij
Change iter_my_projects to iter_my_forks.
342
        raise NotImplementedError(self.iter_my_forks)
7414.5.1 by Jelmer Vernooij
Add functions for managing projects.
343
344
    def delete_project(self, name):
345
        """Delete a project.
346
        """
347
        raise NotImplementedError(self.delete_project)
348
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
349
    @classmethod
350
    def iter_instances(cls):
351
        """Iterate instances.
352
353
        :return: Hoster instances
354
        """
355
        raise NotImplementedError(cls.iter_instances)
356
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
357
7445.1.1 by Jelmer Vernooij
Add Hoster.merge_proposal_description_format and common function for determining title.
358
def determine_title(description):
359
    """Determine the title for a merge proposal based on full description."""
360
    return description.splitlines()[0].split('.')[0]
361
362
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
363
def get_hoster(branch, possible_hosters=None):
7408.3.2 by Jelmer Vernooij
Add some tests.
364
    """Find the hoster for a branch.
365
366
    :param branch: Branch to find hoster for
367
    :param possible_hosters: Optional list of hosters to reuse
368
    :raise UnsupportedHoster: if there is no hoster that supports `branch`
369
    :return: A `Hoster` object
370
    """
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
371
    if possible_hosters:
372
        for hoster in possible_hosters:
373
            if hoster.hosts(branch):
374
                return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
375
    for name, hoster_cls in hosters.items():
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
376
        try:
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
377
            hoster = hoster_cls.probe_from_branch(branch)
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
378
        except UnsupportedHoster:
379
            pass
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
380
        else:
381
            if possible_hosters is not None:
382
                possible_hosters.append(hoster)
383
            return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
384
    raise UnsupportedHoster(branch)
385
386
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
387
def get_proposal_by_url(url):
7408.3.2 by Jelmer Vernooij
Add some tests.
388
    """Get the proposal object associated with a URL.
389
390
    :param url: URL of the proposal
391
    :raise UnsupportedHoster: if there is no hoster that supports the URL
392
    :return: A `MergeProposal` object
393
    """
7296.9.1 by Jelmer Vernooij
Add 'brz land' subcommand.
394
    for name, hoster_cls in hosters.items():
395
        for instance in hoster_cls.iter_instances():
396
            try:
397
                return instance.get_proposal_by_url(url)
398
            except UnsupportedHoster:
399
                pass
400
    raise UnsupportedHoster(url)
401
402
0.432.2 by Jelmer Vernooij
Publish command sort of works.
403
hosters = registry.Registry()