/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,
25
    )
26
27
0.431.38 by Jelmer Vernooij
Add NoSuchProject.
28
class NoSuchProject(errors.BzrError):
29
30
    _fmt = "Project does not exist: %(project)s."
31
32
    def __init__(self, project):
33
        errors.BzrError.__init__(self)
34
        self.project = project
35
36
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
37
class MergeProposalExists(errors.BzrError):
38
39
    _fmt = "A merge proposal already exists: %(url)s."
40
41
    def __init__(self, url):
42
        errors.BzrError.__init__(self)
43
        self.url = url
44
45
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
46
class NoMergeProposal(errors.BzrError):
47
48
    _fmt = "No merge proposal exists."
49
50
    def __init__(self):
51
        errors.BzrError.__init__(self)
52
53
0.432.2 by Jelmer Vernooij
Publish command sort of works.
54
class UnsupportedHoster(errors.BzrError):
55
56
    _fmt = "No supported hoster for %(branch)s."
57
58
    def __init__(self, branch):
59
        errors.BzrError.__init__(self)
60
        self.branch = branch
61
62
0.431.1 by Jelmer Vernooij
Start work on propose command.
63
class ProposeMergeHooks(hooks.Hooks):
64
    """Hooks for proposing a merge on Launchpad."""
65
66
    def __init__(self):
67
        hooks.Hooks.__init__(self, __name__, "Proposer.hooks")
0.431.57 by Jelmer Vernooij
Cleanups.
68
        self.add_hook(
69
            'get_prerequisite',
0.431.1 by Jelmer Vernooij
Start work on propose command.
70
            "Return the prerequisite branch for proposing as merge.", (3, 0))
0.431.57 by Jelmer Vernooij
Cleanups.
71
        self.add_hook(
72
            'merge_proposal_body',
0.431.1 by Jelmer Vernooij
Start work on propose command.
73
            "Return an initial body for the merge proposal message.", (3, 0))
74
75
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
76
class LabelsUnsupported(errors.BzrError):
77
    """Labels not supported by this hoster."""
78
79
    _fmt = "Labels are not supported by %(hoster)r."
80
81
    def __init__(self, hoster):
82
        errors.BzrError.__init__(self)
83
        self.hoster = hoster
84
85
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
86
class PrerequisiteBranchUnsupported(errors.BzrError):
87
    """Prerequisite branch not supported by this hoster."""
88
89
    def __init__(self, hoster):
90
        errors.BzrError.__init__(self)
91
        self.hoster = hoster
92
93
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
94
class MergeProposal(object):
95
    """A merge proposal.
96
97
    :ivar url: URL for the merge proposal
98
    """
99
100
    def __init__(self, url=None):
101
        self.url = url
102
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
103
    def get_description(self):
104
        """Get the description of the merge proposal."""
105
        raise NotImplementedError(self.get_description)
106
107
    def set_description(self, description):
108
        """Set the description of the merge proposal."""
109
        raise NotImplementedError(self.set_description)
110
0.431.64 by Jelmer Vernooij
Add get_source_branch_url/get_target_branch_url methods.
111
    def get_source_branch_url(self):
112
        """Return the source branch."""
113
        raise NotImplementedError(self.get_source_branch_url)
114
115
    def get_target_branch_url(self):
116
        """Return the target branch."""
117
        raise NotImplementedError(self.get_target_branch_url)
118
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
119
    def close(self):
120
        """Close the merge proposal (without merging it)."""
121
        raise NotImplementedError(self.close)
122
0.431.46 by Jelmer Vernooij
Add MergeProposal.is_merged.
123
    def is_merged(self):
124
        """Check whether this merge proposal has been merged."""
125
        raise NotImplementedError(self.is_merged)
126
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
127
0.432.2 by Jelmer Vernooij
Publish command sort of works.
128
class MergeProposalBuilder(object):
0.431.1 by Jelmer Vernooij
Start work on propose command.
129
    """Merge proposal creator.
130
131
    :param source_branch: Branch to propose for merging
132
    :param target_branch: Target branch
133
    """
134
135
    hooks = ProposeMergeHooks()
136
137
    def __init__(self, source_branch, target_branch):
138
        self.source_branch = source_branch
139
        self.target_branch = target_branch
140
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
141
    def get_initial_body(self):
142
        """Get a body for the proposal for the user to modify.
143
144
        :return: a str or None.
145
        """
146
        raise NotImplementedError(self.get_initial_body)
147
148
    def get_infotext(self):
149
        """Determine the initial comment for the merge proposal.
150
        """
151
        raise NotImplementedError(self.get_infotext)
152
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
153
    def create_proposal(self, description, reviewers=None, labels=None,
154
                        prerequisite_branch=None):
