/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-03-22 01:35:14 UTC
  • mfrom: (7490.7.6 work)
  • mto: This revision was merged to the branch mainline in revision 7499.
  • Revision ID: jelmer@jelmer.uk-20200322013514-7vw1ntwho04rcuj3
merge lp:brz/3.1.

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 . import (
 
20
    errors,
 
21
    hooks,
 
22
    registry,
 
23
    urlutils,
 
24
    )
 
25
 
 
26
 
 
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
 
 
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
 
 
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
 
 
54
class ReopenFailed(errors.BzrError):
 
55
 
 
56
    _fmt = "Reopening the merge proposal failed: %(error)s."
 
57
 
 
58
 
 
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")
 
64
        self.add_hook(
 
65
            'get_prerequisite',
 
66
            "Return the prerequisite branch for proposing as merge.", (3, 0))
 
67
        self.add_hook(
 
68
            'merge_proposal_body',
 
69
            "Return an initial body for the merge proposal message.", (3, 0))
 
70
 
 
71
 
 
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
 
 
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
 
 
90
class HosterLoginRequired(errors.BzrError):
 
91
    """Action requires hoster login credentials."""
 
92
 
 
93
    _fmt = "Action requires credentials for hosting site %(hoster)r."""
 
94
 
 
95
    def __init__(self, hoster):
 
96
        errors.BzrError.__init__(self)
 
97
        self.hoster = hoster
 
98
 
 
99
 
 
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
 
 
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
 
 
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
 
 
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
 
 
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
 
 
139
    def close(self):
 
140
        """Close the merge proposal (without merging it)."""
 
141
        raise NotImplementedError(self.close)
 
142
 
 
143
    def is_merged(self):
 
144
        """Check whether this merge proposal has been merged."""
 
145
        raise NotImplementedError(self.is_merged)
 
146
 
 
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
 
 
154
    def merge(self, commit_message=None):
 
155
        """Merge this merge proposal."""
 
156
        raise NotImplementedError(self.merge)
 
157
 
 
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
 
 
165
    def get_merged_by(self):
 
166
        """If this proposal was merged, who merged it.
 
167
        """
 
168
        raise NotImplementedError(self.get_merged_by)
 
169
 
 
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
 
 
175
 
 
176
class MergeProposalBuilder(object):
 
177
    """Merge proposal creator.
 
178
 
 
179
    :param source_branch: Branch to propose for merging
 
180
    :param target_branch: Target branch
 
181
    """
 
182
 
 
183
    hooks = ProposeMergeHooks()
 
184
 
 
185
    def __init__(self, source_branch, target_branch):
 
186
        self.source_branch = source_branch
 
187
        self.target_branch = target_branch
 
188
 
 
189
    def get_initial_body(self):
 
190
        """Get a body for the proposal for the user to modify.
 
191
 
 
192
        :return: a str or None.
 
193
        """
 
194
        raise NotImplementedError(self.get_initial_body)
 
195
 
 
196
    def get_infotext(self):
 
197
        """Determine the initial comment for the merge proposal.
 
198
        """
 
199
        raise NotImplementedError(self.get_infotext)
 
200
 
 
201
    def create_proposal(self, description, reviewers=None, labels=None,
 
202
                        prerequisite_branch=None, commit_message=None,
 
203
                        work_in_progress=False, allow_collaboration=False):
 
204
        """Create a proposal to merge a branch for merging.
 
205
 
 
206
        :param description: Description for the merge proposal
 
207
        :param reviewers: Optional list of people to ask reviews from
 
208
        :param labels: Labels to attach to the proposal
 
209
        :param prerequisite_branch: Optional prerequisite branch
 
210
        :param commit_message: Optional commit message
 
211
        :param work_in_progress:
 
212
            Whether this merge proposal is still a work-in-progress
 
213
        :param allow_collaboration:
 
214
            Whether to allow changes to the branch from the target branch
 
215
            maintainer(s)
 
216
        :return: A `MergeProposal` object
 
217
        """
 
218
        raise NotImplementedError(self.create_proposal)
 
219
 
 
220
 
 
221
class Hoster(object):
 
222
    """A hosting site manager.
 
223
    """
 
224
 
 
225
    # Does this hoster support arbitrary labels being attached to merge
 
226
    # proposals?
 
227
    supports_merge_proposal_labels = None
 
228
 
 
229
    # Does this hoster support suggesting a commit message in the
 
230
    # merge proposal?
 
231
    supports_merge_proposal_commit_message = None
 
232
 
 
233
    # The base_url that would be visible to users. I.e. https://github.com/
 
234
    # rather than https://api.github.com/
 
235
    base_url = None
 
236
 
 
237
    # The syntax to use for formatting merge proposal descriptions.
 
238
    # Common values: 'plain', 'markdown'
 
239
    merge_proposal_description_format = None
 
240
 
 
241
    # Does this hoster support the allow_collaboration flag?
 
242
    supports_allow_collaboration = False
 
243
 
 
244
    def publish_derived(self, new_branch, base_branch, name, project=None,
 
245
                        owner=None, revision_id=None, overwrite=False,
 
246
                        allow_lossy=True, tag_selector=None):
 
247
        """Publish a branch to the site, derived from base_branch.
 
248
 
 
249
        :param base_branch: branch to derive the new branch from
 
250
        :param new_branch: branch to publish
 
251
        :return: resulting branch, public URL
 
252
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
253
            known.
 
254
        """
 
