/
builds.py
164 lines (142 loc) · 5.88 KB
/
builds.py
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# This file is part of Buildbot. Buildbot is free software: you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright Buildbot Team Members
from buildbot.data import base
from buildbot.data import types
from twisted.internet import defer
class Db2DataMixin(object):
def db2data(self, dbdict):
data = {
'buildid': dbdict['id'],
'number': dbdict['number'],
'builderid': dbdict['builderid'],
'buildrequestid': dbdict['buildrequestid'],
'buildslaveid': dbdict['buildslaveid'],
'masterid': dbdict['masterid'],
'started_at': dbdict['started_at'],
'complete_at': dbdict['complete_at'],
'complete': dbdict['complete_at'] is not None,
'state_string': dbdict['state_string'],
'results': dbdict['results'],
}
return defer.succeed(data)
class BuildEndpoint(Db2DataMixin, base.Endpoint):
isCollection = False
pathPatterns = """
/builds/n:buildid
/builders/n:builderid/builds/n:number
"""
@defer.inlineCallbacks
def get(self, resultSpec, kwargs):
if 'buildid' in kwargs:
dbdict = yield self.master.db.builds.getBuild(kwargs['buildid'])
else:
bldr = kwargs['builderid']
num = kwargs['number']
dbdict = yield self.master.db.builds.getBuildByNumber(bldr, num)
defer.returnValue((yield self.db2data(dbdict))
if dbdict else None)
def startConsuming(self, callback, options, kwargs):
builderid = kwargs.get('builderid')
number = kwargs.get('number')
buildid = kwargs.get('buildid')
if builderid is not None:
return self.master.mq.startConsuming(callback,
('builders', str(builderid), 'builds', str(number), None))
else:
return self.master.mq.startConsuming(callback,
('builds', str(buildid), None))
class BuildsEndpoint(Db2DataMixin, base.Endpoint):
isCollection = True
pathPatterns = """
/builds
/builders/n:builderid/builds
/buildrequests/n:buildrequestid/builds
"""
rootLinkName = 'builds'
@defer.inlineCallbacks
def get(self, resultSpec, kwargs):
builds = yield self.master.db.builds.getBuilds(
builderid=kwargs.get('builderid'),
buildrequestid=kwargs.get('buildrequestid'))
defer.returnValue(
[(yield self.db2data(dbdict)) for dbdict in builds])
def startConsuming(self, callback, options, kwargs):
builderid = kwargs.get('builderid')
buildrequestid = kwargs.get('buildrequestid')
if builderid is not None:
return self.master.mq.startConsuming(
callback,
('builders', str(builderid), 'builds', None, None))
elif buildrequestid is not None:
# XXX these messages are never produced
return self.master.mq.startConsuming(callback,
('buildrequests', str(buildrequestid), 'builds', None))
else:
return self.master.mq.startConsuming(callback,
('builds', None, None, None))
class Build(base.ResourceType):
name = "build"
plural = "builds"
endpoints = [BuildEndpoint, BuildsEndpoint]
keyFields = ['builderid', 'buildid']
eventPathPatterns = """
/builders/:builderid/builds/:number
/builds/:buildid
"""
class EntityType(types.Entity):
buildid = types.Integer()
number = types.Integer()
builderid = types.Integer()
buildrequestid = types.Integer()
buildslaveid = types.Integer()
masterid = types.Integer()
started_at = types.DateTime()
complete = types.Boolean()
complete_at = types.NoneOk(types.DateTime())
results = types.NoneOk(types.Integer())
state_string = types.String()
entityType = EntityType(name)
@defer.inlineCallbacks
def generateEvent(self, _id, event):
# get the build and munge the result for the notification
build = yield self.master.data.get(('builds', str(_id)))
self.produceEvent(build, event)
@base.updateMethod
@defer.inlineCallbacks
def newBuild(self, builderid, buildrequestid, buildslaveid):
res = yield self.master.db.builds.addBuild(
builderid=builderid,
buildrequestid=buildrequestid,
buildslaveid=buildslaveid,
masterid=self.master.masterid,
state_string=u'created')
if res is not None:
_id, number = res
yield self.generateEvent(_id, "new")
defer.returnValue(res)
@base.updateMethod
@defer.inlineCallbacks
def setBuildStateString(self, buildid, state_string):
res = yield self.master.db.builds.setBuildStateString(
buildid=buildid, state_string=state_string)
yield self.generateEvent(buildid, "update")
defer.returnValue(res)
@base.updateMethod
@defer.inlineCallbacks
def finishBuild(self, buildid, results):
res = yield self.master.db.builds.finishBuild(
buildid=buildid, results=results)
yield self.generateEvent(buildid, "finished")
defer.returnValue(res)