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

  • Committer: Breezy landing bot
  • Author(s): Jelmer Vernooij
  • Date: 2019-10-13 17:31:55 UTC
  • mfrom: (7397.4.9 remove-unused)
  • Revision ID: breezy.the.bot@gmail.com-20191013173155-yoiokny4mknxb3um
Remove Tree.has_id.

Merged from https://code.launchpad.net/~jelmer/brz/remove-unused/+merge/373320

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2018 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 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
 
 
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
 
 
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
 
 
127
    def get_source_branch_url(self):
 
128
        """Return the source branch."""
 
129
        raise NotImplementedError(self.get_source_branch_url)
 
130
 
 
131
    def get_target_branch_url(self):
 
132
        """Return the target branch."""
 
133
        raise NotImplementedError(self.get_target_branch_url)
 
134
 
 
135
    def close(self):
 
136
        """Close the merge proposal (without merging it)."""
 
137
        raise NotImplementedError(self.close)
 
138
 
 
139
    def is_merged(self):
 
140
        """Check whether this merge proposal has been merged."""
 
141
        raise NotImplementedError(self.is_merged)
 
142
 
 
143
    def is_closed(self):
 
144
        """Check whether this merge proposal is closed
 
145
 
 
146
        This can either mean that it is merged or rejected.
 
147
        """
 
148
        raise NotImplementedError(self.is_closed)
 
149
 
 
150
    def merge(self, commit_message=None):
 
151
        """Merge this merge proposal."""
 
152
        raise NotImplementedError(self.merge)
 
153
 
 
154
    def can_be_merged(self):
 
155
        """Can this merge proposal be merged?
 
156
 
 
157
        The answer to this can be no if e.g. it has conflics.
 
158
        """
 
159
        raise NotImplementedError(self.can_be_merged)
 
160
 
 
161
 
 
162
class MergeProposalBuilder(object):
 
163
    """Merge proposal creator.
 
164
 
 
165
    :param source_branch: Branch to propose for merging
 
166
    :param target_branch: Target branch
 
167
    """
 
168
 
 
169
    hooks = ProposeMergeHooks()
 
170
 
 
171
    def __init__(self, source_branch, target_branch):
 
172
        self.source_branch = source_branch
 
173
        self.target_branch = target_branch
 
174
 
 
175
    def get_initial_body(self):
 
176
        """Get a body for the proposal for the user to modify.
 
177
 
 
178
        :return: a str or None.
 
179
        """
 
180
        raise NotImplementedError(self.get_initial_body)
 
181
 
 
182
    def get_infotext(self):
 
183
        """Determine the initial comment for the merge proposal.
 
184
        """
 
185
        raise NotImplementedError(self.get_infotext)
 
186
 
 
187
    def create_proposal(self, description, reviewers=None, labels=None,
 
188
                        prerequisite_branch=None, commit_message=None):
 
189
        """Create a proposal to merge a branch for merging.
 
190
 
 
191
        :param description: Description for the merge proposal
 
192
        :param reviewers: Optional list of people to ask reviews from
 
193
        :param labels: Labels to attach to the proposal
 
194
        :param prerequisite_branch: Optional prerequisite branch
 
195
        :param commit_message: Optional commit message
 
196
        :return: A `MergeProposal` object
 
197
        """
 
198
        raise NotImplementedError(self.create_proposal)
 
199
 
 
200
 
 
201
class Hoster(object):
 
202
    """A hosting site manager.
 
203
    """
 
204
 
 
205
    # Does this hoster support arbitrary labels being attached to merge
 
206
    # proposals?
 
207
    supports_merge_proposal_labels = None
 
208
 
 
209
    # Does this hoster support suggesting a commit message in the
 
210
    # merge proposal?
 
211
    supports_merge_proposal_commit_message = None
 
212
 
 
213
    # The base_url that would be visible to users. I.e. https://github.com/
 
214
    # rather than https://api.github.com/
 
215
    base_url = None
 
216
 
 
217
    def publish_derived(self, new_branch, base_branch, name, project=None,
 
218
                        owner=None, revision_id=None, overwrite=False,
 
219
                        allow_lossy=True):
 
220
        """Publish a branch to the site, derived from base_branch.
 
221
 
 
222
        :param base_branch: branch to derive the new branch from
 
223
        :param new_branch: branch to publish
 
224
        :return: resulting branch, public URL
 
225
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
226
            known.
 
227
        """
 
228
        raise NotImplementedError(self.publish)
 
229
 
 
230
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
231
        """Get a derived branch ('a fork').
 
232
        """
 