255
        raise NotImplementedError(self.publish)
 
256
 
 
257
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
258
        """Get a derived branch ('a fork').
 
259
        """
 
260
        raise NotImplementedError(self.get_derived_branch)
 
261
 
 
262
    def get_push_url(self, branch):
 
263
        """Get the push URL for a branch."""
 
264
        raise NotImplementedError(self.get_push_url)
 
265
 
 
266
    def get_proposer(self, source_branch, target_branch):
 
267
        """Get a merge proposal creator.
 
268
 
 
269
        :note: source_branch does not have to be hosted by the hoster.
 
270
 
 
271
        :param source_branch: Source branch
 
272
        :param target_branch: Target branch
 
273
        :return: A MergeProposalBuilder object
 
274
        """
 
275
        raise NotImplementedError(self.get_proposer)
 
276
 
 
277
    def iter_proposals(self, source_branch, target_branch, status='open'):
 
278
        """Get the merge proposals for a specified branch tuple.
 
279
 
 
280
        :param source_branch: Source branch
 
281
        :param target_branch: Target branch
 
282
        :param status: Status of proposals to iterate over
 
283
        :return: Iterate over MergeProposal object
 
284
        """
 
285
        raise NotImplementedError(self.iter_proposals)
 
286
 
 
287
    def get_proposal_by_url(self, url):
 
288
        """Retrieve a branch proposal by URL.
 
289
 
 
290
        :param url: Merge proposal URL.
 
291
        :return: MergeProposal object
 
292
        :raise UnsupportedHoster: Hoster does not support this URL
 
293
        """
 
294
        raise NotImplementedError(self.get_proposal_by_url)
 
295
 
 
296
    def hosts(self, branch):
 
297
        """Return true if this hoster hosts given branch."""
 
298
        raise NotImplementedError(self.hosts)
 
299
 
 
300
    @classmethod
 
301
    def probe_from_branch(cls, branch):
 
302
        """Create a Hoster object if this hoster knows about a branch."""
 
303
        url = urlutils.strip_segment_parameters(branch.user_url)
 
304
        return cls.probe_from_url(
 
305
            url, possible_transports=[branch.control_transport])
 
306
 
 
307
    @classmethod
 
308
    def probe_from_url(cls, url, possible_hosters=None):
 
309
        """Create a Hoster object if this hoster knows about a URL."""
 
310
        raise NotImplementedError(cls.probe_from_url)
 
311
 
 
312
    def iter_my_proposals(self, status='open'):
 
313
        """Iterate over the proposals created by the currently logged in user.
 
314
 
 
315
        :param status: Only yield proposals with this status
 
316
            (one of: 'open', 'closed', 'merged', 'all')
 
317
        :return: Iterator over MergeProposal objects
 
318
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
319
            known.
 
320
        """
 
321
        raise NotImplementedError(self.iter_my_proposals)
 
322
 
 
323
    def iter_my_forks(self):
 
324
        """Iterate over the currently logged in users' forks.
 
325
 
 
326
        :return: Iterator over project_name
 
327
        """
 
328
        raise NotImplementedError(self.iter_my_forks)
 
329
 
 
330
    def delete_project(self, name):
 
331
        """Delete a project.
 
332
        """
 
333
        raise NotImplementedError(self.delete_project)
 
334
 
 
335
    @classmethod
 
336
    def iter_instances(cls):
 
337
        """Iterate instances.
 
338
 
 
339
        :return: Hoster instances
 
340
        """
 
341
        raise NotImplementedError(cls.iter_instances)
 
342
 
 
343
 
 
344
def determine_title(description):
 
345
    """Determine the title for a merge proposal based on full description."""
 
346
    return description.splitlines()[0].split('.')[0]
 
347
 
 
348
 
 
349
def get_hoster(branch, possible_hosters=None):
 
350
    """Find the hoster for a branch.
 
351
 
 
352
    :param branch: Branch to find hoster for
 
353
    :param possible_hosters: Optional list of hosters to reuse
 
354
    :raise UnsupportedHoster: if there is no hoster that supports `branch`
 
355
    :return: A `Hoster` object
 
356
    """
 
357
    if possible_hosters:
 
358
        for hoster in possible_hosters:
 
359
            if hoster.hosts(branch):
 
360
                return hoster
 
361
    for name, hoster_cls in hosters.items():
 
362
        try:
 
363
            hoster = hoster_cls.probe_from_branch(branch)
 
364
        except UnsupportedHoster:
 
365
            pass
 
366
        else:
 
367
            if possible_hosters is not None:
 
368
                possible_hosters.append(hoster)
 
369
            return hoster
 
370
    raise UnsupportedHoster(branch)
 
371
 
 
372
 
 
373
def get_proposal_by_url(url):
 
374
    """Get the proposal object associated with a URL.
 
375
 
 
376
    :param url: URL of the proposal
 
377
    :raise UnsupportedHoster: if there is no hoster that supports the URL
 
378
    :return: A `MergeProposal` object
 
379
    """
 
380
    for name, hoster_cls in hosters.items():
 
381
        for instance in hoster_cls.iter_instances():
 
382
            try:
 
383
                return instance.get_proposal_by_url(url)
 
384
            except UnsupportedHoster:
 
385
                pass
 
386
    raise UnsupportedHoster(url)
 
387
 
 
388
 
 
389
hosters = registry.Registry()