Rename 'commit_parent' in 'commits' table to 'commit_previous_commit'.
[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         $previous_commit_revision = array_get($commit_info, 'previousCommit');
51         $previous_commit_id = NULL;
52         if ($previous_commit_revision) {
53             $previous_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $previous_commit_revision));
54             if (!$previous_commit) {
55                 $db->rollback_transaction();
56                 exit_with_error('FailedToFindPreviousCommit', array('commit' => $commit_info));
57             }
58             $previous_commit_id = $previous_commit['commit_id'];
59         }
60
61         $data = array(
62             'repository' => $repository_id,
63             'revision' => $commit_info['revision'],
64             'previous_commit' => $previous_commit_id,
65             'order' => array_get($commit_info, 'order'),
66             'time' => array_get($commit_info, 'time'),
67             'committer' => $committer_id,
68             'message' => array_get($commit_info, 'message'),
69             'reported' => true,
70         );
71         $db->update_or_insert_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $data['revision']), $data);
72     }
73     $db->commit_transaction();
74
75     exit_with_success();
76 }
77
78 main($HTTP_RAW_POST_DATA);
79
80 ?>