The status of a A/B testing request always eventually becomes "Failed"
[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($path, $post_data) {
7     if (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     $triggerable_query = array('name' => array_get($path, 0));
15     $triggerable = $db->select_first_row('build_triggerables', 'triggerable', $triggerable_query);
16     if (!$triggerable)
17         exit_with_error('TriggerableNotFound', $triggerable_query);
18
19     $report = $post_data ? json_decode($post_data, true) : array();
20     $updates = array_get($report, 'buildRequestUpdates');
21     if ($updates) {
22         verify_slave($db, $report);
23
24         $db->begin_transaction();
25         foreach ($updates as $id => $info) {
26             $id = intval($id);
27             $status = $info['status'];
28             $url = array_get($info, 'url');
29             if ($status == 'failedIfNotCompleted') {
30                 $db->query_and_get_affected_rows('UPDATE build_requests SET (request_status, request_url) = ($1, $2)
31                     WHERE request_id = $3 AND request_status != $4', array('failed', $url, $id, 'completed'));
32             } else {
33                 if (!in_array($status, array('pending', 'scheduled', 'running', 'failed', 'completed'))) {
34                     $db->rollback_transaction();
35                     exit_with_error('UnknownBuildRequestStatus', array('buildRequest' => $id, 'status' => $status));
36                 }
37                 $db->update_row('build_requests', 'request', array('id' => $id), array('status' => $status, 'url' => $url));
38             } 
39         }
40         $db->commit_transaction();
41     }
42
43     $requests_fetcher = new BuildRequestsFetcher($db);
44     $requests_fetcher->fetch_incomplete_requests_for_triggerable($triggerable['triggerable_id']);
45
46     exit_with_success(array(
47         'buildRequests' => $requests_fetcher->results_with_resolved_ids(),
48         'rootSets' => $requests_fetcher->root_sets(),
49         'updates' => $updates,
50     ));
51 }
52
53 main(array_key_exists('PATH_INFO', $_SERVER) ? explode('/', trim($_SERVER['PATH_INFO'], '/')) : array(),
54     file_get_contents("php://input"));
55
56 ?>