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