Don't shouldn't create a request to build a patch if there is no patch to build
[WebKit-https.git] / Websites / perf.webkit.org / public / privileged-api / create-test-group.php
1 <?php
2
3 require_once('../include/json-header.php');
4 require_once('../include/repository-group-finder.php');
5
6 function main()
7 {
8     $db = connect();
9     $data = ensure_privileged_api_data_and_token_or_slave($db);
10     $author = remote_user_name($data);
11
12     $arguments = validate_arguments($data, array(
13         'name' => '/.+/',
14         'task' => 'int?',
15         'repetitionCount' => 'int?',
16     ));
17     $name = $arguments['name'];
18     $task_id = array_get($arguments, 'task');
19     $task_name = array_get($data, 'taskName');
20     $repetition_count = $arguments['repetitionCount'];
21     $platform_id = array_get($data, 'platform');
22     $test_id = array_get($data, 'test');
23     $revision_set_list = array_get($data, 'revisionSets');
24     $commit_sets_info = array_get($data, 'commitSets'); // V2 UI compatibility
25
26     if (!$task_id == !$task_name)
27         exit_with_error('InvalidTask');
28
29     if ($task_id)
30         require_format('Task', $task_id, '/^\d+$/');
31     if ($task_name || $platform_id || $test_id) {
32         require_format('Platform', $platform_id, '/^\d+$/');
33         require_format('Test', $test_id, '/^\d+$/');
34     }
35
36     if (!$revision_set_list && !$commit_sets_info)
37         exit_with_error('InvalidCommitSets');
38
39     if ($repetition_count === null)
40         $repetition_count = 1;
41     else if ($repetition_count < 1)
42         exit_with_error('InvalidRepetitionCount', array('repetitionCount' => $repetition_count));
43
44     $triggerable_id = NULL;
45     if ($task_id) {
46         $task = $db->select_first_row('analysis_tasks', 'task', array('id' => $task_id));
47         if (!$task)
48             exit_with_error('InvalidTask', array('task' => $task_id));
49
50         $duplicate_test_group = $db->select_first_row('analysis_test_groups', 'testgroup', array('task' => $task_id, 'name' => $name));
51         if ($duplicate_test_group)
52             exit_with_error('DuplicateTestGroupName', array('task' => $task_id, 'testGroup' => $duplicate_test_group['testgroup_id']));
53
54         // FIXME: Add a check for duplicate test group name.
55         $triggerable = find_triggerable_for_task($db, $task_id);
56         if ($triggerable) {
57             $triggerable_id = $triggerable['id'];
58             if (!$platform_id && !$test_id) {
59                 $platform_id = $triggerable['platform'];
60                 $test_id = $triggerable['test'];
61             } else {
62                 if ($triggerable['platform'] && $platform_id != $triggerable['platform'])
63                     exit_with_error('InconsistentPlatform', array('groupPlatform' => $platform_id, 'taskPlatform' => $triggerable['platform']));
64                 if ($triggerable['test'] && $test_id != $triggerable['test'])
65                     exit_with_error('InconsistentTest', array('groupTest' => $test_id, 'taskTest' => $triggerable['test']));
66             }
67         }
68     }
69     if (!$triggerable_id && $platform_id && $test_id) {
70         $triggerable_configuration = $db->select_first_row('triggerable_configurations', 'trigconfig',
71             array('test' => $test_id, 'platform' => $platform_id));
72         if ($triggerable_configuration)
73             $triggerable_id = $triggerable_configuration['trigconfig_triggerable'];
74     }
75
76     if (!$triggerable_id)
77         exit_with_error('TriggerableNotFoundForTask', array('task' => $task_id, 'platform' => $platform_id, 'test' => $test_id));
78
79     if ($revision_set_list)
80         $commit_sets = commit_sets_from_revision_sets($db, $triggerable_id, $revision_set_list);
81     else // V2 UI compatibility
82         $commit_sets = ensure_commit_sets($db, $triggerable_id, $commit_sets_info);
83
84     $db->begin_transaction();
85
86     if ($task_name)
87         $task_id = $db->insert_row('analysis_tasks', 'task', array('name' => $task_name, 'author' => $author));
88
89     $configuration_list = array();
90     $repository_group_with_builds = array();
91     foreach ($commit_sets as $commit_list) {
92         $commit_set_id = $db->insert_row('commit_sets', 'commitset', array());
93         $need_to_build = FALSE;
94         foreach ($commit_list['set'] as $commit_row) {
95             $commit_row['set'] = $commit_set_id;
96             $need_to_build = $need_to_build || $commit_row['patch_file'];
97             $db->insert_row('commit_set_items', 'commitset', $commit_row, 'commit');
98         }
99         $repository_group = $commit_list['repository_group'];
100         if ($need_to_build)
101             $repository_group_with_builds[$repository_group] = TRUE;
102         array_push($configuration_list, array('commit_set' => $commit_set_id, 'repository_group' => $repository_group));
103     }
104
105     $build_count = 0;
106     foreach ($configuration_list as &$config_item) {
107         if (array_get($repository_group_with_builds, $config_item['repository_group'])) {
108             $config_item['need_to_build'] = TRUE;
109             $build_count++;
110         }
111     }
112
113     $group_id = $db->insert_row('analysis_test_groups', 'testgroup',
114         array('task' => $task_id, 'name' => $name, 'author' => $author));
115
116     if ($build_count) {
117         $order = -$build_count;
118         foreach ($configuration_list as $config) {
119             if (!array_get($config, 'need_to_build'))
120                 continue;
121             assert($order < 0);
122             $db->insert_row('build_requests', 'request', array(
123                 'triggerable' => $triggerable_id,
124                 'repository_group' => $config['repository_group'],
125                 'platform' => $platform_id,
126                 'test' => NULL,
127                 'group' => $group_id,
128                 'order' => $order,
129                 'commit_set' => $config['commit_set']));
130             $order++;
131         }        
132     }
133
134     $order = 0;
135     for ($i = 0; $i < $repetition_count; $i++) {
136         foreach ($configuration_list as $config) {
137             $db->insert_row('build_requests', 'request', array(
138                 'triggerable' => $triggerable_id,
139                 'repository_group' => $config['repository_group'],
140                 'platform' => $platform_id,
141                 'test' => $test_id,
142                 'group' => $group_id,
143                 'order' => $order,
144                 'commit_set' => $config['commit_set']));
145             $order++;
146         }
147     }
148
149     $db->commit_transaction();
150
151     exit_with_success(array('taskId' => $task_id, 'testGroupId' => $group_id));
152 }
153
154 function commit_sets_from_revision_sets($db, $triggerable_id, $revision_set_list)
155 {
156     if (count($revision_set_list) < 2)
157         exit_with_error('InvalidRevisionSets', array('revisionSets' => $revision_set_list));
158
159     $finder = new RepositoryGroupFinder($db, $triggerable_id);
160     $commit_set_list = array();
161     foreach ($revision_set_list as $revision_set) {
162         if (!count($revision_set))
163             exit_with_error('InvalidRevisionSets', array('revisionSets' => $revision_set_list));
164
165         $commit_set = array();
166         $repository_list = array();
167         $repository_with_patch = array();
168         foreach ($revision_set as $repository_id => $data) {
169             if ($repository_id == 'customRoots') {
170                 $file_id_list = $data;
171                 foreach ($file_id_list as $file_id) {
172                     if (!is_numeric($file_id) || !$db->select_first_row('uploaded_files', 'file', array('id' => $file_id)))
173                         exit_with_error('InvalidUploadedFile', array('file' => $file_id));
174                     array_push($commit_set, array('root_file' => $file_id, 'patch_file' => NULL));
175                 }
176                 continue;
177             }
178             if (!is_numeric($repository_id))
179                 exit_with_error('InvalidRepository', array('repository' => $repository_id));
180
181             if (!is_array($data))
182                 exit_with_error('InvalidRepositoryData', array('repository' => $repository_id, 'data' => $data));
183
184             $revision = array_get($data, 'revision');
185             if (!$revision)
186                 exit_with_error('InvalidRevision', array('repository' => $repository_id, 'data' => $data));
187             $commit = $db->select_first_row('commits', 'commit',
188                 array('repository' => intval($repository_id), 'revision' => $revision));
189             if (!$commit)
190                 exit_with_error('RevisionNotFound', array('repository' => $repository_id, 'revision' => $revision));
191
192             $patch_file_id = array_get($data, 'patch');
193             if ($patch_file_id) {
194                 if (!is_numeric($patch_file_id) || !$db->select_first_row('uploaded_files', 'file', array('id' => $patch_file_id)))
195                     exit_with_error('InvalidPatchFile', array('patch' => $patch_file_id));
196                 array_push($repository_with_patch, $repository_id);
197             }
198
199             array_push($commit_set, array('commit' => $commit['commit_id'], 'patch_file' => $patch_file_id));
200             array_push($repository_list, $repository_id);
201         }
202
203         $repository_group_id = $finder->find_by_repositories($repository_list);
204         if (!$repository_group_id)
205             exit_with_error('NoMatchingRepositoryGroup', array('repositoris' => $repository_list));
206
207         foreach ($repository_with_patch as $repository_id) {
208             if (!$finder->accepts_patch($repository_group_id, $repository_id))
209                 exit_with_error('PatchNotAccepted', array('repository' => $repository_id, 'repositoryGroup' => $repository_group_id));
210         }
211
212         array_push($commit_set_list, array('repository_group' => $repository_group_id, 'set' => $commit_set));
213     }
214
215     return $commit_set_list;
216 }
217
218 function ensure_commit_sets($db, $triggerable_id, $commit_sets_info) {
219     $repository_name_to_id = array();
220     foreach ($db->select_rows('repositories', 'repository', array('owner' => NULL)) as $row)
221         $repository_name_to_id[$row['repository_name']] = $row['repository_id'];
222
223     $commit_sets = array();
224     $repository_list = array();
225     foreach ($commit_sets_info as $repository_name => $revisions) {
226         $repository_id = array_get($repository_name_to_id, $repository_name);
227         if (!$repository_id)
228             exit_with_error('RepositoryNotFound', array('name' => $repository_name));
229         array_push($repository_list, $repository_id);
230
231         foreach ($revisions as $i => $revision) {
232             $commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $revision));
233             if (!$commit)
234                 exit_with_error('RevisionNotFound', array('repository' => $repository_name, 'revision' => $revision));
235             array_set_default($commit_sets, $i, array('set' => array()));
236             array_push($commit_sets[$i]['set'], array('commit' => $commit['commit_id'], 'patch_file' => NULL));
237         }
238     }
239
240     $finder = new RepositoryGroupFinder($db, $triggerable_id);
241     $repository_group_id = $finder->find_by_repositories($repository_list);
242     if (!$repository_group_id)
243         exit_with_error('NoMatchingRepositoryGroup', array('repositoris' => $repository_list));
244
245     if (count($commit_sets) < 2)
246         exit_with_error('InvalidCommitSets', array('commitSets' => $commit_sets_info));
247
248     $commit_count_per_set = count($commit_sets[0]['set']);
249     foreach ($commit_sets as &$commits) {
250         $commits['repository_group'] = $repository_group_id;
251         if ($commit_count_per_set != count($commits['set']))
252             exit_with_error('InvalidCommitSets', array('commitSets' => $commit_sets));
253     }
254
255     return $commit_sets;
256 }
257
258 main();
259
260 ?>