Repository navigation
Expand file tree
/
Copy pathCommand.php
More file actions
585 lines (509 loc) · 20.3 KB
/
Copy pathCommand.php
File metadata and controls
585 lines (509 loc) · 20.3 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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
<?php
namespace ProvisionOps\YamlTests;
use ProvisionOps\Tools\PowerProcess as Process;
use ProvisionOps\Tools\Style;
use GuzzleHttp\Psr7\Response;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Command\BaseCommand;
use Symfony\Component\Yaml\Yaml;
use TQ\Git\Repository\Repository;
// @TODO: Figure out why our plugin isn't properly autoloading.
if (file_exists(__DIR__.'/../../../../vendor/autoload.php')) {
$autoloaderPath = __DIR__.'/../../../../vendor/autoload.php';
} elseif (file_exists(__DIR__.'/vendor/autoload.php')) {
$autoloaderPath = __DIR__.'/vendor/autoload.php';
} elseif (file_exists(__DIR__.'/../../autoload.php')) {
$autoloaderPath = __DIR__ . '/../../autoload.php';
} elseif (file_exists(__DIR__.'/../vendor/autoload.php')) {
$autoloaderPath = __DIR__ . '/../vendor/autoload.php';
} else {
die("Could not find autoloader. Run 'composer install'.");
}
require_once $autoloaderPath;
/**
* Class Command
*
* Provides the `git-build` command to composer.
*
* @package jonpugh\ComposerGitBuild
*/
class Command extends BaseCommand
{
protected $createTag = false;
protected $tagName = null;
protected $branchName;
protected $commitMessage;
protected $excludeFileTemp;
/**
* @var SymfonyStyle
*/
protected $io;
/**
* The directory containing composer.json. Loaded from composer option --working-dir.
*
* @var String
*/
protected $workingDir;
/**
* The directory at the root of the git repository.
*
* @var String
*/
protected $gitDir;
/**
* @var Repository
*/
protected $gitRepo;
/**
* The current git commit SHA.
*
* @var String
*/
protected $gitSha;
/**
* The options from the project's composer.json "config" section.
*
* @var array
*/
protected $config = [];
/** @var \Github\Client */
protected $githubClient;
private $addTokenUrl = "https://github.com/settings/tokens/new?description=yaml-tests&scopes=repo:status,public_repo";
protected function configure()
{
$this->setName('yaml-tests');
$this->setDescription('Read tests.yml and runs all commands in it, passing results to GitHub Commit Status API.');
$this->addOption(
'tests-file',
null,
InputOption::VALUE_OPTIONAL,
'Relative path to a yml file to run.',
'tests.yml'
);
$this->addOption(
'github-token',
null,
InputOption::VALUE_REQUIRED,
'An active github token. Create a new token at ' . $this->addTokenUrl
);
$this->addOption(
'ignore-dirty',
null,
InputOption::VALUE_NONE,
'Allow testing even if git working copy is dirty (has modified files).'
);
$this->addOption(
'dry-run',
null,
InputOption::VALUE_NONE,
'Run tests but do not post to GitHub.'
);
$this->addOption(
'ignore-ssl',
null,
InputOption::VALUE_NONE,
'Ignore SSL certificate validation errors. Use only if you receive errors trying to reach the GitHub API with this tool.'
);
$this->addOption(
'hostname',
null,
InputOption::VALUE_OPTIONAL,
'The hostname to use in the status description. Use if automatically detected hostname is not desired.',
gethostname()
);
$this->addOption(
'groups',
null,
InputOption::VALUE_OPTIONAL,
'Only run tests that are in the specified groups. Separate with a comma. Prefix with ! to exclude.'
);
$this->addArgument(
'filter',
InputArgument::IS_ARRAY,
'A list of strings to filter tests by.'
);
}
/**
*
*/
public function initialize(InputInterface $input, OutputInterface $output)
{
$this->io = new Style($input, $output);
$this->input = $input;
$this->output = $input;
$this->logger = $this->io;
$this->workingDir = getcwd();
$this->gitRepo = Repository::open($this->workingDir);
$this->gitRepo->getCurrentCommit();
$this->config = $this->getComposer()->getPackage()->getConfig();
$this->testsFile = $input->getOption('tests-file');
$this->testsFilePath = realpath($this->testsFile);
if (!file_exists($this->testsFilePath) || empty($this->testsFilePath)) {
throw new \Exception("Specified tests file does not exist at {$this->workingDir}/{$this->testsFile}");
}
// Validate YML
$this->loadTestsYml();
// Load Environment variables
$dotenv = new \Dotenv\Dotenv(__DIR__);
$dotenv->safeLoad(array(
// Current user's home directory
isset($_SERVER['HOME'])? $_SERVER['HOME']: '',
// Git repo holding the tests file.
dirname($this->gitRepo->getRepositoryPath()),
// Current directory
getcwd(),
));
// Look for token.
if (!empty($_SERVER['GITHUB_TOKEN'])) {
$token = $_SERVER['GITHUB_TOKEN'];
} else {
$token = $input->getOption('github-token');
}
// This is the actual SHA of the working copy clone.
$this->repoSha = $this->gitRepo->getCurrentCommit();
// Detect a TRAVIS_PULL_REQUEST_SHA
// Travis tests from a commit created from master and our commit.
// It's not the same commit as the pull request branch.
if (!empty($_SERVER['TRAVIS_PULL_REQUEST_SHA']) && $this->gitRepo->getRepositoryPath() == $_SERVER['TRAVIS_BUILD_DIR']) {
$this->repoSha = $_SERVER['TRAVIS_PULL_REQUEST_SHA'];
$this->warningLite("Travis PR detected. Using PR SHA: " . $this->repoSha);
}
$remotes = $this->gitRepo->getCurrentRemote();
$remote_url = current($remotes)['push'];
$remote_url = strtr(
$remote_url,
array(
'git@' => 'https://',
'git://' => 'https://',
'.git' => '',
'github.com:' => 'github.com/',
)
);
$parts = explode('/', parse_url($remote_url, PHP_URL_PATH));
if (isset($parts[1]) && isset($parts[2])) {
$this->repoOwner = isset($parts[1])? $parts[1]: '';
$this->repoName =isset($parts[2])? $parts[2]: '';
} else {
$this->repoOwner = '';
$this->repoName = '';
}
$this->io->title("Yaml Tests Initialized");
// Force dry run if there is no token set.
if (empty($token)) {
$input->setOption('dry-run', true);
$this->warningLite('No GitHub token found. forcing --dry-run');
$this->io->writeln('');
}
$this->say("Git Remote: <comment>{$remote_url}</comment>");
$this->say("Composer working directory: <comment>{$this->workingDir}</comment>");
$this->say("Git Repository directory: <comment>{$this->workingDir}</comment>");
$this->say("Git Commit: <comment>{$this->gitRepo->getCurrentCommit()}</comment>");
$this->say("Tests File: <comment>{$this->testsFilePath}</comment>");
if (!$input->getOption('dry-run')) {
$this->githubClient = new \Github\Client();
if ($input->getOption('ignore-ssl')) {
$this->githubClient->getHttpClient()->client->setDefaultOption('verify', false);
}
$this->githubClient->authenticate($token, \Github\Client::AUTH_HTTP_TOKEN);
$commit = $this->githubClient->repository()->commits()->show($this->repoOwner, $this->repoName, $this->repoSha);
$this->say("GitHub Commit: <comment>" . $commit['html_url'] . "</>");
// Load Repo info to determine if it is a fork. We must post to the fork's parent in the API.
$repo = $this->githubClient->repository()->show($this->repoOwner, $this->repoName);
if (!empty($repo['parent'])) {
$this->successLite('Forked repository. Posting to the parent repo...');
$this->repoOwner = $repo['parent']['owner']['login'];
$this->repoName = $repo['parent']['name'];
}
}
$this->io->table(array("Tests found in " . $this->testsFile), $this->testsToTableRows());
// If there are filters, shorten the list of tests to run.
$filters = $input->getArgument('filter');
$filter_string = implode(' ', $filters);
if (count($filters)) {
foreach ($this->yamlTests as $name => $test) {
$run_the_test = false;
foreach ($filters as $filter) {
// If the filter string was found in the test name, run the test.
if (strpos($name, $filter) !== false) {
$run_the_test = true;
}
}
if (!$run_the_test) {
unset($this->yamlTests[$name]);
}
}
}
// If there are no matches
if (count($filters) && count($this->yamlTests) > 0) {
$this->io->table(array("Tests to run based on filter '$filter_string'"), $this->testsToTableRows());
} elseif (count($filters)) {
// If there are filters but tests were NOT removed, show a warning.
$this->warningLite("The filter '$filter_string' was specified but it did not match any tests.");
exit(1);
}
}
/**
* @param \Symfony\Component\Console\Input\InputInterface $input
* @param \Symfony\Component\Console\Output\OutputInterface $output
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$tests_failed = false;
try {
if (!$input->getOption('dry-run')) {
$client = $this->githubClient;
foreach ($this->yamlTests as $test_name => $test) {
// Set a commit status for this REF
$params = new \stdClass();
$params->state = 'pending';
$params->target_url = 'https:///path/to/file';
$params->description = implode(
' — ',
array(
$input->getOption('hostname'),
!empty($test['description'])? $test['description']: $test_name
)
);
$params->context = $test_name;
// Post status to github
try {
/**
* @var Response $response
*/
$response = $client->getHttpClient()->post("/repos/{$this->repoOwner}/{$this->repoName}/statuses/$this->repoSha", [], json_encode($params));
$this->commitStatusMessage($response, $test_name, $test, $params->state);
} catch (\Exception $e) {
if ($e->getCode() == 404) {
throw new \Exception('Unable to reach commit status API. Check the allowed scopes of your GitHub Token. Skip github interaction with --dry-run, or create a new token with the right scopes at ' . $this->addTokenUrl);
}
}
$tests[] = $test_name;
}
} else {
$this->warningLite('Skipping commit status posting, dry-run enabled.');
}
$this->io->newLine();
$rows = array();
foreach ($this->yamlTests as $test_name => $test) {
if (is_array($test) && isset($test['command'])) {
$command = $test['command'];
} elseif (is_array($test)) {
$command = implode(' && ', $test);
} else {
$command = $test;
}
$results_row = array(
$test_name,
$command,
);
$process = new Process($command, $this->io);
$process->setTimeout(null);
$process->setIo($this->io);
$process->setEnv($_SERVER);
$title = "Running test <fg=white>$test_name</>";
if (!empty($test['description'])) {
$title .= ": <fg=white>{$test['description']}</>";
}
$this->io->section($title);
$exit = $process->run();
// Set a commit status for this REF
$params = new \stdClass();
$params->state = 'pending';
$params->target_url = 'https:///';
$params->description = implode(
' — ',
array(
$input->getOption('hostname'),
!empty($test['description'])? $test['description']: $test_name
)
);
$params->context = $test_name;
if ($exit == 0) {
$results_row[] = '<info>✔</info> Passed';
$params->state = 'success';
} else {
// If the test has the ignore failure flag, ignore it.
if (!empty($test['ignore-failure'])) {
$results_row[] = '<fg=red>!</> Failed but ignoring';
$params->state = 'success';
$params->description .= ' | TEST FAILED but is configured to ignore failures.';
$this->warningLite('Test configured to ignore failures. Not triggering failure exit code or commit status.');
} else {
$results_row[] = '<fg=red>✘</> Failed';
$tests_failed = true;
$params->state = 'failure';
}
if (!$input->getOption('dry-run')) {
// @TODO: Make the commenting optional/configurable
// Write a comment on the commit with the results
// @see https://developer.github.com/v3/repos/comments/#create-a-commit-comment
$comment = array();
$comment['body'] = implode(
"\n",
array(
"###### :x: Test Failed: `$test_name`",
' ```bash',
' ' . $command,
' ```',
' ```bash',
' ' . $process->getOutput(),
' ' . $process->getErrorOutput(),
' ```',
'*AUTOMATED COMMENT FROM provision-ops/yaml-tests, running on ' . $input->getOption('hostname') . '*',
)
);
try {
$comment_response = $client->repos()->comments()->create($this->repoOwner, $this->repoName, $this->repoSha, $comment);
$this->successLite("Comment Created: {$comment_response['html_url']}");
$params->target_url = $comment_response['html_url'];
} catch (\Github\Exception\RuntimeException $e) {
$this->errorLite("Unable to create GitHub Commit Comment: " . $e->getMessage() . ': ' . $e->getCode());
}
}
}
// If TRAVIS_JOB_WEB_URL is present and the target_url was not changed, use that as the target_url.
if ($params->target_url == 'https:///' && !empty($_SERVER['TRAVIS_JOB_WEB_URL'])) {
$params->target_url = $_SERVER['TRAVIS_JOB_WEB_URL'];
}
if (!$input->getOption('dry-run')) {
$response = $client->getHttpClient()->post("/repos/$this->repoOwner/$this->repoName/statuses/$this->repoSha", [], json_encode($params));
$this->commitStatusMessage($response, $test_name, $test, $params->state);
}
$this->io->newLine();
$rows[] = $results_row;
}
} catch (\Github\Exception\RuntimeException $e) {
if ($output->isVerbose()) {
$output->writeln($e->getTraceAsString());
}
if ($e->getCode() == 404) {
throw new \Exception('Something went wrong: ' . $e->getMessage());
} else {
throw new \Exception("Bad token. Set with --github-token option or GITHUB_TOKEN environment variable. Create a new token at {$this->addTokenUrl} Message: " . $e->getMessage());
}
}
$this->io->title("Executed all tests");
$this->io->table(array('Test Results'), $rows);
if ($tests_failed) {
exit(1);
}
}
private function loadTestsYml()
{
$this->yamlTests = Yaml::parse(file_get_contents($this->testsFilePath));
}
private function testsToTableRows()
{
$rows = array();
foreach ($this->yamlTests as $test_name => $test) {
if (is_array($test) && isset($test['command'])) {
$command = $test['command'];
} elseif (is_array($test)) {
$command = implode("\n", $test);
} else {
$command = $test;
}
$rows[] = array($test_name, $command);
}
return $rows;
}
protected function commitStatusMessage(Response $response, $test_name, $test, $state)
{
$message = implode(
': ',
array(
'GitHub Status',
$test_name,
$state
)
);
if (strpos((string) $response->getStatusCode(), '2') === 0) {
if ($state == 'pending') {
$this->customLite($message, "⏺", "fg=yellow");
} elseif ($state == 'error' || $state == 'failure') {
$this->errorLite($message);
} elseif ($state == 'success') {
$this->successLite($message);
}
} else {
// Big error for actual API error.
$this->io->error($message);
}
}
/**
* Wrapper for $this->io->comment().
*
* @param $message
*/
protected function say($message)
{
$this->io->text($message);
}
/**
* Wrapper for $this->io->ask().
*
* @param $message
*/
protected function ask($question)
{
return $this->io->ask($question);
}
/**
* Wrapper for $this->io->ask().
*
* @param $message
*/
protected function askDefault($question, $default)
{
return $this->io->ask($question, $default);
}
public function successLite($message, $newLine = false)
{
$message = sprintf('<info>✔</info> %s', $message);
$this->io->text($message);
if ($newLine) {
$this->io->newLine();
}
}
public function errorLite($message, $newLine = false)
{
$message = sprintf('<fg=red>✘</> %s', $message);
$this->io->text($message);
if ($newLine) {
$this->io->newLine();
}
}
public function warningLite($message, $newLine = false)
{
$message = sprintf('<comment>!</comment> %s', $message);
$this->io->text($message);
if ($newLine) {
$this->io->newLine();
}
}
public function customLite($message, $prefix = '*', $style = '', $newLine = false)
{
if ($style) {
$message = sprintf(
'<%s>%s</%s> %s',
$style,
$prefix,
$style,
$message
);
} else {
$message = sprintf(
'%s %s',
$prefix,
$message
);
}
$this->io->text($message);
if ($newLine) {
$this->io->newLine();
}
}
}