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 all 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: 1 addition & 1 deletion Dlp/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 17 additions & 8 deletions dev/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
# 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|
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();
104 changes: 104 additions & 0 deletions dev/src/Command/ReleaseInfoCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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 release details
* @internal
*/
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
*/
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(
self::REPO_ID,
$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;
}
}
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
84 changes: 84 additions & 0 deletions dev/tests/Unit/Command/ReleaseInfoCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Copyright 2023 Google LLC
*
* 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 Psr\Http\Message\StreamInterface;
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';
$body = $this->prophesize(StreamInterface::class);
$body->__toString()
->shouldBeCalledOnce()
->willReturn(json_encode(self::$mockResponse));
$response = $this->prophesize(ResponseInterface::class);
$response->getBody()
->shouldBeCalledOnce()
->willReturn($body->reveal());
$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]);
}
}