[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorAuditManagementDeleteWorkflow 4 extends PhabricatorAuditManagementWorkflow { 5 6 public function didConstruct() { 7 $this 8 ->setName('delete') 9 ->setExamples('**delete** [--dry-run] ...') 10 ->setSynopsis('Delete audit requests matching parameters.') 11 ->setArguments( 12 array( 13 array( 14 'name' => 'dry-run', 15 'help' => 'Show what would be deleted, but do not actually delete '. 16 'anything.', 17 ), 18 array( 19 'name' => 'users', 20 'param' => 'names', 21 'help' => 'Select only audits by a given list of users.', 22 ), 23 array( 24 'name' => 'repositories', 25 'param' => 'repos', 26 'help' => 'Select only audits in a given list of repositories.', 27 ), 28 array( 29 'name' => 'commits', 30 'param' => 'commits', 31 'help' => 'Select only audits for the given commits.', 32 ), 33 array( 34 'name' => 'min-commit-date', 35 'param' => 'date', 36 'help' => 'Select only audits for commits on or after the given '. 37 'date.', 38 ), 39 array( 40 'name' => 'max-commit-date', 41 'param' => 'date', 42 'help' => 'Select only audits for commits on or before the given '. 43 'date.', 44 ), 45 array( 46 'name' => 'status', 47 'param' => 'status', 48 'help' => 'Select only audits in the given status. By default, '. 49 'only open audits are selected.', 50 ), 51 array( 52 'name' => 'ids', 53 'param' => 'ids', 54 'help' => 'Select only audits with the given IDs.', 55 ), 56 )); 57 } 58 59 public function execute(PhutilArgumentParser $args) { 60 $viewer = $this->getViewer(); 61 $users = $this->loadUsers($args->getArg('users')); 62 $repos = $this->loadRepos($args->getArg('repositories')); 63 $commits = $this->loadCommits($args->getArg('commits')); 64 $ids = $this->parseList($args->getArg('ids')); 65 66 $status = $args->getArg('status'); 67 if (!$status) { 68 $status = DiffusionCommitQuery::AUDIT_STATUS_OPEN; 69 } 70 71 $min_date = $this->loadDate($args->getArg('min-commit-date')); 72 $max_date = $this->loadDate($args->getArg('max-commit-date')); 73 if ($min_date && $max_date && ($min_date > $max_date)) { 74 throw new PhutilArgumentUsageException( 75 'Specified max date must come after specified min date.'); 76 } 77 78 $is_dry_run = $args->getArg('dry-run'); 79 80 $query = id(new DiffusionCommitQuery()) 81 ->setViewer($this->getViewer()) 82 ->needAuditRequests(true); 83 84 if ($status) { 85 $query->withAuditStatus($status); 86 } 87 88 $id_map = array(); 89 if ($ids) { 90 $id_map = array_fuse($ids); 91 $query->withAuditIDs($ids); 92 } 93 94 if ($repos) { 95 $query->withRepositoryIDs(mpull($repos, 'getID')); 96 } 97 98 $auditor_map = array(); 99 if ($users) { 100 $auditor_map = array_fuse(mpull($users, 'getPHID')); 101 $query->withAuditorPHIDs($auditor_map); 102 } 103 104 if ($commits) { 105 $query->withPHIDs(mpull($commits, 'getPHID')); 106 } 107 108 $commits = $query->execute(); 109 $commits = mpull($commits, null, 'getPHID'); 110 $audits = array(); 111 foreach ($commits as $commit) { 112 $commit_audits = $commit->getAudits(); 113 foreach ($commit_audits as $key => $audit) { 114 if ($id_map && empty($id_map[$audit->getID()])) { 115 unset($commit_audits[$key]); 116 continue; 117 } 118 119 if ($auditor_map && empty($auditor_map[$audit->getAuditorPHID()])) { 120 unset($commit_audits[$key]); 121 continue; 122 } 123 124 if ($min_date && $commit->getEpoch() < $min_date) { 125 unset($commit_audits[$key]); 126 continue; 127 } 128 129 if ($max_date && $commit->getEpoch() > $max_date) { 130 unset($commit_audits[$key]); 131 continue; 132 } 133 } 134 $audits[] = $commit_audits; 135 } 136 $audits = array_mergev($audits); 137 138 $console = PhutilConsole::getConsole(); 139 140 if (!$audits) { 141 $console->writeErr("%s\n", pht('No audits match the query.')); 142 return 0; 143 } 144 145 $handles = id(new PhabricatorHandleQuery()) 146 ->setViewer($this->getViewer()) 147 ->withPHIDs(mpull($audits, 'getAuditorPHID')) 148 ->execute(); 149 150 151 foreach ($audits as $audit) { 152 $commit = $commits[$audit->getCommitPHID()]; 153 154 $console->writeOut( 155 "%s\n", 156 sprintf( 157 '%10d %-16s %-16s %s: %s', 158 $audit->getID(), 159 $handles[$audit->getAuditorPHID()]->getName(), 160 PhabricatorAuditStatusConstants::getStatusName( 161 $audit->getAuditStatus()), 162 $commit->getRepository()->formatCommitName( 163 $commit->getCommitIdentifier()), 164 trim($commit->getSummary()))); 165 } 166 167 if (!$is_dry_run) { 168 $message = pht( 169 'Really delete these %d audit(s)? They will be permanently deleted '. 170 'and can not be recovered.', 171 count($audits)); 172 if ($console->confirm($message)) { 173 foreach ($audits as $audit) { 174 $id = $audit->getID(); 175 $console->writeOut("%s\n", pht('Deleting audit %d...', $id)); 176 $audit->delete(); 177 } 178 } 179 } 180 181 return 0; 182 } 183 184 private function loadUsers($users) { 185 $users = $this->parseList($users); 186 if (!$users) { 187 return null; 188 } 189 190 $objects = id(new PhabricatorPeopleQuery()) 191 ->setViewer($this->getViewer()) 192 ->withUsernames($users) 193 ->execute(); 194 $objects = mpull($objects, null, 'getUsername'); 195 196 foreach ($users as $name) { 197 if (empty($objects[$name])) { 198 throw new PhutilArgumentUsageException( 199 pht('No such user with username "%s"!', $name)); 200 } 201 } 202 203 return $objects; 204 } 205 206 private function parseList($list) { 207 $list = preg_split('/\s*,\s*/', $list); 208 209 foreach ($list as $key => $item) { 210 $list[$key] = trim($item); 211 } 212 213 foreach ($list as $key => $item) { 214 if (!strlen($item)) { 215 unset($list[$key]); 216 } 217 } 218 219 return $list; 220 } 221 222 private function loadRepos($callsigns) { 223 $callsigns = $this->parseList($callsigns); 224 if (!$callsigns) { 225 return null; 226 } 227 228 $repos = id(new PhabricatorRepositoryQuery()) 229 ->setViewer($this->getViewer()) 230 ->withCallsigns($callsigns) 231 ->execute(); 232 $repos = mpull($repos, null, 'getCallsign'); 233 234 foreach ($callsigns as $sign) { 235 if (empty($repos[$sign])) { 236 throw new PhutilArgumentUsageException( 237 pht('No such repository with callsign "%s"!', $sign)); 238 } 239 } 240 241 return $repos; 242 } 243 244 private function loadDate($date) { 245 if (!$date) { 246 return null; 247 } 248 249 $epoch = strtotime($date); 250 if (!$epoch || $epoch < 1) { 251 throw new PhutilArgumentUsageException( 252 pht( 253 'Unable to parse date "%s". Use a format like "2000-01-01".', 254 $date)); 255 } 256 257 return $epoch; 258 } 259 260 private function loadCommits($commits) { 261 $names = $this->parseList($commits); 262 if (!$names) { 263 return null; 264 } 265 266 $query = id(new DiffusionCommitQuery()) 267 ->setViewer($this->getViewer()) 268 ->withIdentifiers($names); 269 270 $commits = $query->execute(); 271 272 $map = $query->getIdentifierMap(); 273 foreach ($names as $name) { 274 if (empty($map[$name])) { 275 throw new PhutilArgumentUsageException( 276 pht('No such commit "%s"!', $name)); 277 } 278 } 279 280 return $commits; 281 } 282 283 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |