1# This file is part of Buildbot.  Buildbot is free software: you can
2# redistribute it and/or modify it under the terms of the GNU General Public
3# License as published by the Free Software Foundation, version 2.
4#
5# This program is distributed in the hope that it will be useful, but WITHOUT
6# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
7# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
8# details.
9#
10# You should have received a copy of the GNU General Public License along with
11# this program; if not, write to the Free Software Foundation, Inc., 51
12# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
13#
14# Copyright Buildbot Team Members
15
16
17from twisted.internet import defer
18
19from buildbot.data import base
20from buildbot.data import patches
21from buildbot.data import types
22
23
24def _db2data(ss):
25    data = {
26        'ssid': ss['ssid'],
27        'branch': ss['branch'],
28        'revision': ss['revision'],
29        'project': ss['project'],
30        'repository': ss['repository'],
31        'codebase': ss['codebase'],
32        'created_at': ss['created_at'],
33        'patch': None,
34    }
35    if ss['patch_body']:
36        data['patch'] = {
37            'patchid': ss['patchid'],
38            'level': ss['patch_level'],
39            'subdir': ss['patch_subdir'],
40            'author': ss['patch_author'],
41            'comment': ss['patch_comment'],
42            'body': ss['patch_body'],
43        }
44    return data
45
46
47class SourceStampEndpoint(base.Endpoint):
48
49    isCollection = False
50    pathPatterns = """
51        /sourcestamps/n:ssid
52    """
53
54    @defer.inlineCallbacks
55    def get(self, resultSpec, kwargs):
56        ssdict = yield self.master.db.sourcestamps.getSourceStamp(
57            kwargs['ssid'])
58        return _db2data(ssdict) if ssdict else None
59
60
61class SourceStampsEndpoint(base.Endpoint):
62
63    isCollection = True
64    pathPatterns = """
65        /sourcestamps
66    """
67    rootLinkName = 'sourcestamps'
68
69    @defer.inlineCallbacks
70    def get(self, resultSpec, kwargs):
71        return [_db2data(ssdict) for ssdict in
72            (yield self.master.db.sourcestamps.getSourceStamps())]
73
74
75class SourceStamp(base.ResourceType):
76
77    name = "sourcestamp"
78    plural = "sourcestamps"
79    endpoints = [SourceStampEndpoint, SourceStampsEndpoint]
80    keyField = 'ssid'
81    subresources = ["Change"]
82
83    class EntityType(types.Entity):
84        ssid = types.Integer()
85        revision = types.NoneOk(types.String())
86        branch = types.NoneOk(types.String())
87        repository = types.String()
88        project = types.String()
89        codebase = types.String()
90        patch = types.NoneOk(patches.Patch.entityType)
91        created_at = types.DateTime()
92    entityType = EntityType(name, 'Sourcestamp')
93