/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: Jelmer Vernooij
  • Date: 2019-03-04 00:16:27 UTC
  • mfrom: (7293 work)
  • mto: This revision was merged to the branch mainline in revision 7318.
  • Revision ID: jelmer@jelmer.uk-20190304001627-v6u7o6pf97tukhek
Merge trunk.

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 ProposeMergeHooks(hooks.Hooks):
 
57
    """Hooks for proposing a merge on Launchpad."""
 
58
 
 
59
    def __init__(self):
 
60
        hooks.Hooks.__init__(self, __name__, "Proposer.hooks")
 
61
        self.add_hook(
 
62
            'get_prerequisite',
 
63
            "Return the prerequisite branch for proposing as merge.", (3, 0))
 
64
        self.add_hook(
 
65
            'merge_proposal_body',
 
66
            "Return an initial body for the merge proposal message.", (3, 0))
 
67
 
 
68
 
 
69
class LabelsUnsupported(errors.BzrError):
 
70
    """Labels not supported by this hoster."""
 
71
 
 
72
    _fmt = "Labels are not supported by %(hoster)r."
 
73
 
 
74
    def __init__(self, hoster):
 
75
        errors.BzrError.__init__(self)
 
76
        self.hoster = hoster
 
77
 
 
78
 
 
79
class PrerequisiteBranchUnsupported(errors.BzrError):
 
80
    """Prerequisite branch not supported by this hoster."""
 
81
 
 
82
    def __init__(self, hoster):
 
83
        errors.BzrError.__init__(self)
 
84
        self.hoster = hoster
 
85
 
 
86
 
 
87
class HosterLoginRequired(errors.BzrError):
 
88
    """Action requires hoster login credentials."""
 
89
 
 
90
    _fmt = "Action requires credentials for hosting site %(hoster)r."""
 
91
 
 
92
    def __init__(self, hoster):
 
93
        errors.BzrError.__init__(self)
 
94
        self.hoster = hoster
 
95
 
 
96
 
 
97
class MergeProposal(object):
 
98
    """A merge proposal.
 
99
 
 
100
    :ivar url: URL for the merge proposal
 
101
    """
 
102
 
 
103
    def __init__(self, url=None):
 
104
        self.url = url
 
105
 
 
106
    def get_description(self):
 
107
        """Get the description of the merge proposal."""
 
108
        raise NotImplementedError(self.get_description)
 
109
 
 
110
    def set_description(self, description):
 
111
        """Set the description of the merge proposal."""
 
112
        raise NotImplementedError(self.set_description)
 
113
 
 
114
    def get_source_branch_url(self):
 
115
        """Return the source branch."""
 
116
        raise NotImplementedError(self.get_source_branch_url)
 
117
 
 
118
    def get_target_branch_url(self):
 
119
        """Return the target branch."""
 
120
        raise NotImplementedError(self.get_target_branch_url)
 
121
 
 
122
    def close(self):
 
123
        """Close the merge proposal (without merging it)."""
 
124
        raise NotImplementedError(self.close)
 
125
 
 
126
    def is_merged(self):
 
127
        """Check whether this merge proposal has been merged."""
 
128
        raise NotImplementedError(self.is_merged)
 
129
 
 
130
 
 
131
class MergeProposalBuilder(object):
 
132
    """Merge proposal creator.
 
133
 
 
134
    :param source_branch: Branch to propose for merging
 
135
    :param target_branch: Target branch
 
136
    """
 
137
 
 
138
    hooks = ProposeMergeHooks()
 
139
 
 
140
    def __init__(self, source_branch, target_branch):
 
141
        self.source_branch = source_branch
 
142
        self.target_branch = target_branch
 
143
 
 
144
    def get_initial_body(self):
 
145
        """Get a body for the proposal for the user to modify.
 
146
 
 
147
        :return: a str or None.
 
148
        """
 
149
        raise NotImplementedError(self.get_initial_body)
 
150
 
 
151
    def get_infotext(self):
 
152
        """Determine the initial comment for the merge proposal.
 
153
        """
 
154
        raise NotImplementedError(self.get_infotext)
 
155
 
 
156
    def create_proposal(self, description, reviewers=None, labels=None,
 
157
                        prerequisite_branch=None):
 
158
        """Create a proposal to merge a branch for merging.
 
159
 
 
160
        :param description: Description for the merge proposal
 
161
        :param reviewers: Optional list of people to ask reviews from
 
162
        :param labels: Labels to attach to the proposal
 
163
        :param prerequisite_branch: Optional prerequisite branch
 
164
        :return: A `MergeProposal` object
 
165
        """
 
166
        raise NotImplementedError(self.create_proposal)
 
167
 
 
168
 
 
169
class Hoster(object):
 
