Skip to content

Commit 10d3278

Browse files
committed
Added scrutinizer support
1 parent 82b6da8 commit 10d3278

File tree

4 files changed

+168
-7
lines changed

4 files changed

+168
-7
lines changed

examples/advanced-enable-cis-for-non-forks-and-files-with-matching-ci-file.php

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
<?php declare(strict_types=1);
22

33
/**
4-
* This example enables travis for all repositories
5-
* with a .travis.yml that aren't forks and aren't enabled yet.
4+
* This example enables travis, appveyor, and scrutinizer for all
5+
* repositories with .travis.yml, appveyor.yml, and .scrutinizer.yml
6+
* files that aren't forks and aren't enabled yet.
67
*/
78

89
use ApiClients\Client\AppVeyor\AsyncClient as AsyncAppVeyorClient;
@@ -11,6 +12,8 @@
1112
use ApiClients\Client\Github\Resource\Async\Repository;
1213
use ApiClients\Client\Github\Resource\Contents\FileInterface;
1314
use ApiClients\Client\Github\Resource\UserInterface;
15+
use ApiClients\Client\Scrutinizer\AsyncClient as AsyncScrutinizerClient;
16+
use ApiClients\Client\Scrutinizer\AsyncClientInterface as AsyncScrutinizerClientInterface;
1417
use ApiClients\Client\Travis\AsyncClient as AsyncTravisClient;
1518
use ApiClients\Client\Travis\AsyncClientInterface as AsyncTravisClientInterface;
1619
use ApiClients\Client\Travis\Resource\Async\Repository as TravisRepository;
@@ -19,6 +22,7 @@
1922
use ApiClients\Middleware\Delay\DelayMiddleware;
2023
use ApiClients\Middleware\Pool\PoolMiddleware;
2124
use React\EventLoop\Factory;
25+
use function React\Promise\resolve;
2226
use ResourcePool\Pool;
2327
use Rx\Observable;
2428
use function ApiClients\Foundation\resource_pretty_print;
@@ -52,12 +56,16 @@
5256
$travisClient = AsyncTravisClient::create($loop, require 'resolve_travis-key.php', [
5357
Options::TRANSPORT_OPTIONS => $transportOptions,
5458
]);
59+
$scrutinizerClient = AsyncScrutinizerClient::create($loop, require 'resolve_scrutinizer-token.php', [
60+
Options::TRANSPORT_OPTIONS => $transportOptions,
61+
]);
5562
$githubClient = AsyncClient::create($loop, require 'resolve_token.php', [
5663
Options::TRANSPORT_OPTIONS => $transportOptions,
5764
// Pass the AppVeyor and Travis client into the Github client internal container
5865
Options::CONTAINER_DEFINITIONS => [
5966
AsyncAppVeyorClient::class => $appVeyorClient,
6067
AsyncTravisClientInterface::class => $travisClient,
68+
AsyncScrutinizerClientInterface::class => $scrutinizerClient,
6169
],
6270
]);
6371

