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