Remove superfluously duplicated code in public/api/report-commits.php.
[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 ?>