Differential D25040 Diff 97 src/applications/drydock/management/DrydockManagementReleaseResourceWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/management/DrydockManagementReleaseResourceWorkflow.php
Show All 9 Lines | $this | ||||
->setArguments( | ->setArguments( | ||||
array( | array( | ||||
array( | array( | ||||
'name' => 'id', | 'name' => 'id', | ||||
'param' => 'id', | 'param' => 'id', | ||||
'repeat' => true, | 'repeat' => true, | ||||
'help' => pht('Resource ID to release.'), | 'help' => pht('Resource ID to release.'), | ||||
), | ), | ||||
array( | |||||
'name' => 'all', | |||||
'help' => pht('Release all resources. Dangerous!'), | |||||
), | |||||
)); | )); | ||||
} | } | ||||
public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
$is_all = $args->getArg('all'); | |||||
$ids = $args->getArg('id'); | $ids = $args->getArg('id'); | ||||
if (!$ids) { | if (!$ids && !$is_all) { | ||||
throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
pht( | pht( | ||||
'Specify one or more resource IDs to release with "%s".', | 'Specify which resources you want to release. See "--help" for '. | ||||
'--id')); | 'guidance.')); | ||||
} | } | ||||
$viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID(); | $statuses = $this->getReleaseableResourceStatuses(); | ||||
$resources = id(new DrydockResourceQuery()) | $query = id(new DrydockResourceQuery()) | ||||
->setViewer($viewer) | ->setViewer($viewer) | ||||
->withIDs($ids) | ->withStatuses(mpull($statuses, 'getKey')); | ||||
->execute(); | |||||
if ($ids) { | |||||
$query->withIDs($ids); | |||||
} | |||||
$resources = $query->execute(); | |||||
if ($ids) { | |||||
$id_map = mpull($resources, null, 'getID'); | |||||
PhabricatorWorker::setRunAllTasksInProcess(true); | |||||
foreach ($ids as $id) { | foreach ($ids as $id) { | ||||
$resource = idx($resources, $id); | $resource = idx($resources, $id); | ||||
if (!$resource) { | if (!$resource) { | ||||
throw new PhutilArgumentUsageException( | |||||
pht('Resource "%s" does not exist.', $id)); | |||||
} | |||||
} | |||||
$resources = array_select_keys($id_map, $ids); | |||||
} | |||||
if (!$resources) { | |||||
echo tsprintf( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht('Resource "%s" does not exist.', $id)); | pht('No resources selected for release.')); | ||||
continue; | |||||
return 0; | |||||
} | } | ||||
$drydock_phid = id(new PhabricatorDrydockApplication())->getPHID(); | |||||
PhabricatorWorker::setRunAllTasksInProcess(true); | |||||
foreach ($resources as $resource) { | |||||
if (!$resource->canRelease()) { | if (!$resource->canRelease()) { | ||||
echo tsprintf( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht('Resource "%s" is not releasable.', $id)); | pht( | ||||
'Resource "%s" is not releasable.', | |||||
$resource->getDisplayName())); | |||||
continue; | continue; | ||||
} | } | ||||
$command = DrydockCommand::initializeNewCommand($viewer) | $command = DrydockCommand::initializeNewCommand($viewer) | ||||
->setTargetPHID($resource->getPHID()) | ->setTargetPHID($resource->getPHID()) | ||||
->setAuthorPHID($drydock_phid) | ->setAuthorPHID($drydock_phid) | ||||
->setCommand(DrydockCommand::COMMAND_RELEASE) | ->setCommand(DrydockCommand::COMMAND_RELEASE) | ||||
->save(); | ->save(); | ||||
$resource->scheduleUpdate(); | $resource->scheduleUpdate(); | ||||
echo tsprintf( | echo tsprintf( | ||||
"%s\n", | "%s\n", | ||||
pht('Scheduled release of resource "%s".', $id)); | pht( | ||||
'Scheduled release of resource "%s".', | |||||
$resource->getDisplayName())); | |||||
} | |||||
return 0; | |||||
} | } | ||||
private function getReleaseableResourceStatuses() { | |||||
$statuses = DrydockResourceStatus::getAllStatuses(); | |||||
foreach ($statuses as $key => $status) { | |||||
$statuses[$key] = DrydockResourceStatus::newStatusObject($status); | |||||
} | } | ||||
foreach ($statuses as $key => $status) { | |||||
if (!$status->canRelease()) { | |||||
unset($statuses[$key]); | |||||
} | |||||
} | |||||
return $statuses; | |||||
} | |||||
} | } |
Content licensed under Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) unless otherwise noted; code licensed under Apache 2.0 or other open source licenses. · CC BY-SA 4.0 · Apache 2.0