Path: blob/master/src/applications/diffusion/conduit/DiffusionUpdateCoverageConduitAPIMethod.php
12242 views
<?php12final class DiffusionUpdateCoverageConduitAPIMethod3extends DiffusionConduitAPIMethod {45public function getAPIMethodName() {6return 'diffusion.updatecoverage';7}89public function getMethodStatus() {10return self::METHOD_STATUS_UNSTABLE;11}1213public function getMethodDescription() {14return pht('Publish coverage information for a repository.');15}1617protected function defineReturnType() {18return 'void';19}2021protected function defineParamTypes() {22$modes = array(23'overwrite',24'update',25);2627return array(28'repositoryPHID' => 'required phid',29'branch' => 'required string',30'commit' => 'required string',31'coverage' => 'required map<string, string>',32'mode' => 'optional '.$this->formatStringConstants($modes),33);34}3536protected function execute(ConduitAPIRequest $request) {37$viewer = $request->getUser();3839$repository_phid = $request->getValue('repositoryPHID');40$repository = id(new PhabricatorRepositoryQuery())41->setViewer($viewer)42->withPHIDs(array($repository_phid))43->executeOne();4445if (!$repository) {46throw new Exception(47pht('No repository exists with PHID "%s".', $repository_phid));48}4950$commit_name = $request->getValue('commit');51$commit = id(new DiffusionCommitQuery())52->setViewer($viewer)53->withRepository($repository)54->withIdentifiers(array($commit_name))55->executeOne();56if (!$commit) {57throw new Exception(58pht('No commit exists with identifier "%s".', $commit_name));59}6061$branch = PhabricatorRepositoryBranch::loadOrCreateBranch(62$repository->getID(),63$request->getValue('branch'));6465$coverage = $request->getValue('coverage');66$path_map = id(new DiffusionPathIDQuery(array_keys($coverage)))67->loadPathIDs();6869$conn = $repository->establishConnection('w');7071$sql = array();72foreach ($coverage as $path => $coverage_info) {73$sql[] = qsprintf(74$conn,75'(%d, %d, %d, %s)',76$branch->getID(),77$path_map[$path],78$commit->getID(),79$coverage_info);80}8182$table_name = 'repository_coverage';8384$conn->openTransaction();85$mode = $request->getValue('mode');86switch ($mode) {87case '':88case 'overwrite':89// sets the coverage for the whole branch, deleting all previous90// coverage information91queryfx(92$conn,93'DELETE FROM %T WHERE branchID = %d',94$table_name,95$branch->getID());96break;97case 'update':98// sets the coverage for the provided files on the specified commit99break;100default:101$conn->killTransaction();102throw new Exception(pht('Invalid mode "%s".', $mode));103}104105foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) {106queryfx(107$conn,108'INSERT INTO %T (branchID, pathID, commitID, coverage) VALUES %LQ'.109' ON DUPLICATE KEY UPDATE coverage = VALUES(coverage)',110$table_name,111$chunk);112}113$conn->saveTransaction();114}115116}117118119