Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/differential/conduit/DifferentialCreateDiffConduitAPIMethod.php
12256 views
1
<?php
2
3
final class DifferentialCreateDiffConduitAPIMethod
4
extends DifferentialConduitAPIMethod {
5
6
public function getAPIMethodName() {
7
return 'differential.creatediff';
8
}
9
10
public function getMethodDescription() {
11
return pht('Create a new Differential diff.');
12
}
13
14
protected function defineParamTypes() {
15
$vcs_const = $this->formatStringConstants(
16
array(
17
'svn',
18
'git',
19
'hg',
20
));
21
22
$status_const = $this->formatStringConstants(
23
array(
24
'none',
25
'skip',
26
'okay',
27
'warn',
28
'fail',
29
));
30
31
return array(
32
'changes' => 'required list<dict>',
33
'sourceMachine' => 'required string',
34
'sourcePath' => 'required string',
35
'branch' => 'required string',
36
'bookmark' => 'optional string',
37
'sourceControlSystem' => 'required '.$vcs_const,
38
'sourceControlPath' => 'required string',
39
'sourceControlBaseRevision' => 'required string',
40
'creationMethod' => 'optional string',
41
'lintStatus' => 'required '.$status_const,
42
'unitStatus' => 'required '.$status_const,
43
'repositoryPHID' => 'optional phid',
44
45
'parentRevisionID' => 'deprecated',
46
'authorPHID' => 'deprecated',
47
'repositoryUUID' => 'deprecated',
48
);
49
}
50
51
protected function defineReturnType() {
52
return 'nonempty dict';
53
}
54
55
protected function execute(ConduitAPIRequest $request) {
56
$viewer = $request->getUser();
57
$change_data = $request->getValue('changes');
58
if ($change_data === null) {
59
throw new Exception(pht('Field "changes" must be non-empty.'));
60
}
61
62
$changes = array();
63
foreach ($change_data as $dict) {
64
$changes[] = ArcanistDiffChange::newFromDictionary($dict);
65
}
66
67
$diff = DifferentialDiff::newFromRawChanges($viewer, $changes);
68
69
// TODO: Remove repository UUID eventually; for now continue writing
70
// the UUID. Note that we'll overwrite it below if we identify a
71
// repository, and `arc` no longer sends it. This stuff is retained for
72
// backward compatibility.
73
74
$repository_uuid = $request->getValue('repositoryUUID');
75
$repository_phid = $request->getValue('repositoryPHID');
76
if ($repository_phid) {
77
$repository = id(new PhabricatorRepositoryQuery())
78
->setViewer($viewer)
79
->withPHIDs(array($repository_phid))
80
->executeOne();
81
if ($repository) {
82
$repository_phid = $repository->getPHID();
83
$repository_uuid = $repository->getUUID();
84
}
85
}
86
87
switch ($request->getValue('lintStatus')) {
88
case 'skip':
89
$lint_status = DifferentialLintStatus::LINT_SKIP;
90
break;
91
case 'okay':
92
$lint_status = DifferentialLintStatus::LINT_OKAY;
93
break;
94
case 'warn':
95
$lint_status = DifferentialLintStatus::LINT_WARN;
96
break;
97
case 'fail':
98
$lint_status = DifferentialLintStatus::LINT_FAIL;
99
break;
100
case 'none':
101
default:
102
$lint_status = DifferentialLintStatus::LINT_NONE;
103
break;
104
}
105
106
switch ($request->getValue('unitStatus')) {
107
case 'skip':
108
$unit_status = DifferentialUnitStatus::UNIT_SKIP;
109
break;
110
case 'okay':
111
$unit_status = DifferentialUnitStatus::UNIT_OKAY;
112
break;
113
case 'warn':
114
$unit_status = DifferentialUnitStatus::UNIT_WARN;
115
break;
116
case 'fail':
117
$unit_status = DifferentialUnitStatus::UNIT_FAIL;
118
break;
119
case 'none':
120
default:
121
$unit_status = DifferentialUnitStatus::UNIT_NONE;
122
break;
123
}
124
125
$source_path = $request->getValue('sourcePath');
126
$source_path = $this->normalizeSourcePath($source_path);
127
128
$diff_data_dict = array(
129
'sourcePath' => $source_path,
130
'sourceMachine' => $request->getValue('sourceMachine'),
131
'branch' => $request->getValue('branch'),
132
'creationMethod' => $request->getValue('creationMethod'),
133
'authorPHID' => $viewer->getPHID(),
134
'bookmark' => $request->getValue('bookmark'),
135
'repositoryUUID' => $repository_uuid,
136
'repositoryPHID' => $repository_phid,
137
'sourceControlSystem' => $request->getValue('sourceControlSystem'),
138
'sourceControlPath' => $request->getValue('sourceControlPath'),
139
'sourceControlBaseRevision' =>
140
$request->getValue('sourceControlBaseRevision'),
141
'lintStatus' => $lint_status,
142
'unitStatus' => $unit_status,
143
);
144
145
$xactions = array(
146
id(new DifferentialDiffTransaction())
147
->setTransactionType(DifferentialDiffTransaction::TYPE_DIFF_CREATE)
148
->setNewValue($diff_data_dict),
149
);
150
151
id(new DifferentialDiffEditor())
152
->setActor($viewer)
153
->setContentSource($request->newContentSource())
154
->setContinueOnNoEffect(true)
155
->applyTransactions($diff, $xactions);
156
157
$path = '/differential/diff/'.$diff->getID().'/';
158
$uri = PhabricatorEnv::getURI($path);
159
160
return array(
161
'diffid' => $diff->getID(),
162
'phid' => $diff->getPHID(),
163
'uri' => $uri,
164
);
165
}
166
167
private function normalizeSourcePath($source_path) {
168
// See T13385. This property is probably headed for deletion. Until we get
169
// there, stop errors arising from running "arc diff" in a working copy
170
// with too many characters.
171
172
$max_size = id(new DifferentialDiff())
173
->getColumnMaximumByteLength('sourcePath');
174
175
return id(new PhutilUTF8StringTruncator())
176
->setMaximumBytes($max_size)
177
->setTerminator('')
178
->truncateString($source_path);
179
}
180
181
}
182
183