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