Perf dashboard should store commit logs
[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         $parent_revision = array_get($commit_info, 'parent');
38         $parent_id = NULL;
39         if ($parent_revision) {
40             $parent_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $parent_revision));
41             if (!$parent_commit) {
42                 $db->rollback_transaction();
43                 exit_with_error('FailedToFindParentCommit', array('commit' => $commit_info));
44             }
45             $parent_id = $parent_commit['commit_id'];
46         }
47
48         $data = array(
49             'repository' => $repository_id,
50             'revision' => $commit_info['revision'],
51             'parent' => $parent_id,
52             'time' => $commit_info['time'],
53             'author_name' => array_get($commit_info['author'], 'name'),
54             'author_email' => array_get($commit_info['author'], 'email'),
55             'message' => $commit_info['message'],
56             'reported' => true,
57         );
58         $db->update_or_insert_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $data['revision']), $data);
59     }
60     $db->commit_transaction();
61
62     exit_with_success();
63 }
64
65 function verify_builder($db, $report) {
66     array_key_exists('builderName', $report) or exit_with_error('MissingBuilderName');
67     array_key_exists('builderPassword', $report) or exit_with_error('MissingBuilderPassword');
68
69     $builder_info = array(
70         'name' => $report['builderName'],
71         'password_hash' => hash('sha256', $report['builderPassword'])
72     );
73
74     $matched_builder = $db->select_first_row('builders', 'builder', $builder_info);
75     if (!$matched_builder)
76         exit_with_error('BuilderNotFound', array('name' => $builder_info['name']));
77 }
78
79 main($HTTP_RAW_POST_DATA);
80
81 ?>
82 <?php
83
84 require('../include/json-header.php');
85
86 function main($post_data) {
87     $db = new Database;
88     if (!$db->connect())
89         exit_with_error('DatabaseConnectionFailure');
90
91     $report = json_decode($post_data, true);
92
93     verify_builder($db, $report);
94
95     $commits = array_get($report, 'commits', array());
96
97     foreach ($commits as $commit_info) {
98         if (!array_key_exists('repository', $commit_info))
99             exit_with_error('MissingRepositoryName', array('commit' => $commit_info));
100         if (!array_key_exists('revision', $commit_info))
101             exit_with_error('MissingRevision', array('commit' => $commit_info));
102         if (!ctype_alnum($commit_info['revision']))
103             exit_with_error('InvalidRevision', array('commit' => $commit_info));
104         if (!array_key_exists('time', $commit_info))
105             exit_with_error('MissingTimestamp', array('commit' => $commit_info));
106         if (!array_key_exists('author', $commit_info) || !is_array($commit_info['author']))
107             exit_with_error('MissingAuthorOrInvalidFormat', array('commit' => $commit_info));
108     }
109
110     $db->begin_transaction();
111     foreach ($commits as $commit_info) {
112         $repository_id = $db->select_or_insert_row('repositories', 'repository', array('name' => $commit_info['repository']));
113         if (!$repository_id) {
114             $db->rollback_transaction();
115             exit_with_error('FailedToInsertRepository', array('commit' => $commit_info));
116         }
117
118         $parent_revision = array_get($commit_info, 'parent');
119         $parent_id = NULL;
120         if ($parent_revision) {
121             $parent_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $parent_revision));
122             if (!$parent_commit) {
123                 $db->rollback_transaction();
124                 exit_with_error('FailedToFindParentCommit', array('commit' => $commit_info));
125             }
126             $parent_id = $parent_commit['commit_id'];
127         }
128
129         $data = array(
130             'repository' => $repository_id,
131             'revision' => $commit_info['revision'],
132             'parent' => $parent_id,
133             'time' => $commit_info['time'],
134             'author_name' => array_get($commit_info['author'], 'name'),
135             'author_email' => array_get($commit_info['author'], 'email'),
136             'message' => $commit_info['message'],
137             'reported' => true,
138         );
139         $db->update_or_insert_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $data['revision']), $data);
140     }
141     $db->commit_transaction();
142
143     exit_with_success();
144 }
145
146 function verify_builder($db, $report) {
147     array_key_exists('builderName', $report) or exit_with_error('MissingBuilderName');
148     array_key_exists('builderPassword', $report) or exit_with_error('MissingBuilderPassword');
149
150     $builder_info = array(
151         'name' => $report['builderName'],
152         'password_hash' => hash('sha256', $report['builderPassword'])
153     );
154
155     $matched_builder = $db->select_first_row('builders', 'builder', $builder_info);
156     if (!$matched_builder)
157         exit_with_error('BuilderNotFound', array('name' => $builder_info['name']));
158 }
159
160 main($HTTP_RAW_POST_DATA);
161
162 ?>