Differential D21368 Diff 50859 src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php
| Show All 22 Lines | $this | ||||
| array( | array( | ||||
| 'name' => 'silent', | 'name' => 'silent', | ||||
| 'help' => pht('Set the "silent" flag on the request.'), | 'help' => pht('Set the "silent" flag on the request.'), | ||||
| ), | ), | ||||
| array( | array( | ||||
| 'name' => 'secure', | 'name' => 'secure', | ||||
| 'help' => pht('Set the "secure" flag on the request.'), | 'help' => pht('Set the "secure" flag on the request.'), | ||||
| ), | ), | ||||
| array( | |||||
| 'name' => 'count', | |||||
| 'param' => 'N', | |||||
| 'help' => pht('Make a total of __N__ copies of the call.'), | |||||
| ), | |||||
| array( | |||||
| 'name' => 'background', | |||||
| 'help' => pht( | |||||
| 'Instead of making calls in the foreground, add the tasks '. | |||||
| 'to the daemon queue.'), | |||||
| ), | |||||
| )); | )); | ||||
| } | } | ||||
| public function execute(PhutilArgumentParser $args) { | public function execute(PhutilArgumentParser $args) { | ||||
| $viewer = $this->getViewer(); | $viewer = $this->getViewer(); | ||||
| $id = $args->getArg('id'); | $id = $args->getArg('id'); | ||||
| if (!$id) { | if (!$id) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Specify a webhook to call with "--id".')); | 'Specify a webhook to call with "--id".')); | ||||
| } | } | ||||
| $count = $args->getArg('count'); | |||||
| if ($count === null) { | |||||
| $count = 1; | |||||
| } | |||||
| if ($count <= 0) { | |||||
| throw new PhutilArgumentUsageException( | |||||
| pht( | |||||
| 'Specified "--count" must be larger than 0.')); | |||||
| } | |||||
| $hook = id(new HeraldWebhookQuery()) | $hook = id(new HeraldWebhookQuery()) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->withIDs(array($id)) | ->withIDs(array($id)) | ||||
| ->executeOne(); | ->executeOne(); | ||||
| if (!$hook) { | if (!$hook) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Unable to load specified webhook ("%s").', | 'Unable to load specified webhook ("%s").', | ||||
| Show All 12 Lines | if ($object_name === null) { | ||||
| throw new PhutilArgumentUsageException( | throw new PhutilArgumentUsageException( | ||||
| pht( | pht( | ||||
| 'Unable to load specified object ("%s").', | 'Unable to load specified object ("%s").', | ||||
| $object_name)); | $object_name)); | ||||
| } | } | ||||
| $object = head($objects); | $object = head($objects); | ||||
| } | } | ||||
| $is_background = $args->getArg('background'); | |||||
| $xaction_query = | $xaction_query = | ||||
| PhabricatorApplicationTransactionQuery::newQueryForObject($object); | PhabricatorApplicationTransactionQuery::newQueryForObject($object); | ||||
| $xactions = $xaction_query | $xactions = $xaction_query | ||||
| ->withObjectPHIDs(array($object->getPHID())) | ->withObjectPHIDs(array($object->getPHID())) | ||||
| ->setViewer($viewer) | ->setViewer($viewer) | ||||
| ->setLimit(10) | ->setLimit(10) | ||||
| ->execute(); | ->execute(); | ||||
| $application_phid = id(new PhabricatorHeraldApplication())->getPHID(); | $application_phid = id(new PhabricatorHeraldApplication())->getPHID(); | ||||
| if ($is_background) { | |||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| 'Queueing webhook calls...')); | |||||
| $progress_bar = id(new PhutilConsoleProgressBar()) | |||||
| ->setTotal($count); | |||||
| } else { | |||||
| echo tsprintf( | |||||
| "%s\n", | |||||
| pht( | |||||
| 'Calling webhook...')); | |||||
| PhabricatorWorker::setRunAllTasksInProcess(true); | |||||
| } | |||||
| for ($ii = 0; $ii < $count; $ii++) { | |||||
| $request = HeraldWebhookRequest::initializeNewWebhookRequest($hook) | $request = HeraldWebhookRequest::initializeNewWebhookRequest($hook) | ||||
| ->setObjectPHID($object->getPHID()) | ->setObjectPHID($object->getPHID()) | ||||
| ->setIsTestAction(true) | ->setIsTestAction(true) | ||||
| ->setIsSilentAction((bool)$args->getArg('silent')) | ->setIsSilentAction((bool)$args->getArg('silent')) | ||||
| ->setIsSecureAction((bool)$args->getArg('secure')) | ->setIsSecureAction((bool)$args->getArg('secure')) | ||||
| ->setTriggerPHIDs(array($application_phid)) | ->setTriggerPHIDs(array($application_phid)) | ||||
| ->setTransactionPHIDs(mpull($xactions, 'getPHID')) | ->setTransactionPHIDs(mpull($xactions, 'getPHID')) | ||||
| ->save(); | ->save(); | ||||
| PhabricatorWorker::setRunAllTasksInProcess(true); | |||||
| $request->queueCall(); | $request->queueCall(); | ||||
| if ($is_background) { | |||||
| $progress_bar->update(1); | |||||
| } else { | |||||
| $request->reload(); | $request->reload(); | ||||
| echo tsprintf( | echo tsprintf( | ||||
| "%s\n", | "%s\n", | ||||
| pht( | pht( | ||||
| 'Success, got HTTP %s from webhook.', | 'Success, got HTTP %s from webhook.', | ||||
| $request->getErrorCode())); | $request->getErrorCode())); | ||||
| } | |||||
| } | |||||
| if ($is_background) { | |||||
| $progress_bar->done(); | |||||
| } | |||||
| return 0; | return 0; | ||||
| } | } | ||||
| } | } | ||||