Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diffusion/conduit/DiffusionUpdateCoverageConduitAPIMethod.php
12242 views
1
<?php
2
3
final class DiffusionUpdateCoverageConduitAPIMethod
4
extends DiffusionConduitAPIMethod {
5
6
public function getAPIMethodName() {
7
return 'diffusion.updatecoverage';
8
}
9
10
public function getMethodStatus() {
11
return self::METHOD_STATUS_UNSTABLE;
12
}
13
14
public function getMethodDescription() {
15
return pht('Publish coverage information for a repository.');
16
}
17
18
protected function defineReturnType() {
19
return 'void';
20
}
21
22
protected function defineParamTypes() {
23
$modes = array(
24
'overwrite',
25
'update',
26
);
27
28
return array(
29
'repositoryPHID' => 'required phid',
30
'branch' => 'required string',
31
'commit' => 'required string',
32
'coverage' => 'required map<string, string>',
33
'mode' => 'optional '.$this->formatStringConstants($modes),
34
);
35
}
36
37
protected function execute(ConduitAPIRequest $request) {
38
$viewer = $request->getUser();
39
40
$repository_phid = $request->getValue('repositoryPHID');
41
$repository = id(new PhabricatorRepositoryQuery())
42
->setViewer($viewer)
43
->withPHIDs(array($repository_phid))
44
->executeOne();
45
46
if (!$repository) {
47
throw new Exception(
48
pht('No repository exists with PHID "%s".', $repository_phid));
49
}
50
51
$commit_name = $request->getValue('commit');
52
$commit = id(new DiffusionCommitQuery())
53
->setViewer($viewer)
54
->withRepository($repository)
55
->withIdentifiers(array($commit_name))
56
->executeOne();
57
if (!$commit) {
58
throw new Exception(
59
pht('No commit exists with identifier "%s".', $commit_name));
60
}
61
62
$branch = PhabricatorRepositoryBranch::loadOrCreateBranch(
63
$repository->getID(),
64
$request->getValue('branch'));
65
66
$coverage = $request->getValue('coverage');
67
$path_map = id(new DiffusionPathIDQuery(array_keys($coverage)))
68
->loadPathIDs();
69
70
$conn = $repository->establishConnection('w');
71
72
$sql = array();
73
foreach ($coverage as $path => $coverage_info) {
74
$sql[] = qsprintf(
75
$conn,
76
'(%d, %d, %d, %s)',
77
$branch->getID(),
78
$path_map[$path],
79
$commit->getID(),
80
$coverage_info);
81
}
82
83
$table_name = 'repository_coverage';
84
85
$conn->openTransaction();
86
$mode = $request->getValue('mode');
87
switch ($mode) {
88
case '':
89
case 'overwrite':
90
// sets the coverage for the whole branch, deleting all previous
91
// coverage information
92
queryfx(
93
$conn,
94
'DELETE FROM %T WHERE branchID = %d',
95
$table_name,
96
$branch->getID());
97
break;
98
case 'update':
99
// sets the coverage for the provided files on the specified commit
100
break;
101
default:
102
$conn->killTransaction();
103
throw new Exception(pht('Invalid mode "%s".', $mode));
104
}
105
106
foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {
107
queryfx(
108
$conn,
109
'INSERT INTO %T (branchID, pathID, commitID, coverage) VALUES %LQ'.
110
' ON DUPLICATE KEY UPDATE coverage = VALUES(coverage)',
111
$table_name,
112
$chunk);
113
}
114
$conn->saveTransaction();
115
}
116
117
}
118
119