1
# Copyright (C) 2018-2019 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 ReopenFailed(errors.BzrError):
58
_fmt = "Reopening the merge proposal failed: %(error)s."
61
class ProposeMergeHooks(hooks.Hooks):
62
"""Hooks for proposing a merge on Launchpad."""
65
hooks.Hooks.__init__(self, __name__, "Proposer.hooks")
68
"Return the prerequisite branch for proposing as merge.", (3, 0))
70
'merge_proposal_body',
71
"Return an initial body for the merge proposal message.", (3, 0))
74
class LabelsUnsupported(errors.BzrError):
75
"""Labels not supported by this hoster."""
77
_fmt = "Labels are not supported by %(hoster)r."
79
def __init__(self, hoster):
80
errors.BzrError.__init__(self)
84
class PrerequisiteBranchUnsupported(errors.BzrError):
85
"""Prerequisite branch not supported by this hoster."""
87
def __init__(self, hoster):
88
errors.BzrError.__init__(self)
92
class HosterLoginRequired(errors.BzrError):
93
"""Action requires hoster login credentials."""
95
_fmt = "Action requires credentials for hosting site %(hoster)r."""
97
def __init__(self, hoster):
98
errors.BzrError.__init__(self)
102
class MergeProposal(object):
105
:ivar url: URL for the merge proposal
108
def __init__(self, url=None):
111
def get_description(self):
112
"""Get the description of the merge proposal."""
113
raise NotImplementedError(self.get_description)
115
def set_description(self, description):
116
"""Set the description of the merge proposal."""
117
raise NotImplementedError(self.set_description)
119
def get_commit_message(self):
120
"""Get the proposed commit message."""
121
raise NotImplementedError(self.get_commit_message)
123
def set_commit_message(self, commit_message):
124
"""Set the propose commit message."""
125
raise NotImplementedError(self.set_commit_message)
127
def get_source_branch_url(self):
128
"""Return the source branch."""
129
raise NotImplementedError(self.get_source_branch_url)
131
def get_target_branch_url(self):
132
"""Return the target branch."""
133
raise NotImplementedError(self.get_target_branch_url)
135
def get_source_project(self):
136
raise NotImplementedError(self.get_source_project)
138
def get_target_project(self):
139
raise NotImplementedError(self.get_target_project)
142
"""Close the merge proposal (without merging it)."""
143
raise NotImplementedError(self.close)
146
"""Check whether this merge proposal has been merged."""
147
raise NotImplementedError(self.is_merged)
150
"""Check whether this merge proposal is closed
152
This can either mean that it is merged or rejected.
154
raise NotImplementedError(self.is_closed)
156
def merge(self, commit_message=None):
157
"""Merge this merge proposal."""
158
raise NotImplementedError(self.merge)
160
def can_be_merged(self):
161
"""Can this merge proposal be merged?
163
The answer to this can be no if e.g. it has conflics.
165
raise NotImplementedError(self.can_be_merged)
167
def get_merged_by(self):
168
"""If this proposal was merged, who merged it.
170
raise NotImplementedError(self.get_merged_by)
172
def get_merged_at(self):
173
"""If this proposal was merged, when it was merged.
175
raise NotImplementedError(self.get_merged_at)
178
class MergeProposalBuilder(object):
179
"""Merge proposal creator.
181
:param source_branch: Branch to propose for merging
182
:param target_branch: Target branch
185
hooks = ProposeMergeHooks()
187
def __init__(self, source_branch, target_branch):
188
self.source_branch = source_branch
189
self.target_branch = target_branch
191
def get_initial_body(self):
192
"""Get a body for the proposal for the user to modify.
194
:return: a str or None.
196
raise NotImplementedError(self.get_initial_body)
198
def get_infotext(self):
199
"""Determine the initial comment for the merge proposal.
201
raise NotImplementedError(self.get_infotext)
203
def create_proposal(self, description, reviewers=None, labels=None,
204
prerequisite_branch=None, commit_message=None):
205
"""Create a proposal to merge a branch for merging.
207
:param description: Description for the merge proposal
208
:param reviewers: Optional list of people to ask reviews from
209
:param labels: Labels to attach to the proposal
210
:param prerequisite_branch: Optional prerequisite branch
211
:param commit_message: Optional commit message
212
:return: A `MergeProposal` object
214
raise NotImplementedError(self.create_proposal)
217
class Hoster(object):
218
"""A hosting site manager.
221
# Does this hoster support arbitrary labels being attached to merge
223
supports_merge_proposal_labels = None
225
# Does this hoster support suggesting a commit message in the
227
supports_merge_proposal_commit_message = None
229
# The base_url that would be visible to users. I.e. https://github.com/
230
# rather than https://api.github.com/
233
# The syntax to use for formatting merge proposal descriptions.
234
# Common values: 'plain', 'markdown'
235
merge_proposal_description_format = None
237
def publish_derived(self, new_branch, base_branch, name, project=None,
238
owner=None, revision_id=None, overwrite=False,
240
"""Publish a branch to the site, derived from base_branch.
242
:param base_branch: branch to derive the new branch from
243
:param new_branch: branch to publish
244
:return: resulting branch, public URL
245
:raise HosterLoginRequired: Action requires a hoster login, but none is
248
raise NotImplementedError(self.publish)
250
def get_derived_branch(self, base_branch, name, project=None, owner=None):
251
"""Get a derived branch ('a fork').
253
raise NotImplementedError(self.get_derived_branch)
255
def get_push_url(self, branch):
256
"""Get the push URL for a branch."""
257
raise NotImplementedError(self.get_push_url)
259
def get_proposer(self, source_branch, target_branch):
260
"""Get a merge proposal creator.
262
:note: source_branch does not have to be hosted by the hoster.
264
:param source_branch: Source branch
265
:param target_branch: Target branch
266
:return: A MergeProposalBuilder object
268
raise NotImplementedError(self.get_proposer)
270
def iter_proposals(self, source_branch, target_branch, status='open'):
271
"""Get the merge proposals for a specified branch tuple.
273
:param source_branch: Source branch
274
:param target_branch: Target branch
275
:param status: Status of proposals to iterate over
276
:return: Iterate over MergeProposal object
278
raise NotImplementedError(self.iter_proposals)
280
def get_proposal_by_url(self, url):
281
"""Retrieve a branch proposal by URL.
283
:param url: Merge proposal URL.
284
:return: MergeProposal object
285
:raise UnsupportedHoster: Hoster does not support this URL
287
raise NotImplementedError(self.get_proposal_by_url)
289
def hosts(self, branch):
290
"""Return true if this hoster hosts given branch."""
291
raise NotImplementedError(self.hosts)
294
def probe_from_branch(cls, branch):
295
"""Create a Hoster object if this hoster knows about a branch."""
296
url = urlutils.strip_segment_parameters(branch.user_url)
297
return cls.probe_from_url(
298
url, possible_transports=[branch.control_transport])
301
def probe_from_url(cls, url, possible_hosters=None):
302
"""Create a Hoster object if this hoster knows about a URL."""
303
raise NotImplementedError(cls.probe_from_url)
305
def iter_my_proposals(self, status='open'):
306
"""Iterate over the proposals created by the currently logged in user.
308
:param status: Only yield proposals with this status
309
(one of: 'open', 'closed', 'merged', 'all')
310
:return: Iterator over MergeProposal objects
311
:raise HosterLoginRequired: Action requires a hoster login, but none is
314
raise NotImplementedError(self.iter_my_proposals)
316
def iter_my_forks(self):
317
"""Iterate over the currently logged in users' forks.
319
:return: Iterator over project_name
321
raise NotImplementedError(self.iter_my_forks)
323
def delete_project(self, name):
326
raise NotImplementedError(self.delete_project)
329
def iter_instances(cls):
330
"""Iterate instances.
332
:return: Hoster instances
334
raise NotImplementedError(cls.iter_instances)
337
def determine_title(description):
338
"""Determine the title for a merge proposal based on full description."""
339
return description.splitlines()[0].split('.')[0]
342
def get_hoster(branch, possible_hosters=None):
343
"""Find the hoster for a branch.
345
:param branch: Branch to find hoster for
346
:param possible_hosters: Optional list of hosters to reuse
347
:raise UnsupportedHoster: if there is no hoster that supports `branch`
348
:return: A `Hoster` object
351
for hoster in possible_hosters:
352
if hoster.hosts(branch):
354
for name, hoster_cls in hosters.items():
356
hoster = hoster_cls.probe_from_branch(branch)
357
except UnsupportedHoster:
360
if possible_hosters is not None:
361
possible_hosters.append(hoster)
363
raise UnsupportedHoster(branch)
366
def get_proposal_by_url(url):
367
"""Get the proposal object associated with a URL.
369
:param url: URL of the proposal
370
:raise UnsupportedHoster: if there is no hoster that supports the URL
371
:return: A `MergeProposal` object
373
for name, hoster_cls in hosters.items():
374
for instance in hoster_cls.iter_instances():
376
return instance.get_proposal_by_url(url)
377
except UnsupportedHoster:
379
raise UnsupportedHoster(url)
382
hosters = registry.Registry()