6e6c734629bb97bb26cb90981ccafe92d6db7560
[WebKit-https.git] / Websites / perf.webkit.org / public / api / report-commits.php
1 <?php
2
3 require('../include/json-header.php');
4
5 function main($post_data) {
6     $db = new Database;
7     if (!$db->connect())
8         exit_with_error('DatabaseConnectionFailure');
9
10     $report = json_decode($post_data, true);
11
12     verify_slave($db, $report);
13
14     $commits = array_get($report, 'commits', array());
15
16     foreach ($commits as $commit_info) {
17         if (!array_key_exists('repository', $commit_info))
18             exit_with_error('MissingRepositoryName', array('commit' => $commit_info));
19         if (!array_key_exists('revision', $commit_info))
20             exit_with_error('MissingRevision', array('commit' => $commit_info));
21         require_format('Revision', $commit_info['revision'], '/^[A-Za-z0-9 \.]+$/');
22         if (array_key_exists('author', $commit_info) && !is_array($commit_info['author']))
23             exit_with_error('InvalidAuthorFormat', array('commit' => $commit_info));
24     }
25
26     $db->begin_transaction();
27     foreach ($commits as $commit_info) {
28         $repository_id = $db->select_or_insert_row('repositories', 'repository', array('name' => $commit_info['repository']));
29         if (!$repository_id) {
30             $db->rollback_transaction();
31             exit_with_error('FailedToInsertRepository', array('commit' => $commit_info));
32         }
33
34         $author = array_get($commit_info, 'author');
35         $committer_id = NULL;
36         if ($author) {
37             $account = array_get($author, 'account');
38             $committer_query = array('repository' => $repository_id, 'account' => $account);
39             $committer_data = $committer_query;
40             $name = array_get($author, 'name');
41             if ($name)
42                 $committer_data['name'] = $name;
43             $committer_id = $db->update_or_insert_row('committers', 'committer', $committer_query, $committer_data);
44             if (!$committer_id) {
45                 $db->rollback_transaction();
46                 exit_with_error('FailedToInsertCommitter', array('committer' => $committer_data));
47             }
48         }
49
50         $parent_revision = array_get($commit_info, 'parent');
51         $parent_id = NULL;
52         if ($parent_revision) {
53             $parent_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $parent_revision));
54             if (!$parent_commit) {
55                 $db->rollback_transaction();
56                 exit_with_error('FailedToFindParentCommit', array('commit' => $commit_info));
57             }
58             $parent_id = $parent_commit['commit_id'];
59         }
60
61         $data = array(
62             'repository' => $repository_id,
63             'revision' => $commit_info['revision'],
64             'parent' => $parent_id,
65             'time' => array_get($commit_info, 'time'),
66             'committer' => $committer_id,
67             'message' => array_get($commit_info, 'message'),
68             'reported' => true,
69         );
70         $db->update_or_insert_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $data['revision']), $data);
71     }
72     $db->commit_transaction();
73
74     exit_with_success();
75 }
76
77 main($HTTP_RAW_POST_DATA);
78
79 ?>