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
28
class NoSuchProject(errors.BzrError):
30
_fmt = "Project does not exist: %(project)s."
32
def __init__(self, project):
33
errors.BzrError.__init__(self)
34
self.project = project
37
class MergeProposalExists(errors.BzrError):
39
_fmt = "A merge proposal already exists: %(url)s."
41
def __init__(self, url):
42
errors.BzrError.__init__(self)
46
class UnsupportedHoster(errors.BzrError):
48
_fmt = "No supported hoster for %(branch)s."
50
def __init__(self, branch):
51
errors.BzrError.__init__(self)
55
class ProposeMergeHooks(hooks.Hooks):
56
"""Hooks for proposing a merge on Launchpad."""
59
hooks.Hooks.__init__(self, __name__, "Proposer.hooks")
62
"Return the prerequisite branch for proposing as merge.", (3, 0))
64
'merge_proposal_body',
65
"Return an initial body for the merge proposal message.", (3, 0))
68
class LabelsUnsupported(errors.BzrError):
69
"""Labels not supported by this hoster."""
71
_fmt = "Labels are not supported by %(hoster)r."
73
def __init__(self, hoster):
74
errors.BzrError.__init__(self)
78
class PrerequisiteBranchUnsupported(errors.BzrError):
79
"""Prerequisite branch not supported by this hoster."""
81
def __init__(self, hoster):
82
errors.BzrError.__init__(self)
86
class MergeProposal(object):
89
:ivar url: URL for the merge proposal
92
def __init__(self, url=None):
95
def get_description(self):
96
"""Get the description of the merge proposal."""
97
raise NotImplementedError(self.get_description)
99
def set_description(self, description):
100
"""Set the description of the merge proposal."""
101
raise NotImplementedError(self.set_description)
103
def get_source_branch_url(self):
104
"""Return the source branch."""
105
raise NotImplementedError(self.get_source_branch_url)
107
def get_target_branch_url(self):
108
"""Return the target branch."""
109
raise NotImplementedError(self.get_target_branch_url)
112
"""Close the merge proposal (without merging it)."""
113
raise NotImplementedError(self.close)
116
"""Check whether this merge proposal has been merged."""
117
raise NotImplementedError(self.is_merged)
120
class MergeProposalBuilder(object):
121
"""Merge proposal creator.
123
:param source_branch: Branch to propose for merging
124
:param target_branch: Target branch
127
hooks = ProposeMergeHooks()
129
def __init__(self, source_branch, target_branch):
130
self.source_branch = source_branch
131
self.target_branch = target_branch
133
def get_initial_body(self):
134
"""Get a body for the proposal for the user to modify.
136
:return: a str or None.
138
raise NotImplementedError(self.get_initial_body)
140
def get_infotext(self):
141
"""Determine the initial comment for the merge proposal.
143
raise NotImplementedError(self.get_infotext)
145
def create_proposal(self, description, reviewers=None, labels=None,
146
prerequisite_branch=None):
147
"""Create a proposal to merge a branch for merging.
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
155
raise NotImplementedError(self.create_proposal)
158
class Hoster(object):
159
"""A hosting site manager.
162
# Does this hoster support arbitrary labels being attached to merge
164
supports_merge_proposal_labels = None
166
# The base_url that would be visible to users. I.e. https://github.com/
167
# rather than https://api.github.com/
170
def publish_derived(self, new_branch, base_branch, name, project=None,
171
owner=None, revision_id=None, overwrite=False,
173
"""Publish a branch to the site, derived from base_branch.
175
:param base_branch: branch to derive the new branch from
176
:param new_branch: branch to publish
177
:return: resulting branch, public URL
179
raise NotImplementedError(self.publish)
181
def get_derived_branch(self, base_branch, name, project=None, owner=None):
182
"""Get a derived branch ('a fork').
184
raise NotImplementedError(self.get_derived_branch)
186
def get_push_url(self, branch):
187
"""Get the push URL for a branch."""
188
raise NotImplementedError(self.get_push_url)
190
def get_proposer(self, source_branch, target_branch):
191
"""Get a merge proposal creator.
193
:note: source_branch does not have to be hosted by the hoster.
195
:param source_branch: Source branch
196
:param target_branch: Target branch
197
:return: A MergeProposalBuilder object
199
raise NotImplementedError(self.get_proposer)
201
def iter_proposals(self, source_branch, target_branch, status='open'):
202
"""Get a merge proposal for a specified branch tuple.
204
:param source_branch: Source branch
205
:param target_branch: Target branch
206
:param status: Status of proposals to iterate over
207
:return: Iterate over MergeProposal object
209
raise NotImplementedError(self.iter_proposals)
211
def hosts(self, branch):
212
"""Return true if this hoster hosts given branch."""
213
raise NotImplementedError(self.hosts)
216
def probe(cls, branch):
217
"""Create a Hoster object if this hoster knows about a branch."""
218
raise NotImplementedError(cls.probe)
220
# TODO(jelmer): Some way of cleaning up old branch proposals/branches
222
def iter_my_proposals(self, status='open'):
223
"""Iterate over the proposals created by the currently logged in user.
225
:param status: Only yield proposals with this status
226
(one of: 'open', 'closed', 'merged', 'all')
227
:return: Iterator over MergeProposal objects
229
raise NotImplementedError(self.iter_my_proposals)
232
def iter_instances(cls):
233
"""Iterate instances.
235
:return: Hoster instances
237
raise NotImplementedError(cls.iter_instances)
240
def get_hoster(branch, possible_hosters=None):
241
"""Find the hoster for a branch."""
243
for hoster in possible_hosters:
244
if hoster.hosts(branch):
246
for name, hoster_cls in hosters.items():
248
hoster = hoster_cls.probe(branch)
249
except UnsupportedHoster:
252
if possible_hosters is not None:
253
possible_hosters.append(hoster)
255
raise UnsupportedHoster(branch)
258
hosters = registry.Registry()
259
hosters.register_lazy(
260
"launchpad", "breezy.plugins.propose.launchpad",
262
hosters.register_lazy(
263
"github", "breezy.plugins.propose.github",
265
hosters.register_lazy(
266
"gitlab", "breezy.plugins.propose.gitlabs",