233
        raise NotImplementedError(self.get_derived_branch)
 
234
 
 
235
    def get_push_url(self, branch):
 
236
        """Get the push URL for a branch."""
 
237
        raise NotImplementedError(self.get_push_url)
 
238
 
 
239
    def get_proposer(self, source_branch, target_branch):
 
240
        """Get a merge proposal creator.
 
241
 
 
242
        :note: source_branch does not have to be hosted by the hoster.
 
243
 
 
244
        :param source_branch: Source branch
 
245
        :param target_branch: Target branch
 
246
        :return: A MergeProposalBuilder object
 
247
        """
 
248
        raise NotImplementedError(self.get_proposer)
 
249
 
 
250
    def iter_proposals(self, source_branch, target_branch, status='open'):
 
251
        """Get the merge proposals for a specified branch tuple.
 
252
 
 
253
        :param source_branch: Source branch
 
254
        :param target_branch: Target branch
 
255
        :param status: Status of proposals to iterate over
 
256
        :return: Iterate over MergeProposal object
 
257
        """
 
258
        raise NotImplementedError(self.iter_proposals)
 
259
 
 
260
    def get_proposal_by_url(self, url):
 
261
        """Retrieve a branch proposal by URL.
 
262
 
 
263
        :param url: Merge proposal URL.
 
264
        :return: MergeProposal object
 
265
        :raise UnsupportedHoster: Hoster does not support this URL
 
266
        """
 
267
        raise NotImplementedError(self.get_proposal_by_url)
 
268
 
 
269
    def hosts(self, branch):
 
270
        """Return true if this hoster hosts given branch."""
 
271
        raise NotImplementedError(self.hosts)
 
272
 
 
273
    @classmethod
 
274
    def probe_from_branch(cls, branch):
 
275
        """Create a Hoster object if this hoster knows about a branch."""
 
276
        url = urlutils.split_segment_parameters(branch.user_url)[0]
 
277
        return cls.probe_from_url(
 
278
            url, possible_transports=[branch.control_transport])
 
279
 
 
280
    @classmethod
 
281
    def probe_from_url(cls, url, possible_hosters=None):
 
282
        """Create a Hoster object if this hoster knows about a URL."""
 
283
        raise NotImplementedError(cls.probe_from_url)
 
284
 
 
285
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
 
286
 
 
287
    def iter_my_proposals(self, status='open'):
 
288
        """Iterate over the proposals created by the currently logged in user.
 
289
 
 
290
        :param status: Only yield proposals with this status
 
291
            (one of: 'open', 'closed', 'merged', 'all')
 
292
        :return: Iterator over MergeProposal objects
 
293
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
294
            known.
 
295
        """
 
296
        raise NotImplementedError(self.iter_my_proposals)
 
297
 
 
298
    @classmethod
 
299
    def iter_instances(cls):
 
300
        """Iterate instances.
 
301
 
 
302
        :return: Hoster instances
 
303
        """
 
304
        raise NotImplementedError(cls.iter_instances)
 
305
 
 
306
 
 
307
def get_hoster(branch, possible_hosters=None):
 
308
    """Find the hoster for a branch."""
 
309
    if possible_hosters:
 
310
        for hoster in possible_hosters:
 
311
            if hoster.hosts(branch):
 
312
                return hoster
 
313
    for name, hoster_cls in hosters.items():
 
314
        try:
 
315
            hoster = hoster_cls.probe_from_branch(branch)
 
316
        except UnsupportedHoster:
 
317
            pass
 
318
        else:
 
319
            if possible_hosters is not None:
 
320
                possible_hosters.append(hoster)
 
321
            return hoster
 
322
    raise UnsupportedHoster(branch)
 
323
 
 
324
 
 
325
def get_proposal_by_url(url):
 
326
    for name, hoster_cls in hosters.items():
 
327
        for instance in hoster_cls.iter_instances():
 
328
            try:
 
329
                return instance.get_proposal_by_url(url)
 
330
            except UnsupportedHoster:
 
331
                pass
 
332
    raise UnsupportedHoster(url)
 
333
 
 
334
 
 
335
hosters = registry.Registry()
 
336
hosters.register_lazy(
 
337
    "launchpad", "breezy.plugins.propose.launchpad",
 
338
    "Launchpad")
 
339
hosters.register_lazy(
 
340
    "github", "breezy.plugins.propose.github",
 
341
    "GitHub")
 
342
hosters.register_lazy(
 
343
    "gitlab", "breezy.plugins.propose.gitlabs",
 
344
    "GitLab")