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 SourceNotDerivedFromTarget(errors.BzrError):
103
"""Source branch is not derived from target branch."""
105
_fmt = ("Source %(source_branch)r not derived from "
106
"target %(target_branch)r.")
108
def __init__(self, source_branch, target_branch):
109
errors.BzrError.__init__(
110
self, source_branch=source_branch,
111
target_branch=target_branch)
114
class MergeProposal(object):
117
:ivar url: URL for the merge proposal
120
def __init__(self, url=None):
123
def get_description(self):
124
"""Get the description of the merge proposal."""
125
raise NotImplementedError(self.get_description)
127
def set_description(self, description):
128
"""Set the description of the merge proposal."""
129
raise NotImplementedError(self.set_description)
131
def get_commit_message(self):
132
"""Get the proposed commit message."""
133
raise NotImplementedError(self.get_commit_message)
135
def set_commit_message(self, commit_message):
136
"""Set the propose commit message."""
137
raise NotImplementedError(self.set_commit_message)
139
def get_source_branch_url(self):
140
"""Return the source branch."""
141
raise NotImplementedError(self.get_source_branch_url)
143
def get_source_revision(self):
144
"""Return the latest revision for the source branch."""
145
raise NotImplementedError(self.get_source_revision)
147
def get_target_branch_url(self):
148
"""Return the target branch."""
149
raise NotImplementedError(self.get_target_branch_url)
151
def get_source_project(self):
152
raise NotImplementedError(self.get_source_project)
154
def get_target_project(self):
155
raise NotImplementedError(self.get_target_project)
158
"""Close the merge proposal (without merging it)."""
159
raise NotImplementedError(self.close)
162
"""Check whether this merge proposal has been merged."""
163
raise NotImplementedError(self.is_merged)
166
"""Check whether this merge proposal is closed
168
This can either mean that it is merged or rejected.
170
raise NotImplementedError(self.is_closed)
172
def merge(self, commit_message=None):
173
"""Merge this merge proposal."""
174
raise NotImplementedError(self.merge)
176
def can_be_merged(self):
177
"""Can this merge proposal be merged?
179
The answer to this can be no if e.g. it has conflics.
181
raise NotImplementedError(self.can_be_merged)
183
def get_merged_by(self):
184
"""If this proposal was merged, who merged it.
186
raise NotImplementedError(self.get_merged_by)
188
def get_merged_at(self):
189
"""If this proposal was merged, when it was merged.
191
raise NotImplementedError(self.get_merged_at)
193
def post_comment(self, body):
194
"""Post a comment on the merge proposal.
197
body: Body of the comment
199
raise NotImplementedError(self.post_comment)
202
class MergeProposalBuilder(object):
203
"""Merge proposal creator.
205
:param source_branch: Branch to propose for merging
206
:param target_branch: Target branch
209
hooks = ProposeMergeHooks()
211
def __init__(self, source_branch, target_branch):
212
self.source_branch = source_branch
213
self.target_branch = target_branch
215
def get_initial_body(self):
216
"""Get a body for the proposal for the user to modify.
218
:return: a str or None.
220
raise NotImplementedError(self.get_initial_body)
222
def get_infotext(self):
223
"""Determine the initial comment for the merge proposal.
225
raise NotImplementedError(self.get_infotext)
227
def create_proposal(self, description, reviewers=None, labels=None,
228
prerequisite_branch=None, commit_message=None,
229
work_in_progress=False, allow_collaboration=False):
230
"""Create a proposal to merge a branch for merging.
232
:param description: Description for the merge proposal
233
:param reviewers: Optional list of people to ask reviews from
234
:param labels: Labels to attach to the proposal
235
:param prerequisite_branch: Optional prerequisite branch
236
:param commit_message: Optional commit message
237
:param work_in_progress:
238
Whether this merge proposal is still a work-in-progress
239
:param allow_collaboration:
240
Whether to allow changes to the branch from the target branch
242
:return: A `MergeProposal` object
244
raise NotImplementedError(self.create_proposal)
247
class Hoster(object):
248
"""A hosting site manager.
251
# Does this hoster support arbitrary labels being attached to merge
253
supports_merge_proposal_labels = None
257
"""Name of this instance."""
258
return "%s at %s" % (type(self).__name__, self.base_url)
260
# Does this hoster support suggesting a commit message in the
262
supports_merge_proposal_commit_message = None
264
# The base_url that would be visible to users. I.e. https://github.com/
265
# rather than https://api.github.com/
268
# The syntax to use for formatting merge proposal descriptions.
269
# Common values: 'plain', 'markdown'
270
merge_proposal_description_format = None
272
# Does this hoster support the allow_collaboration flag?
273
supports_allow_collaboration = False
275
def publish_derived(self, new_branch, base_branch, name, project=None,
276
owner=None, revision_id=None, overwrite=False,
277
allow_lossy=True, tag_selector=None):
278
"""Publish a branch to the site, derived from base_branch.
280
:param base_branch: branch to derive the new branch from
281
:param new_branch: branch to publish
282
:return: resulting branch, public URL
283
:raise HosterLoginRequired: Action requires a hoster login, but none is
286
raise NotImplementedError(self.publish_derived)
288
def get_derived_branch(self, base_branch, name, project=None, owner=None):
289
"""Get a derived branch ('a fork').
291
raise NotImplementedError(self.get_derived_branch)
293
def get_push_url(self, branch):
294
"""Get the push URL for a branch."""
295
raise NotImplementedError(self.get_push_url)
297
def get_proposer(self, source_branch, target_branch):
298
"""Get a merge proposal creator.
300
:note: source_branch does not have to be hosted by the hoster.
302
:param source_branch: Source branch
303
:param target_branch: Target branch
304
:return: A MergeProposalBuilder object
306
raise NotImplementedError(self.get_proposer)
308
def iter_proposals(self, source_branch, target_branch, status='open'):
309
"""Get the merge proposals for a specified branch tuple.
311
:param source_branch: Source branch
312
:param target_branch: Target branch
313
:param status: Status of proposals to iterate over
314
:return: Iterate over MergeProposal object
316
raise NotImplementedError(self.iter_proposals)
318
def get_proposal_by_url(self, url):
319
"""Retrieve a branch proposal by URL.
321
:param url: Merge proposal URL.
322
:return: MergeProposal object
323
:raise UnsupportedHoster: Hoster does not support this URL
325
raise NotImplementedError(self.get_proposal_by_url)
327
def hosts(self, branch):
328
"""Return true if this hoster hosts given branch."""
329
raise NotImplementedError(self.hosts)
332
def probe_from_branch(cls, branch):
333
"""Create a Hoster object if this hoster knows about a branch."""
334
url = urlutils.strip_segment_parameters(branch.user_url)
335
return cls.probe_from_url(
336
url, possible_transports=[branch.control_transport])
339
def probe_from_url(cls, url, possible_hosters=None):
340
"""Create a Hoster object if this hoster knows about a URL."""
341
raise NotImplementedError(cls.probe_from_url)
343
def iter_my_proposals(self, status='open'):
344
"""Iterate over the proposals created by the currently logged in user.
346
:param status: Only yield proposals with this status
347
(one of: 'open', 'closed', 'merged', 'all')
348
:return: Iterator over MergeProposal objects
349
:raise HosterLoginRequired: Action requires a hoster login, but none is
352
raise NotImplementedError(self.iter_my_proposals)
354
def iter_my_forks(self):
355
"""Iterate over the currently logged in users' forks.
357
:return: Iterator over project_name
359
raise NotImplementedError(self.iter_my_forks)
361
def delete_project(self, name):
364
raise NotImplementedError(self.delete_project)
367
def iter_instances(cls):
368
"""Iterate instances.
370
:return: Hoster instances
372
raise NotImplementedError(cls.iter_instances)
374
def get_current_user(self):
375
"""Retrieve the name of the currently logged in user.
377
:return: Username or None if not logged in
379
raise NotImplementedError(self.get_current_user)
381
def get_user_url(self, user):
382
"""Rerieve the web URL for a user."""
383
raise NotImplementedError(self.get_user_url)
386
def determine_title(description):
387
"""Determine the title for a merge proposal based on full description."""
388
return description.splitlines()[0].split('.')[0]
391
def get_hoster(branch, possible_hosters=None):
392
"""Find the hoster for a branch.
394
:param branch: Branch to find hoster for
395
:param possible_hosters: Optional list of hosters to reuse
396
:raise UnsupportedHoster: if there is no hoster that supports `branch`
397
:return: A `Hoster` object
400
for hoster in possible_hosters:
401
if hoster.hosts(branch):
403
for name, hoster_cls in hosters.items():
405
hoster = hoster_cls.probe_from_branch(branch)
406
except UnsupportedHoster:
409
if possible_hosters is not None:
410
possible_hosters.append(hoster)
412
raise UnsupportedHoster(branch)
415
def iter_hoster_instances(hoster=None):
416
"""Iterate over all known hoster instances.
418
:return: Iterator over Hoster instances
421
hoster_clses = [hoster_cls for name, hoster_cls in hosters.items()]
423
hoster_clses = [hoster]
424
for hoster_cls in hoster_clses:
425
for instance in hoster_cls.iter_instances():
429
def get_proposal_by_url(url):
430
"""Get the proposal object associated with a URL.
432
:param url: URL of the proposal
433
:raise UnsupportedHoster: if there is no hoster that supports the URL
434
:return: A `MergeProposal` object
436
for instance in iter_hoster_instances():
438
return instance.get_proposal_by_url(url)
439
except UnsupportedHoster:
441
raise UnsupportedHoster(url)
444
hosters = registry.Registry()