Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/herald/management/HeraldTestManagementWorkflow.php
12256 views
1
<?php
2
3
final class HeraldTestManagementWorkflow
4
extends HeraldManagementWorkflow {
5
6
protected function didConstruct() {
7
$this
8
->setName('test')
9
->setExamples('**test** --object __object__ --type __type__')
10
->setSynopsis(
11
pht(
12
'Test content rules for an object. Executes a dry run, like the '.
13
'web UI test console.'))
14
->setArguments(
15
array(
16
array(
17
'name' => 'object',
18
'param' => 'object',
19
'help' => pht('Run rules on this object.'),
20
),
21
array(
22
'name' => 'type',
23
'param' => 'type',
24
'help' => pht('Run rules for this content type.'),
25
),
26
));
27
}
28
29
public function execute(PhutilArgumentParser $args) {
30
$viewer = $this->getViewer();
31
32
$object_name = $args->getArg('object');
33
if (!strlen($object_name)) {
34
throw new PhutilArgumentUsageException(
35
pht('Specify an object to test rules for with "--object".'));
36
}
37
38
$objects = id(new PhabricatorObjectQuery())
39
->setViewer($viewer)
40
->withNames(array($object_name))
41
->execute();
42
if (!$objects) {
43
throw new PhutilArgumentUsageException(
44
pht(
45
'Unable to load specified object ("%s").',
46
$object_name));
47
}
48
$object = head($objects);
49
50
$adapters = HeraldAdapter::getAllAdapters();
51
52
$can_select = array();
53
$display_adapters = array();
54
foreach ($adapters as $key => $adapter) {
55
if (!$adapter->isTestAdapterForObject($object)) {
56
continue;
57
}
58
59
if (!$adapter->isAvailableToUser($viewer)) {
60
continue;
61
}
62
63
$display_adapters[$key] = $adapter;
64
65
if ($adapter->canCreateTestAdapterForObject($object)) {
66
$can_select[$key] = $adapter;
67
}
68
}
69
70
71
$content_type = $args->getArg('type');
72
if (!strlen($content_type)) {
73
throw new PhutilArgumentUsageException(
74
pht(
75
'Specify a content type to run rules for. For this object, valid '.
76
'content types are: %s.',
77
implode(', ', array_keys($can_select))));
78
}
79
80
if (!isset($can_select[$content_type])) {
81
if (!isset($display_adapters[$content_type])) {
82
throw new PhutilArgumentUsageException(
83
pht(
84
'Specify a content type to run rules for. The specified content '.
85
'type ("%s") is not valid. For this object, valid content types '.
86
'are: %s.',
87
$content_type,
88
implode(', ', array_keys($can_select))));
89
} else {
90
throw new PhutilArgumentUsageException(
91
pht(
92
'The specified content type ("%s") does not support dry runs. '.
93
'Choose a testable content type. For this object, valid content '.
94
'types are: %s.',
95
$content_type,
96
implode(', ', array_keys($can_select))));
97
}
98
}
99
100
$adapter = $can_select[$content_type]->newTestAdapter(
101
$viewer,
102
$object);
103
104
$content_source = $this->newContentSource();
105
106
$adapter
107
->setContentSource($content_source)
108
->setIsNewObject(false)
109
->setViewer($viewer);
110
111
$rules = id(new HeraldRuleQuery())
112
->setViewer($viewer)
113
->withContentTypes(array($adapter->getAdapterContentType()))
114
->withDisabled(false)
115
->needConditionsAndActions(true)
116
->needAppliedToPHIDs(array($object->getPHID()))
117
->needValidateAuthors(true)
118
->execute();
119
120
$engine = id(new HeraldEngine())
121
->setDryRun(true);
122
123
$effects = $engine->applyRules($rules, $adapter);
124
$engine->applyEffects($effects, $adapter, $rules);
125
126
$xscript = $engine->getTranscript();
127
128
$uri = '/herald/transcript/'.$xscript->getID().'/';
129
$uri = PhabricatorEnv::getProductionURI($uri);
130
131
echo tsprintf(
132
"%s\n\n __%s__\n\n",
133
pht('Test run complete. Transcript:'),
134
$uri);
135
136
return 0;
137
}
138
139
}
140
141