Path: blob/master/src/applications/differential/conduit/DifferentialCreateDiffConduitAPIMethod.php
12256 views
<?php12final class DifferentialCreateDiffConduitAPIMethod3extends DifferentialConduitAPIMethod {45public function getAPIMethodName() {6return 'differential.creatediff';7}89public function getMethodDescription() {10return pht('Create a new Differential diff.');11}1213protected function defineParamTypes() {14$vcs_const = $this->formatStringConstants(15array(16'svn',17'git',18'hg',19));2021$status_const = $this->formatStringConstants(22array(23'none',24'skip',25'okay',26'warn',27'fail',28));2930return array(31'changes' => 'required list<dict>',32'sourceMachine' => 'required string',33'sourcePath' => 'required string',34'branch' => 'required string',35'bookmark' => 'optional string',36'sourceControlSystem' => 'required '.$vcs_const,37'sourceControlPath' => 'required string',38'sourceControlBaseRevision' => 'required string',39'creationMethod' => 'optional string',40'lintStatus' => 'required '.$status_const,41'unitStatus' => 'required '.$status_const,42'repositoryPHID' => 'optional phid',4344'parentRevisionID' => 'deprecated',45'authorPHID' => 'deprecated',46'repositoryUUID' => 'deprecated',47);48}4950protected function defineReturnType() {51return 'nonempty dict';52}5354protected function execute(ConduitAPIRequest $request) {55$viewer = $request->getUser();56$change_data = $request->getValue('changes');57if ($change_data === null) {58throw new Exception(pht('Field "changes" must be non-empty.'));59}6061$changes = array();62foreach ($change_data as $dict) {63$changes[] = ArcanistDiffChange::newFromDictionary($dict);64}6566$diff = DifferentialDiff::newFromRawChanges($viewer, $changes);6768// TODO: Remove repository UUID eventually; for now continue writing69// the UUID. Note that we'll overwrite it below if we identify a70// repository, and `arc` no longer sends it. This stuff is retained for71// backward compatibility.7273$repository_uuid = $request->getValue('repositoryUUID');74$repository_phid = $request->getValue('repositoryPHID');75if ($repository_phid) {76$repository = id(new PhabricatorRepositoryQuery())77->setViewer($viewer)78->withPHIDs(array($repository_phid))79->executeOne();80if ($repository) {81$repository_phid = $repository->getPHID();82$repository_uuid = $repository->getUUID();83}84}8586switch ($request->getValue('lintStatus')) {87case 'skip':88$lint_status = DifferentialLintStatus::LINT_SKIP;89break;90case 'okay':91$lint_status = DifferentialLintStatus::LINT_OKAY;92break;93case 'warn':94$lint_status = DifferentialLintStatus::LINT_WARN;95break;96case 'fail':97$lint_status = DifferentialLintStatus::LINT_FAIL;98break;99case 'none':100default:101$lint_status = DifferentialLintStatus::LINT_NONE;102break;103}104105switch ($request->getValue('unitStatus')) {106case 'skip':107$unit_status = DifferentialUnitStatus::UNIT_SKIP;108break;109case 'okay':110$unit_status = DifferentialUnitStatus::UNIT_OKAY;111break;112case 'warn':113$unit_status = DifferentialUnitStatus::UNIT_WARN;114break;115case 'fail':116$unit_status = DifferentialUnitStatus::UNIT_FAIL;117break;118case 'none':119default:120$unit_status = DifferentialUnitStatus::UNIT_NONE;121break;122}123124$source_path = $request->getValue('sourcePath');125$source_path = $this->normalizeSourcePath($source_path);126127$diff_data_dict = array(128'sourcePath' => $source_path,129'sourceMachine' => $request->getValue('sourceMachine'),130'branch' => $request->getValue('branch'),131'creationMethod' => $request->getValue('creationMethod'),132'authorPHID' => $viewer->getPHID(),133'bookmark' => $request->getValue('bookmark'),134'repositoryUUID' => $repository_uuid,135'repositoryPHID' => $repository_phid,136'sourceControlSystem' => $request->getValue('sourceControlSystem'),137'sourceControlPath' => $request->getValue('sourceControlPath'),138'sourceControlBaseRevision' =>139$request->getValue('sourceControlBaseRevision'),140'lintStatus' => $lint_status,141'unitStatus' => $unit_status,142);143144$xactions = array(145id(new DifferentialDiffTransaction())146->setTransactionType(DifferentialDiffTransaction::TYPE_DIFF_CREATE)147->setNewValue($diff_data_dict),148);149150id(new DifferentialDiffEditor())151->setActor($viewer)152->setContentSource($request->newContentSource())153->setContinueOnNoEffect(true)154->applyTransactions($diff, $xactions);155156$path = '/differential/diff/'.$diff->getID().'/';157$uri = PhabricatorEnv::getURI($path);158159return array(160'diffid' => $diff->getID(),161'phid' => $diff->getPHID(),162'uri' => $uri,163);164}165166private function normalizeSourcePath($source_path) {167// See T13385. This property is probably headed for deletion. Until we get168// there, stop errors arising from running "arc diff" in a working copy169// with too many characters.170171$max_size = id(new DifferentialDiff())172->getColumnMaximumByteLength('sourcePath');173174return id(new PhutilUTF8StringTruncator())175->setMaximumBytes($max_size)176->setTerminator('')177->truncateString($source_path);178}179180}181182183