-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathExecutor.php
More file actions
303 lines (253 loc) · 8.92 KB
/
Copy pathExecutor.php
File metadata and controls
303 lines (253 loc) · 8.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
<?php
namespace PHPCensor\Plugin\Util;
use Exception;
use PHPCensor\Common\Exception\RuntimeException;
use PHPCensor\Helper\Lang;
use PHPCensor\Logging\BuildLogger;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
use PHPCensor\Store\BuildStore;
use PHPCensor\StoreRegistry;
/**
* Plugin Executor - Runs the configured plugins for a given build stage.
*
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class Executor
{
/**
* @var BuildLogger
*/
protected $logger;
/**
* @var Factory
*/
protected $pluginFactory;
/**
* @var BuildStore
*/
protected $store;
protected StoreRegistry $storeRegistry;
public function __construct(
StoreRegistry $storeRegistry,
Factory $pluginFactory,
BuildLogger $logger,
BuildStore $store = null
) {
$this->storeRegistry = $storeRegistry;
$this->pluginFactory = $pluginFactory;
$this->logger = $logger;
$this->store = $store;
}
/**
* Execute a the appropriate set of plugins for a given build stage.
*
* @param array $config Configuration
* @param string $stage
*
* @return bool
*/
public function executePlugins($config, $stage)
{
$success = true;
$pluginsToExecute = [];
// If we have global plugins to execute for this stage, add them to the list to be executed:
if (\array_key_exists($stage, $config) && \is_array($config[$stage])) {
$pluginsToExecute[] = $config[$stage];
}
$pluginsToExecute = $this->getBranchSpecificPlugins($config, $stage, $pluginsToExecute);
foreach ($pluginsToExecute as $pluginSet) {
if (!$this->doExecutePlugins($pluginSet, $stage)) {
$success = false;
}
}
return $success;
}
/**
* @param array $config
* @param string $branch
*
* @return array|bool
*/
public function getBranchSpecificConfig($config, $branch)
{
$configSections = \array_keys($config);
foreach ($configSections as $configSection) {
if (0 === \strpos($configSection, 'branch-')) {
if ($configSection === ('branch-' . $branch)) {
return $config[$configSection];
}
if (0 === \strpos($configSection, 'branch-regex:')) {
$pattern = '#' . \substr($configSection, 13) . '#u';
\preg_match($pattern, $branch, $matches);
if (!empty($matches[0])) {
return $config[$configSection];
}
}
}
}
return [];
}
/**
* Check the config for any plugins specific to the branch we're currently building.
*
* @param array $config
* @param string $stage
* @param array $pluginsToExecute
*
* @return array
*/
protected function getBranchSpecificPlugins($config, $stage, $pluginsToExecute)
{
$build = $this->pluginFactory->getBuild();
$branch = $build->getBranch();
$branchConfig = $this->getBranchSpecificConfig($config, $branch);
if (!$branchConfig) {
return $pluginsToExecute;
}
$plugins = !empty($branchConfig[$stage]) ? $branchConfig[$stage] : [];
$runOption = 'after';
if (!empty($branchConfig['run-option'])) {
$runOption = $branchConfig['run-option'];
}
switch ($runOption) {
// Replace standard plugin set for this stage with just the branch-specific ones:
case 'replace':
$pluginsToExecute = [];
$pluginsToExecute[] = $plugins;
break;
// Run branch-specific plugins before standard plugins:
case 'before':
\array_unshift($pluginsToExecute, $plugins);
break;
// Run branch-specific plugins after standard plugins:
case 'after':
default:
\array_push($pluginsToExecute, $plugins);
break;
}
return $pluginsToExecute;
}
/**
* Execute the list of plugins found for a given testing stage.
* @return bool
* @throws Exception
*/
protected function doExecutePlugins($plugins, $stage)
{
$success = true;
foreach ($plugins as $step => $options) {
$plugin = $step;
if (isset($options['plugin'])) {
$plugin = $options['plugin'];
}
$this->logger->log('');
$this->logger->logSuccess(
\sprintf('RUNNING PLUGIN: %s (Step: %s) (Stage: %s)', Lang::get($plugin), $step, \ucfirst($stage))
);
$this->setPluginStatus($stage, $step, $plugin, Plugin::STATUS_RUNNING);
// Try and execute it
if ($this->executePlugin($plugin, $options)) {
// Execution was successful
$this->logger->logSuccess('PLUGIN: SUCCESS');
$this->setPluginStatus($stage, $step, $plugin, Plugin::STATUS_SUCCESS);
} else {
$status = Plugin::STATUS_FAILED;
if ($stage === Build::STAGE_SETUP) {
$this->logger->logFailure('PLUGIN: FAILED');
// If we're in the "setup" stage, execution should not continue after
// a plugin has failed:
throw new RuntimeException('Plugin failed: ' . $plugin . ' (Step: ' . $step . ')');
} elseif ($stage === Build::STAGE_DEPLOY) {
$this->logger->logFailure('PLUGIN: FAILED');
$success = false;
} else {
// If we're in the "test" stage and the plugin is not allowed to fail,
// then mark the build as failed:
if (empty($options['allow_failures']) && $stage === Build::STAGE_TEST) {
$this->logger->logFailure('PLUGIN: FAILED');
$success = false;
} else {
$status = Plugin::STATUS_FAILED_ALLOWED;
$this->logger->logFailure('PLUGIN: FAILED (ALLOWED)');
}
}
$this->setPluginStatus($stage, $step, $plugin, $status);
}
}
return $success;
}
/**
* Executes a given plugin, with options and returns the result.
*/
public function executePlugin($plugin, $options)
{
$class = $plugin;
if (!\class_exists($class)) {
$class = \str_replace('_', ' ', $plugin);
$class = \ucwords($class);
$class = 'PHPCensor\Plugin\\' . \str_replace(' ', '', $class);
if (!\class_exists($class)) {
$this->logger->logFailure(\sprintf('Plugin does not exist: %s', $plugin));
return false;
}
}
try {
// Build and run it
$obj = $this->pluginFactory->buildPlugin($class, (\is_null($options) ? [] : $options));
$obj->setStoreRegistry($this->storeRegistry);
return $obj->execute();
} catch (\Throwable $ex) {
$this->logger->logFailure('Exception: ' . $ex->getMessage(), $ex);
return false;
}
}
/**
* Change the status of a plugin for a given stage.
*
* @param string $stage The builder stage.
* @param string $step The name of the step
* @param string $plugin The plugin name.
* @param int $status The new status.
*/
protected function setPluginStatus($stage, $step, $plugin, $status)
{
$summary = $this->getBuildSummary();
if (!isset($summary[$stage][$step])) {
$summary[$stage][$step] = [
'plugin' => $plugin
];
}
$summary[$stage][$step]['status'] = $status;
if ($status === Plugin::STATUS_RUNNING) {
$summary[$stage][$step]['started'] = \time();
} elseif ($status >= Plugin::STATUS_SUCCESS) {
$summary[$stage][$step]['ended'] = \time();
}
$this->setBuildSummary($summary);
}
/**
* Fetch the summary data of the current build.
*
* @return array
*/
private function getBuildSummary()
{
$build = $this->pluginFactory->getBuild();
$metas = $this->store->getMeta('plugin-summary', $build->getProjectId(), $build->getId());
return isset($metas[0]['meta_value']) ? $metas[0]['meta_value'] : [];
}
/**
* Sets the summary data of the current build.
*
* @param array $summary
*/
private function setBuildSummary($summary)
{
$build = $this->pluginFactory->getBuild();
$this->store->setMeta($build->getId(), 'plugin-summary', \json_encode($summary));
}
}