Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/conduit/DifferentialCreateInlineConduitAPIMethod.php
12256 views
1
<?php
2
3
final class DifferentialCreateInlineConduitAPIMethod
4
extends DifferentialConduitAPIMethod {
5
6
public function getAPIMethodName() {
7
return 'differential.createinline';
8
}
9
10
public function getMethodDescription() {
11
return pht('Add an inline comment to a Differential revision.');
12
}
13
14
protected function defineParamTypes() {
15
return array(
16
'revisionID' => 'optional revisionid',
17
'diffID' => 'optional diffid',
18
'filePath' => 'required string',
19
'isNewFile' => 'required bool',
20
'lineNumber' => 'required int',
21
'lineLength' => 'optional int',
22
'content' => 'required string',
23
);
24
}
25
26
protected function defineReturnType() {
27
return 'nonempty dict';
28
}
29
30
protected function defineErrorTypes() {
31
return array(
32
'ERR-BAD-REVISION' => pht(
33
'Bad revision ID.'),
34
'ERR-BAD-DIFF' => pht(
35
'Bad diff ID, or diff does not belong to revision.'),
36
'ERR-NEED-DIFF' => pht(
37
'Neither revision ID nor diff ID was provided.'),
38
'ERR-NEED-FILE' => pht(
39
'A file path was not provided.'),
40
'ERR-BAD-FILE' => pht(
41
"Requested file doesn't exist in this revision."),
42
);
43
}
44
45
protected function execute(ConduitAPIRequest $request) {
46
$rid = $request->getValue('revisionID');
47
$did = $request->getValue('diffID');
48
49
if ($rid) {
50
// Given both a revision and a diff, check that they match.
51
// Given only a revision, find the active diff.
52
$revision = id(new DifferentialRevisionQuery())
53
->setViewer($request->getUser())
54
->withIDs(array($rid))
55
->executeOne();
56
if (!$revision) {
57
throw new ConduitException('ERR-BAD-REVISION');
58
}
59
60
if (!$did) { // did not!
61
$diff = $revision->loadActiveDiff();
62
$did = $diff->getID();
63
} else { // did too!
64
$diff = id(new DifferentialDiff())->load($did);
65
if (!$diff || $diff->getRevisionID() != $rid) {
66
throw new ConduitException('ERR-BAD-DIFF');
67
}
68
}
69
} else if ($did) {
70
// Given only a diff, find the parent revision.
71
$diff = id(new DifferentialDiff())->load($did);
72
if (!$diff) {
73
throw new ConduitException('ERR-BAD-DIFF');
74
}
75
$rid = $diff->getRevisionID();
76
} else {
77
// Given neither, bail.
78
throw new ConduitException('ERR-NEED-DIFF');
79
}
80
81
$file = $request->getValue('filePath');
82
if (!$file) {
83
throw new ConduitException('ERR-NEED-FILE');
84
}
85
$changes = id(new DifferentialChangeset())->loadAllWhere(
86
'diffID = %d',
87
$did);
88
$cid = null;
89
foreach ($changes as $id => $change) {
90
if ($file == $change->getFilename()) {
91
$cid = $id;
92
}
93
}
94
if ($cid == null) {
95
throw new ConduitException('ERR-BAD-FILE');
96
}
97
98
$inline = id(new DifferentialInlineComment())
99
->setRevisionID($rid)
100
->setChangesetID($cid)
101
->setAuthorPHID($request->getUser()->getPHID())
102
->setContent($request->getValue('content'))
103
->setIsNewFile((int)$request->getValue('isNewFile'))
104
->setLineNumber($request->getValue('lineNumber'))
105
->setLineLength($request->getValue('lineLength', 0))
106
->save();
107
108
// Load everything again, just to be safe.
109
$changeset = id(new DifferentialChangeset())
110
->load($inline->getChangesetID());
111
return $this->buildInlineInfoDictionary($inline, $changeset);
112
}
113
114
}
115
116