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