/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-03-05 07:32:38 UTC
  • mto: (7290.1.21 work)
  • mto: This revision was merged to the branch mainline in revision 7311.
  • Revision ID: jelmer@jelmer.uk-20190305073238-zlqn981opwnqsmzi
Add appveyor configuration.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2018-2019 Breezy Developers
 
1
# Copyright (C) 2018 Breezy Developers
2
2
#
3
3
# This program is free software; you can redistribute it and/or modify
4
4
# it under the terms of the GNU General Public License as published by
16
16
 
17
17
"""Helper functions for proposing merges."""
18
18
 
19
 
from . import (
 
19
from __future__ import absolute_import
 
20
 
 
21
from ... import (
20
22
    errors,
21
23
    hooks,
22
24
    registry,
23
 
    urlutils,
24
25
    )
25
26
 
26
27
 
51
52
        self.branch = branch
52
53
 
53
54
 
54
 
class ReopenFailed(errors.BzrError):
55
 
 
56
 
    _fmt = "Reopening the merge proposal failed: %(error)s."
57
 
 
58
 
 
59
55
class ProposeMergeHooks(hooks.Hooks):
60
56
    """Hooks for proposing a merge on Launchpad."""
61
57
 
97
93
        self.hoster = hoster
98
94
 
99
95
 
100
 
class SourceNotDerivedFromTarget(errors.BzrError):
101
 
    """Source branch is not derived from target branch."""
102
 
 
103
 
    _fmt = ("Source %(source_branch)r not derived from "
104
 
            "target %(target_branch)r.")
105
 
 
106
 
    def __init__(self, source_branch, target_branch):
107
 
        errors.BzrError.__init__(
108
 
            self, source_branch=source_branch,
109
 
            target_branch=target_branch)
110
 
 
111
 
 
112
96
class MergeProposal(object):
113
97
    """A merge proposal.
114
98
 
126
110
        """Set the description of the merge proposal."""
127
111
        raise NotImplementedError(self.set_description)
128
112
 
129
 
    def get_commit_message(self):
130
 
        """Get the proposed commit message."""
131
 
        raise NotImplementedError(self.get_commit_message)
132
 
 
133
 
    def set_commit_message(self, commit_message):
134
 
        """Set the propose commit message."""
135
 
        raise NotImplementedError(self.set_commit_message)
136
 
 
137
113
    def get_source_branch_url(self):
138
114
        """Return the source branch."""
139
115
        raise NotImplementedError(self.get_source_branch_url)
140
116
 
141
 
    def get_source_revision(self):
142
 
        """Return the latest revision for the source branch."""
143
 
        raise NotImplementedError(self.get_source_revision)
144
 
 
145
117
    def get_target_branch_url(self):
146
118
        """Return the target branch."""
147
119
        raise NotImplementedError(self.get_target_branch_url)
148
120
 
149
 
    def get_source_project(self):
150
 
        raise NotImplementedError(self.get_source_project)
151
 
 
152
 
    def get_target_project(self):
153
 
        raise NotImplementedError(self.get_target_project)
154
 
 
155
121
    def close(self):
156
122
        """Close the merge proposal (without merging it)."""
157
123
        raise NotImplementedError(self.close)
160
126
        """Check whether this merge proposal has been merged."""
161
127
        raise NotImplementedError(self.is_merged)
162
128
 
163
 
    def is_closed(self):
164
 
        """Check whether this merge proposal is closed
165
 
 
166
 
        This can either mean that it is merged or rejected.
167
 
        """
168
 
        raise NotImplementedError(self.is_closed)
169
 
 
170
 
    def merge(self, commit_message=None):
171
 
        """Merge this merge proposal."""
172
 
        raise NotImplementedError(self.merge)
173
 
 
174
 
    def can_be_merged(self):
175
 
        """Can this merge proposal be merged?
176
 
 
177
 
        The answer to this can be no if e.g. it has conflics.
178
 
        """
179
 
        raise NotImplementedError(self.can_be_merged)
180
 
 
181
 
    def get_merged_by(self):
182
 
        """If this proposal was merged, who merged it.
183
 
        """
184
 
        raise NotImplementedError(self.get_merged_by)
185
 
 
186
 
    def get_merged_at(self):
187
 
        """If this proposal was merged, when it was merged.
188
 
        """
189
 
        raise NotImplementedError(self.get_merged_at)
190
 
 
191
 
    def post_comment(self, body):
192
 
        """Post a comment on the merge proposal.
193
 
 
194
 
        Args:
195
 
          body: Body of the comment
196
 
        """
197
 
        raise NotImplementedError(self.post_comment)
198
 
 
199
129
 
200
130
class MergeProposalBuilder(object):
201
131
    """Merge proposal creator.
223
153
        raise NotImplementedError(self.get_infotext)
224
154
 
225
155
    def create_proposal(self, description, reviewers=None, labels=None,
226
 
                        prerequisite_branch=None, commit_message=None,
227
 
                        work_in_progress=False, allow_collaboration=False):
 
156
                        prerequisite_branch=None):
