From c3db66d8c3ae7e26206095327111395837c5e504 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 14 Jun 2023 13:24:02 -0700 Subject: [PATCH 1/7] feat(dev): add release-info command --- dev/google-cloud | 2 + dev/src/Command/ReleaseInfoCommand.php | 102 ++++++++++++++++++ .../Unit/Command/ComponentInfoCommandTest.php | 5 +- dev/tests/Unit/Command/DocFxCommandTest.php | 5 +- .../Unit/Command/ReleaseInfoCommandTest.php | 79 ++++++++++++++ 5 files changed, 185 insertions(+), 8 deletions(-) create mode 100644 dev/src/Command/ReleaseInfoCommand.php create mode 100644 dev/tests/Unit/Command/ReleaseInfoCommandTest.php diff --git a/dev/google-cloud b/dev/google-cloud index 5cde74fd6071..7a1cedeaabb5 100755 --- a/dev/google-cloud +++ b/dev/google-cloud @@ -21,6 +21,7 @@ require __DIR__ . '/vendor/autoload.php'; use Google\Cloud\Dev\Command\AddComponentCommand; use Google\Cloud\Dev\Command\ComponentInfoCommand; use Google\Cloud\Dev\Command\DocFxCommand; +use Google\Cloud\Dev\Command\ReleaseInfoCommand; use Google\Cloud\Dev\Command\SplitCommand; use Symfony\Component\Console\Application; @@ -38,5 +39,6 @@ $app = new Application; $app->add(new AddComponentCommand($rootDirectory)); $app->add(new ComponentInfoCommand()); $app->add(new DocFxCommand()); +$app->add(new ReleaseInfoCommand()); $app->add(new SplitCommand($rootDirectory)); $app->run(); diff --git a/dev/src/Command/ReleaseInfoCommand.php b/dev/src/Command/ReleaseInfoCommand.php new file mode 100644 index 000000000000..9f1504248f4c --- /dev/null +++ b/dev/src/Command/ReleaseInfoCommand.php @@ -0,0 +1,102 @@ +httpClient = $httpClient ?: new Client(); + parent::__construct(); + } + + protected function configure() + { + $this->setName('release-info') + ->setDescription('list information for a google-cloud-php release') + ->addArgument('tag', InputArgument::REQUIRED, 'The git tag of the release (e.g. v0.200.0)') + ->addOption('token', 't', InputOption::VALUE_REQUIRED, 'Github token to use for authentication', '') + ->addOption('format', 'f', InputOption::VALUE_REQUIRED, 'output format, can be "json" or "shell"', 'shell') + ; + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $github = new Github( + new RunShell(), + $this->httpClient, + $input->getOption('token') + ); + + $changelog = $github->getChangelog( + 'googleapis/google-cloud-php', + $tag = $input->getArgument('tag') + ); + + $releaseNotes = new ReleaseNotes($changelog); + + $releases = []; + foreach (Component::getComponents() as $component) { + if ($version = $releaseNotes->getVersion($component->getId())) { + $releases[] = [ + 'component' => $component->getName(), + 'id' => $component->getId(), + 'version' => $version, + ]; + } + } + + switch ($input->getOption('format')) { + case 'shell': + (new Table($output)) + ->setHeaders(['component', 'id', 'version']) + ->setRows($releases) + ->render(); + break; + case 'json': + $releases = ['version' => $tag, 'releases' => $releases]; + $output->writeln(json_encode($releases, JSON_PRETTY_PRINT)); + break; + default: + throw new \InvalidArgumentException('invalid format'); + } + + return 0; + } +} \ No newline at end of file diff --git a/dev/tests/Unit/Command/ComponentInfoCommandTest.php b/dev/tests/Unit/Command/ComponentInfoCommandTest.php index b3d7964bb6d8..1ad8f63d0b02 100644 --- a/dev/tests/Unit/Command/ComponentInfoCommandTest.php +++ b/dev/tests/Unit/Command/ComponentInfoCommandTest.php @@ -20,7 +20,6 @@ use Google\Cloud\Dev\Command\ComponentInfoCommand; use Google\Cloud\Dev\Composer; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; @@ -46,9 +45,7 @@ class ComponentInfoCommandTest extends TestCase public static function setUpBeforeClass(): void { - $application = new Application(); - $application->add(new ComponentInfoCommand()); - self::$commandTester = new CommandTester($application->get('component-info')); + self::$commandTester = new CommandTester(new ComponentInfoCommand()); } public function testListAll() diff --git a/dev/tests/Unit/Command/DocFxCommandTest.php b/dev/tests/Unit/Command/DocFxCommandTest.php index 09d93281de07..cff9f394ea01 100644 --- a/dev/tests/Unit/Command/DocFxCommandTest.php +++ b/dev/tests/Unit/Command/DocFxCommandTest.php @@ -20,7 +20,6 @@ use Google\Cloud\Dev\Command\DocFxCommand; use Google\Cloud\Dev\DocFx\Node\ClassNode; use PHPUnit\Framework\TestCase; -use Symfony\Component\Console\Application; use Symfony\Component\Console\Tester\CommandTester; use Symfony\Component\Yaml\Yaml; use SimpleXMLElement; @@ -200,9 +199,7 @@ private function assertFileEqualsWithDiff(string $left, string $right, bool $upd private static function getCommandTester(): CommandTester { if (!isset(self::$commandTester)) { - $application = new Application(); - $application->add(new DocFxCommand()); - self::$commandTester = new CommandTester($application->get('docfx')); + self::$commandTester = new CommandTester(new DocFxCommand()); } return self::$commandTester; } diff --git a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php new file mode 100644 index 000000000000..55a41d89b117 --- /dev/null +++ b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php @@ -0,0 +1,79 @@ + '
google\/cloud-billing-budgets: 1.2.0<\/summary>' . + '### Features\r\n\r\n* Add resource_ancestors field to support filtering by folders & organizations ' . + '([#6320](https:\/\/github.com\/googleapis\/google-cloud-php\/issues\/6320))<\/details>' . + '
google\/cloud-build: 0.7.2<\/summary><\/details>' . + '\r\n\r\n---\r\nThis PR was generated with Release Please.', + ]; + + public function testReleaseInfo() + { + $tag = 'v0.1000.0'; + $response = $this->prophesize(ResponseInterface::class); + $response->getBody() + ->shouldBeCalledOnce() + ->willReturn(json_encode(self::$mockResponse)); + $http = $this->prophesize(Client::class); + $http->get( + 'https://api.github.com/repos/googleapis/google-cloud-php/releases/tags/' . $tag, + ['auth' => [null, null]] + ) + ->shouldBeCalledOnce() + ->willReturn($response->reveal()); +; + $commandTester = new CommandTester(new ReleaseInfoCommand($http->reveal())); + $commandTester->execute(['tag' => $tag, '--format' => 'json']); + + $display = $commandTester->getDisplay(); + $json = json_decode($display, true); + + $this->assertArrayHasKey('version', $json); + $this->assertEquals($tag, $json['version']); + $this->assertArrayHasKey('releases', $json); + $this->assertCount(2, $json['releases']); + $this->assertEquals([ + 'component' => 'BillingBudgets', + 'id' => 'cloud-billing-budgets', + 'version' => '1.2.0' + ], $json['releases'][0]); + $this->assertEquals([ + 'component' => 'Build', + 'id' => 'cloud-build', + 'version' => '0.7.2' + ], $json['releases'][1]); + } +} From 16f1d18269d612615ea85974f3f89a6289944fa9 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 14 Jun 2023 13:31:17 -0700 Subject: [PATCH 2/7] Update dev/src/Command/ReleaseInfoCommand.php --- dev/src/Command/ReleaseInfoCommand.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/src/Command/ReleaseInfoCommand.php b/dev/src/Command/ReleaseInfoCommand.php index 9f1504248f4c..188a2b7b8a49 100644 --- a/dev/src/Command/ReleaseInfoCommand.php +++ b/dev/src/Command/ReleaseInfoCommand.php @@ -38,7 +38,7 @@ class ReleaseInfoCommand extends Command private Client $httpClient; /** - * @param string $rootPath The path to the repository root directory. + * @param Client $httpClient specify the HTTP client, useful for testing */ public function __construct(Client $httpClient = null) { From 971bba16940b10bfab7d27407e0cf54542da717e Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 21 Jun 2023 10:30:37 -0700 Subject: [PATCH 3/7] Apply suggestions from code review --- dev/src/Command/ReleaseInfoCommand.php | 4 ++-- dev/tests/Unit/Command/ReleaseInfoCommandTest.php | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dev/src/Command/ReleaseInfoCommand.php b/dev/src/Command/ReleaseInfoCommand.php index 188a2b7b8a49..ba5e0aeaf6fd 100644 --- a/dev/src/Command/ReleaseInfoCommand.php +++ b/dev/src/Command/ReleaseInfoCommand.php @@ -30,7 +30,7 @@ use Symfony\Component\Console\Output\OutputInterface; /** - * List component details + * List release details * @internal */ class ReleaseInfoCommand extends Command @@ -99,4 +99,4 @@ protected function execute(InputInterface $input, OutputInterface $output) return 0; } -} \ No newline at end of file +} diff --git a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php index 55a41d89b117..804dff8825da 100644 --- a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php +++ b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php @@ -1,6 +1,6 @@ Date: Wed, 21 Jun 2023 10:41:59 -0700 Subject: [PATCH 4/7] fix test --- dev/tests/Unit/Command/ReleaseInfoCommandTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php index 804dff8825da..e3b7820f4b0d 100644 --- a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php +++ b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php @@ -54,7 +54,7 @@ public function testReleaseInfo() ) ->shouldBeCalledOnce() ->willReturn($response->reveal()); -; + $commandTester = new CommandTester(new ReleaseInfoCommand($http->reveal())); $commandTester->execute(['tag' => $tag, '--format' => 'json']); From 3dedeba35e6c3a63d91df19d94bc152f1f060288 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 21 Jun 2023 10:51:46 -0700 Subject: [PATCH 5/7] add release-info to README --- dev/README.md | 24 ++++++++++++------- .../Unit/Command/ReleaseInfoCommandTest.php | 7 +++++- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/dev/README.md b/dev/README.md index e16eb1968783..b41afe846116 100644 --- a/dev/README.md +++ b/dev/README.md @@ -1,10 +1,18 @@ # Google Cloud PHP Development Scripts -The `dev` component features helpful development tools for the following: - - Generating new libraries (`google-cloud add-component`) - - Listing component information (`google-cloud component-info`) - - Generating DocFX documentation (`google-cloud docfx`) - - Splitting the monorepo into sub repositories for releases (`google-cloud split`) - - Testing commands (`sh/static-analysis`) - -Run `dev/google-cloud` for a list of all available commands. +The `dev` component features helpful development tools. Run `dev/google-cloud` +for a list of all available commands: + +| Command | Description | +| ---------------- | ---------------------------------------------------------------- | +| `add-component` | Generate a new library | +| `component-info` | List component information | +| `docfx` | Generate DocFX YAML | +| `release-info` | List components and versions for a monorepo release | +| `split` | Split `google-cloud-php` into sub repositories and tag a release | + + +Additionally, there are scripts in the `sh` directory which are used in our CI: + +| Command | Description | +| `sh/static-analysis`| Run phpstan static ananlysis| diff --git a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php index e3b7820f4b0d..ddab9a42a1b1 100644 --- a/dev/tests/Unit/Command/ReleaseInfoCommandTest.php +++ b/dev/tests/Unit/Command/ReleaseInfoCommandTest.php @@ -22,6 +22,7 @@ use GuzzleHttp\Client; use PHPUnit\Framework\TestCase; use Psr\Http\Message\ResponseInterface; +use Psr\Http\Message\StreamInterface; use Symfony\Component\Console\Tester\CommandTester; use Prophecy\PhpUnit\ProphecyTrait; @@ -43,10 +44,14 @@ class ReleaseInfoCommandTest extends TestCase public function testReleaseInfo() { $tag = 'v0.1000.0'; + $body = $this->prophesize(StreamInterface::class); + $body->__toString() + ->shouldBeCalledOnce() + ->willReturn(json_encode(self::$mockResponse)); $response = $this->prophesize(ResponseInterface::class); $response->getBody() ->shouldBeCalledOnce() - ->willReturn(json_encode(self::$mockResponse)); + ->willReturn($body->reveal()); $http = $this->prophesize(Client::class); $http->get( 'https://api.github.com/repos/googleapis/google-cloud-php/releases/tags/' . $tag, From f222482bf96f5da9065076140e59878dd0ea91a0 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 21 Jun 2023 10:52:54 -0700 Subject: [PATCH 6/7] fix readme style --- dev/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/dev/README.md b/dev/README.md index b41afe846116..fd7a41530acc 100644 --- a/dev/README.md +++ b/dev/README.md @@ -14,5 +14,6 @@ for a list of all available commands: Additionally, there are scripts in the `sh` directory which are used in our CI: -| Command | Description | +| Command | Description | +| ------------------- | --------------------------- | | `sh/static-analysis`| Run phpstan static ananlysis| From 453df783e801552884130907398592853b129536 Mon Sep 17 00:00:00 2001 From: Brent Shaffer Date: Wed, 21 Jun 2023 13:37:23 -0700 Subject: [PATCH 7/7] move string to constant --- Dlp/composer.json | 2 +- dev/src/Command/ReleaseInfoCommand.php | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Dlp/composer.json b/Dlp/composer.json index f3040e7e6fed..eb89623054c5 100644 --- a/Dlp/composer.json +++ b/Dlp/composer.json @@ -5,7 +5,7 @@ "minimum-stability": "stable", "require": { "php": ">=7.4", - "google/gax": "^1.1" + "google/gax": "dev-middleware-enhance as v1.22.0" }, "require-dev": { "phpunit/phpunit": "^9.0", diff --git a/dev/src/Command/ReleaseInfoCommand.php b/dev/src/Command/ReleaseInfoCommand.php index ba5e0aeaf6fd..4638143c7ed8 100644 --- a/dev/src/Command/ReleaseInfoCommand.php +++ b/dev/src/Command/ReleaseInfoCommand.php @@ -37,6 +37,8 @@ class ReleaseInfoCommand extends Command { private Client $httpClient; + private const REPO_ID = 'googleapis/google-cloud-php'; + /** * @param Client $httpClient specify the HTTP client, useful for testing */ @@ -65,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output) ); $changelog = $github->getChangelog( - 'googleapis/google-cloud-php', + self::REPO_ID, $tag = $input->getArgument('tag') );