-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPayload.class.php
432 lines (401 loc) · 10.9 KB
/
Payload.class.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* GitHUB PayLoad Class Handler
*
* Requires GIT executable
* Passwordless SSH Key should be set up on the server
*
* Make sure this script has sufficient rights to run git
*
* @author Tamas Kalman <[email protected]>
*/
Class Payload
{
const GIT_PATH = '/usr/bin/git';
const PHP_PATH = '/usr/bin/php';
const PHPCS_PATH = '/usr/bin/phpcs';
const SUDO_PATH = '/usr/bin/sudo';
protected $_debug;
var $payloadPost;
var $payload;
var $logDir;
var $sourceDir;
var $standard;
/**
* Constructor
*
* @return void
*/
function __construct()
{
$masterDir = __DIR__ . DIRECTORY_SEPARATOR;
$uniqueSourceId = 'source-' . date('U') . '-' . rand(100000, 999999);
$this->setLogDir($masterDir . 'log');
$this->setSourceDir($masterDir . $uniqueSourceId);
$this->setCodingStandard('Zend');
$this->loadPayloadFromPost();
$this->setDebug(false);
}
/**
* Debug Functions on/off
*
* @param String $debug Debug
*
* @return void
*/
public function setDebug($debug)
{
$this->_debug = $debug;
}
/**
* Loads payload from HTTP Post
*
* @return void
*/
public function loadPayloadFromPost()
{
$this->payloadPost = false;
if (isset($_POST['payload'])) {
$this->payloadPost = $_POST['payload'];
}
$this->setPayLoad($this->payloadPost);
}
/**
* Loads Payload from Logfile from the log directory
*
* @param String $filename Log Filename
*
* @return Boolean Result
*/
public function loadPayloadFromLog($filename)
{
$logFilePath = $this->logDir . DIRECTORY_SEPARATOR . $filename;
if (is_file($logFilePath)) {
$this->payloadPost = file_get_contents($logFilePath);
$this->setPayLoad($this->payloadPost);
return true;
}
return false;
}
/**
* Get Parsed Payload
*
* @return Array Payload
*/
public function getPayLoad()
{
return $this->payload;
}
/**
* Sets Coding Standard for PHPCS
*
* @param String $standard Standard
*
* @return void
*/
public function setCodingStandard($standard)
{
$this->standard = $standard;
}
/**
* Sets source dir to download GIT repository
*
* @param String $sourceDir Source Dir
*
* @return void
*/
public function setSourceDir($sourceDir)
{
$this->sourceDir = $sourceDir;
}
/**
* Decodes & Sets Payload from String
*
* @param String $payload Github API Payload post string
*
* @return void
*/
public function setPayLoad($payload)
{
$this->payload = ($payload) ? json_decode($payload, true) : false;
}
/**
* Sets Log Dir
*
* @param String $logDir Log Dir
*
* @return void
*/
public function setLogDir($logDir)
{
$this->logDir = $logDir;
}
/**
* Logs the raw request
*
* @param String $logFile Log Filename (optional)
*
* @return void
*/
public function log($logFile = null)
{
if (!is_dir($this->logDir)) {
mkdir($this->logDir);
}
$uniqueId = Array(time(), rand(100000, 999999));
$logFile = $logFile ? : vsprintf('post-%s-%s.log', $uniqueId);
$logFilePath = $this->logDir . DIRECTORY_SEPARATOR . $logFile;
file_put_contents($logFilePath, $this->payloadPost);
}
/**
* Removes source files
* Requires SUDO
*
* @return void
*/
public function removeSourceDir()
{
if (is_dir($this->sourceDir)) {
exec($this::SUDO_PATH . ' rm -rf ' . $this->sourceDir);
}
}
/**
* Generates a GIT clone shell command based on the payload
* Requires SUDO
*
* @return String $command
*/
public function getGitCommand()
{
$repositoryUrl = $this->payload['repository']['url'];
$sshPath = str_replace(
'https://github.com/',
$repositoryUrl
);
$command = $this::SUDO_PATH . ' ' . $this::GIT_PATH . ' clone ' .
' -b ' . $this->getBranchName() . ' ' .
$sshPath . ' ' . $this->sourceDir;
return $command;
}
/**
* Downloads Repository
*
* @return Boolean Success or Failure
*/
public function downloadRepository()
{
$cmd = $this->getGitCommand();
$output = shell_exec($cmd);
$this->debugLog($cmd);
$this->debugLog($output);
return is_dir($this->sourceDir);
}
/**
* Using PHP Lint, check the file's syntax
*
* @param String $filename Filename to check in source dir
*
* @return Boolean|Array
*/
protected function _checkSyntax($filename)
{
$filePath = $this->sourceDir . DIRECTORY_SEPARATOR . $filename;
$cmd = $this::PHP_PATH . ' -l ' . $filePath;
exec($cmd, $output);
$this->debugLog($cmd);
$this->debugLog($output);
if (empty($output)) {
return true;
}
$noSyntax = 'No syntax errors detected';
if (isset($output[0]) && ($this->_startsWith($output[0], $noSyntax))) {
return true;
}
$output = $this->_removeSourceDirFromTextArray($output);
return $output;
}
/**
* Using PHPCS, checking standards
*
* @param String $filename Filename
*
* @return Boolean|Array
*/
protected function _checkStandards($filename)
{
$cmd = $this::PHPCS_PATH . ' --standard=' . $this->standard . ' ' .
$this->sourceDir . DIRECTORY_SEPARATOR . $filename;
exec($cmd, $output);
$this->debugLog($cmd);
$this->debugLog($output);
if (isset($output[0]) && ($this->_startsWith($output[0], 'Time'))) {
return true;
}
$output = $this->_removeSourceDirFromTextArray($output);
return $output;
}
/**
* Debug logging (if enabled)
*
* @param Mixed $var Variable to log
* @param String $prefix Prefix for Debug log filename
*
* @return void
*/
public function debugLog($var, $prefix = '')
{
if ($this->_debug) {
file_put_contents(
$this->logDir . DIRECTORY_SEPARATOR . $prefix .
'debug.log', print_r($var, true)."\n\n\n", FILE_APPEND
);
}
}
/**
* Strips out source dir path from the given Array
*
* @param Array $textArray Text Array
*
* @return Array
*/
protected function _removeSourceDirFromTextArray($textArray)
{
foreach ($textArray as &$line) {
$line = str_replace($this->sourceDir, '', $line);
}
return $textArray;
}
/**
* Last Git Commit SHA from Payload
*
* @return String Git Commit SHA
*/
public function getCommitId()
{
return $this->payload['head_commit']['id'];
}
/**
* Returns with the branch name which was used during last commit
*
* @return String Branch name
*/
public function getBranchName()
{
$ref = $this->payload['ref'];
$refParts = explode('/', $ref);
$lastRef = end($refParts);
return $lastRef;
}
/**
* Validates a file against syntax and Coding Standards
* Currently only PHP is supported
*
* @param String $filename Filename
*
* @return Boolean|String True or an Array containing the problem
*/
protected function _validateFile($filename)
{
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if ($ext == 'php') {
$syntax = $this->_checkSyntax($filename);
if ($syntax !== true) {
$problem = Array(
'file' => $filename,
'type' => 'syntax error',
'description' => $syntax
);
return $problem;
}
$cs = $this->_checkStandards($filename);
if ($cs !== true) {
$problem = Array(
'file' => $filename,
'type' => 'coding standards validation',
'description' => $cs
);
return $problem;
}
}
return true;
}
/**
* Returns with the details of the committer ['name', 'email']
*
* @return Array
*/
public function getCommitterDetails()
{
$committer = $this->payload['head_commit']['committer'];
return $committer;
}
/**
* Validates all commits from head
*
* @return Boolean|Array True or an Array of Problems
*/
public function validateCommits()
{
$commit = $this->payload['head_commit'];
$mask = array('added', 'modified');
$problems = Array();
foreach ($mask as $m) {
$filelist = $commit[$m];
foreach ($filelist as $filename) {
$result = $this->_validateFile($filename);
if ($result !== true) {
$problems[] = $result;
}
}
}
if (count($problems) == 0) {
return true;
}
return $problems;
}
/**
* Compares a substring with a string from the beginning
*
* @param String $haystack Haystack
* @param String $needle Needle to search
*
* @return Boolean Result
*/
private function _startsWith($haystack, $needle)
{
return !strncmp($haystack, $needle, strlen($needle));
}
/**
* Sends an Email to the Committer with the details
*
* @param Array $problems Problems
*
* @return void
*/
public function sendEmail($problems)
{
$committer = $this->getCommitterDetails();
$subject = "Coding Standards information about your Commit #" .
$this->getCommitId();
$message = 'Hey ' . $committer['name'] . ",\n\n";
$message .= "We've found the following coding standards ";
$message .= "notifications in your recent commit:\n\n";
foreach ($problems as $problem) {
$message .= 'File: ' . $problem['file'] . "\n";
$message .= 'Type: ' . $problem['type'] . "\n\n";
foreach ($problem['description'] as $line) {
$message .= $line . "\n";
}
$message .= "\n";
}
$message .= "Sincerely,\n\nThe Coding Standards Validator Robot";
$ses = new SimpleEmailService(SES_ACCESS, SES_SECRET);
$mail = new SimpleEmailServiceMessage();
$mail->addTo($committer['email']);
$mail->setFrom(SES_EMAIL);
$mail->setSubject($subject);
$mail->setMessageFromString($message);
$ses->sendEmail($mail);
}
}