This repository has been archived by the owner on Jan 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
tester.php
324 lines (290 loc) · 10.6 KB
/
tester.php
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
<?php
require_once __DIR__.'/app/autoload.php';
// require_once __DIR__.'/app/bootstrap.php.cache';
require_once __DIR__.'/app/AppKernel.php';
require __DIR__.'/vendor/autoload.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\BufferedOutput;
class MauticPatchTester
{
public $localfile = 'patch-to-apply.patch';
public $url = 'https://patch-diff.githubusercontent.com/raw/mautic/mautic/pull/';
public $testerfilename;
public function __construct()
{
$this->testerfilename = basename(__FILE__);
$allowedTasks = array(
'apply',
'remove'
);
$task = isset($_REQUEST['task']) ? $_REQUEST['task'] : '';
$patch = isset($_REQUEST['patch']) ? $_REQUEST['patch'] : '';
$cmd = isset($_REQUEST['cmd']) ? $_REQUEST['cmd'] : '';
// Controller
if (in_array($task, $allowedTasks)) {
@set_time_limit(9999);
if(empty($cmd)) {
$cmd = [];
}
$count = 2 + count($cmd);
$counter = 1;
file_put_contents(__DIR__ . '/progress.json', json_encode(['progress'=>floor((100/$count)*$counter)]));
$this->$task($patch);
foreach ($cmd as $c => $value) {
$counter++;
file_put_contents(__DIR__ . '/progress.json', json_encode(['progress'=>floor((100/$count)*$counter)]));
$this->executeCommand($c);
}
file_put_contents(__DIR__ . '/progress.json', json_encode(['progress'=>100]));
}
else
{
$this->start();
file_put_contents(__DIR__ . '/progress.json', null);
}
}
function apply($patch = null)
{
if ($patch) {
$patchUrl = $this->url.$patch.'.patch';
$result = exec('curl ' . $patchUrl . ' | git apply');
} else {
echo 'Apply with no Patch ID';
}
}
function remove($patch = null)
{
if ($patch) {
$patchUrl = $this->url.$patch.'.patch';
$result = exec('curl ' . $patchUrl . ' | git apply -R');
return $result;
} else {
echo 'Could not remove Patch';
}
}
function executeCommand($cmd){
// cache clear by remove dir, not by command due performance
if($cmd == "cache:clear"){
@exec('rm -r app/cache/dev/');
@exec('rm -r app/cache/prod/');
return;
}
$fullCommand = explode(' ', $cmd);
$command = $fullCommand[0];
$argsCount = count($fullCommand) - 1;
$args = array('console', $command);
if ($argsCount) {
for ($i = 1; $i <= $argsCount; $i++) {
$args[] = $fullCommand[$i];
}
}
defined('IN_MAUTIC_CONSOLE') or define('IN_MAUTIC_CONSOLE', 1);
try {
$input = new ArgvInput($args);
$output = new BufferedOutput();
$kernel = new AppKernel('prod', false);
$app = new Application($kernel);
$app->setAutoExit(false);
$result = $app->run($input, $output);
echo "<pre>\n".$output->fetch().'</pre>';
} catch (\Exception $exception) {
echo $exception->getMessage();
}
}
// View
function start()
{ ?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Mautic Patch Tester</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="icon" href="https://www.mautic.org/wp-content/uploads/2014/08/favicon.png">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="container">
<nav class="navbar navbar-light bg-light navbar-expand-lg">
<a class="navbar-brand" href="https://www.mautic.org">
<img src="https://www.mautic.org/media/logos/notagline/horizontal/Mautic_Logo_RGB_LB.png" height="30" class="d-inline-block align-top" alt="">
</a>
<div class="collapse navbar-collapse" id="navbarNav">
<ul class="nav navbar-nav">
<li class="nav-item"><a class="nav-link" href="https://github.com/mautic/mautic/pulls?q=is%3Apr+is%3Aopen+label%3A%22Ready+To+Test%22" target="_blank">GitHub PR's</a></li>
</ul>
</div>
</nav>
<?php
if (!is_writable(dirname($this->localfile)))
{
$dir_class = "alert alert-danger";
$msg = "<p class='alert alert-danger'><span class='glyphicon glyphicon-warning-sign'></span> This path is not writable. Please change permissions before continuing.</p>";
// $continue = "disabled";
}
else
{
$dir_class = "alert alert-secondary";
$msg = "";
$continue = "";
}
?>
<?php echo $msg; ?>
<div class="container mt-4">
<h1>Start Testing!</h1>
<p class="lead">This app will allow you to immediately begin testing pull requests against your Mautic installation. You will need to make sure this file is in the root of your Mautic test instance.</p>
<div class="h6">Current Path</div>
<div class="<?php echo $dir_class; ?>"><?php echo __DIR__; ?></div>
<div class="hidden progressMsg">
Progress: <span class="label label-info">Ready.</span>
</div>
<div class="progress progress-striped mb-4">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: 0%">
</div>
</div>
<div class="row">
<div class="col">
<form id="apply-form" action="" method="post">
<input type="hidden" name="task" value="apply">
<div class="card">
<div class="card-body">
<div class="card-title">
<h4>Apply Pull Request</h4>
</div>
<div class="card-text">
<div class="form-group">
<label for="patch">Enter Pull Request Number</label>
<input type='text' id='apply-patch' name='patch' class='form-control' />
<small class="form-text text-muted">e.g. Simply enter 3456 for PR #3456</small>
</div>
<div class="form-group">
<h5>Run after apply pull request</h5>
<div class="checkbox">
<label>
<input type="checkbox" class="cmd" id="cache-clear" name="cmd[cache:clear]" value="1">
<label for="cache-clear">clear cache</label>
</label>
</div>
<div class="checkbox">
<label>
<input class="cmd" type="checkbox" id="dsu" name="cmd[doctrine:schema:update --force]" value="1">
<label for="dsu">doctrine:schema:update --force</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" id="mag" class="cmd" name="cmd[mautic:assets:generate]" value="1">
<label for="mag">mautic:assets:generate</label>
<small style="color:red">(it can take a few minutes)</small>
</label>
</div>
</div>
<span class="pt-3 text-small float-right text-success" id="pr-applied-message"></span>
<input type="submit" id="apply" class="btn btn-success btn-lg" data-loading-text="Applying..." value="Apply PR"/>
</div>
</div>
</div>
</form>
</div>
<div class="col">
<form id="remove-form" action="" method="post">
<input type="hidden" name="task" value="remove">
<div class="card card-danger">
<div class="card-body">
<div class="card-title">
<h4>Remove Pull Request</h4>
</div>
<div class="card-text">
<div class="form-group">
<label for="patch">Enter Pull Request Number</label>
<input type='text' id='remove-patch' name='patch' class='form-control' />
<small class="form-text text-muted">e.g. Simply enter 3456 for PR #3456</small>
</div>
<div class="form-group">
<h5>Run after remove pull request</h5>
<div class="checkbox">
<label>
<input type="checkbox" class="cmd" id="cache-clear-r" name="cmd[cache:clear]" value="1">
<label for="cache-clear-r">clear cache</label>
</label>
</div>
<div class="checkbox">
<label>
<input class="cmd" type="checkbox" id="dsu-r" name="cmd[doctrine:schema:update --force]" value="1">
<label for="dsu-r">doctrine:schema:update --force</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" id="mag-r" class="cmd" name="cmd[mautic:assets:generate]" value="1">
<label for="mag-r">mautic:assets:generate</label>
<small style="color:red">(it can take a few minutes)</small>
</label>
</div>
<span class="pt-3 text-small float-right text-danger" id="pr-removed-message"></span>
<input type="submit" id="remove" class="btn btn-success btn-lg" data-loading-text="Removing..." value="Remove PR"/>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
<div class="text-muted pt-4"><small>*This app does not yet take into account any pull requests that require database changes.</small></div>
</div>
<!-- /container -->
<script src="//code.jquery.com/jquery.js"></script>
<script src="//netdna.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script type="text/javascript">
var finished = false;
function progress() {
jQuery.ajax({'url': 'progress.json', 'type': 'post', 'dataType': 'json'})
.done(function (msg) {
jQuery('.progress-bar').css('width', msg.progress + '%');
jQuery('.label-info').html(msg.progress + '%');
});
if (!( finished )) {
setTimeout(function () {
progress();
}, 1000);
}
}
jQuery(document).ready(function () {
jQuery('#apply-form').on( "submit", function( e ) {
e.preventDefault();
var patch = jQuery('#apply-patch').val();
if(!patch) { alert('Please enter a valid PR'); return false; }
setTimeout(function () {
progress();
}, 1000);
jQuery.ajax({'url': './<?php echo $this->testerfilename ?>', 'data': $( this ).serializeArray(), 'type': 'post', 'dataType': 'text'})
.done(function () {
jQuery('.progress-bar').css('width', '100%');
jQuery('.label-info').html('100%');
finished = true;
jQuery('#pr-applied-message').html('PR #'+patch+' successfully applied');
jQuery('#apply-patch').val('');
});
return false;
});
jQuery('#remove-form').on( "submit", function( e ) {
e.preventDefault();
var patch = jQuery('#remove-patch').val();
if(!patch) { alert('Please enter a valid PR'); return; }
setTimeout(function () {
progress();
}, 1000);
jQuery.ajax({'url': './<?php echo $this->testerfilename ?>', 'data': $( this ).serializeArray(), 'type': 'post', 'dataType': 'text'})
.done(function () {
jQuery('.progress-bar').css('width', '100%');
jQuery('.label-info').html('100%');
finished = true;
jQuery('#pr-removed-message').html('PR #'+patch+' successfully removed');
jQuery('#remove-patch').val('');
});
});
})
</script>
</body>
</html>
<?php }
}
new MauticPatchTester;