228
157
        """Create a proposal to merge a branch for merging.
229
158
 
230
159
        :param description: Description for the merge proposal
231
160
        :param reviewers: Optional list of people to ask reviews from
232
161
        :param labels: Labels to attach to the proposal
233
162
        :param prerequisite_branch: Optional prerequisite branch
234
 
        :param commit_message: Optional commit message
235
 
        :param work_in_progress:
236
 
            Whether this merge proposal is still a work-in-progress
237
 
        :param allow_collaboration:
238
 
            Whether to allow changes to the branch from the target branch
239
 
            maintainer(s)
240
163
        :return: A `MergeProposal` object
241
164
        """
242
165
        raise NotImplementedError(self.create_proposal)
250
173
    # proposals?
251
174
    supports_merge_proposal_labels = None
252
175
 
253
 
    @property
254
 
    def name(self):
255
 
        """Name of this instance."""
256
 
        return "%s at %s" % (type(self).__name__, self.base_url)
257
 
 
258
 
    # Does this hoster support suggesting a commit message in the
259
 
    # merge proposal?
260
 
    supports_merge_proposal_commit_message = None
261
 
 
262
176
    # The base_url that would be visible to users. I.e. https://github.com/
263
177
    # rather than https://api.github.com/
264
178
    base_url = None
265
179
 
266
 
    # The syntax to use for formatting merge proposal descriptions.
267
 
    # Common values: 'plain', 'markdown'
268
 
    merge_proposal_description_format = None
269
 
 
270
 
    # Does this hoster support the allow_collaboration flag?
271
 
    supports_allow_collaboration = False
272
 
 
273
180
    def publish_derived(self, new_branch, base_branch, name, project=None,
274
181
                        owner=None, revision_id=None, overwrite=False,
275
 
                        allow_lossy=True, tag_selector=None):
 
182
                        allow_lossy=True):
276
183
        """Publish a branch to the site, derived from base_branch.
277
184
 
278
185
        :param base_branch: branch to derive the new branch from
281
188
        :raise HosterLoginRequired: Action requires a hoster login, but none is
282
189
            known.
283
190
        """
284
 
        raise NotImplementedError(self.publish_derived)
 
191
        raise NotImplementedError(self.publish)
285
192
 
286
193
    def get_derived_branch(self, base_branch, name, project=None, owner=None):
287
194
        """Get a derived branch ('a fork').
304
211
        raise NotImplementedError(self.get_proposer)
305
212
 
306
213
    def iter_proposals(self, source_branch, target_branch, status='open'):
307
 
        """Get the merge proposals for a specified branch tuple.
 
214
        """Get a merge proposal for a specified branch tuple.
308
215
 
309
216
        :param source_branch: Source branch
310
217
        :param target_branch: Target branch
313
220
        """
314
221
        raise NotImplementedError(self.iter_proposals)
315
222
 
316
 
    def get_proposal_by_url(self, url):
317
 
        """Retrieve a branch proposal by URL.
318
 
 
319
 
        :param url: Merge proposal URL.
320
 
        :return: MergeProposal object
321
 
        :raise UnsupportedHoster: Hoster does not support this URL
322
 
        """
323
 
        raise NotImplementedError(self.get_proposal_by_url)
324
 
 
325
223
    def hosts(self, branch):
326
224
        """Return true if this hoster hosts given branch."""
327
225
        raise NotImplementedError(self.hosts)
328
226
 
329
227
    @classmethod
330
 
    def probe_from_branch(cls, branch):
 
228
    def probe(cls, branch):
331
229
        """Create a Hoster object if this hoster knows about a branch."""
