/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")
68
        self.add_hook('get_prerequisite',
69
            "Return the prerequisite branch for proposing as merge.", (3, 0))
70
        self.add_hook('merge_proposal_body',
71
            "Return an initial body for the merge proposal message.", (3, 0))
72
73
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
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
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
84
class MergeProposal(object):
85
    """A merge proposal.
86
87
    :ivar url: URL for the merge proposal
88
    """
89
90
    def __init__(self, url=None):
91
        self.url = url
92
0.432.8 by Jelmer Vernooij
More todo items.
93
    # TODO(jelmer): provide some way to check if this merge proposal has been
94
    # merged.
95
0.431.39 by Jelmer Vernooij
Extend the merge proposal abstraction a bit.
96
    def get_description(self):
97
        """Get the description of the merge proposal."""
98
        raise NotImplementedError(self.get_description)
99
100
    def set_description(self, description):
101
        """Set the description of the merge proposal."""
102
        raise NotImplementedError(self.set_description)
103
104
    def close(self):
105
        """Close the merge proposal (without merging it)."""
106
        raise NotImplementedError(self.close)
107
0.431.46 by Jelmer Vernooij
Add MergeProposal.is_merged.
108
    def is_merged(self):
109
        """Check whether this merge proposal has been merged."""
110
        raise NotImplementedError(self.is_merged)
111
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
112
0.432.2 by Jelmer Vernooij
Publish command sort of works.
113
class MergeProposalBuilder(object):
0.431.1 by Jelmer Vernooij
Start work on propose command.
114
    """Merge proposal creator.
115
116
    :param source_branch: Branch to propose for merging
117
    :param target_branch: Target branch
118
    """
119
120
    hooks = ProposeMergeHooks()
121
122
    def __init__(self, source_branch, target_branch):
123
        self.source_branch = source_branch
124
        self.target_branch = target_branch
125
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
126
    def get_initial_body(self):
127
        """Get a body for the proposal for the user to modify.
128
129
        :return: a str or None.
130
        """
131
        raise NotImplementedError(self.get_initial_body)
132
133
    def get_infotext(self):
134
        """Determine the initial comment for the merge proposal.
135
        """
136
        raise NotImplementedError(self.get_infotext)
137
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
138
    def create_proposal(self, description, reviewers=None, labels=None):
0.431.1 by Jelmer Vernooij
Start work on propose command.
139
        """Create a proposal to merge a branch for merging.
0.431.2 by Jelmer Vernooij
Add launchpad implementation.
140
141
        :param description: Description for the merge proposal
0.431.5 by Jelmer Vernooij
Initial work on gitlab support.
142
        :param reviewers: Optional list of people to ask reviews from
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
143
        :param labels: Labels to attach to the proposal
0.431.3 by Jelmer Vernooij
Add a MergeProposal object.
144
        :return: A `MergeProposal` object
0.431.1 by Jelmer Vernooij
Start work on propose command.
145
        """
146
        raise NotImplementedError(self.create_proposal)
147
148
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
149
class Hoster(object):
150
    """A hosting site manager.
151
    """
152
0.431.13 by Jelmer Vernooij
Add support for labels on merge proposals.
153
    supports_merge_proposal_labels = None
154
0.431.20 by Jelmer Vernooij
publish -> publish_derived.
155
    def publish_derived(self, new_branch, base_branch, name, project=None,
0.431.51 by Jelmer Vernooij
Allow fallback to lossy by default.
156
                        owner=None, revision_id=None, overwrite=False,
157
                        allow_lossy=True):
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
158
        """Publish a branch to the site, derived from base_branch.
159
160
        :param base_branch: branch to derive the new branch from
161
        :param new_branch: branch to publish
0.432.3 by Jelmer Vernooij
Publish command works for github.
162
        :return: resulting branch, public URL
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
163
        """
164
        raise NotImplementedError(self.publish)
165
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
166
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
0.431.31 by Jelmer Vernooij
Drop autopropose command.
167
        """Get a derived branch ('a fork').
168
        """
0.431.22 by Jelmer Vernooij
Add Hoster.get_derived_branch.
169
        raise NotImplementedError(self.get_derived_branch)
170
0.431.28 by Jelmer Vernooij
Implement Hoster.get_push_url.
171
    def get_push_url(self, branch):
172
        """Get the push URL for a branch."""
173
        raise NotImplementedError(self.get_push_url)
174
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
175
    def get_proposer(self, source_branch, target_branch):
176
        """Get a merge proposal creator.
177
0.431.31 by Jelmer Vernooij
Drop autopropose command.
178
        :note: source_branch does not have to be hosted by the hoster.
179
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
180
        :param source_branch: Source branch
181
        :param target_branch: Target branch
0.432.2 by Jelmer Vernooij
Publish command sort of works.
182
        :return: A MergeProposalBuilder object
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
183
        """
184
        raise NotImplementedError(self.get_proposer)
185
0.431.35 by Jelmer Vernooij
Add Hoster.get_proposal.
186
    def get_proposal(self, source_branch, target_branch):
187
        """Get a merge proposal for a specified branch tuple.
188
189
        :param source_branch: Source branch
190
        :param target_branch: Target branch
191
        :raise NoMergeProposal: if no merge proposal can be found
192
        :return: A MergeProposal object
193
        """
194
        raise NotImplementedError(self.get_proposal)
195
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
196
    def hosts(self, branch):
197
        """Return true if this hoster hosts given branch."""
198
        raise NotImplementedError(self.hosts)
199
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
200
    @classmethod
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
201
    def probe(cls, branch):
202
        """Create a Hoster object if this hoster knows about a branch."""
203
        raise NotImplementedError(cls.probe)
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
204
0.432.4 by Jelmer Vernooij
Some work on gitlab.
205
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
0.432.8 by Jelmer Vernooij
More todo items.
206
    # TODO(jelmer): Some way of checking up on outstanding merge proposals
0.432.4 by Jelmer Vernooij
Some work on gitlab.
207
0.432.1 by Jelmer Vernooij
Initial work on hoster support.
208
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
209
def get_hoster(branch, possible_hosters=None):
0.432.2 by Jelmer Vernooij
Publish command sort of works.
210
    """Find the hoster for a branch."""
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
211
    if possible_hosters:
212
        for hoster in possible_hosters:
213
            if hoster.hosts(branch):
214
                return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
215
    for name, hoster_cls in hosters.items():
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
216
        try:
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
217
            hoster = hoster_cls.probe(branch)
0.432.9 by Jelmer Vernooij
Drop is_compatible nonesense.
218
        except UnsupportedHoster:
219
            pass
0.433.1 by Jelmer Vernooij
Add Hoster.hosts.
220
        else:
221
            if possible_hosters is not None:
222
                possible_hosters.append(hoster)
223
            return hoster
0.432.2 by Jelmer Vernooij
Publish command sort of works.
224
    raise UnsupportedHoster(branch)
225
226
227
hosters = registry.Registry()
228
hosters.register_lazy(
229
        "launchpad", "breezy.plugins.propose.launchpad",
230
        "Launchpad")
231
hosters.register_lazy(
232
        "github", "breezy.plugins.propose.github",
233
        "GitHub")
234
hosters.register_lazy(
0.432.4 by Jelmer Vernooij
Some work on gitlab.
235
        "gitlab", "breezy.plugins.propose.gitlabs",
0.432.2 by Jelmer Vernooij
Publish command sort of works.
236
        "GitLab")