Extend create-analysis-test API to be able to create with confirming test group.
[WebKit-https.git] / Websites / perf.webkit.org / public / include / commit-sets-helpers.php
1 <?php
2
3 require_once('repository-group-finder.php');
4 require_once('commit-log-fetcher.php');
5
6 # FIXME: Should create a helper class for below 3 helper functions to avoid passing long argument list.
7 function create_test_group_and_build_requests($db, $commit_sets, $task_id, $name, $author, $triggerable_id, $platform_id, $test_id, $repetition_count) {
8
9     list ($build_configuration_list, $test_configuration_list) = insert_commit_sets_and_construct_configuration_list($db, $commit_sets);
10
11     $group_id = $db->insert_row('analysis_test_groups', 'testgroup',
12         array('task' => $task_id, 'name' => $name, 'author' => $author));
13
14     $build_count = count($build_configuration_list);
15     $order = -$build_count;
16     foreach($build_configuration_list as $build_configuration)
17         insert_build_request_for_configuration($db, $build_configuration, $order++, $triggerable_id, $platform_id, NULL, $group_id);
18
19     for ($i = 0; $i < $repetition_count; $i++) {
20         foreach($test_configuration_list as $test_configuration)
21             insert_build_request_for_configuration($db, $test_configuration, $order++, $triggerable_id, $platform_id, $test_id, $group_id);
22     }
23     return $group_id;
24 }
25
26 function insert_commit_sets_and_construct_configuration_list($db, $commit_sets)
27 {
28     $repository_group_with_builds = array();
29     $test_configuration_list = array();
30     $build_configuration_list = array();
31
32     foreach ($commit_sets as $commit_list) {
33         $commit_set_id = $db->insert_row('commit_sets', 'commitset', array());
34         $need_to_build = FALSE;
35         foreach ($commit_list['set'] as $commit_row) {
36             $commit_row['set'] = $commit_set_id;
37             $requires_build =  $commit_row['requires_build'];
38             assert(is_bool($requires_build));
39             $need_to_build = $need_to_build || $requires_build;
40             $db->insert_row('commit_set_items', 'commitset', $commit_row, 'commit');
41         }
42         $repository_group = $commit_list['repository_group'];
43         if ($need_to_build)
44             $repository_group_with_builds[$repository_group] = TRUE;
45         array_push($test_configuration_list, array('commit_set' => $commit_set_id, 'repository_group' => $repository_group));
46     }
47
48     foreach ($test_configuration_list as &$config) {
49         if (array_get($repository_group_with_builds, $config['repository_group']))
50             array_push($build_configuration_list, $config);
51     }
52     return array($build_configuration_list, $test_configuration_list);
53 }
54
55 function insert_build_request_for_configuration($db, $configuration, $order, $triggerable_id, $platform_id, $test_id, $group_id)
56 {
57     $db->insert_row('build_requests', 'request', array(
58         'triggerable' => $triggerable_id,
59         'repository_group' => $configuration['repository_group'],
60         'platform' => $platform_id,
61         'test' => $test_id,
62         'group' => $group_id,
63         'order' => $order,
64         'commit_set' => $configuration['commit_set']));
65 }
66
67 function commit_sets_from_revision_sets($db, $triggerable_id, $revision_set_list)
68 {
69     if (count($revision_set_list) < 2)
70         exit_with_error('InvalidRevisionSets', array('revisionSets' => $revision_set_list));
71
72     $finder = new RepositoryGroupFinder($db, $triggerable_id);
73     $commit_set_list = array();
74     $repository_owner_list = array();
75     $repositories_require_build = array();
76     $commit_set_items_by_repository = array();
77     foreach ($revision_set_list as $revision_set) {
78         if (!count($revision_set))
79             exit_with_error('InvalidRevisionSets', array('revisionSets' => $revision_set_list));
80
81         $commit_set = array();
82         $repository_list = array();
83         $repository_with_patch = array();
84         foreach ($revision_set as $repository_id => $data) {
85             if ($repository_id == 'customRoots') {
86                 $file_id_list = $data;
87                 foreach ($file_id_list as $file_id) {
88                     if (!is_numeric($file_id) || !$db->select_first_row('uploaded_files', 'file', array('id' => $file_id)))
89                         exit_with_error('InvalidUploadedFile', array('file' => $file_id));
90                     array_push($commit_set, array('root_file' => $file_id, 'patch_file' => NULL, 'requires_build' => FALSE, 'commit_owner' => NULL));
91                 }
92                 continue;
93             }
94             if (!is_numeric($repository_id))
95                 exit_with_error('InvalidRepository', array('repository' => $repository_id));
96
97             if (!is_array($data))
98                 exit_with_error('InvalidRepositoryData', array('repository' => $repository_id, 'data' => $data));
99
100             $revision = array_get($data, 'revision');
101             if (!$revision)
102                 exit_with_error('InvalidRevision', array('repository' => $repository_id, 'data' => $data));
103             $commit_id = CommitLogFetcher::find_commit_id_by_revision($db, $repository_id, $revision);
104             if ($commit_id < 0)
105                 exit_with_error('AmbiguousRevision', array('repository' => $repository_id, 'revision' => $revision));
106             if (!$commit_id)
107                 exit_with_error('RevisionNotFound', array('repository' => $repository_id, 'revision' => $revision));
108
109             $owner_revision = array_get($data, 'ownerRevision');
110             $patch_file_id = array_get($data, 'patch');
111             if ($patch_file_id) {
112                 if (!is_numeric($patch_file_id) || !$db->select_first_row('uploaded_files', 'file', array('id' => $patch_file_id)))
113                     exit_with_error('InvalidPatchFile', array('patch' => $patch_file_id));
114                 array_push($repository_with_patch, $repository_id);
115                 $repositories_require_build[$repository_id] =  TRUE;
116             }
117
118             $repository = NULL;
119             $owner_commit_id = NULL;
120             if ($owner_revision) {
121                 $repository = $db->select_first_row('repositories', 'repository', array('id' => intval($repository_id)));
122                 if (!$repository)
123                     exit_with_error('RepositoryNotFound', array('repository' => $repository_id));
124                 $owner_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository['repository_owner'], 'revision' => $owner_revision));
125                 if (!$owner_commit)
126                     exit_with_error('InvalidOwnerRevision', array('repository' => $repository['repository_owner'], 'revision' => $owner_revision));
127                 if (!$db->select_first_row('commit_ownerships', 'commit', array('owned' => $commit_id, 'owner' => $owner_commit['commit_id'])))
128                     exit_with_error('InvalidCommitOwnership', array('commitOwner' => $owner_commit['commit_id'], 'commitOwned' => $commit_id));
129                 $repositories_require_build[$repository_id] =  TRUE;
130                 $owner_commit_id = $owner_commit['commit_id'];
131             }
132
133             array_push($commit_set, array('commit' => $commit_id, 'patch_file' => $patch_file_id, 'requires_build' => FALSE, 'commit_owner' => $owner_commit_id));
134
135             array_ensure_item_has_array($commit_set_items_by_repository, $repository_id);
136             $commit_set_items_by_repository[$repository_id][] = &$commit_set[count($commit_set) - 1];
137
138             if ($owner_commit_id)
139                 continue;
140             array_push($repository_list, $repository_id);
141         }
142         $repository_group_id = $finder->find_by_repositories($repository_list);
143         if (!$repository_group_id)
144             exit_with_error('NoMatchingRepositoryGroup', array('repositories' => $repository_list));
145
146         foreach ($repository_with_patch as $repository_id) {
147             if (!$finder->accepts_patch($repository_group_id, $repository_id))
148                 exit_with_error('PatchNotAccepted', array('repository' => $repository_id, 'repositoryGroup' => $repository_group_id));
149         }
150
151         array_push($commit_set_list, array('repository_group' => $repository_group_id, 'set' => $commit_set));
152     }
153
154     foreach (array_keys($repositories_require_build) as $repository_id) {
155         foreach($commit_set_items_by_repository[$repository_id] as &$commit_set_item)
156             $commit_set_item['requires_build'] = TRUE;
157     }
158     return $commit_set_list;
159 }
160 ?>