OLD | NEW |
1 # Copyright 2016 The Chromium Authors. All rights reserved. | 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import json | 5 import json |
6 import webapp2 | 6 import webapp2 |
7 | 7 |
8 from dashboard.api import api_auth | 8 from dashboard.api import api_auth |
9 from dashboard.pinpoint.models import change | 9 from dashboard.pinpoint.models import change |
10 from dashboard.pinpoint.models import job as job_module | 10 from dashboard.pinpoint.models import job as job_module |
11 | 11 from dashboard.pinpoint.models import quest_generator as quest_generator_module |
12 _ERROR_METRIC_NO_TEST_SUITE = "Specified a metric but there's no test_suite "\ | |
13 "to run." | |
14 _ERROR_BUG_ID = 'Bug ID must be integer value.' | |
15 | 12 |
16 | 13 |
17 class ParameterValidationError(Exception): | 14 _ERROR_BUG_ID = 'Bug ID must be an integer.' |
18 pass | |
19 | 15 |
20 | 16 |
21 class New(webapp2.RequestHandler): | 17 class New(webapp2.RequestHandler): |
22 """Handler that cooks up a fresh Pinpoint job.""" | 18 """Handler that cooks up a fresh Pinpoint job.""" |
23 | 19 |
24 def post(self): | 20 def post(self): |
25 try: | 21 try: |
26 self._CreateJob() | 22 self._CreateJob() |
27 except (api_auth.ApiAuthException, ParameterValidationError) as e: | 23 except (api_auth.ApiAuthException, KeyError, TypeError, ValueError) as e: |
28 self._WriteErrorMessage(e.message) | 24 self._WriteErrorMessage(e.message) |
29 | 25 |
30 def _WriteErrorMessage(self, message): | 26 def _WriteErrorMessage(self, message): |
31 self.response.out.write(json.dumps({'error': message})) | 27 self.response.out.write(json.dumps({'error': message})) |
32 | 28 |
33 @api_auth.Authorize | 29 @api_auth.Authorize |
34 def _CreateJob(self): | 30 def _CreateJob(self): |
35 """Start a new Pinpoint job.""" | 31 """Start a new Pinpoint job.""" |
36 configuration = self.request.get('configuration') | |
37 test_suite = self.request.get('test_suite') | |
38 test = self.request.get('test') | |
39 metric = self.request.get('metric') | |
40 auto_explore = self.request.get('auto_explore') == '1' | 32 auto_explore = self.request.get('auto_explore') == '1' |
41 bug_id = self._ValidateBugId(self.request.get('bug_id')) | 33 bug_id = self.request.get('bug_id') |
42 | 34 |
43 change_1 = { | 35 change_1 = { |
44 'base_commit': { | 36 'base_commit': { |
45 'repository': self.request.get('start_repository'), | 37 'repository': self.request.get('start_repository'), |
46 'git_hash': self.request.get('start_git_hash') | 38 'git_hash': self.request.get('start_git_hash') |
47 } | 39 } |
48 } | 40 } |
49 | 41 |
50 change_2 = { | 42 change_2 = { |
51 'base_commit': { | 43 'base_commit': { |
52 'repository': self.request.get('end_repository'), | 44 'repository': self.request.get('end_repository'), |
53 'git_hash': self.request.get('end_git_hash') | 45 'git_hash': self.request.get('end_git_hash') |
54 } | 46 } |
55 } | 47 } |
56 | 48 |
57 # Validate parameters. | 49 # Validate arguments and convert them to canonical internal representation. |
58 self._ValidateMetric(test_suite, metric) | 50 quest_generator = quest_generator_module.QuestGenerator(self.request) |
59 | 51 bug_id = self._ValidateBugId(bug_id) |
60 # Convert parameters to canonical internal representation. | |
61 changes = self._ValidateChanges(change_1, change_2) | 52 changes = self._ValidateChanges(change_1, change_2) |
62 | 53 |
63 # Create job. | 54 # Create job. |
64 job = job_module.Job.New( | 55 job = job_module.Job.New( |
65 configuration=configuration, | 56 arguments=quest_generator.AsDict(), |
66 test_suite=test_suite, | 57 quests=quest_generator.Quests(), |
67 test=test, | |
68 metric=metric, | |
69 auto_explore=auto_explore, | 58 auto_explore=auto_explore, |
70 bug_id=bug_id) | 59 bug_id=bug_id) |
71 | 60 |
72 # Add changes. | 61 # Add changes. |
73 for c in changes: | 62 for c in changes: |
74 job.AddChange(c) | 63 job.AddChange(c) |
75 | 64 |
76 # Put job into datastore. | 65 # Put job into datastore. |
77 job_id = job.put().urlsafe() | 66 job_id = job.put().urlsafe() |
78 | 67 |
79 # Start job. | 68 # Start job. |
80 job.Start() | 69 job.Start() |
81 job.put() | 70 job.put() |
82 | 71 |
| 72 # TODO: Figure out if these should be underscores or lowerCamelCase. |
| 73 # TODO: They should match the input arguments. |
83 self.response.out.write(json.dumps({ | 74 self.response.out.write(json.dumps({ |
84 'jobId': job_id, | 75 'jobId': job_id, |
85 'jobUrl': job.url | 76 'jobUrl': job.url |
86 })) | 77 })) |
87 | 78 |
88 def _ValidateBugId(self, bug_id): | 79 def _ValidateBugId(self, bug_id): |
89 if not bug_id: | 80 if not bug_id: |
90 return None | 81 return None |
91 | 82 |
92 try: | 83 try: |
93 return int(bug_id) | 84 return int(bug_id) |
94 except ValueError: | 85 except ValueError: |
95 raise ParameterValidationError(_ERROR_BUG_ID) | 86 raise ValueError(_ERROR_BUG_ID) |
96 | 87 |
97 def _ValidateChanges(self, change_1, change_2): | 88 def _ValidateChanges(self, change_1, change_2): |
98 try: | 89 return (change.Change.FromDict(change_1), change.Change.FromDict(change_2)) |
99 changes = (change.Change.FromDict(change_1), | |
100 change.Change.FromDict(change_2)) | |
101 except (KeyError, ValueError) as e: | |
102 raise ParameterValidationError(str(e)) | |
103 | |
104 return changes | |
105 | |
106 def _ValidateMetric(self, test_suite, metric): | |
107 if metric and not test_suite: | |
108 raise ParameterValidationError(_ERROR_METRIC_NO_TEST_SUITE) | |
OLD | NEW |