170
    """A hosting site manager.
 
171
    """
 
172
 
 
173
    # Does this hoster support arbitrary labels being attached to merge
 
174
    # proposals?
 
175
    supports_merge_proposal_labels = None
 
176
 
 
177
    # The base_url that would be visible to users. I.e. https://github.com/
 
178
    # rather than https://api.github.com/
 
179
    base_url = None
 
180
 
 
181
    def publish_derived(self, new_branch, base_branch, name, project=None,
 
182
                        owner=None, revision_id=None, overwrite=False,
 
183
                        allow_lossy=True):
 
184
        """Publish a branch to the site, derived from base_branch.
 
185
 
 
186
        :param base_branch: branch to derive the new branch from
 
187
        :param new_branch: branch to publish
 
188
        :return: resulting branch, public URL
 
189
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
190
            known.
 
191
        """
 
192
        raise NotImplementedError(self.publish)
 
193
 
 
194
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
195
        """Get a derived branch ('a fork').
 
196
        """
 
197
        raise NotImplementedError(self.get_derived_branch)
 
198
 
 
199
    def get_push_url(self, branch):
 
200
        """Get the push URL for a branch."""
 
201
        raise NotImplementedError(self.get_push_url)
 
202
 
 
203
    def get_proposer(self, source_branch, target_branch):
 
204
        """Get a merge proposal creator.
 
205
 
 
206
        :note: source_branch does not have to be hosted by the hoster.
 
207
 
 
208
        :param source_branch: Source branch
 
209
        :param target_branch: Target branch
 
210
        :return: A MergeProposalBuilder object
 
211
        """
 
212
        raise NotImplementedError(self.get_proposer)
 
213
 
 
214
    def iter_proposals(self, source_branch, target_branch, status='open'):
 
215
        """Get a merge proposal for a specified branch tuple.
 
216
 
 
217
        :param source_branch: Source branch
 
218
        :param target_branch: Target branch
 
219
        :param status: Status of proposals to iterate over
 
220
        :return: Iterate over MergeProposal object
 
221
        """
 
222
        raise NotImplementedError(self.iter_proposals)
 
223
 
 
224
    def hosts(self, branch):
 
225
        """Return true if this hoster hosts given branch."""
 
226
        raise NotImplementedError(self.hosts)
 
227
 
 
228
    @classmethod
 
229
    def probe_from_branch(cls, branch):
 
230
        """Create a Hoster object if this hoster knows about a branch."""
 
231
        url = urlutils.split_segment_parameters(branch.user_url)[0]
 
232
        return cls.probe_from_url(url)
 
233
 
 
234
    @classmethod
 
235
    def probe_from_url(cls, url):
 
236
        """Create a Hoster object if this hoster knows about a URL."""
 
237
        raise NotImplementedError(cls.probe_from_url)
 
238
 
 
239
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
 
240
 
 
241
    def iter_my_proposals(self, status='open'):
 
242
        """Iterate over the proposals created by the currently logged in user.
 
243
 
 
244
        :param status: Only yield proposals with this status
 
245
            (one of: 'open', 'closed', 'merged', 'all')
 
246
        :return: Iterator over MergeProposal objects
 
247
        :raise HosterLoginRequired: Action requires a hoster login, but none is
 
248
            known.
 
249
        """
 
250
        raise NotImplementedError(self.iter_my_proposals)
 
251
 
 
252
    @classmethod
 
253
    def iter_instances(cls):
 
254
        """Iterate instances.
 
255
 
 
256
        :return: Hoster instances
 
257
        """
 
258
        raise NotImplementedError(cls.iter_instances)
 
259
 
 
260
 
 
261
def get_hoster(branch, possible_hosters=None):
 
262
    """Find the hoster for a branch."""
 
263
    if possible_hosters:
 
264
        for hoster in possible_hosters:
 
265
            if hoster.hosts(branch):
 
266
                return hoster
 
267
    for name, hoster_cls in hosters.items():
 
268
        try:
 
269
            hoster = hoster_cls.probe_from_branch(branch)
 
270
        except UnsupportedHoster:
 
271
            pass
 
272
        else:
 
273
            if possible_hosters is not None:
 
274
                possible_hosters.append(hoster)
 
275
            return hoster
 
276
    raise UnsupportedHoster(branch)
 
277
 
 
278
 
 
279
hosters = registry.Registry()
 
280
hosters.register_lazy(
 
281
    "launchpad", "breezy.plugins.propose.launchpad",
 
282
    "Launchpad")
 
283
hosters.register_lazy(
 
284
    "github", "breezy.plugins.propose.github",
 
285
    "GitHub")
 
286
hosters.register_lazy(
 
287
    "gitlab", "breezy.plugins.propose.gitlabs",
 
288
    "GitLab")