Add a bisect button to automatically schedule bisecting A/B tasks.
[WebKit-https.git] / Websites / perf.webkit.org / public / api / build-requests.php
1 <?php
2
3 require_once('../include/json-header.php');
4 require_once('../include/build-requests-fetcher.php');
5
6 function main($id, $path, $post_data) {
7     if (!$id && (count($path) < 1 || count($path) > 2))
8         exit_with_error('InvalidRequest');
9
10     $db = new Database;
11     if (!$db->connect())
12         exit_with_error('DatabaseConnectionFailure');
13
14     $report = $post_data ? json_decode($post_data, true) : array();
15     $updates = array_get($report, 'buildRequestUpdates');
16     if ($updates) {
17         verify_slave($db, $report);
18         update_builds($db, $updates);
19     }
20
21     $requests_fetcher = new BuildRequestsFetcher($db);
22
23     if ($id)
24         $requests_fetcher->fetch_request($id);
25     else {
26         $triggerable_query = array('name' => array_get($path, 0));
27         $triggerable = $db->select_first_row('build_triggerables', 'triggerable', $triggerable_query);
28         if (!$triggerable)
29             exit_with_error('TriggerableNotFound', $triggerable_query);
30         $requests_fetcher->fetch_incomplete_requests_for_triggerable($triggerable['triggerable_id']);
31     }
32
33     $resolve_id = array_get($_GET, 'useLegacyIdResolution');
34     exit_with_success(array(
35         'buildRequests' => $resolve_id ? $requests_fetcher->results_with_resolved_ids() : $requests_fetcher->results(),
36         'commitSets' => $requests_fetcher->commit_sets(),
37         'commits' => $requests_fetcher->commits(),
38         'uploadedFiles' => $requests_fetcher->uploaded_files(),
39     ));
40 }
41
42 function update_builds($db, $updates) {
43     $db->begin_transaction();
44     foreach ($updates as $id => $info) {
45         $id = intval($id);
46         $status = $info['status'];
47         $url = array_get($info, 'url');
48         if ($status == 'failedIfNotCompleted') {
49             $request_row = $db->select_first_row('build_requests', 'request', array('id' => $id));
50             if (!$request_row) {
51                 $db->rollback_transaction();
52                 exit_with_error('FailedToFindBuildRequest', array('buildRequest' => $id));
53             }
54             if ($request_row['request_status'] == 'completed')
55                 continue;
56             $is_build = $request_row['request_order'] < 0;
57             if ($is_build) {
58                 $db->query_and_fetch_all('UPDATE build_requests SET request_status = \'failed\'
59                     WHERE request_group = $1 AND request_order > $2',
60                     array($request_row['request_group'], $request_row['request_order']));
61             }
62             $db->update_row('build_requests', 'request', array('id' => $id), array('status' => 'failed', 'url' => $url));
63         } else {
64             if (!in_array($status, array('pending', 'scheduled', 'running', 'failed', 'completed', 'canceled'))) {
65                 $db->rollback_transaction();
66                 exit_with_error('UnknownBuildRequestStatus', array('buildRequest' => $id, 'status' => $status));
67             }
68             $db->update_row('build_requests', 'request', array('id' => $id), array('status' => $status, 'url' => $url));
69         }
70     }
71     $db->commit_transaction();
72 }
73
74 main(array_get($_GET, 'id'),
75     array_key_exists('PATH_INFO', $_SERVER) ? explode('/', trim($_SERVER['PATH_INFO'], '/')) : array(),
76     file_get_contents("php://input"));
77
78 ?>