~dpb/tarmac/backout-409-allow-all-branches

« back to all changes in this revision

Viewing changes to tarmac/bin/commands.py

  • Committer: Tarmac Traffic Controller
  • Author(s): David Britton
  • Date: 2014-02-14 18:43:10 UTC
  • mfrom: (424.2.4 better-prereq-branches)
  • Revision ID: tarmac_traffic_controller-20140214184310-lqd318via4qi7op8
Better prerequisite handling.

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from tarmac.utility import get_review_url
26
26
 
27
27
 
 
28
def _compare_proposals(a, b):
 
29
    """Helper to sort proposals based on a prerequisite branch"""
 
30
    if a.prerequisite_branch is not None:
 
31
        if a.prerequisite_branch.unique_name == b.source_branch.unique_name:
 
32
            return 1
 
33
    if b.prerequisite_branch is not None:
 
34
        if b.prerequisite_branch.unique_name == a.source_branch.unique_name:
 
35
            return -1
 
36
    return 0
 
37
 
 
38
 
28
39
class TarmacCommand(Command):
29
40
    '''A command class.'''
30
41
 
210
221
                try:
211
222
                    prerequisite = proposal.prerequisite_branch
212
223
                    if prerequisite:
213
 
                        merges = [x for x in  prerequisite.landing_targets
214
 
                                  if x.target_branch == target.lp_branch and
215
 
                                  x.queue_status != u'Superseded']
 
224
                        merges = self._get_prerequisite_proposals(proposal)
216
225
                        if len(merges) == 0:
217
226
                            raise TarmacMergeError(
218
227
                                u'No proposals of prerequisite branch.',
227
236
                                u'of %s into %s, which is not Superseded.' % (
228
237
                                    prerequisite.web_link,
229
238
                                    target.lp_branch.web_link))
230
 
                        elif len(merges) == 1:
231
 
                            if merges[0].queue_status != u'Merged':
232
 
                                raise TarmacMergeError(
233
 
                                    u'Prerequisite not yet merged.',
234
 
                                    u'The prerequisite %s has not yet been '
235
 
                                    u'merged into %s.' % (
236
 
                                        prerequisite.web_link,
237
 
                                        target.lp_branch.web_link))
238
239
 
239
240
                    if not proposal.reviewed_revid:
240
241
                        raise TarmacMergeError(
325
326
            target.cleanup()
326
327
 
327
328
    def _get_mergable_proposals_for_branch(self, lp_branch):
328
 
        """Return a list of the mergable proposals for the given branch."""
 
329
        """
 
330
        Return a list of the mergable proposals for the given branch.  The
 
331
        list returned will be in the order that they should be processed.
 
332
        """
329
333
        proposals = []
330
 
        for entry in lp_branch.landing_candidates:
 
334
        sorted_proposals = sorted(
 
335
            lp_branch.landing_candidates, cmp=_compare_proposals)
 
336
        for entry in sorted_proposals:
331
337
            self.logger.debug("Considering merge proposal: {0}".format(entry.web_link))
 
338
            prereqs = self._get_prerequisite_proposals(entry)
332
339
 
333
340
            if entry.queue_status != u'Approved':
334
341
                self.logger.debug(
342
349
                    "  Skipping proposal: proposal has no commit message")
343
350
                continue
344
351
 
 
352
            if len(prereqs) == 1 and prereqs[0].queue_status != u'Merged':
 
353
                # N.B.: The case of a MP with more than one prereq MP open
 
354
                #       will be caught as a merge error.
 
355
                self.logger.debug(
 
356
                    "  Skipping proposal: prerequisite not yet merged")
 
357
                continue
 
358
 
345
359
            proposals.append(entry)
346
360
        return proposals
347
361
 
 
362
    def _get_prerequisite_proposals(self, proposal):
 
363
        """
 
364
        Given a proposal, return all prerequisite
 
365
        proposals that are not in the superseded state.  There ideally
 
366
        should be one and only one here (or zero), but sometimes there
 
367
        are not, depending on developer habits
 
368
        """
 
369
        prerequisite = proposal.prerequisite_branch
 
370
        target_branch = proposal.target_branch
 
371
        if not prerequisite or not prerequisite.landing_targets:
 
372
            return []
 
373
        return [x for x in prerequisite.landing_targets
 
374
            if x.target_branch.unique_name == target_branch.unique_name and
 
375
            x.queue_status != u'Superseded']
 
376
 
348
377
    def _get_reviews(self, proposal):
349
378
        """Get the set of reviews from the proposal."""
350
379
        reviews = []
405
434
                        'An error occurred trying to merge %s: %s',
406
435
                        branch, error)
407
436
                    raise
 
437