/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar
0.431.1 by Jelmer Vernooij
Start work on propose command.
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,
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
0.431.1 by Jelmer Vernooij
Start work on propose command.
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")
0.431.57 by Jelmer Vernooij
Cleanups.
61
        self.add_hook(
62
            'get_prerequisite',
0.431.1 by Jelmer Vernooij
Start work on propose command.
63
            "Return the prerequisite branch for proposing as merge.", (3, 0))
0.431.57 by Jelmer Vernooij
Cleanups.
64
        self.add_hook(
65
            'merge_proposal_body',
0.431.1 by Jelmer Vernooij
Start work on propose command.
66
            "Return an initial body for the merge proposal message.", (3, 0))
67
68
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
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
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
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
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
87
class HosterLoginRequired(errors.BzrError):
88
    """Action requires hoster login credentials."""
89
7268.8.2 by Jelmer Vernooij
Handle GitHub errors.
90
    _fmt = "Action requires credentials for hosting site %(hoster)r."""
91
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
92
    def __init__(self, hoster):
93
        errors.BzrError.__init__(self)
94
        self.hoster = hoster
95
96
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
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
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
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
7296.8.2 by Jelmer Vernooij
Add feature flag for commit message.
114
    def get_commit_message(self):
115
        """Get the proposed commit message."""
116
        raise NotImplementedError(self.get_commit_message)
117
118
    def set_commit_message(self, commit_message):
119
        """Set the propose commit message."""
120
        raise NotImplementedError(self.set_commit_message)
121
0.431.64 by Jelmer Vernooij
Add get_source_branch_url/get_target_branch_url methods.
122
    def get_source_branch_url(self):
123
        """Return the source branch."""
124
        raise NotImplementedError(self.get_source_branch_url)
125
126
    def get_target_branch_url(self):
127
        """Return the target branch."""
128
        raise NotImplementedError(self.get_target_branch_url)
129
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
130
    def close(self):
131
        """Close the merge proposal (without merging it)."""
132
        raise NotImplementedError(self.close)
133
0.431.46 by Jelmer Vernooij
Add MergeProposal.is_merged.
134
    def is_merged(self):
135
        """Check whether this merge proposal has been merged."""
136
        raise NotImplementedError(self.is_merged)
137
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
138
0.432.2 by Jelmer Vernooij
Publish command sort of works.
139
class MergeProposalBuilder(object):
0.431.1 by Jelmer Vernooij
Start work on propose command.
140
    """Merge proposal creator.
141
142
    :param source_branch: Branch to propose for merging
143
    :param target_branch: Target branch
144
    """
145
146
    hooks = ProposeMergeHooks()
147
148
    def __init__(self, source_branch, target_branch):
149
        self.source_branch = source_branch
150
        self.target_branch = target_branch
151
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
152
    def get_initial_body(self):
153
        """Get a body for the proposal for the user to modify.
154
155
        :return: a str or None.
156
        """
157
        raise NotImplementedError(self.get_initial_body)
158
159
    def get_infotext(self):
160
        """Determine the initial comment for the merge proposal.
161
        """
162
        raise NotImplementedError(self.get_infotext)
163
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
164
    def create_proposal(self, description, reviewers=None, labels=None,
7296.8.1 by Jelmer Vernooij
Add commit-message option to 'brz propose'.
165
                        prerequisite_branch=None, commit_message=None):
0.431.1 by Jelmer Vernooij
Start work on propose command.
166
        """Create a proposal to merge a branch for merging.
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
167
168
        :param description: Description for the merge proposal
0.431.5 by Jelmer Vernooij
Initial work on gitlab support.
169
        :param reviewers: Optional list of people to ask reviews from
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
170
        :param labels: Labels to attach to the proposal
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
171
        :param prerequisite_branch: Optional prerequisite branch
7296.8.1 by Jelmer Vernooij
Add commit-message option to 'brz propose'.
172
        :param commit_message: Optional commit message
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
173
        :return: A `MergeProposal` object
0.431.1 by Jelmer Vernooij
Start work on propose command.
174
        """
175
        raise NotImplementedError(self.create_proposal)
176
177
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
178
class Hoster(object):
179
    """A hosting site manager.
180
    """
181
7260.1.1 by Jelmer Vernooij
Add .base_url property to Hoster.
182
    # Does this hoster support arbitrary labels being attached to merge
183
    # proposals?
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
184
    supports_merge_proposal_labels = None
185
7296.8.2 by Jelmer Vernooij
Add feature flag for commit message.
186
    # Does this hoster support suggesting a commit message in the
187
    # merge proposal?
188
    supports_merge_proposal_commit_message = None
189
7260.1.1 by Jelmer Vernooij
Add .base_url property to Hoster.
190
    # The base_url that would be visible to users. I.e. https://github.com/
191
    # rather than https://api.github.com/
192
    base_url = None
