1
# Copyright (C) 2018 Breezy Developers
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.
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.
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
17
"""Helper functions for proposing merges."""
19
from __future__ import absolute_import
29
class NoSuchProject(errors.BzrError):
31
_fmt = "Project does not exist: %(project)s."
33
def __init__(self, project):
34
errors.BzrError.__init__(self)
35
self.project = project
38
class MergeProposalExists(errors.BzrError):
40
_fmt = "A merge proposal already exists: %(url)s."
42
def __init__(self, url):
43
errors.BzrError.__init__(self)
47
class UnsupportedHoster(errors.BzrError):
49
_fmt = "No supported hoster for %(branch)s."
51
def __init__(self, branch):
52
errors.BzrError.__init__(self)
56
class ProposeMergeHooks(hooks.Hooks):
57
"""Hooks for proposing a merge on Launchpad."""
60
hooks.Hooks.__init__(self, __name__, "Proposer.hooks")
63
"Return the prerequisite branch for proposing as merge.", (3, 0))
65
'merge_proposal_body',
66
"Return an initial body for the merge proposal message.", (3, 0))
69
class LabelsUnsupported(errors.BzrError):
70
"""Labels not supported by this hoster."""
72
_fmt = "Labels are not supported by %(hoster)r."
74
def __init__(self, hoster):
75
errors.BzrError.__init__(self)
79
class PrerequisiteBranchUnsupported(errors.BzrError):
80
"""Prerequisite branch not supported by this hoster."""
82
def __init__(self, hoster):
83
errors.BzrError.__init__(self)
87
class HosterLoginRequired(errors.BzrError):
88
"""Action requires hoster login credentials."""
90
_fmt = "Action requires credentials for hosting site %(hoster)r."""
92
def __init__(self, hoster):
93
errors.BzrError.__init__(self)
97
class MergeProposal(object):
100
:ivar url: URL for the merge proposal
103
def __init__(self, url=None):
106
def get_description(self):
107
"""Get the description of the merge proposal."""
108
raise NotImplementedError(self.get_description)
110
def set_description(self, description):
111
"""Set the description of the merge proposal."""
112
raise NotImplementedError(self.set_description)
114
def get_commit_message(self):
115
"""Get the proposed commit message."""
116
raise NotImplementedError(self.get_commit_message)
118
def set_commit_message(self, commit_message):
119
"""Set the propose commit message."""
120
raise NotImplementedError(self.set_commit_message)
122
def get_source_branch_url(self):
123
"""Return the source branch."""
124
raise NotImplementedError(self.get_source_branch_url)
126
def get_target_branch_url(self):
127
"""Return the target branch."""
128
raise NotImplementedError(self.get_target_branch_url)
131
"""Close the merge proposal (without merging it)."""
132
raise NotImplementedError(self.close)
135
"""Check whether this merge proposal has been merged."""
136
raise NotImplementedError(self.is_merged)
139
class MergeProposalBuilder(object):
140
"""Merge proposal creator.
142
:param source_branch: Branch to propose for merging
143
:param target_branch: Target branch
146
hooks = ProposeMergeHooks()
148
def __init__(self, source_branch, target_branch):
149
self.source_branch = source_branch
150
self.target_branch = target_branch
152
def get_initial_body(self):
153
"""Get a body for the proposal for the user to modify.
155
:return: a str or None.
157
raise NotImplementedError(self.get_initial_body)
159
def get_infotext(self):
160
"""Determine the initial comment for the merge proposal.
162
raise NotImplementedError(self.get_infotext)
164
def create_proposal(self, description, reviewers=None, labels=None,
165
prerequisite_branch=None, commit_message=None):
166
"""Create a proposal to merge a branch for merging.
168
:param description: Description for the merge proposal
169
:param reviewers: Optional list of people to ask reviews from
170
:param labels: Labels to attach to the proposal
171
:param prerequisite_branch: Optional prerequisite branch
172
:param commit_message: Optional commit message
173
:return: A `MergeProposal` object
175
raise NotImplementedError(self.create_proposal)
178
class Hoster(object):
179
"""A hosting site manager.
182
# Does this hoster support arbitrary labels being attached to merge
184
supports_merge_proposal_labels = None
186
# Does this hoster support suggesting a commit message in the
188
supports_merge_proposal_commit_message = None
190
# The base_url that would be visible to users. I.e. https://github.com/
191
# rather than https://api.github.com/
194
def publish_derived(self, new_branch, base_branch, name, project=None,
195
owner=None, revision_id=None, overwrite=False,
197
"""Publish a branch to the site, derived from base_branch.
199
:param base_branch: branch to derive the new branch from
200
:param new_branch: branch to publish
201
:return: resulting branch, public URL
202
:raise HosterLoginRequired: Action requires a hoster login, but none is
205
raise NotImplementedError(self.publish)
207
def get_derived_branch(self, base_branch, name, project=None, owner=None):
208
"""Get a derived branch ('a fork').
210
raise NotImplementedError(self.get_derived_branch)
212
def get_push_url(self, branch):
213
"""Get the push URL for a branch."""
214
raise NotImplementedError(self.get_push_url)
216
def get_proposer(self, source_branch, target_branch):
217
"""Get a merge proposal creator.
219
:note: source_branch does not have to be hosted by the hoster.
221
:param source_branch: Source branch
222
:param target_branch: Target branch
223
:return: A MergeProposalBuilder object
225
raise NotImplementedError(self.get_proposer)
227
def iter_proposals(self, source_branch, target_branch, status='open'):
228
"""Get a merge proposal for a specified branch tuple.
230
:param source_branch: Source branch
231
:param target_branch: Target branch
232
:param status: Status of proposals to iterate over
233
:return: Iterate over MergeProposal object
235
raise NotImplementedError(self.iter_proposals)
237
def hosts(self, branch):
238
"""Return true if this hoster hosts given branch."""
239
raise NotImplementedError(self.hosts)
242
def probe_from_branch(cls, branch):
243
"""Create a Hoster object if this hoster knows about a branch."""
244
url = urlutils.split_segment_parameters(branch.user_url)[0]
245
return cls.probe_from_url(
246
url, possible_transports=[branch.control_transport])
249
def probe_from_url(cls, url, possible_hosters=None):
250
"""Create a Hoster object if this hoster knows about a URL."""
251
raise NotImplementedError(cls.probe_from_url)
253
# TODO(jelmer): Some way of cleaning up old branch proposals/branches
255
def iter_my_proposals(self, status='open'):
256
"""Iterate over the proposals created by the currently logged in user.
258
:param status: Only yield proposals with this status
259
(one of: 'open', 'closed', 'merged', 'all')
260
:return: Iterator over MergeProposal objects
261
:raise HosterLoginRequired: Action requires a hoster login, but none is
264
raise NotImplementedError(self.iter_my_proposals)
267
def iter_instances(cls):
268
"""Iterate instances.
270
:return: Hoster instances
272
raise NotImplementedError(cls.iter_instances)
275
def get_hoster(branch, possible_hosters=None):
276
"""Find the hoster for a branch."""
278
for hoster in possible_hosters:
279
if hoster.hosts(branch):
281
for name, hoster_cls in hosters.items():
283
hoster = hoster_cls.probe_from_branch(branch)
284
except UnsupportedHoster:
287
if possible_hosters is not None:
288
possible_hosters.append(hoster)
290
raise UnsupportedHoster(branch)
293
hosters = registry.Registry()
294
hosters.register_lazy(
295
"launchpad", "breezy.plugins.propose.launchpad",
297
hosters.register_lazy(
298
"github", "breezy.plugins.propose.github",
300
hosters.register_lazy(
301
"gitlab", "breezy.plugins.propose.gitlabs",