0.431.1 by Jelmer Vernooij
Start work on propose command.
155
        """Create a proposal to merge a branch for merging.
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
156
157
        :param description: Description for the merge proposal
0.431.5 by Jelmer Vernooij
Initial work on gitlab support.
158
        :param reviewers: Optional list of people to ask reviews from
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
159
        :param labels: Labels to attach to the proposal
0.431.56 by Jelmer Vernooij
Add support for prerequisite branches.
160
        :param prerequisite_branch: Optional prerequisite branch
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
161
        :return: A `MergeProposal` object
0.431.1 by Jelmer Vernooij
Start work on propose command.
162
        """
163
        raise NotImplementedError(self.create_proposal)
164
165
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
166
class Hoster(object):
167
    """A hosting site manager.
168
    """
169
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
170
    supports_merge_proposal_labels = None
171
0.431.20 by Jelmer Vernooij
publish -> publish_derived.
172
    def publish_derived(self, new_branch, base_branch, name, project=None,
0.431.51 by Jelmer Vernooij
Allow fallback to lossy by default.
173
                        owner=None, revision_id=None, overwrite=False,
174
                        allow_lossy=True):
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
175
        """Publish a branch to the site, derived from base_branch.
176
177
        :param base_branch: branch to derive the new branch from
178
        :param new_branch: branch to publish
0.432.3 by Jelmer Vernooij
Publish command works for github.
179
        :return: resulting branch, public URL
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
180
        """
181
        raise NotImplementedError(self.publish)
182
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
183
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
0.431.31 by Jelmer Vernooij
Drop autopropose command.
184
        """Get a derived branch ('a fork').
185
        """
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
186
        raise NotImplementedError(self.get_derived_branch)
187
0.431.28 by Jelmer Vernooij
Implement Hoster.get_push_url.
188
    def get_push_url(self, branch):
189
        """Get the push URL for a branch."""
190
        raise NotImplementedError(self.get_push_url)
191
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
192
    def get_proposer(self, source_branch, target_branch):
193
        """Get a merge proposal creator.
194
0.431.31 by Jelmer Vernooij
Drop autopropose command.
195
        :note: source_branch does not have to be hosted by the hoster.
196
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
197
        :param source_branch: Source branch
198
        :param target_branch: Target branch
0.432.2 by Jelmer Vernooij
Publish command sort of works.
199
        :return: A MergeProposalBuilder object
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
200
        """
201
        raise NotImplementedError(self.get_proposer)
202
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
203
    def get_proposal(self, source_branch, target_branch):
204
        """Get a merge proposal for a specified branch tuple.
205
206
        :param source_branch: Source branch
207
        :param target_branch: Target branch
208
        :raise NoMergeProposal: if no merge proposal can be found
209
        :return: A MergeProposal object
210
        """
211
        raise NotImplementedError(self.get_proposal)
212
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
213
    def hosts(self, branch):
214
        """Return true if this hoster hosts given branch."""
215
        raise NotImplementedError(self.hosts)
216
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
217
    @classmethod
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
218
    def probe(cls, branch):
219
        """Create a Hoster object if this hoster knows about a branch."""
220
        raise NotImplementedError(cls.probe)
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
221
0.432.4 by Jelmer Vernooij
Some work on gitlab.
222
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
0.432.8 by Jelmer Vernooij
More todo items.
223
    # TODO(jelmer): Some way of checking up on outstanding merge proposals
0.432.4 by Jelmer Vernooij
Some work on gitlab.
224
0.431.63 by Jelmer Vernooij
Add 'brz my-proposals' command.
225
    def iter_my_proposals(self):
226
        """Iterate over the proposals created by the currently logged in user.
227
228
        :return: Iterator over MergeProposal objects
229
        """
230
        raise NotImplementedError(self.iter_my_proposals)
231
232
    @classmethod
233
    def iter_instances(cls):
234
        """Iterate instances.
235
236
        :return: Hoster instances
237
        """
238
        raise NotImplementedError(cls.iter_instances)
239
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
240
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
241
def get_hoster(branch, possible_hosters=None):
0.432.2 by Jelmer Vernooij
Publish command sort of works.
242
    """Find the hoster for a branch."""
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
243
    if possible_hosters:
244
        for hoster in possible_hosters:
245
            if hoster.hosts(branch):
246
                return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
247
    for name, hoster_cls in hosters.items():
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
248
        try:
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
249
            hoster = hoster_cls.probe(branch)
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
250
        except UnsupportedHoster:
251
            pass
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
252
        else:
253
            if possible_hosters is not None:
254
                possible_hosters.append(hoster)
255
            return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
256
    raise UnsupportedHoster(branch)
257
258
259
hosters = registry.Registry()
260
hosters.register_lazy(
7211.13.7 by Jelmer Vernooij
Fix formatting.
261
    "launchpad", "breezy.plugins.propose.launchpad",
262
    "Launchpad")
263
hosters.register_lazy(
264
    "github", "breezy.plugins.propose.github",
265
    "GitHub")
266
hosters.register_lazy(
267
    "gitlab", "breezy.plugins.propose.gitlabs",
268
    "GitLab")