Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/herald/management/HeraldWebhookCallManagementWorkflow.php
12256 views
1
<?php
2
3
final class HeraldWebhookCallManagementWorkflow
4
extends HeraldWebhookManagementWorkflow {
5
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'),
17
),
18
array(
19
'name' => 'object',
20
'param' => 'object',
21
'help' => pht('Submit transactions for a particular object.'),
22
),
23
array(
24
'name' => 'silent',
25
'help' => pht('Set the "silent" flag on the request.'),
26
),
27
array(
28
'name' => 'secure',
29
'help' => pht('Set the "secure" flag on the request.'),
30
),
31
array(
32
'name' => 'count',
33
'param' => 'N',
34
'help' => pht('Make a total of __N__ copies of the call.'),
35
),
36
array(
37
'name' => 'background',
38
'help' => pht(
39
'Instead of making calls in the foreground, add the tasks '.
40
'to the daemon queue.'),
41
),
42
));
43
}
44
45
public function execute(PhutilArgumentParser $args) {
46
$viewer = $this->getViewer();
47
48
$id = $args->getArg('id');
49
if (!$id) {
50
throw new PhutilArgumentUsageException(
51
pht(
52
'Specify a webhook to call with "--id".'));
53
}
54
55
$count = $args->getArg('count');
56
if ($count === null) {
57
$count = 1;
58
}
59
60
if ($count <= 0) {
61
throw new PhutilArgumentUsageException(
62
pht(
63
'Specified "--count" must be larger than 0.'));
64
}
65
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));
75
}
76
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));
90
}
91
$object = head($objects);
92
}
93
94
$is_background = $args->getArg('background');
95
96
$xaction_query =
97
PhabricatorApplicationTransactionQuery::newQueryForObject($object);
98
99
$xactions = $xaction_query
100
->withObjectPHIDs(array($object->getPHID()))
101
->setViewer($viewer)
102
->setLimit(10)
103
->execute();
104
105
$application_phid = id(new PhabricatorHeraldApplication())->getPHID();
106
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);
120
}
121
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();
131
132
$request->queueCall();
133
134
if ($is_background) {
135
$progress_bar->update(1);
136
} else {
137
$request->reload();
138
139
echo tsprintf(
140
"%s\n",
141
pht(
142
'Success, got HTTP %s from webhook.',
143
$request->getErrorCode()));
144
}
145
}
146
147
if ($is_background) {
148
$progress_bar->done();
149
}
150
151
return 0;
152
}
153
154
}
155
156