Add the support for scheduling a A/B testing with a patch.
[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     $needs_to_build = FALSE;
91     foreach ($commit_sets as $commit_list) {
92         $commit_set_id = $db->insert_row('commit_sets', 'commitset', array());
93         foreach ($commit_list['set'] as $commit_row) {
94             $commit_row['set'] = $commit_set_id;
95             $needs_to_build = $needs_to_build || $commit_row['patch_file'];
96             $db->insert_row('commit_set_items', 'commitset', $commit_row, 'commit');
97         }
98         array_push($configuration_list, array('commit_set' => $commit_set_id, 'repository_group' => $commit_list['repository_group']));
99     }
100
101     $group_id = $db->insert_row('analysis_test_groups', 'testgroup',
102         array('task' => $task_id, 'name' => $name, 'author' => $author));
103
104     if ($needs_to_build) {
105         $order = -count($configuration_list);
106         foreach ($configuration_list as $config) {
107             assert($order < 0);
108             $db->insert_row('build_requests', 'request', array(
109                 'triggerable' => $triggerable_id,
110                 'repository_group' => $config['repository_group'],
111                 'platform' => $platform_id,
112                 'test' => NULL,
113                 'group' => $group_id,
114                 'order' => $order,
115                 'commit_set' => $config['commit_set']));
116             $order++;
117         }        
118     }
119
120     $order = 0;
121     for ($i = 0; $i < $repetition_count; $i++) {
122         foreach ($configuration_list as $config) {
123             $db->insert_row('build_requests', 'request', array(
124                 'triggerable' => $triggerable_id,
125                 'repository_group' => $config['repository_group'],
126                 'platform' => $platform_id,
127                 'test' => $test_id,
128                 'group' => $group_id,
129                 'order' => $order,
130                 'commit_set' => $config['commit_set']));
131             $order++;
132         }
133     }
134
135     $db->commit_transaction();
136
137     exit_with_success(array('taskId' => $task_id, 'testGroupId' => $group_id));
138 }
139
140 function commit_sets_from_revision_sets($db, $triggerable_id, $revision_set_list)
141 {
142     if (count($revision_set_list) < 2)
143         exit_with_error('InvalidRevisionSets', array('revisionSets' => $revision_set_list));
144
145     $finder = new RepositoryGroupFinder($db, $triggerable_id);
146     $commit_set_list = array();
147     foreach ($revision_set_list as $revision_set) {
148         if (!count($revision_set))
149             exit_with_error('InvalidRevisionSets', array('revisionSets' => $revision_set_list));
150
151         $commit_set = array();
152         $repository_list = array();
153         $repository_with_patch = array();
154         foreach ($revision_set as $repository_id => $data) {
155             if ($repository_id == 'customRoots') {
156                 $file_id_list = $data;
157                 foreach ($file_id_list as $file_id) {
158                     if (!is_numeric($file_id) || !$db->select_first_row('uploaded_files', 'file', array('id' => $file_id)))
159                         exit_with_error('InvalidUploadedFile', array('file' => $file_id));
160                     array_push($commit_set, array('root_file' => $file_id, 'patch_file' => NULL));
161                 }
162                 continue;
163             }
164             if (!is_numeric($repository_id))
165                 exit_with_error('InvalidRepository', array('repository' => $repository_id));
166
167             if (!is_array($data))
168                 exit_with_error('InvalidRepositoryData', array('repository' => $repository_id, 'data' => $data));
169
170             $revision = array_get($data, 'revision');
171             if (!$revision)
172                 exit_with_error('InvalidRevision', array('repository' => $repository_id, 'data' => $data));
173             $commit = $db->select_first_row('commits', 'commit',
174                 array('repository' => intval($repository_id), 'revision' => $revision));
175             if (!$commit)
176                 exit_with_error('RevisionNotFound', array('repository' => $repository_id, 'revision' => $revision));
177
178             $patch_file_id = array_get($data, 'patch');
179             if ($patch_file_id) {
180                 if (!is_numeric($patch_file_id) || !$db->select_first_row('uploaded_files', 'file', array('id' => $patch_file_id)))
181                     exit_with_error('InvalidPatchFile', array('patch' => $patch_file_id));
182                 array_push($repository_with_patch, $repository_id);
183             }
184
185             array_push($commit_set, array('commit' => $commit['commit_id'], 'patch_file' => $patch_file_id));
186             array_push($repository_list, $repository_id);
187         }
188
189         $repository_group_id = $finder->find_by_repositories($repository_list);
190         if (!$repository_group_id)
191             exit_with_error('NoMatchingRepositoryGroup', array('repositoris' => $repository_list));
192
193         foreach ($repository_with_patch as $repository_id) {
194             if (!$finder->accepts_patch($repository_group_id, $repository_id))
195                 exit_with_error('PatchNotAccepted', array('repository' => $repository_id, 'repositoryGroup' => $repository_group_id));
196         }
197
198         array_push($commit_set_list, array('repository_group' => $repository_group_id, 'set' => $commit_set));
199     }
200
201     return $commit_set_list;
202 }
203
204 function ensure_commit_sets($db, $triggerable_id, $commit_sets_info) {
205     $repository_name_to_id = array();
206     foreach ($db->select_rows('repositories', 'repository', array('owner' => NULL)) as $row)
207         $repository_name_to_id[$row['repository_name']] = $row['repository_id'];
208
209     $commit_sets = array();
210     $repository_list = array();
211     foreach ($commit_sets_info as $repository_name => $revisions) {
212         $repository_id = array_get($repository_name_to_id, $repository_name);
213         if (!$repository_id)
214             exit_with_error('RepositoryNotFound', array('name' => $repository_name));
215         array_push($repository_list, $repository_id);
216
217         foreach ($revisions as $i => $revision) {
218             $commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $revision));
219             if (!$commit)
220                 exit_with_error('RevisionNotFound', array('repository' => $repository_name, 'revision' => $revision));
221             array_set_default($commit_sets, $i, array('set' => array()));
222             array_push($commit_sets[$i]['set'], array('commit' => $commit['commit_id'], 'patch_file' => NULL));
223         }
224     }
225
226     $finder = new RepositoryGroupFinder($db, $triggerable_id);
227     $repository_group_id = $finder->find_by_repositories($repository_list);
228     if (!$repository_group_id)
229         exit_with_error('NoMatchingRepositoryGroup', array('repositoris' => $repository_list));
230
231     if (count($commit_sets) < 2)
232         exit_with_error('InvalidCommitSets', array('commitSets' => $commit_sets_info));
233
234     $commit_count_per_set = count($commit_sets[0]['set']);
235     foreach ($commit_sets as &$commits) {
236         $commits['repository_group'] = $repository_group_id;
237         if ($commit_count_per_set != count($commits['set']))
238             exit_with_error('InvalidCommitSets', array('commitSets' => $commit_sets));
239     }
240
241     return $commit_sets;
242 }
243
244 main();
245
246 ?>