@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
at upstream/main 113 lines 3.1 kB view raw
1<?php 2 3final class DiffusionUpdateCoverageConduitAPIMethod 4 extends DiffusionConduitAPIMethod { 5 6 public function getAPIMethodName() { 7 return 'diffusion.updatecoverage'; 8 } 9 10 public function getMethodDescription() { 11 return pht('Publish coverage information for a repository.'); 12 } 13 14 protected function defineReturnType() { 15 return 'void'; 16 } 17 18 protected function defineParamTypes() { 19 $modes = array( 20 'overwrite', 21 'update', 22 ); 23 24 return array( 25 'repositoryPHID' => 'required phid', 26 'branch' => 'required string', 27 'commit' => 'required string', 28 'coverage' => 'required map<string, string>', 29 'mode' => 'optional '.$this->formatStringConstants($modes), 30 ); 31 } 32 33 protected function execute(ConduitAPIRequest $request) { 34 $viewer = $request->getUser(); 35 36 $repository_phid = $request->getValue('repositoryPHID'); 37 $repository = id(new PhabricatorRepositoryQuery()) 38 ->setViewer($viewer) 39 ->withPHIDs(array($repository_phid)) 40 ->executeOne(); 41 42 if (!$repository) { 43 throw new Exception( 44 pht('No repository exists with PHID "%s".', $repository_phid)); 45 } 46 47 $commit_name = $request->getValue('commit'); 48 $commit = id(new DiffusionCommitQuery()) 49 ->setViewer($viewer) 50 ->withRepository($repository) 51 ->withIdentifiers(array($commit_name)) 52 ->executeOne(); 53 if (!$commit) { 54 throw new Exception( 55 pht('No commit exists with identifier "%s".', $commit_name)); 56 } 57 58 $branch = PhabricatorRepositoryBranch::loadOrCreateBranch( 59 $repository->getID(), 60 $request->getValue('branch')); 61 62 $coverage = $request->getValue('coverage'); 63 $path_map = id(new DiffusionPathIDQuery(array_keys($coverage))) 64 ->loadPathIDs(); 65 66 $conn = $repository->establishConnection('w'); 67 68 $sql = array(); 69 foreach ($coverage as $path => $coverage_info) { 70 $sql[] = qsprintf( 71 $conn, 72 '(%d, %d, %d, %s)', 73 $branch->getID(), 74 $path_map[$path], 75 $commit->getID(), 76 $coverage_info); 77 } 78 79 $table_name = 'repository_coverage'; 80 81 $conn->openTransaction(); 82 $mode = $request->getValue('mode'); 83 switch ($mode) { 84 case '': 85 case 'overwrite': 86 // sets the coverage for the whole branch, deleting all previous 87 // coverage information 88 queryfx( 89 $conn, 90 'DELETE FROM %T WHERE branchID = %d', 91 $table_name, 92 $branch->getID()); 93 break; 94 case 'update': 95 // sets the coverage for the provided files on the specified commit 96 break; 97 default: 98 $conn->killTransaction(); 99 throw new Exception(pht('Invalid mode "%s".', $mode)); 100 } 101 102 foreach (PhabricatorLiskDAO::chunkSQL($sql) as $chunk) { 103 queryfx( 104 $conn, 105 'INSERT INTO %T (branchID, pathID, commitID, coverage) VALUES %LQ'. 106 ' ON DUPLICATE KEY UPDATE coverage = VALUES(coverage)', 107 $table_name, 108 $chunk); 109 } 110 $conn->saveTransaction(); 111 } 112 113}