Differential D20127 Diff 48137 src/applications/audit/management/PhabricatorAuditManagementDeleteWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/audit/management/PhabricatorAuditManagementDeleteWorkflow.php
Show First 20 Lines • Show All 99 Lines • ▼ Show 20 Lines | if ($users) { | ||||
$auditor_map = array_fuse(mpull($users, 'getPHID')); | $auditor_map = array_fuse(mpull($users, 'getPHID')); | ||||
$query->withAuditorPHIDs($auditor_map); | $query->withAuditorPHIDs($auditor_map); | ||||
} | } | ||||
if ($commits) { | if ($commits) { | ||||
$query->withPHIDs(mpull($commits, 'getPHID')); | $query->withPHIDs(mpull($commits, 'getPHID')); | ||||
} | } | ||||
$commits = $query->execute(); | $commit_iterator = new PhabricatorQueryIterator($query); | ||||
$commits = mpull($commits, null, 'getPHID'); | |||||
$audits = array(); | $audits = array(); | ||||
foreach ($commits as $commit) { | foreach ($commit_iterator as $commit) { | ||||
$commit_audits = $commit->getAudits(); | $commit_audits = $commit->getAudits(); | ||||
foreach ($commit_audits as $key => $audit) { | foreach ($commit_audits as $key => $audit) { | ||||
if ($id_map && empty($id_map[$audit->getID()])) { | if ($id_map && empty($id_map[$audit->getID()])) { | ||||
unset($commit_audits[$key]); | unset($commit_audits[$key]); | ||||
continue; | continue; | ||||
} | } | ||||
if ($auditor_map && empty($auditor_map[$audit->getAuditorPHID()])) { | if ($auditor_map && empty($auditor_map[$audit->getAuditorPHID()])) { | ||||
unset($commit_audits[$key]); | unset($commit_audits[$key]); | ||||
continue; | continue; | ||||
} | } | ||||
if ($min_date && $commit->getEpoch() < $min_date) { | if ($min_date && $commit->getEpoch() < $min_date) { | ||||
unset($commit_audits[$key]); | unset($commit_audits[$key]); | ||||
continue; | continue; | ||||
} | } | ||||
if ($max_date && $commit->getEpoch() > $max_date) { | if ($max_date && $commit->getEpoch() > $max_date) { | ||||
unset($commit_audits[$key]); | unset($commit_audits[$key]); | ||||
continue; | continue; | ||||
} | } | ||||
} | } | ||||
$audits[] = $commit_audits; | |||||
} | |||||
$audits = array_mergev($audits); | |||||
$console = PhutilConsole::getConsole(); | if (!$commit_audits) { | ||||
continue; | |||||
if (!$audits) { | |||||
$console->writeErr("%s\n", pht('No audits match the query.')); | |||||
return 0; | |||||
} | } | ||||
$handles = id(new PhabricatorHandleQuery()) | $handles = id(new PhabricatorHandleQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($viewer) | ||||
->withPHIDs(mpull($audits, 'getAuditorPHID')) | ->withPHIDs(mpull($commit_audits, 'getAuditorPHID')) | ||||
->execute(); | ->execute(); | ||||
foreach ($commit_audits as $audit) { | |||||
$audit_id = $audit->getID(); | |||||
foreach ($audits as $audit) { | $description = sprintf( | ||||
$commit = $commits[$audit->getCommitPHID()]; | |||||
$console->writeOut( | |||||
"%s\n", | |||||
sprintf( | |||||
'%10d %-16s %-16s %s: %s', | '%10d %-16s %-16s %s: %s', | ||||
$audit->getID(), | $audit_id, | ||||
$handles[$audit->getAuditorPHID()]->getName(), | $handles[$audit->getAuditorPHID()]->getName(), | ||||
PhabricatorAuditStatusConstants::getStatusName( | PhabricatorAuditStatusConstants::getStatusName( | ||||
$audit->getAuditStatus()), | $audit->getAuditStatus()), | ||||
$commit->getRepository()->formatCommitName( | $commit->getRepository()->formatCommitName( | ||||
$commit->getCommitIdentifier()), | $commit->getCommitIdentifier()), | ||||
trim($commit->getSummary()))); | trim($commit->getSummary())); | ||||
$audits[] = array( | |||||
'auditID' => $audit_id, | |||||
'commitPHID' => $commit->getPHID(), | |||||
'description' => $description, | |||||
); | |||||
} | |||||
} | |||||
if (!$audits) { | |||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('No audits match the query.')); | |||||
return 0; | |||||
} | |||||
foreach ($audits as $audit_spec) { | |||||
amckinley: Why not do this with one big query? | |||||
Done Inline ActionsI'm hoping to cheat and sneak under conditions where we explode by using too much memory, if you delete 1M audits with this script or something. At some point, both this and synchronize probably need to be rewritten to use Iterators instead. Maybe I'll just do that now since it sounds like the immediate problems found workarounds. epriestley: I'm hoping to cheat and sneak under conditions where we explode by using too much memory, if… | |||||
echo tsprintf( | |||||
"%s\n", | |||||
$audit_spec['description']); | |||||
} | |||||
if ($is_dry_run) { | |||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('This is a dry run, so no changes will be made.')); | |||||
return 0; | |||||
} | } | ||||
if (!$is_dry_run) { | |||||
$message = pht( | $message = pht( | ||||
'Really delete these %d audit(s)? They will be permanently deleted '. | 'Really delete these %s audit(s)? They will be permanently deleted '. | ||||
'and can not be recovered.', | 'and can not be recovered.', | ||||
count($audits)); | phutil_count($audits)); | ||||
if ($console->confirm($message)) { | if (!phutil_console_confirm($message)) { | ||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('User aborted the workflow.')); | |||||
return 1; | |||||
} | |||||
$audits_by_commit = igroup($audits, 'commitPHID'); | |||||
foreach ($audits_by_commit as $commit_phid => $audit_specs) { | |||||
$audit_ids = ipull($audit_specs, 'auditID'); | |||||
$audits = id(new PhabricatorRepositoryAuditRequest())->loadAllWhere( | |||||
'id IN (%Ld)', | |||||
$audit_ids); | |||||
foreach ($audits as $audit) { | foreach ($audits as $audit) { | ||||
$id = $audit->getID(); | $id = $audit->getID(); | ||||
$console->writeOut("%s\n", pht('Deleting audit %d...', $id)); | |||||
echo tsprintf( | |||||
"%s\n", | |||||
pht('Deleting audit %d...', $id)); | |||||
$audit->delete(); | $audit->delete(); | ||||
} | } | ||||
} | |||||
$this->synchronizeCommitAuditState($commit_phid); | |||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
private function loadUsers($users) { | private function loadUsers($users) { | ||||
$users = $this->parseList($users); | $users = $this->parseList($users); | ||||
if (!$users) { | if (!$users) { | ||||
▲ Show 20 Lines • Show All 99 Lines • Show Last 20 Lines |
Why not do this with one big query?