@@ -81,14 +89,15 @@
8189
// Only check repositories that start with reactphp-http
8290
// This is optional and you can remove this to check all repositories
8391
// BUT that takes a lot of calls to check and time due to throttling
84-
return strpos($repository->name(), 'php-super') === 0;
92+
return strpos($repository->name(), 'reactphp-') === 0;
8593
})->flatMap(function (Repository $repository) {
8694
// Check if the repository contains a .travis.yml
8795
return Observable::fromPromise(new React\Promise\Promise(function ($resolve, $reject) use ($repository) {
8896
$hasCi = [
8997
'repo' => $repository,
9098
'travis' => false,
9199
'appveyor' => false,
100+
'scrutinizer' => false,
92101
];
93102
$repository->contents()->filter(function ($node) {
94103
// Only let through files
@@ -102,6 +111,11 @@
102111
if ($file->name() === 'appveyor.yml') {
103112
$hasCi['appveyor'] = true;
104113

114+
return;
115+
}
116+
if ($file->name() === '.scrutinizer.yml') {
117+
$hasCi['scrutinizer'] = true;
118+
105119
return;
106120
}
107121
}, function ($error) use ($reject) {
@@ -134,8 +148,14 @@
134148
// Activate repository on Travis
135149
$repository->enable()->done(function (TravisRepository $repository) {
136150
resource_pretty_print($repository);
137-
}, 'display_throwable');
138-
}, 'display_throwable');
151+
}, function ($e) {
152+
echo 'Travis', PHP_EOL;
153+
echo (string)$e;
154+
});
155+
}, function ($e) {
156+
echo 'Travis', PHP_EOL;
157+
echo (string)$e;
158+
});
139159

140160
/**
141161
* Stream handling the AppVeyor side of things.
@@ -153,8 +173,47 @@
153173
return $appVeyorClient->addProject('gitHub', $repository->fullName());
154174
})->done(function ($repository) {
155175
resource_pretty_print($repository);
156-
}, 'display_throwable');
157-
}, 'display_throwable');
176+
}, function ($e) {
177+
if ($e === null) {
178+
return;
179+
}
180+
181+
echo 'AppVeyor', PHP_EOL;
182+
echo (string)$e;
183+
});
184+
}, function ($e) {
185+
echo 'AppVeyor', PHP_EOL;
186+
echo (string)$e;
187+
});
188+
189+
/**
190+
* Stream handling the Scrutinizer side of things.
191+
*/
192+
$baseStream->filter(function (array $d) {
193+
return $d['scrutinizer'];
194+
})->map(function (array $d) {
195+
return $d['repo'];
196+
})->subscribe(function (Repository $repository) use ($scrutinizerClient) {
197+
$repository->scrutinizerRepository()->then(function ($scrutinizer) use ($scrutinizerClient, $repository) {
198+
if ($scrutinizer !== false) {
199+
return reject();
200+
}
201+
202+
return $scrutinizerClient->github()->addRepository($repository->fullName());
203+
})->done(function ($repository) {
204+
resource_pretty_print($repository);
205+
}, function ($e) {
206+
if ($e === null) {
207+
return;
208+
}
209+
210+
echo 'Scrutinizer', PHP_EOL;
211+
echo (string)$e;
212+
});
213+
}, function ($e) {
214+
echo 'Scrutinizer', PHP_EOL;
215+
echo (string)$e;
216+
});
158217

159218
$loop->run();
160219

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Command\Repository;
4+
5+
use WyriHaximus\Tactician\CommandHandler\Annotations\Handler;
6+
7+
/**
8+
* @Handler("ApiClients\Client\Github\CommandBus\Handler\Repository\ScrutinizerHandler")
9+
*/
10+
final class ScrutinizerCommand
11+
{
12+
/**
13+
* @var string
14+
*/
15+
private $login;
16+
17+
/**
18+
* @var string
19+
*/
20+
private $name;
21+
22+
/**
23+
* @param string $login
24+
* @param string $name
25+
*/
26+
public function __construct(string $login, string $name)
27+
{
28+
$this->login = $login;
29+
$this->name = $name;
30+
}
31+
32+
/**
33+
* @return string
34+
*/
35+
public function getLogin(): string
36+
{
37+
return $this->login;
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function getName(): string
44+
{
45+
return $this->name;
46+
}
47+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace ApiClients\Client\Github\CommandBus\Handler\Repository;
4+
5+
use ApiClients\Client\Github\CommandBus\Command\Repository\ScrutinizerCommand;
6+
use ApiClients\Client\Scrutinizer\AsyncClientInterface;
7+
use ApiClients\Client\Scrutinizer\Github\AsyncClientInterface as ScrutinizerGithubAsyncClientInterface;
8+
use Clue\React\Buzz\Message\ResponseException;
9+
use React\Promise\PromiseInterface;
10+
use function React\Promise\reject;
11+
use function React\Promise\resolve;
12+
13+
final class ScrutinizerHandler
14+
{
15+
/**
16+
* @var ScrutinizerGithubAsyncClientInterface
17+
*/
18+
private $scrutinizer;
19+
20+
/**
21+
* @param AsyncClientInterface $client
22+
*/
23+
public function __construct(AsyncClientInterface $client)
24+
{
25+
$this->scrutinizer = $client->github();
26+
}
27+
28+
/**
29+
* @param ScrutinizerCommand $command
30+
* @return PromiseInterface
31+
*/
32+
public function handle(ScrutinizerCommand $command): PromiseInterface
33+
{
34+
return $this->scrutinizer->repository(
35+
$command->getLogin(),
36+
$command->getName()
37+
)->then(null, function ($throwable) {
38+
if (!($throwable instanceof ResponseException)) {
39+
return reject($throwable);
40+
}
41+
42+
if ($throwable->getCode() !== 404) {
43+
return reject($throwable);
44+
}
45+
46+
return resolve(false);
47+
});
48+
}
49+
}

src/Resource/Async/Repository.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use ApiClients\Client\Github\CommandBus\Command\Repository\LanguagesCommand;
1515
use ApiClients\Client\Github\CommandBus\Command\Repository\ReleasesCommand;
1616
use ApiClients\Client\Github\CommandBus\Command\Repository\ReplaceTopicsCommand;
17+
use ApiClients\Client\Github\CommandBus\Command\Repository\ScrutinizerCommand;
1718
use ApiClients\Client\Github\CommandBus\Command\Repository\SubscribeCommand;
1819
use ApiClients\Client\Github\CommandBus\Command\Repository\TagsCommand;
1920
use ApiClients\Client\Github\CommandBus\Command\Repository\TravisCommand;
@@ -149,4 +150,9 @@ public function appVeyorRepository(): PromiseInterface
149150
{
150151
return $this->handleCommand(new AppVeyorCommand($this->fullName()));
151152
}
153+
154+
public function scrutinizerRepository(): PromiseInterface
155+
{
156+
return $this->handleCommand(new ScrutinizerCommand(...explode('/', $this->fullName())));
157+
}
152158
}

0 commit comments

Comments
 (0)