-
Notifications
You must be signed in to change notification settings - Fork 0
/
prstatus.py
491 lines (395 loc) · 16.7 KB
/
prstatus.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import cgi
import datetime
import logging
from twisted.internet import defer
from twisted.web import resource
from twisted.web import server
from twisted.web.resource import NoResource
from buildbot.util import json
from buildbot.status.web.base import AccessorMixin
from . import context, database, serviceloops
from .constants import BuildStatus
from buildbot.status.web.status_json import RequestArgToBool
from twisted.web.server import Request
from twisted.python import log
from pullrequest.utils import NotFound, Forbidden, NeedUpdate, Conflict, BadRequest, RequestArg
from pullrequest.database import getTimestamp, mainThreadCall, DBMethodCall
from twisted.python.failure import Failure
logger = logging.getLogger(__package__)
class ApiData(AccessorMixin):
def __init__(self, context, request):
self.context = context
self.request = request
@DBMethodCall
def initialize(self, publicOnly=False):
self.authz = self.getAuthz(self.request)
if not publicOnly:
@defer.inlineCallbacks
def fn():
self.showOperations = yield self.authz.actionAllowed('forceBuild', self.request)
self.showPerf = yield self.authz.actionAllowed('prShowPerf', self.request)
self.showRevertOperation = yield self.authz.actionAllowed('prRevertBuild', self.request)
yield mainThreadCall(fn)
else:
self.showOperations = False
self.showPerf = False
self.db = db = self.context.db
assert(isinstance(db, database.Database))
self.active_builders = db.bcc.getActiveBuilders()
self.active_pullrequests = db.prcc.getActivePullRequests()
self.all_bstatuses = db.scc.getAllActiveStatuses()
@DBMethodCall
def getBuildersList(self):
result = {}
for builder in self.active_builders:
if builder.isPerf and not self.showPerf:
continue
b = {}
b.update(dict(name=builder.name, short_name=builder.name, order=builder.order, id=str(builder.bid)))
b['status'] = 'active' # TODO
result[builder.order] = b
return result
def getPr(self, prid):
prs = [pr for pr in self.active_pullrequests if str(pr.prid) == str(prid)]
if len(prs) == 0:
return None
assert len(prs) == 1
return prs[0]
def getBuilder(self, bid):
b = [b for b in self.active_builders if str(b.bid) == str(bid)]
if len(b) == 0:
return None
assert len(b) == 1
return b[0]
@DBMethodCall
def getPullrequestInfo(self, pr=None, prid=None):
if pr is None:
pr = self.getPr(prid)
if pr is None:
return None
result = {}
dict_ = pr.__dict__
for k, v in dict_.items():
if k.startswith('_'):
continue
if k in ['created_at', 'updated_at']:
v = getTimestamp(v)
if k == 'prid':
k = 'id'
result[k] = v
result['url'] = self.context.getWebAddressPullRequest(pr)
testFilter = self.context.extractRegressionTestFilter(pr.description)
havePerfReport = testFilter is not None
if havePerfReport:
result['url_perf_report'] = self.context.getWebAddressPerfRegressionReport(pr)
result['buildstatus'] = self.getPullrequestStatuses(pr)
return result
@DBMethodCall
def getPullrequestStatusShort(self, pr=None, prid=None):
if pr is None:
pr = self.getPr(prid)
if pr is None:
return None
result = {}
result['buildstatus'] = self.getPullrequestStatusesShort(pr)
return result
@DBMethodCall
def getPullrequestStatuses(self, pr):
bstatuses = [s for s in self.all_bstatuses if str(s.prid) == str(pr.prid)]
result = {}
for b in self.active_builders:
s = self.getPullrequestStatus(pr=pr, bid=b.bid, bstatuses=bstatuses)
if s:
result[b.bid] = s
return result
@DBMethodCall
def getPullrequestStatus(self, pr=None, prid=None, b=None, bid=None, bstatuses=None, shortMode=False):
if pr is None:
pr = self.getPr(prid)
if pr is None:
return None
prid = pr.prid
if b is None:
b = self.getBuilder(bid)
if b is None:
return None
bid = b.bid
if bstatuses is None:
bstatuses = [s for s in self.all_bstatuses if str(s.prid) == str(prid)]
if b.isPerf and not self.showPerf:
return None
testFilter = self.context.extractRegressionTestFilter(pr.description)
bstatus = [s for s in bstatuses if str(s.bid) == str(bid)]
bstatus = bstatus[0] if len(bstatus) > 0 else None
s = {}
operations = []
if testFilter is not None or not b.isPerf:
if bstatus:
if not shortMode:
s['created_at'] = getTimestamp(bstatus.created_at)
s['updated_at'] = getTimestamp(bstatus.updated_at)
if bstatus.build_number >= 0:
s['build_number'] = bstatus.build_number
s['build_url'] = 'builders/%s/builds/%s' % (b.builders[0], bstatus.build_number)
s['last_update'] = (datetime.datetime.utcnow() - bstatus.updated_at).total_seconds()
stopAvailable = True
status = bstatus.status
if status == BuildStatus.INQUEUE:
s['status'] = 'queued'
elif status == BuildStatus.SCHEDULING:
s['status'] = "scheduling"
elif status == BuildStatus.SCHEDULED:
s['status'] = "scheduled"
elif status == BuildStatus.BUILDING:
s['status'] = "building"
elif status == BuildStatus.SUCCESS:
s['status'] = "success"
stopAvailable = False
elif status == BuildStatus.WARNINGS:
s['status'] = "warnings"
stopAvailable = False
elif status == BuildStatus.FAILURE:
s['status'] = "failure"
stopAvailable = False
else:
s['status'] = "exception"
stopAvailable = False
if self.showOperations:
if status != BuildStatus.INQUEUE:
operations.append('restart')
if stopAvailable:
operations.append('stop')
elif self.showRevertOperation:
operations.append('revert')
else:
s['status'] = 'not_queued'
if self.showOperations:
operations.append('restart')
else:
pass
if not shortMode and len(operations) > 0:
s['operations'] = operations
s['operations_url'] = '%s/%s/%s' % (self.context.urlpath, prid, bid)
if len(s) > 0:
return s
return None
@DBMethodCall
def getPullrequestStatusesShort(self, pr):
bstatuses = [s for s in self.all_bstatuses if str(s.prid) == str(pr.prid)]
result = {}
for b in self.active_builders:
s = self.getPullrequestStatus(pr=pr, bid=b.bid, bstatuses=bstatuses, shortMode=True)
if s:
result[b.name] = s
return result
class JsonResource(resource.Resource):
requiredAuthAction = None
def getRequiredAuthAction(self, request):
return self.requiredAuthAction
def render(self, request):
assert isinstance(request, Request)
@defer.inlineCallbacks
def handle():
try:
try:
authAction = self.getRequiredAuthAction(request)
if authAction is not None:
authz = self.getAuthz(request)
res = yield authz.actionAllowed(authAction, request)
if not res:
logger.info("Auth action '%s' is not allowed: %s" % (authAction, request.uri))
raise Forbidden('Not allowed: %s' % request.uri)
data = yield self.asDict(request)
if data is None:
raise NotFound("Not found: %s" % request.uri)
except NotFound as e:
data = dict(message=str(e), _httpCode=404)
except Forbidden as e:
data = dict(message=str(e), _httpCode=403)
except Conflict as e:
data = dict(message=str(e), _httpCode=409)
except NeedUpdate as e:
data = dict(message=str(e), _httpCode=410)
except BadRequest as e:
data = dict(message=str(e), _httpCode=400)
except Exception as e:
log.err()
data = dict(message=str(e), _httpCode=500)
assert isinstance(data, dict)
httpCode = data.get('_httpCode', None)
if httpCode is not None:
request.setResponseCode(httpCode)
del data['_httpCode']
compact = RequestArgToBool(request, 'compact', False)
if compact:
data = json.dumps(data, sort_keys=True, separators=(',', ':'))
else:
data = json.dumps(data, sort_keys=True, indent=2)
data = data.encode("utf-8")
request.setHeader("Access-Control-Allow-Origin", "*")
request.setHeader("content-type", "application/json")
if httpCode is None or httpCode == 200:
if RequestArgToBool(request, 'as_file', False):
request.setHeader("content-disposition",
"attachment; filename=\"%s.json\"" % request.path)
# Make sure we get fresh pages.
request.setHeader("Pragma", "no-cache")
request.write(data)
request.finish()
except Exception as e:
request.processingFailed(Failure(e))
return
defer.maybeDeferred(handle)
return server.NOT_DONE_YET
@defer.inlineCallbacks
def asDict(self, request):
raise NotImplementedError()
# /pullrequest*
class PullRequestsResource(JsonResource):
def __init__(self, *args, **kw):
JsonResource.__init__(self)
self.context = kw.pop('context', None)
@defer.inlineCallbacks
def asDict(self, request):
def fn(_):
import time
start = time.time()
apiData = ApiData(self.context, request)
yield apiData.initialize()
result = {}
result['builders'] = yield apiData.getBuildersList()
result['pullrequests'] = {}
for prOrigin in apiData.active_pullrequests:
pr = yield apiData.getPullrequestInfo(pr=prOrigin)
result['pullrequests'][prOrigin.prid] = pr
end = time.time()
print 'PR API status time: %s' % (end - start)
defer.returnValue(result)
result = yield self.context.db.asyncRun(fn)
defer.returnValue(result)
def getChild(self, path, req):
try:
if path == '':
return self
if path == 'status':
return PullRequestsStatusResource(self.context)
prid = path
return OnePullRequestResource(self.context, prid)
except KeyError:
return NoResource("No such pullrequest '%s'" % cgi.escape(path))
class OnePullRequestResource(JsonResource):
def __init__(self, context, prid):
JsonResource.__init__(self)
self.context = context
self.prid = prid
def getChild(self, path, req):
if path == 'status':
return OnePullRequestStatusResource(self.context, self.prid)
bid = path
return OnePullRequestBuildResource(self.context, self.prid, bid)
@defer.inlineCallbacks
def asDict(self, request):
def fn(_):
apiData = ApiData(self.context, request)
yield apiData.initialize()
result = yield apiData.getPullrequestInfo(prid=self.prid)
defer.returnValue(result)
result = yield self.context.db.asyncRun(fn)
defer.returnValue(result)
# for merge service
class OnePullRequestStatusResource(JsonResource):
def __init__(self, context, prid):
JsonResource.__init__(self)
self.context = context
self.prid = prid
@defer.inlineCallbacks
def asDict(self, request):
def fn(_):
apiData = ApiData(self.context, request)
yield apiData.initialize(publicOnly=True)
result = yield apiData.getPullrequestStatusShort(prid=self.prid)
defer.returnValue(result)
result = yield self.context.db.asyncRun(fn)
defer.returnValue(result)
# for merge service
class PullRequestsStatusResource(JsonResource):
def __init__(self, context):
JsonResource.__init__(self)
self.context = context
@defer.inlineCallbacks
def asDict(self, request):
def fn(_):
apiData = ApiData(self.context, request)
yield apiData.initialize(publicOnly=True)
result = {}
result['pullrequests'] = {}
for pr in apiData.active_pullrequests:
pr_status = yield apiData.getPullrequestStatusShort(prid=pr.prid)
result['pullrequests'][pr.prid] = pr_status
defer.returnValue(result)
result = yield self.context.db.asyncRun(fn)
defer.returnValue(result)
class OnePullRequestBuildResourceBase(JsonResource, AccessorMixin):
def __init__(self, context, prid, bid):
JsonResource.__init__(self)
self.context = context
self.prid = prid
self.bid = bid
@defer.inlineCallbacks
def asDict(self, request):
def fn(_):
apiData = ApiData(self.context, request)
yield apiData.initialize()
result = yield apiData.getPullrequestStatus(prid=self.prid, bid=self.bid)
defer.returnValue(result)
result = yield self.context.db.asyncRun(fn)
defer.returnValue(result)
class OnePullRequestBuildResource(OnePullRequestBuildResourceBase):
def getChild(self, path, request):
action = None
if path == "restart":
action = RestartBuildResource(self.context, self.prid, self.bid)
if path == "stop":
action = StopBuildResource(self.context, self.prid, self.bid)
if path == "revert":
action = RevertBuildResource(self.context, self.prid, self.bid)
if action is None:
return NoResource()
return action
@defer.inlineCallbacks
def asDict(self, request):
def fn(_):
apiData = ApiData(self.context, request)
yield apiData.initialize()
result = yield apiData.getPullrequestStatus(prid=self.prid, bid=self.bid)
defer.returnValue(result)
result = yield self.context.db.asyncRun(fn)
defer.returnValue(result)
class RestartBuildResource(OnePullRequestBuildResourceBase):
requiredAuthAction = "prRestartBuild"
@defer.inlineCallbacks
def asDict(self, request):
updated_at = RequestArg(request, 'updated_at', None)
yield serviceloops.retryBuild(self.context, self.prid, self.bid, updated_at=updated_at)
res = yield OnePullRequestBuildResourceBase.asDict(self, request)
defer.returnValue(res)
class StopBuildResource(OnePullRequestBuildResourceBase):
requiredAuthAction = "prStopBuild"
@defer.inlineCallbacks
def asDict(self, request):
updated_at = RequestArg(request, 'updated_at', None)
if updated_at is None:
raise BadRequest('updated_at parameter is missing')
yield serviceloops.stopBuild(self.context, self.prid, self.bid, updated_at=updated_at)
res = yield OnePullRequestBuildResourceBase.asDict(self, request)
defer.returnValue(res)
class RevertBuildResource(OnePullRequestBuildResourceBase):
requiredAuthAction = "prRevertBuild"
@defer.inlineCallbacks
def asDict(self, request):
updated_at = RequestArg(request, 'updated_at', None)
if updated_at is None:
raise BadRequest('updated_at parameter is missing')
yield serviceloops.revertBuild(self.context, self.prid, self.bid, updated_at=updated_at)
res = yield OnePullRequestBuildResourceBase.asDict(self, request)
defer.returnValue(res)