193
0.431.20 by Jelmer Vernooij
publish -> publish_derived.
194
    def publish_derived(self, new_branch, base_branch, name, project=None,
0.431.51 by Jelmer Vernooij
Allow fallback to lossy by default.
195
                        owner=None, revision_id=None, overwrite=False,
196
                        allow_lossy=True):
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
197
        """Publish a branch to the site, derived from base_branch.
198
199
        :param base_branch: branch to derive the new branch from
200
        :param new_branch: branch to publish
0.432.3 by Jelmer Vernooij
Publish command works for github.
201
        :return: resulting branch, public URL
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
202
        :raise HosterLoginRequired: Action requires a hoster login, but none is
203
            known.
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
204
        """
205
        raise NotImplementedError(self.publish)
206
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
207
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
0.431.31 by Jelmer Vernooij
Drop autopropose command.
208
        """Get a derived branch ('a fork').
209
        """
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
210
        raise NotImplementedError(self.get_derived_branch)
211
0.431.28 by Jelmer Vernooij
Implement Hoster.get_push_url.
212
    def get_push_url(self, branch):
213
        """Get the push URL for a branch."""
214
        raise NotImplementedError(self.get_push_url)
215
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
216
    def get_proposer(self, source_branch, target_branch):
217
        """Get a merge proposal creator.
218
0.431.31 by Jelmer Vernooij
Drop autopropose command.
219
        :note: source_branch does not have to be hosted by the hoster.
220
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
221
        :param source_branch: Source branch
222
        :param target_branch: Target branch
0.432.2 by Jelmer Vernooij
Publish command sort of works.
223
        :return: A MergeProposalBuilder object
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
224
        """
225
        raise NotImplementedError(self.get_proposer)
226
0.431.68 by Jelmer Vernooij
Add status to other Hosters.
227
    def iter_proposals(self, source_branch, target_branch, status='open'):
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
228
        """Get a merge proposal for a specified branch tuple.
229
230
        :param source_branch: Source branch
231
        :param target_branch: Target branch
0.431.68 by Jelmer Vernooij
Add status to other Hosters.
232
        :param status: Status of proposals to iterate over
0.431.67 by Jelmer Vernooij
Support multiple merge proposals per branch.
233
        :return: Iterate over MergeProposal object
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
234
        """
0.431.67 by Jelmer Vernooij
Support multiple merge proposals per branch.
235
        raise NotImplementedError(self.iter_proposals)
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
236
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
237
    def hosts(self, branch):
238
        """Return true if this hoster hosts given branch."""
239
        raise NotImplementedError(self.hosts)
240
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
241
    @classmethod
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
242
    def probe_from_branch(cls, branch):
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
243
        """Create a Hoster object if this hoster knows about a branch."""
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
244
        url = urlutils.split_segment_parameters(branch.user_url)[0]
245
        return cls.probe_from_url(url)
246
247
    @classmethod
248
    def probe_from_url(cls, url):
249
        """Create a Hoster object if this hoster knows about a URL."""
250
        raise NotImplementedError(cls.probe_from_url)
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
251
0.432.4 by Jelmer Vernooij
Some work on gitlab.
252
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
253
0.431.66 by Jelmer Vernooij
Add support for status argument.
254
    def iter_my_proposals(self, status='open'):
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
255
        """Iterate over the proposals created by the currently logged in user.
256
0.431.66 by Jelmer Vernooij
Add support for status argument.
257
        :param status: Only yield proposals with this status
258
            (one of: 'open', 'closed', 'merged', 'all')
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
259
        :return: Iterator over MergeProposal objects
7268.8.1 by Jelmer Vernooij
Add HosterLoginRequired exception.
260
        :raise HosterLoginRequired: Action requires a hoster login, but none is
261
            known.
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
262
        """
263
        raise NotImplementedError(self.iter_my_proposals)
264
265
    @classmethod
266
    def iter_instances(cls):
267
        """Iterate instances.
268
269
        :return: Hoster instances
270
        """
271
        raise NotImplementedError(cls.iter_instances)
272
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
273
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
274
def get_hoster(branch, possible_hosters=None):
0.432.2 by Jelmer Vernooij
Publish command sort of works.
275
    """Find the hoster for a branch."""
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
276
    if possible_hosters:
277
        for hoster in possible_hosters:
278
            if hoster.hosts(branch):
279
                return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
280
    for name, hoster_cls in hosters.items():
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
281
        try:
7268.12.1 by Jelmer Vernooij
Split out probe_from_url.
282
            hoster = hoster_cls.probe_from_branch(branch)
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
283
        except UnsupportedHoster:
284
            pass
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
285
        else:
286
            if possible_hosters is not None:
287
                possible_hosters.append(hoster)
288
            return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
289
    raise UnsupportedHoster(branch)
290
291
292
hosters = registry.Registry()
293
hosters.register_lazy(
7211.13.7 by Jelmer Vernooij
Fix formatting.
294
    "launchpad", "breezy.plugins.propose.launchpad",
295
    "Launchpad")
296
hosters.register_lazy(
297
    "github", "breezy.plugins.propose.github",
298
    "GitHub")
299
hosters.register_lazy(
300
    "gitlab", "breezy.plugins.propose.gitlabs",
301
    "GitLab")