/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-01-13 17:32:22 UTC
  • mto: This revision was merged to the branch mainline in revision 7248.
  • Revision ID: jelmer@jelmer.uk-20190113173222-dteocq7209l6y0m6
Use tree timestamp when exporting.

This helps generate consistent hashes.

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
    )
 
26
 
 
27
 
 
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
 
 
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
 
 
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
 
 
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")
 
60
        self.add_hook(
 
61
            'get_prerequisite',
 
62
            "Return the prerequisite branch for proposing as merge.", (3, 0))
 
63
        self.add_hook(
 
64
            'merge_proposal_body',
 
65
            "Return an initial body for the merge proposal message.", (3, 0))
 
66
 
 
67
 
 
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
 
 
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
 
 
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
 
 
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
 
 
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
 
 
111
    def close(self):
 
112
        """Close the merge proposal (without merging it)."""
 
113
        raise NotImplementedError(self.close)
 
114
 
 
115
    def is_merged(self):
 
116
        """Check whether this merge proposal has been merged."""
 
117
        raise NotImplementedError(self.is_merged)
 
118
 
 
119
 
 
120
class MergeProposalBuilder(object):
 
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
 
 
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
 
 
145
    def create_proposal(self, description, reviewers=None, labels=None,
 
146
                        prerequisite_branch=None):
 
147
        """Create a proposal to merge a branch for merging.
 
148
 
 
149
        :param description: Description for the merge proposal
 
150
        :param reviewers: Optional list of people to ask reviews from
 
151
        :param labels: Labels to attach to the proposal
 
152
        :param prerequisite_branch: Optional prerequisite branch
 
153
        :return: A `MergeProposal` object
 
154
        """
 
155
        raise NotImplementedError(self.create_proposal)
 
156
 
 
157
 
 
158
class Hoster(object):
 
159
    """A hosting site manager.
 
160
    """
 
161
 
 
162
    supports_merge_proposal_labels = None
 
163
 
 
164
    def publish_derived(self, new_branch, base_branch, name, project=None,
 
165
                        owner=None, revision_id=None, overwrite=False,
 
166
                        allow_lossy=True):
 
167
        """Publish a branch to the site, derived from base_branch.
 
168
 
 
169
        :param base_branch: branch to derive the new branch from
 
170
        :param new_branch: branch to publish
 
171
        :return: resulting branch, public URL
 
172
        """
 
173
        raise NotImplementedError(self.publish)
 
174
 
 
175
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
 
176
        """Get a derived branch ('a fork').
 
177
        """
 
178
        raise NotImplementedError(self.get_derived_branch)
 
179
 
 
180
    def get_push_url(self, branch):
 
181
        """Get the push URL for a branch."""
 
182
        raise NotImplementedError(self.get_push_url)
 
183
 
 
184
    def get_proposer(self, source_branch, target_branch):
 
185
        """Get a merge proposal creator.
 
186
 
 
187
        :note: source_branch does not have to be hosted by the hoster.
 
188
 
 
189
        :param source_branch: Source branch
 
190
        :param target_branch: Target branch
 
191
        :return: A MergeProposalBuilder object
 
192
        """
 
193
        raise NotImplementedError(self.get_proposer)
 
194
 
 
195
    def iter_proposals(self, source_branch, target_branch, status='open'):
 
196
        """Get a merge proposal for a specified branch tuple.
 
197
 
 
198
        :param source_branch: Source branch
 
199
        :param target_branch: Target branch
 
200
        :param status: Status of proposals to iterate over
 
201
        :return: Iterate over MergeProposal object
 
202
        """
 
203
        raise NotImplementedError(self.iter_proposals)
 
204
 
 
205
    def hosts(self, branch):
 
206
        """Return true if this hoster hosts given branch."""
 
207
        raise NotImplementedError(self.hosts)
 
208
 
 
209
    @classmethod
 
210
    def probe(cls, branch):
 
211
        """Create a Hoster object if this hoster knows about a branch."""
 
212
        raise NotImplementedError(cls.probe)
 
213
 
 
214
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
 
215
 
 
216
    def iter_my_proposals(self, status='open'):
 
217
        """Iterate over the proposals created by the currently logged in user.
 
218
 
 
219
        :param status: Only yield proposals with this status
 
220
            (one of: 'open', 'closed', 'merged', 'all')
 
221
        :return: Iterator over MergeProposal objects
 
222
        """
 
223
        raise NotImplementedError(self.iter_my_proposals)
 
224
 
 
225
    @classmethod
 
226
    def iter_instances(cls):
 
227
        """Iterate instances.
 
228
 
 
229
        :return: Hoster instances
 
230
        """
 
231
        raise NotImplementedError(cls.iter_instances)
 
232
 
 
233
 
 
234
def get_hoster(branch, possible_hosters=None):
 
235
    """Find the hoster for a branch."""
 
236
    if possible_hosters:
 
237
        for hoster in possible_hosters:
 
238
            if hoster.hosts(branch):
 
239
                return hoster
 
240
    for name, hoster_cls in hosters.items():
 
241
        try:
 
242
            hoster = hoster_cls.probe(branch)
 
243
        except UnsupportedHoster:
 
244
            pass
 
245
        else:
 
246
            if possible_hosters is not None:
 
247
                possible_hosters.append(hoster)
 
248
            return hoster
 
249
    raise UnsupportedHoster(branch)
 
250
 
 
251
 
 
252
hosters = registry.Registry()
 
253
hosters.register_lazy(
 
254
    "launchpad", "breezy.plugins.propose.launchpad",
 
255
    "Launchpad")
 
256
hosters.register_lazy(
 
257
    "github", "breezy.plugins.propose.github",
 
258
    "GitHub")
 
259
hosters.register_lazy(
 
260
    "gitlab", "breezy.plugins.propose.gitlabs",
 
261
    "GitLab")