Rewrite 'pull-os-versions' script in Javascript to add support for reporting os revis...
[WebKit.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 {
7     $db = new Database;
8     if (!$db->connect())
9         exit_with_error('DatabaseConnectionFailure');
10
11     $report = json_decode($post_data, true);
12
13     verify_slave($db, $report);
14
15     $commits = array_get($report, 'commits', array());
16
17     foreach ($commits as $commit_info) {
18         if (!array_key_exists('repository', $commit_info))
19             exit_with_error('MissingRepositoryName', array('commit' => $commit_info));
20         if (!array_key_exists('revision', $commit_info))
21             exit_with_error('MissingRevision', array('commit' => $commit_info));
22         require_format('Revision', $commit_info['revision'], '/^[A-Za-z0-9 \.]+$/');
23         if (array_key_exists('author', $commit_info) && !is_array($commit_info['author']))
24             exit_with_error('InvalidAuthorFormat', array('commit' => $commit_info));
25     }
26
27     $db->begin_transaction();
28     foreach ($commits as $commit_info) {
29         $repository_id = $db->select_or_insert_row('repositories', 'repository', array('name' => $commit_info['repository'], 'owner' => NULL));
30         if (!$repository_id) {
31             $db->rollback_transaction();
32             exit_with_error('FailedToInsertRepository', array('commit' => $commit_info));
33         }
34         $owner_commit_id = insert_commit($db, $commit_info, $repository_id, NULL);
35         if (!array_key_exists('subCommits', $commit_info))
36             continue;
37
38         foreach($commit_info['subCommits'] as $sub_commit_repository_name => $sub_commit_info) {
39             if (array_key_exists('time', $sub_commit_info)) {
40                 $db->rollback_transaction();
41                 exit_with_error('SubCommitShouldNotContainTimestamp', array('commit' => $sub_commit_info));
42             }
43             $sub_commit_repository_id = $db->select_or_insert_row('repositories', 'repository', array('name' => $sub_commit_repository_name, 'owner' => $repository_id));
44             if (!$sub_commit_repository_id) {
45                 $db->rollback_transaction();
46                 exit_with_error('FailedToInsertRepository', array('commit' => $sub_commit_info));
47             }
48             insert_commit($db, $sub_commit_info, $sub_commit_repository_id, $owner_commit_id);
49         }
50     }
51     $db->commit_transaction();
52
53     exit_with_success();
54 }
55
56 function insert_commit($db, $commit_info, $repository_id, $owner_commit_id)
57 {
58     $author = array_get($commit_info, 'author');
59     $committer_id = NULL;
60     if ($author) {
61         $account = array_get($author, 'account');
62         $committer_query = array('repository' => $repository_id, 'account' => $account);
63         $committer_data = $committer_query;
64         $name = array_get($author, 'name');
65         if ($name)
66             $committer_data['name'] = $name;
67         $committer_id = $db->update_or_insert_row('committers', 'committer', $committer_query, $committer_data);
68         if (!$committer_id) {
69             $db->rollback_transaction();
70             exit_with_error('FailedToInsertCommitter', array('committer' => $committer_data));
71         }
72     }
73
74     $previous_commit_revision = array_get($commit_info, 'previousCommit');
75     $previous_commit_id = NULL;
76     if ($previous_commit_revision) {
77         $previous_commit = $db->select_first_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $previous_commit_revision));
78         if (!$previous_commit) {
79             $db->rollback_transaction();
80             exit_with_error('FailedToFindPreviousCommit', array('commit' => $commit_info));
81         }
82         $previous_commit_id = $previous_commit['commit_id'];
83     }
84
85     $data = array(
86         'repository' => $repository_id,
87         'revision' => $commit_info['revision'],
88         'previous_commit' => $previous_commit_id,
89         'order' => array_get($commit_info, 'order'),
90         'time' => array_get($commit_info, 'time'),
91         'committer' => $committer_id,
92         'message' => array_get($commit_info, 'message'),
93         'reported' => true,
94     );
95     $inserted_commit_id = $db->update_or_insert_row('commits', 'commit', array('repository' => $repository_id, 'revision' => $data['revision']), $data);
96
97     if ($owner_commit_id)
98         $db->select_or_insert_row('commit_ownerships', 'commit', array('owner' => $owner_commit_id, 'owned' => $inserted_commit_id), NULL, '*');
99     return $inserted_commit_id;
100 }
101
102 main($HTTP_RAW_POST_DATA);
103
104 ?>