-
Notifications
You must be signed in to change notification settings - Fork 352
/
push.py
282 lines (253 loc) · 11.9 KB
/
push.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
import datetime
import newrelic.agent
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.status import (HTTP_400_BAD_REQUEST,
HTTP_404_NOT_FOUND)
from six import iteritems
from treeherder.model.models import (Push,
Repository)
from treeherder.webapp.api.serializers import PushSerializer
from treeherder.webapp.api.utils import (to_datetime,
to_timestamp)
class PushViewSet(viewsets.ViewSet):
"""
View for ``push`` records
"""
def list(self, request, project):
"""
GET method for list of ``push`` records with revisions
"""
# What is the upper limit on the number of pushes returned by the api
MAX_PUSH_COUNT = 1000
# make a mutable copy of these params
filter_params = request.query_params.copy()
# This will contain some meta data about the request and results
meta = {}
# support ranges for date as well as revisions(changes) like old tbpl
for param in ["fromchange", "tochange", "startdate", "enddate", "revision", "commit_revision"]:
v = filter_params.get(param, None)
if v:
del filter_params[param]
meta[param] = v
try:
repository = Repository.objects.get(name=project)
except Repository.DoesNotExist:
return Response({
"detail": "No project with name {}".format(project)
}, status=HTTP_404_NOT_FOUND)
pushes = Push.objects.filter(repository=repository).order_by('-time')
for (param, value) in iteritems(meta):
if param == 'fromchange':
revision_field = 'revision__startswith' if len(value) < 40 else 'revision'
filter_kwargs = {revision_field: value, 'repository': repository}
frompush_time = Push.objects.values_list('time', flat=True).get(
**filter_kwargs)
pushes = pushes.filter(time__gte=frompush_time)
filter_params.update({
"push_timestamp__gte": to_timestamp(frompush_time)
})
self.report_if_short_revision(param, value)
elif param == 'tochange':
revision_field = 'revision__startswith' if len(value) < 40 else 'revision'
filter_kwargs = {revision_field: value, 'repository': repository}
topush_time = Push.objects.values_list('time', flat=True).get(
**filter_kwargs)
pushes = pushes.filter(time__lte=topush_time)
filter_params.update({
"push_timestamp__lte": to_timestamp(topush_time)
})
self.report_if_short_revision(param, value)
elif param == 'startdate':
pushes = pushes.filter(time__gte=to_datetime(value))
filter_params.update({
"push_timestamp__gte": to_timestamp(to_datetime(value))
})
elif param == 'enddate':
real_end_date = to_datetime(value) + datetime.timedelta(days=1)
pushes = pushes.filter(time__lte=real_end_date)
filter_params.update({
"push_timestamp__lt": to_timestamp(real_end_date)
})
elif param == 'revision':
# revision must be the tip revision of the push itself
revision_field = 'revision__startswith' if len(value) < 40 else 'revision'
filter_kwargs = {revision_field: value}
pushes = pushes.filter(**filter_kwargs)
rev_key = "revisions_long_revision" \
if len(meta['revision']) == 40 else "revisions_short_revision"
filter_params.update({rev_key: meta['revision']})
self.report_if_short_revision(param, value)
elif param == 'commit_revision':
# revision can be either the revision of the push itself, or
# any of the commits it refers to
pushes = pushes.filter(commits__revision=value)
self.report_if_short_revision(param, value)
for param in ['push_timestamp__lt', 'push_timestamp__lte',
'push_timestamp__gt', 'push_timestamp__gte']:
if filter_params.get(param):
# translate push timestamp directly into a filter
try:
value = datetime.datetime.fromtimestamp(
float(filter_params.get(param)))
except ValueError:
return Response({
"detail": "Invalid timestamp specified for {}".format(
param)
}, status=HTTP_400_BAD_REQUEST)
pushes = pushes.filter(**{
param.replace('push_timestamp', 'time'): value
})
for param in ['id__lt', 'id__lte', 'id__gt', 'id__gte', 'id']:
try:
value = int(filter_params.get(param, 0))
except ValueError:
return Response({
"detail": "Invalid timestamp specified for {}".format(
param)
}, status=HTTP_400_BAD_REQUEST)
if value:
pushes = pushes.filter(**{param: value})
id_in = filter_params.get("id__in")
if id_in:
try:
id_in_list = [int(id) for id in id_in.split(',')]
except ValueError:
return Response({"detail": "Invalid id__in specification"},
status=HTTP_400_BAD_REQUEST)
pushes = pushes.filter(id__in=id_in_list)
author = filter_params.get("author")
if author:
pushes = pushes.filter(author=author)
try:
count = int(filter_params.get("count", 10))
except ValueError:
return Response({"detail": "Valid count value required"},
status=HTTP_400_BAD_REQUEST)
if count > MAX_PUSH_COUNT:
msg = "Specified count exceeds api limit: {}".format(MAX_PUSH_COUNT)
return Response({"detail": msg}, status=HTTP_400_BAD_REQUEST)
# we used to have a "full" parameter for this endpoint so you could
# specify to not fetch the revision information if it was set to
# false. however AFAIK no one ever used it (default was to fetch
# everything), so let's just leave it out. it doesn't break
# anything to send extra data when not required.
pushes = pushes.select_related('repository').prefetch_related('commits')[:count]
serializer = PushSerializer(pushes, many=True)
meta['count'] = len(pushes)
meta['repository'] = project
meta['filter_params'] = filter_params
resp = {
'meta': meta,
'results': serializer.data
}
return Response(resp)
def retrieve(self, request, project, pk=None):
"""
GET method implementation for detail view of ``push``
"""
try:
push = Push.objects.get(repository__name=project,
id=pk)
serializer = PushSerializer(push)
return Response(serializer.data)
except Push.DoesNotExist:
return Response("No push with id: {0}".format(pk),
status=HTTP_404_NOT_FOUND)
@action(detail=True)
def status(self, request, project, pk=None):
"""
Return a count of the jobs belonging to this push
grouped by job status.
"""
try:
push = Push.objects.get(id=pk)
except Push.DoesNotExist:
return Response("No push with id: {0}".format(pk),
status=HTTP_404_NOT_FOUND)
return Response(push.get_status())
@action(detail=False)
def health(self, request, project):
"""
Return a calculated assessment of the health of this push.
TODO: Replace this static dummy data with real data.
"""
revision = request.query_params.get('revision')
try:
push = Push.objects.get(revision=revision, repository__name=project)
except Push.DoesNotExist:
return Response("No push with revision: {0}".format(revision),
status=HTTP_404_NOT_FOUND)
return Response({
'revision': revision,
'id': push.id,
'result': 'fail',
'metrics': [
{
'name': 'Builds',
'result': 'pass',
'value': 10,
'details': ['Wow, everything passed!'],
},
{
'name': 'Linting',
'result': 'pass',
'value': 10,
'details': ['Gosh, this code is really nicely formatted.'],
},
{
'name': 'Tests',
'result': 'fail',
'value': 2,
'failures': [
{
'testName': 'dom/tests/mochitest/fetch/test_fetch_cors_sw_reroute.html',
'jobName': 'test-linux32/opt-mochitest-browser-chrome-e10s-4',
'jobId': 223458405,
'classification': 'intermittent',
'failureLine':
'REFTEST TEST-UNEXPECTED-FAIL | file:///builds/worker/workspace/build/tests/reftest/tests/layout/reftests/border-dotted/border-dashed-no-radius.html == file:///builds/worker/workspace/build/tests/reftest/tests/layout/reftests/border-dotted/masked.html | image comparison, max difference: 255, number of differing pixels: 54468',
'confidence': 3,
},
{
'testName':
'browser/components/extensions/test/browser/test-oop-extensions/browser_ext_pageAction_context.js',
'jobName': 'test-linux64/debug-mochitest-plain-headless-e10s-8',
'jobId': 223458405,
'classification': 'intermittent',
'failureLine':
"raptor-main TEST-UNEXPECTED-FAIL: test 'raptor-tp6-bing-firefox' timed out loading test page: https://www.bing.com/search?q=barack+obama",
'confidence': 4,
},
],
'details': [
'Ran some tests that did not go so well',
'See [foo.bar.baz/mongo/rational/fee]',
],
},
{
'name': 'Coverage',
'result': 'indeterminate',
'value': 5,
'details': [
'Covered 42% of the tests that are needed for feature ``foo``.',
'Covered 100% of the tests that are needed for feature ``bar``.',
'The ratio of people to cake is too many...',
],
},
{
'name': 'Performance',
'result': 'pass',
'value': 10,
'details': ['Ludicrous Speed'],
},
],
})
# TODO: Remove when we no longer support short revisions: Bug 1306707
def report_if_short_revision(self, param, revision):
if len(revision) < 40:
newrelic.agent.record_custom_event(
'short_revision_push_api',
{'error': 'Revision <40 chars', 'param': param, 'revision': revision}
)