332
 
        url = urlutils.strip_segment_parameters(branch.user_url)
333
 
        return cls.probe_from_url(
334
 
            url, possible_transports=[branch.control_transport])
 
230
        raise NotImplementedError(cls.probe)
335
231
 
336
 
    @classmethod
337
 
    def probe_from_url(cls, url, possible_hosters=None):
338
 
        """Create a Hoster object if this hoster knows about a URL."""
339
 
        raise NotImplementedError(cls.probe_from_url)
 
232
    # TODO(jelmer): Some way of cleaning up old branch proposals/branches
340
233
 
341
234
    def iter_my_proposals(self, status='open'):
342
235
        """Iterate over the proposals created by the currently logged in user.
349
242
        """
350
243
        raise NotImplementedError(self.iter_my_proposals)
351
244
 
352
 
    def iter_my_forks(self):
353
 
        """Iterate over the currently logged in users' forks.
354
 
 
355
 
        :return: Iterator over project_name
356
 
        """
357
 
        raise NotImplementedError(self.iter_my_forks)
358
 
 
359
 
    def delete_project(self, name):
360
 
        """Delete a project.
361
 
        """
362
 
        raise NotImplementedError(self.delete_project)
363
 
 
364
245
    @classmethod
365
246
    def iter_instances(cls):
366
247
        """Iterate instances.
369
250
        """
370
251
        raise NotImplementedError(cls.iter_instances)
371
252
 
372
 
    def get_current_user(self):
373
 
        """Retrieve the name of the currently logged in user.
374
 
 
375
 
        :return: Username or None if not logged in
376
 
        """
377
 
        raise NotImplementedError(self.get_current_user)
378
 
 
379
 
    def get_user_url(self, user):
380
 
        """Rerieve the web URL for a user."""
381
 
        raise NotImplementedError(self.get_user_url)
382
 
 
383
 
 
384
 
def determine_title(description):
385
 
    """Determine the title for a merge proposal based on full description."""
386
 
    return description.splitlines()[0].split('.')[0]
387
 
 
388
253
 
389
254
def get_hoster(branch, possible_hosters=None):
390
 
    """Find the hoster for a branch.
391
 
 
392
 
    :param branch: Branch to find hoster for
393
 
    :param possible_hosters: Optional list of hosters to reuse
394
 
    :raise UnsupportedHoster: if there is no hoster that supports `branch`
395
 
    :return: A `Hoster` object
396
 
    """
 
255
    """Find the hoster for a branch."""
397
256
    if possible_hosters:
398
257
        for hoster in possible_hosters:
399
258
            if hoster.hosts(branch):
400
259
                return hoster
401
260
    for name, hoster_cls in hosters.items():
402
261
        try:
403
 
            hoster = hoster_cls.probe_from_branch(branch)
 
262
            hoster = hoster_cls.probe(branch)
404
263
        except UnsupportedHoster:
405
264
            pass
406
265
        else:
410
269
    raise UnsupportedHoster(branch)
411
270
 
412
271
 
413
 
def iter_hoster_instances():
414
 
    """Iterate over all known hoster instances.
415
 
 
416
 
    :return: Iterator over Hoster instances
417
 
    """
418
 
    for name, hoster_cls in hosters.items():
419
 
        for instance in hoster_cls.iter_instances():
420
 
            yield instance
421
 
 
422
 
 
423
 
def get_proposal_by_url(url):
424
 
    """Get the proposal object associated with a URL.
425
 
 
426
 
    :param url: URL of the proposal
427
 
    :raise UnsupportedHoster: if there is no hoster that supports the URL
428
 
    :return: A `MergeProposal` object
429
 
    """
430
 
    for instance in iter_hoster_instances():
431
 
        try:
432
 
            return instance.get_proposal_by_url(url)
433
 
        except UnsupportedHoster:
434
 
            pass
435
 
    raise UnsupportedHoster(url)
436
 
 
437
 
 
438
272
hosters = registry.Registry()
 
273
hosters.register_lazy(
 
274
    "launchpad", "breezy.plugins.propose.launchpad",
 
275
    "Launchpad")
 
276
hosters.register_lazy(
 
277
    "github", "breezy.plugins.propose.github",
 
278
    "GitHub")
 
279
hosters.register_lazy(
 
280
    "gitlab", "breezy.plugins.propose.gitlabs",
 
281
    "GitLab")