e823966de4ba04f135283ea4bd7b9a8c558b99c1
[WebKit-https.git] / Websites / perf.webkit.org / public / api / update-triggerable.php
1 <?php
2
3 require_once('../include/json-header.php');
4 require_once('../include/repository-group-finder.php');
5
6 function main($post_data)
7 {
8     $db = new Database;
9     if (!$db->connect())
10         exit_with_error('DatabaseConnectionFailure');
11
12     $report = json_decode($post_data, true);
13     verify_slave($db, $report);
14
15     $triggerable_name = array_get($report, 'triggerable');
16     $triggerable = $db->select_first_row('build_triggerables', 'triggerable', array('name' => $triggerable_name));
17     if (!$triggerable)
18         exit_with_error('TriggerableNotFound', array('triggerable' => $triggerable_name));
19     $triggerable_id = $triggerable['triggerable_id'];
20
21     $configurations = array_get($report, 'configurations');
22     validate_configurations($db, $configurations);
23
24     $repository_groups = array_get($report, 'repositoryGroups', array());
25     validate_repository_groups($db, $repository_groups);
26
27     $finder = new RepositoryGroupFinder($db, $triggerable_id);
28     foreach ($repository_groups as &$group)
29         $group['existingGroup'] = $finder->find_by_repositories($group['repositories']);
30
31     $db->begin_transaction();
32     if ($db->query_and_get_affected_rows('DELETE FROM triggerable_configurations WHERE trigconfig_triggerable = $1', array($triggerable_id)) === false) {
33         $db->rollback_transaction();
34         exit_with_error('FailedToDeleteExistingConfigurations', array('triggerable' => $triggerable_id));
35     }
36
37     foreach ($configurations as &$entry) {
38         $config_info = array('test' => $entry['test'], 'platform' => $entry['platform'], 'triggerable' => $triggerable_id);
39         if (!$db->insert_row('triggerable_configurations', 'trigconfig', $config_info, null)) {
40             $db->rollback_transaction();
41             exit_with_error('FailedToInsertConfiguration', array('entry' => $entry));
42         }
43     }
44
45     foreach ($repository_groups as &$group) {
46         $group_id = $group['existingGroup'];
47         if ($group_id) {
48             $group_info = array('name' => $group['name'], 'description' => array_get($group, 'description'));
49             if (!$db->update_row('triggerable_repository_groups', 'repositorygroup', array('id' => $group_id), $group_info)) {
50                 $db->rollback_transaction();
51                 exit_with_error('FailedToInsertRepositoryGroup', array('repositoryGroup' => $group));
52             }
53         } else {
54             $group_id = $db->update_or_insert_row('triggerable_repository_groups', 'repositorygroup',
55                 array('triggerable' => $triggerable_id, 'name' => $group['name']),
56                 array('triggerable' => $triggerable_id, 'name' => $group['name'], 'description' => array_get($group, 'description')));
57             if (!$group_id) {
58                 $db->rollback_transaction();
59                 exit_with_error('FailedToInsertRepositoryGroup', array('repositoryGroup' => $group));
60             }
61         }
62         if ($db->query_and_get_affected_rows('DELETE FROM triggerable_repositories WHERE trigrepo_group = $1', array($group_id)) === FALSE) {
63             $db->rollback_transaction();
64             exit_with_error('FailedToDisassociateRepositories', array('repositoryGroup' => $group));
65         }
66         foreach ($group['repositories'] as $repository_id) {
67             if (!$db->insert_row('triggerable_repositories', 'trigrepo', array('group' => $group_id, 'repository' => $repository_id), null)) {
68                 $db->rollback_transaction();
69                 exit_with_error('FailedToAssociateRepository', array('repositoryGroup' => $group, 'repository' => $repository_id));
70             }
71         }
72     }
73
74     $db->commit_transaction();
75     exit_with_success();
76 }
77
78 function validate_configurations($db, $configurations)
79 {
80     if (!is_array($configurations))
81         exit_with_error('InvalidConfigurations', array('configurations' => $configurations));
82
83     foreach ($configurations as $entry) {
84         if (!is_array($entry) || !array_key_exists('test', $entry) || !array_key_exists('platform', $entry))
85             exit_with_error('InvalidConfigurationEntry', array('configurationEntry' => $entry));
86     }
87 }
88
89 function validate_repository_groups($db, $repository_groups)
90 {
91     if (!is_array($repository_groups))
92         exit_with_error('InvalidRepositoryGroups', array('repositoryGroups' => $repository_groups));
93
94     $top_level_repositories = $db->select_rows('repositories', 'repository', array('owner' => null));
95     $top_level_repository_ids = array();
96     foreach ($top_level_repositories as $repository_row)
97         $top_level_repository_ids[$repository_row['repository_id']] = true;
98
99     foreach ($repository_groups as &$group) {
100         if (!is_array($group) || !array_key_exists('name', $group) || !array_key_exists('repositories', $group) || !is_array($group['repositories']))
101             exit_with_error('InvalidRepositoryGroup', array('repositoryGroup' => $group));
102         $repository_list = $group['repositories'];
103         $group_repository_list = array();
104         foreach ($repository_list as $repository_id) {
105             if (!array_key_exists($repository_id, $top_level_repository_ids) || array_key_exists($repository_id, $group_repository_list))
106                 exit_with_error('InvalidRepository', array('repositoryGroup' => $group, 'repository' => $repository_id));
107             $group_repository_list[$repository_id] = true;
108         }
109     }
110 }
111
112 main($HTTP_RAW_POST_DATA);
113
114 ?>