Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev): add release-info command #6363

Merged
merged 7 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dev/google-cloud
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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();
102 changes: 102 additions & 0 deletions dev/src/Command/ReleaseInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Copyright 2023 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Dev\Command;

use Google\Cloud\Dev\Component;
use Google\Cloud\Dev\GitHub;
use Google\Cloud\Dev\ReleaseNotes;
use Google\Cloud\Dev\RunShell;
use GuzzleHttp\Client;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* List component details
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
* @internal
*/
class ReleaseInfoCommand extends Command
{
private Client $httpClient;

/**
* @param Client $httpClient specify the HTTP client, useful for testing
*/
public function __construct(Client $httpClient = null)
{
$this->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',
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
$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;
}
}
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
5 changes: 1 addition & 4 deletions dev/tests/Unit/Command/ComponentInfoCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -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());
Copy link
Contributor Author

@bshaffer bshaffer Jun 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just cleaning up / simplifying these tests, as we can just create the CommandTester directly when we don't need to test things like Question Helpers (e.g. $this->getHelper('question'))

TLDR: it's cleanup that is unrelated to the release-info command

}

public function testListAll()
Expand Down
5 changes: 1 addition & 4 deletions dev/tests/Unit/Command/DocFxCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
return self::$commandTester;
}
Expand Down
79 changes: 79 additions & 0 deletions dev/tests/Unit/Command/ReleaseInfoCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
/**
* Copyright 2022 Google LLC
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Google\Cloud\Dev\Tests\Unit\Command;

use Google\Cloud\Dev\Command\ReleaseInfoCommand;
use Google\Cloud\Dev\Composer;
use GuzzleHttp\Client;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Prophecy\PhpUnit\ProphecyTrait;

/**
* @group dev
*/
class ReleaseInfoCommandTest extends TestCase
{
use ProphecyTrait;

private static array $mockResponse = [
'body' => '<details><summary>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>' .
'<details><summary>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());
;
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
$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]);
}
}