Path: blob/master/src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php
12256 views
<?php12final class HeraldWebhookCallManagementWorkflow3extends HeraldWebhookManagementWorkflow {45protected function didConstruct() {6$this7->setName('call')8->setExamples('**call** --id __id__ [--object __object__]')9->setSynopsis(pht('Call a webhook.'))10->setArguments(11array(12array(13'name' => 'id',14'param' => 'id',15'help' => pht('Webhook ID to call'),16),17array(18'name' => 'object',19'param' => 'object',20'help' => pht('Submit transactions for a particular object.'),21),22array(23'name' => 'silent',24'help' => pht('Set the "silent" flag on the request.'),25),26array(27'name' => 'secure',28'help' => pht('Set the "secure" flag on the request.'),29),30array(31'name' => 'count',32'param' => 'N',33'help' => pht('Make a total of __N__ copies of the call.'),34),35array(36'name' => 'background',37'help' => pht(38'Instead of making calls in the foreground, add the tasks '.39'to the daemon queue.'),40),41));42}4344public function execute(PhutilArgumentParser $args) {45$viewer = $this->getViewer();4647$id = $args->getArg('id');48if (!$id) {49throw new PhutilArgumentUsageException(50pht(51'Specify a webhook to call with "--id".'));52}5354$count = $args->getArg('count');55if ($count === null) {56$count = 1;57}5859if ($count <= 0) {60throw new PhutilArgumentUsageException(61pht(62'Specified "--count" must be larger than 0.'));63}6465$hook = id(new HeraldWebhookQuery())66->setViewer($viewer)67->withIDs(array($id))68->executeOne();69if (!$hook) {70throw new PhutilArgumentUsageException(71pht(72'Unable to load specified webhook ("%s").',73$id));74}7576$object_name = $args->getArg('object');77if ($object_name === null) {78$object = $hook;79} else {80$objects = id(new PhabricatorObjectQuery())81->setViewer($viewer)82->withNames(array($object_name))83->execute();84if (!$objects) {85throw new PhutilArgumentUsageException(86pht(87'Unable to load specified object ("%s").',88$object_name));89}90$object = head($objects);91}9293$is_background = $args->getArg('background');9495$xaction_query =96PhabricatorApplicationTransactionQuery::newQueryForObject($object);9798$xactions = $xaction_query99->withObjectPHIDs(array($object->getPHID()))100->setViewer($viewer)101->setLimit(10)102->execute();103104$application_phid = id(new PhabricatorHeraldApplication())->getPHID();105106if ($is_background) {107echo tsprintf(108"%s\n",109pht(110'Queueing webhook calls...'));111$progress_bar = id(new PhutilConsoleProgressBar())112->setTotal($count);113} else {114echo tsprintf(115"%s\n",116pht(117'Calling webhook...'));118PhabricatorWorker::setRunAllTasksInProcess(true);119}120121for ($ii = 0; $ii < $count; $ii++) {122$request = HeraldWebhookRequest::initializeNewWebhookRequest($hook)123->setObjectPHID($object->getPHID())124->setIsTestAction(true)125->setIsSilentAction((bool)$args->getArg('silent'))126->setIsSecureAction((bool)$args->getArg('secure'))127->setTriggerPHIDs(array($application_phid))128->setTransactionPHIDs(mpull($xactions, 'getPHID'))129->save();130131$request->queueCall();132133if ($is_background) {134$progress_bar->update(1);135} else {136$request->reload();137138echo tsprintf(139"%s\n",140pht(141'Success, got HTTP %s from webhook.',142$request->getErrorCode()));143}144}145146if ($is_background) {147$progress_bar->done();148}149150return 0;151}152153}154155156