-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added commands to link dependencies and trigger regression run
- Loading branch information
Showing
10 changed files
with
446 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
/vendor/ | ||
composer.lock | ||
.php_cs.cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
return EzSystems\EzPlatformCodeStyle\PhpCsFixer\EzPlatformInternalConfigFactory::build() | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->in(__DIR__ . '/src') | ||
->files()->name('*.php') | ||
) | ||
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$application = new Symfony\Component\Console\Application; | ||
|
||
$application->add(new Ibexa\Platform\ContiniousIntegrationScripts\Command\LinkDependenciesCommand()); | ||
$application->add(new Ibexa\Platform\ContiniousIntegrationScripts\Command\RunRegressionCommand()); | ||
$application->setDefaultCommand('dependencies:link'); | ||
|
||
$application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: "#^Offset 'head' does not exist on array\\|string\\.$#" | ||
count: 2 | ||
path: src/Command/LinkDependenciesCommand.php | ||
|
||
- | ||
message: "#^Property Ibexa\\\\Platform\\\\ContiniousIntegrationScripts\\\\Command\\\\LinkDependenciesCommand\\:\\:\\$token \\(string\\|null\\) does not accept array\\<string\\>\\|string\\|null\\.$#" | ||
count: 1 | ||
path: src/Command/LinkDependenciesCommand.php | ||
|
||
- | ||
message: "#^Parameter \\#1 \\$productVersion of method Ibexa\\\\Platform\\\\ContiniousIntegrationScripts\\\\Command\\\\RunRegressionCommand\\:\\:mapProductVersionToPageBuilder\\(\\) expects string, array\\<string\\>\\|string\\|null given\\.$#" | ||
count: 1 | ||
path: src/Command/RunRegressionCommand.php | ||
|
||
- | ||
message: "#^Property Ibexa\\\\Platform\\\\ContiniousIntegrationScripts\\\\Command\\\\RunRegressionCommand\\:\\:\\$token \\(string\\|null\\) does not accept array\\<string\\>\\|string\\|null\\.$#" | ||
count: 1 | ||
path: src/Command/RunRegressionCommand.php | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
includes: | ||
- phpstan-baseline.neon | ||
|
||
parameters: | ||
level: 7 | ||
paths: | ||
- src |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Platform\ContiniousIntegrationScripts\Command; | ||
|
||
use Github\Client; | ||
use Ibexa\Platform\ContiniousIntegrationScripts\Helper\ComposerHelper; | ||
use Ibexa\Platform\ContiniousIntegrationScripts\ValueObject\ComposerPullRequestData; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Serializer\Encoder\JsonEncoder; | ||
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; | ||
use Symfony\Component\Serializer\Serializer; | ||
|
||
class LinkDependenciesCommand extends Command | ||
{ | ||
public const DEPENDENCIES_FILE = 'dependencies.json'; | ||
|
||
/** @var ?string */ | ||
private $token; | ||
|
||
/** @var \Symfony\Component\Serializer\Serializer */ | ||
private $serializer; | ||
|
||
/** @var string[] */ | ||
private $pullRequestUrls; | ||
|
||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
$this->serializer = new Serializer([new ObjectNormalizer()], [new JsonEncoder()]); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
$this->token = $input->getArgument('token'); | ||
|
||
$pullRequestsData = []; | ||
foreach ($this->pullRequestUrls as $pullRequestUrl) { | ||
$pullRequestsData[] = $this->getPullRequestData($pullRequestUrl); | ||
} | ||
|
||
$this->createDependenciesFile($pullRequestsData, $io); | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setName('dependencies:link') | ||
->setDescription(sprintf('Outputs a %s file that contains data about related Pull Requests', self::DEPENDENCIES_FILE)) | ||
->addArgument('token', InputArgument::OPTIONAL, 'GitHub OAuth token') | ||
; | ||
} | ||
|
||
private function getPullRequestData(string $pullRequestURL): ComposerPullRequestData | ||
{ | ||
$matches = []; | ||
preg_match('/.*github.com\/(.*)\/(.*)\/pull\/(\d+).*/', $pullRequestURL, $matches); | ||
[, $owner, $repository, $prNumber] = $matches; | ||
|
||
$client = new Client(); | ||
if ($this->token) { | ||
$client->authenticate($this->token, null, Client::AUTH_ACCESS_TOKEN); | ||
} | ||
|
||
$pullRequestDetails = $client->pullRequests()->show($owner, $repository, $prNumber); | ||
|
||
$pullRequestData = new ComposerPullRequestData(); | ||
$pullRequestData->repositoryUrl = $pullRequestDetails['head']['repo']['html_url']; | ||
$branchName = $pullRequestDetails['head']['ref']; | ||
|
||
$composerData = json_decode($client->repos()->contents()->download($owner, $repository, 'composer.json'), true); | ||
|
||
$aliases = array_keys($composerData['extra']['branch-alias']); | ||
$branchAlias = $composerData['extra']['branch-alias'][$aliases[0]]; | ||
|
||
$pullRequestData->package = $composerData['name']; | ||
$pullRequestData->requirement = sprintf('dev-%s as %s', $branchName, $branchAlias); | ||
|
||
return $pullRequestData; | ||
} | ||
|
||
protected function interact(InputInterface $input, OutputInterface $output): void | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
|
||
if (!$input->getArgument('token')) { | ||
$input->setArgument('token', ComposerHelper::getGitHubToken()); | ||
} | ||
|
||
$relatedPRsNumber = $io->ask('Please enter the number of related Pull Requests', '1', static function ($number) { | ||
if (!is_numeric($number) || $number < 1) { | ||
throw new \RuntimeException('Positive integer expected.'); | ||
} | ||
|
||
return (int) $number; | ||
}); | ||
|
||
$pullRequestUrls = []; | ||
for ($i = 0; $i < $relatedPRsNumber; ++$i) { | ||
$pullRequestUrls[] = $io->ask('Link to GitHub PR', null, static function ($answer) { | ||
if (!is_string($answer) || !str_contains($answer, 'github.com')) { | ||
throw new \RuntimeException( | ||
'Link to Pull Request on GitHub expected. Example: https://github.com/ibexa/recipes/pull/22' | ||
); | ||
} | ||
|
||
return $answer; | ||
}); | ||
} | ||
|
||
$this->pullRequestUrls = array_unique($pullRequestUrls); | ||
} | ||
|
||
/** | ||
* @param \Ibexa\Platform\ContiniousIntegrationScripts\ValueObject\ComposerPullRequestData[] $pullRequestsData | ||
*/ | ||
private function createDependenciesFile(array $pullRequestsData, SymfonyStyle $io): void | ||
{ | ||
$jsonContent = $this->serializer->serialize($pullRequestsData, 'json', ['json_encode_options' => JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT]); | ||
file_put_contents(self::DEPENDENCIES_FILE, $jsonContent); | ||
$io->success(sprintf('Successfully generated %s file', self::DEPENDENCIES_FILE)); | ||
} | ||
} |
Oops, something went wrong.