Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/conduit/DifferentialCreateRawDiffConduitAPIMethod.php
12256 views
1
<?php
2
3
final class DifferentialCreateRawDiffConduitAPIMethod
4
extends DifferentialConduitAPIMethod {
5
6
public function getAPIMethodName() {
7
return 'differential.createrawdiff';
8
}
9
10
public function getMethodDescription() {
11
return pht('Create a new Differential diff from a raw diff source.');
12
}
13
14
protected function defineParamTypes() {
15
return array(
16
'diff' => 'required string',
17
'repositoryPHID' => 'optional string',
18
'viewPolicy' => 'optional string',
19
);
20
}
21
22
protected function defineReturnType() {
23
return 'nonempty dict';
24
}
25
26
protected function execute(ConduitAPIRequest $request) {
27
$viewer = $request->getUser();
28
$raw_diff = $request->getValue('diff');
29
if ($raw_diff === null || !strlen($raw_diff)) {
30
throw new Exception(pht('Field "raw_diff" must be non-empty.'));
31
}
32
33
$repository_phid = $request->getValue('repositoryPHID');
34
if ($repository_phid) {
35
$repository = id(new PhabricatorRepositoryQuery())
36
->setViewer($viewer)
37
->withPHIDs(array($repository_phid))
38
->executeOne();
39
if (!$repository) {
40
throw new Exception(
41
pht('No such repository "%s"!', $repository_phid));
42
}
43
}
44
45
$parser = new ArcanistDiffParser();
46
$changes = $parser->parseDiff($raw_diff);
47
$diff = DifferentialDiff::newFromRawChanges($viewer, $changes);
48
49
// We're bounded by doing INSERTs for all the hunks and changesets, so
50
// estimate the number of inserts we'll require.
51
$size = 0;
52
foreach ($diff->getChangesets() as $changeset) {
53
$hunks = $changeset->getHunks();
54
$size += 1 + count($hunks);
55
}
56
57
$raw_limit = 10000;
58
if ($size > $raw_limit) {
59
throw new Exception(
60
pht(
61
'The raw diff you have submitted is too large to parse (it affects '.
62
'more than %s paths and hunks).',
63
new PhutilNumber($raw_limit)));
64
}
65
66
$diff_data_dict = array(
67
'creationMethod' => 'web',
68
'authorPHID' => $viewer->getPHID(),
69
'repositoryPHID' => $repository_phid,
70
'lintStatus' => DifferentialLintStatus::LINT_SKIP,
71
'unitStatus' => DifferentialUnitStatus::UNIT_SKIP,
72
);
73
74
$xactions = array(
75
id(new DifferentialDiffTransaction())
76
->setTransactionType(DifferentialDiffTransaction::TYPE_DIFF_CREATE)
77
->setNewValue($diff_data_dict),
78
);
79
80
if ($request->getValue('viewPolicy')) {
81
$xactions[] = id(new DifferentialDiffTransaction())
82
->setTransactionType(PhabricatorTransactions::TYPE_VIEW_POLICY)
83
->setNewValue($request->getValue('viewPolicy'));
84
}
85
86
id(new DifferentialDiffEditor())
87
->setActor($viewer)
88
->setContentSource($request->newContentSource())
89
->setContinueOnNoEffect(true)
90
->setLookupRepository(false) // respect user choice
91
->applyTransactions($diff, $xactions);
92
93
return $this->buildDiffInfoDictionary($diff);
94
}
95
96
}
97
98