Analysis task should look for a git commit based on abridged hashes
[WebKit-https.git] / Websites / perf.webkit.org / public / privileged-api / associate-commit.php
1 <?php
2
3 require_once('../include/json-header.php');
4
5 function main() {
6     $data = ensure_privileged_api_data_and_token();
7
8     $analysis_task_id = array_get($data, 'task');
9     $repository_id = array_get($data, 'repository');
10     $revision = array_get($data, 'revision');
11     $kind = array_get($data, 'kind');
12     $commit_id_to_diassociate = array_get($data, 'commit');
13
14     $db = connect();
15     $db->begin_transaction();
16
17     require_format('AnalysisTask', $analysis_task_id, '/^\d+$/');
18     if ($commit_id_to_diassociate) {
19         require_format('Commit', $commit_id_to_diassociate, '/^\d*$/');
20
21         $count = $db->query_and_get_affected_rows("DELETE FROM task_commits WHERE taskcommit_task = $1 AND taskcommit_commit = $2",
22             array($analysis_task_id, $commit_id_to_diassociate));
23         if ($count != 1) {
24             $db->rollback_transaction();
25             exit_with_error('UnexpectedNumberOfAffectedRows', array('affectedRows' => $count));
26         }
27     } else {
28         require_format('Repository', $repository_id, '/^\d+$/');
29         require_format('Kind', $kind, '/^(cause|fix)$/');
30
31         $commit_info = array('repository' => $repository_id, 'revision' => $revision);
32         $commit_rows = $db->query_and_fetch_all('SELECT commit_id FROM commits WHERE commit_repository = $1 AND commit_revision LIKE $2 LIMIT 2',
33             array($repository_id, '%' . Database::escape_for_like($revision) . '%'));
34         if (count($commit_rows) > 1) {
35             $db->rollback_transaction();
36             exit_with_error('AmbiguousRevision', $commit_info);            
37         } else if (!$commit_rows) {
38             $db->rollback_transaction();
39             exit_with_error('CommitNotFound', $commit_info);
40         }
41
42         $commit_id = $commit_rows[0]['commit_id'];
43
44         $association = array('task' => $analysis_task_id, 'commit' => $commit_id, 'is_fix' => Database::to_database_boolean($kind == 'fix'));
45         $commit_id = $db->update_or_insert_row('task_commits', 'taskcommit',
46             array('task' => $analysis_task_id, 'commit' => $commit_id), $association, 'commit');
47         if (!$commit_id) {
48             $db->rollback_transaction();
49             exit_with_error('FailedToAssociateCommit', $association);
50         }
51     }
52
53     $db->commit_transaction();
54
55     exit_with_success();
56 }
57
58 main();
59
60 ?>