New perf dashboard should not duplicate author information in each 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_builder($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         if (!ctype_alnum($commit_info['revision']))
22             exit_with_error('InvalidRevision', array('commit' => $commit_info));
23         if (!array_key_exists('time', $commit_info))
24             exit_with_error('MissingTimestamp', array('commit' => $commit_info));
25         if (!array_key_exists('author', $commit_info) || !is_array($commit_info['author']))
26             exit_with_error('MissingAuthorOrInvalidFormat', array('commit' => $commit_info));
27     }
28
29     $db->begin_transaction();
30     foreach ($commits as $commit_info) {
31         $repository_id = $db->select_or_insert_row('repositories', 'repository', array('name' => $commit_info['repository']));
32         if (!$repository_id) {
33             $db->rollback_transaction();
34             exit_with_error('FailedToInsertRepository', array('commit' => $commit_info));
35         }
36
37         $account = array_get($commit_info['author'], 'account');
38         $committer_query = array('repository' => $repository_id, 'account' => $account);
39         $committer_data = $committer_query;
40         $name = array_get($commit_info['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         $parent_revision = array_get($commit_info, 'parent');
50         $parent_id = NULL;
51         if ($parent_revision) {
52             $parent_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $parent_revision));
53             if (!$parent_commit) {
54                 $db->rollback_transaction();
55                 exit_with_error('FailedToFindParentCommit', array('commit' => $commit_info));
56             }
57             $parent_id = $parent_commit['commit_id'];
58         }
59
60         $data = array(
61             'repository' => $repository_id,
62             'revision' => $commit_info['revision'],
63             'parent' => $parent_id,
64             'time' => $commit_info['time'],
65             'committer' => $committer_id,
66             'message' => $commit_info['message'],
67             'reported' => true,
68         );
69         $db->update_or_insert_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $data['revision']), $data);
70     }
71     $db->commit_transaction();
72
73     exit_with_success();
74 }
75
76 function verify_builder($db, $report) {
77     array_key_exists('builderName', $report) or exit_with_error('MissingBuilderName');
78     array_key_exists('builderPassword', $report) or exit_with_error('MissingBuilderPassword');
79
80     $builder_info = array(
81         'name' => $report['builderName'],
82         'password_hash' => hash('sha256', $report['builderPassword'])
83     );
84
85     $matched_builder = $db->select_first_row('builders', 'builder', $builder_info);
86     if (!$matched_builder)
87         exit_with_error('BuilderNotFound', array('name' => $builder_info['name']));
88 }
89
90 main($HTTP_RAW_POST_DATA);
91
92 ?>