Skip to content

Commit

Permalink
Added commands to link dependencies and trigger regression run
Browse files Browse the repository at this point in the history
  • Loading branch information
mnocon committed Apr 20, 2021
1 parent e26f635 commit 59ff34f
Show file tree
Hide file tree
Showing 10 changed files with 446 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
/vendor/
composer.lock
.php_cs.cache
8 changes: 8 additions & 0 deletions .php_cs
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')
)
;
12 changes: 12 additions & 0 deletions bin/travis
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();
35 changes: 33 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,42 @@
"type": "library",
"license": "GPL-2.0-only",
"minimum-stability": "stable",
"require": {},
"require": {
"php": "^7.3",
"ext-json": "*",
"symfony/console": "^5.2",
"symfony/serializer": "^5.2",
"symfony/property-access": "^5.2",
"czproject/git-php": "^3.18",
"knplabs/github-api": "^3.0",
"php-http/guzzle6-adapter": "^2.0",
"guzzlehttp/guzzle": "^6.5",
"symfony/filesystem": "^5.0",
"http-interop/http-factory-guzzle": "^1.0"
},
"autoload": {
"psr-4": {
"Ibexa\\Platform\\ContiniousIntegrationScripts\\": "src/"
}
},
"extra": {
"branch-alias": {
"dev-main": "0.1.x-dev"
}
},
"bin": ["bin/check_cs.sh", "bin/prepare_project_edition.sh", "bin/update_docker.sh"]
"bin": [
"bin/check_cs.sh",
"bin/prepare_project_edition.sh",
"bin/update_docker.sh",
"bin/travis"
],
"require-dev": {
"ezsystems/ezplatform-code-style": "^1.0@dev",
"phpstan/phpstan": "~0.12"
},
"scripts": {
"phpstan": "phpstan analyse -c phpstan.neon",
"phpstan-baseline": "phpstan analyse -c phpstan.neon --generate-baseline",
"fix-cs": "php-cs-fixer fix -v --show-progress=estimating"
}
}
22 changes: 22 additions & 0 deletions phpstan-baseline.neon
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

7 changes: 7 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
includes:
- phpstan-baseline.neon

parameters:
level: 7
paths:
- src
136 changes: 136 additions & 0 deletions src/Command/LinkDependenciesCommand.php
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));
}
}
Loading

0 comments on commit 59ff34f

Please sign in to comment.