Add the support for scheduling a A/B testing with a patch.
[WebKit-https.git] / Websites / perf.webkit.org / public / admin / triggerables.php
1 <?php
2
3 require('../include/admin-header.php');
4
5 if ($db) {
6
7     if ($action == 'add') {
8         if ($db->insert_row('build_triggerables', 'triggerable', array('name' => $_POST['name']))) {
9             notice('Inserted the new triggerable.');
10             regenerate_manifest();
11         } else
12             notice('Could not add the triggerable.');
13     } else if ($action == 'update') {
14         if (update_field('build_triggerables', 'triggerable', 'name'))
15             regenerate_manifest();
16         else if (update_field('build_triggerables', 'triggerable', 'disabled', Database::to_database_boolean(array_get($_POST, 'disabled'))))
17             regenerate_manifest();
18         else
19             notice('Invalid parameters.');
20     } else if ($action == 'update-group-name') {
21         if (update_field('triggerable_repository_groups', 'repositorygroup', 'name'))
22             regenerate_manifest();
23     } else if ($action == 'update-group-description') {
24         if (update_field('triggerable_repository_groups', 'repositorygroup', 'description'))
25             regenerate_manifest();
26     } else if ($action == 'update-group-accept-roots') {
27         if (update_field('triggerable_repository_groups', 'repositorygroup', 'accepts_roots',
28             Database::to_database_boolean(array_get($_POST, 'accepts'))))
29             regenerate_manifest();
30     } else if ($action == 'update-repositories') {
31         $group_id = intval($_POST['group']);
32
33         $db->begin_transaction();
34         $db->query_and_get_affected_rows("DELETE FROM triggerable_repositories WHERE trigrepo_group = $1", array($group_id));
35         $suceeded = insert_triggerable_repositories($db, $group_id, array_get($_POST, 'repositories'));
36         if ($suceeded) {
37             $db->commit_transaction();
38             notice('Updated the association.');
39             regenerate_manifest();
40         } else
41             $db->rollback_transaction();
42     }  else if ($action == 'update-accept-patch') {
43         $group_id = intval($_POST['group']);
44         $repositories_that_accepts_patch = array_get($_POST, 'repositories', array());
45
46         $db->begin_transaction();
47         if (!$db->query_and_get_affected_rows("UPDATE triggerable_repositories SET trigrepo_accepts_patch = FALSE WHERE trigrepo_group = $1", array($group_id))) {
48             notice('Failed to update the accept-patch status.');
49             $db->rollback_transaction();
50         } else {
51             foreach ($repositories_that_accepts_patch as $repository_id) {
52                 if (!$db->query_and_get_affected_rows("UPDATE triggerable_repositories SET trigrepo_accepts_patch = TRUE
53                     WHERE trigrepo_group = $1 AND trigrepo_repository = $2", array($group_id, $repository_id))) {
54                     notice('Failed to update the accept-patch status.');
55                     $db->rollback_transaction();
56                 }
57             }
58             $db->commit_transaction();
59             notice('Updated the accept-patch status.');
60             regenerate_manifest();
61         }
62     } else if ($action == 'add-repository-group') {
63         $triggerable_id = intval($_POST['triggerable']);
64         $name = $_POST['name'];
65
66         $db->begin_transaction();
67         $group_id = $db->insert_row('triggerable_repository_groups', 'repositorygroup', array('name' => $name, 'triggerable' => $triggerable_id));
68         if (!$group_id)
69             notice('Failed to insert the specified repository group.');
70         else if (!insert_triggerable_repositories($db, $group_id, array_get($_POST, 'repositories')))
71             $db->rollback_transaction();
72         else {
73             $db->commit_transaction();
74             notice('Updated the association.');
75             regenerate_manifest();
76         }
77     }
78
79     $repository_rows = $db->fetch_table('repositories', 'repository_name');
80
81     $page = new AdministrativePage($db, 'build_triggerables', 'triggerable', array(
82         'name' => array('editing_mode' => 'string'),
83         'disabled' => array('editing_mode' => 'boolean', 'post_insertion' => TRUE),
84         'repositories' => array(
85             'label' => 'Repository Groups',
86             'subcolumns'=> array('ID', 'Name', 'Description', 'Accepts Roots', 'Repositories', 'Accept patches'),
87             'custom' => function ($triggerable_row) use (&$db, &$repository_rows) {
88                 return generate_repository_list($db, $triggerable_row['triggerable_id'], $repository_rows);
89             }),
90     ));
91
92     $page->render_table('name');
93     $page->render_form_to_add();
94 }
95
96 function insert_triggerable_repositories($db, $group_id, $repositories)
97 {
98     if (!$repositories)
99         return TRUE;
100     foreach ($repositories as $repository_id) {
101         if (!$db->insert_row('triggerable_repositories', 'trigrepo', array('group' => $group_id, 'repository' => $repository_id), NULL)) {
102             notice("Failed to associate repository $repository_id with repository group $group_id.");
103             return FALSE;
104         }
105     }
106     return TRUE;
107 }
108
109
110 function generate_repository_list($db, $triggerable_id, $repository_rows) {
111     $group_forms = array();
112
113     $repository_groups = $db->select_rows('triggerable_repository_groups', 'repositorygroup', array('triggerable' => $triggerable_id), 'name');
114     foreach ($repository_groups as $group_row) {
115         $group_id = $group_row['repositorygroup_id'];
116         $group_name = $group_row['repositorygroup_name'];
117         $group_description = $group_row['repositorygroup_description'];
118         $checked_if_accepts_roots = Database::is_true($group_row['repositorygroup_accepts_roots']) ? 'checked' : '';
119
120         $group_name_form = <<< END
121             <form method="POST">
122             <input type="hidden" name="action" value="update-group-name">
123             <input type="hidden" name="id" value="$group_id">
124             <input type="text" name="name" value="$group_name">
125             </form>
126 END;
127
128         $group_description_form = <<< END
129             <form method="POST">
130             <input type="hidden" name="action" value="update-group-description">
131             <input type="hidden" name="id" value="$group_id">
132             <input name="description" value="$group_description">
133             </form>
134 END;
135
136         $group_accepts_roots = <<< END
137             <form method="POST">
138             <input type="hidden" name="action" value="update-group-accept-roots">
139             <input type="hidden" name="id" value="$group_id">
140             <input type="checkbox" name="accepts" $checked_if_accepts_roots>
141             <button type="submit">Save</button>
142             </form>
143 END;
144
145         $group_repository_rows = $db->select_rows('triggerable_repositories', 'trigrepo', array('group' => $group_id));
146         $repositories_in_group = array();
147         $repositories_that_accepts_patch = array();
148         foreach ($repository_rows as $row)
149             $repositories_in_group[$row['repository_id']] = FALSE;
150         foreach ($group_repository_rows as $row) {
151             $repository_id = $row['trigrepo_repository'];
152             $repositories_in_group[$repository_id] = TRUE;
153             $repositories_that_accepts_patch[$repository_id] = Database::is_true($row['trigrepo_accepts_patch']);
154         }
155
156         array_push($group_forms, array($group_id, $group_name_form, $group_description_form, $group_accepts_roots,
157             generate_repository_form('update-repositories', $group_id, generate_repository_checkboxes($db, $repository_rows, $repositories_in_group)),
158             generate_repository_form('update-accept-patch', $group_id, generate_repository_checkboxes($db, $repository_rows, $repositories_that_accepts_patch)),
159         ));
160     }
161
162     $new_group_checkboxes = generate_repository_checkboxes($db, $repository_rows);
163     $new_group_form = <<< END
164         <form method="POST">
165         <input type="hidden" name="action" value="add-repository-group">
166         <input type="hidden" name="triggerable" value="$triggerable_id">
167         <input type="text" name="name" value="" required><br>
168         $new_group_checkboxes
169         <br><button type="submit">Add</button></form>
170 END;
171
172     array_push($group_forms, $new_group_form);
173
174     return $group_forms;
175 }
176
177 function generate_repository_form($action, $group_id, $checkboxes)
178 {
179     return <<< END
180         <form method="POST">
181         <input type="hidden" name="action" value="$action">
182         <input type="hidden" name="group" value="$group_id">
183         $checkboxes
184         <br><button type="submit">Save</button></form>
185 END;
186 }
187
188 function generate_repository_checkboxes($db, $repository_rows, $selected_repositories = array())
189 {
190     $form = '';
191     foreach ($repository_rows as $row) {
192         $id = $row['repository_id'];
193         if (!array_key_exists($id, $selected_repositories))
194             continue;
195         $name = $row['repository_name'];
196         $checked = $selected_repositories[$id] ? 'checked' : '';
197         $form .= "<label><input type=\"checkbox\" name=\"repositories[]\" value=\"$id\" $checked>$name</label>";
198     }
199     return $form;
200 }
201
202 require('../include/admin-footer.php');
203
204 ?>