~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Implementation classes for IBranchMergeQueue, etc."""

__metaclass__ = type
__all__ = ['BranchMergeQueue']

import simplejson
from storm.locals import (
    Int,
    Reference,
    Store,
    Storm,
    Unicode,
    )
from zope.interface import (
    classProvides,
    implements,
    )

from canonical.database.datetimecol import UtcDateTimeCol
from canonical.launchpad.interfaces.lpstorm import IMasterStore
from lp.code.errors import InvalidMergeQueueConfig
from lp.code.interfaces.branchmergequeue import (
    IBranchMergeQueue,
    IBranchMergeQueueSource,
    )
from lp.code.model.branch import Branch


class BranchMergeQueue(Storm):
    """See `IBranchMergeQueue`."""

    __storm_table__ = 'BranchMergeQueue'
    implements(IBranchMergeQueue)
    classProvides(IBranchMergeQueueSource)

    id = Int(primary=True)

    registrant_id = Int(name='registrant', allow_none=True)
    registrant = Reference(registrant_id, 'Person.id')

    owner_id = Int(name='owner', allow_none=True)
    owner = Reference(owner_id, 'Person.id')

    name = Unicode(allow_none=False)
    description = Unicode(allow_none=False)
    configuration = Unicode(allow_none=False)

    date_created = UtcDateTimeCol(notNull=True)

    @property
    def branches(self):
        """See `IBranchMergeQueue`."""
        return Store.of(self).find(
            Branch,
            Branch.merge_queue_id == self.id)

    def setMergeQueueConfig(self, config):
        """See `IBranchMergeQueue`."""
        try:
            simplejson.loads(config)
            self.configuration = config
        except ValueError: # The config string is not valid JSON
            raise InvalidMergeQueueConfig

    @classmethod
    def new(cls, name, owner, registrant, description=None,
            configuration=None, branches=None):
        """See `IBranchMergeQueueSource`."""
        store = IMasterStore(BranchMergeQueue)

        if configuration is None:
            configuration = unicode(simplejson.dumps({}))

        queue = cls()
        queue.name = name
        queue.owner = owner
        queue.registrant = registrant
        queue.description = description
        queue.configuration = configuration
        if branches is not None:
            for branch in branches:
                branch.addToQueue(queue)

        store.add(queue)
        return queue