-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add exclude-patterns to cs fixer shared workflow (#153)
- Loading branch information
Showing
6 changed files
with
264 additions
and
14 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
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 |
---|---|---|
@@ -1,16 +1,25 @@ | ||
<?php | ||
|
||
require __DIR__ . '/vendor/autoload.php'; | ||
|
||
use Symfony\Component\Yaml\Yaml; | ||
|
||
$workflow = Yaml::parse(file_get_contents(__DIR__ . '/.github/workflows/code-standards.yml')); | ||
$rules = json_decode($workflow['on']['workflow_call']['inputs']['rules']['default'], true); | ||
$rulesJson = getenv('RULES'); | ||
$configPath = getenv('CONFIG_PATH'); | ||
$excludePatternsJson = getenv('EXCLUDE_PATTERNS'); | ||
|
||
if (!$rulesJson) { | ||
// Use default rules | ||
$workflow = Yaml::parse(file_get_contents(__DIR__ . '/.github/workflows/code-standards.yml')); | ||
$rulesJson = $workflow['on']['workflow_call']['inputs']['rules']['default']; | ||
} | ||
|
||
$rules = json_decode($rulesJson, true); | ||
$excludePatterns = json_decode($excludePatternsJson ?: '[]', true); | ||
|
||
return (new PhpCsFixer\Config()) | ||
->setRules($rules) | ||
->setFinder( | ||
PhpCsFixer\Finder::create() | ||
->in(__DIR__) | ||
->in($configPath ?: __DIR__) | ||
->notPath($excludePatterns) | ||
) | ||
; |
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,32 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
/** | ||
* Copyright 2024 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. | ||
*/ | ||
|
||
if (file_exists($autoload = __DIR__ . '/../vendor/autoload.php')) { | ||
// ran in cloned repo (e.g. "git clone https://github.com/GoogleCloudPlatform/php-tools") | ||
require $autoload; | ||
} elseif (file_exists($autoload = __DIR__ . '/../../../autoload.php')) { | ||
// ran in composer installed repo (e.g. "composer install google/cloud-tools") | ||
require $autoload; | ||
} | ||
|
||
use Google\Cloud\Utils\Actions\RunCsFixerCommand; | ||
use Symfony\Component\Console\Application; | ||
|
||
$app = new Application(); | ||
$app->add(new RunCsFixerCommand()); | ||
$app->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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,171 @@ | ||
<?php | ||
/** | ||
* Copyright 2024 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\Utils\Actions; | ||
|
||
use GuzzleHttp\Client; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Yaml\Yaml; | ||
|
||
/** | ||
* CLI command for running the CS Fixer shared workflow. | ||
*/ | ||
class RunCsFixerCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
$this | ||
->setName('cs-fixer') | ||
->setDescription('Execute a command with the deployed image') | ||
->addArgument( | ||
'repo', | ||
InputArgument::REQUIRED, | ||
'The name of the repo to run the CS fixer for' | ||
) | ||
->addOption( | ||
'workflow-file', | ||
'', | ||
InputOption::VALUE_REQUIRED, | ||
'name of the github workflow file which contains the configuration', | ||
'lint.yaml' | ||
) | ||
->addOption( | ||
'ref', | ||
'', | ||
InputOption::VALUE_REQUIRED, | ||
'The branch of the repo run the CS fixer for', | ||
'main' | ||
) | ||
->addOption( | ||
'flags', | ||
'', | ||
InputOption::VALUE_REQUIRED, | ||
'The flags to pass down to the CS fixer', | ||
'--dry-run --diff' | ||
); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$repo = $input->getArgument('repo'); | ||
if (false === strpos($repo, '/')) { | ||
throw new \Exception('Invalid repo name. Use the format: owner/repo'); | ||
} | ||
|
||
$url = sprintf( | ||
'https://raw.githubusercontent.com/%s/%s/.github/workflows/%s', | ||
$repo, | ||
$input->getOption('ref'), | ||
$input->getOption('workflow-file'), | ||
); | ||
|
||
$client = new Client(['http_errors' => false]); | ||
$response = $client->request('GET', $url); | ||
if ($response->getStatusCode() === 404) { | ||
throw new \Exception(sprintf( | ||
'Failed to fetch the workflow file at "%s", maybe it doesn\'t exist? ' | ||
. 'Try supplying the "--workflow-file" option.', | ||
$url | ||
)); | ||
} | ||
$workflow = Yaml::parse($response->getBody()); | ||
$job = null; | ||
|
||
foreach ($workflow['jobs'] as $id => $workflowJob) { | ||
if (str_contains($workflowJob['uses'] ?? '', '.github/workflows/code-standards.yml')) { | ||
$output->writeln(sprintf('Found job "%s"', $workflowJob['name'] ?? $id)); | ||
$job = $workflowJob; | ||
break; | ||
} | ||
} | ||
if (!$job) { | ||
throw new \Exception('No job found for php-tools/code-standards.yaml in the workflow file'); | ||
} | ||
|
||
// get the default config | ||
$defaultWorkflow = Yaml::parse(file_get_contents(__DIR__ . '/../../../.github/workflows/code-standards.yml')); | ||
$defaults = []; | ||
foreach ($defaultWorkflow['on']['workflow_call']['inputs'] as $name => $inputOptions) { | ||
$defaults[$name] = $inputOptions['default'] ?? ''; | ||
} | ||
$options = array_merge($defaults, $job['with']); | ||
|
||
if (str_starts_with($options['config'], 'GoogleCloudPlatform/php-tools/')) { | ||
// use local file | ||
$options['config'] = str_replace( | ||
'GoogleCloudPlatform/php-tools/', | ||
__DIR__ . '/../../../', | ||
$options['config'] | ||
); | ||
// strip branch (we'll ignore it in favor of the current branch) | ||
if (false !== $i = strpos($options['config'], '@')) { | ||
$options['config'] = substr($options['config'], 0, $i); | ||
} | ||
if (!file_exists($options['config'])) { | ||
throw new \Exception('config file not found: ' . realpath($options['config'])); | ||
} | ||
} | ||
|
||
// go through config options and set env vars accordingly | ||
$rules = json_encode(array_merge( | ||
json_decode($options['rules'], true), | ||
json_decode($options['add-rules'], true) | ||
)); | ||
|
||
$excludePatterns = str_replace(["\n", ' '], '', $options['exclude-patterns']); | ||
|
||
// use config path only if EXCLUDE_PATTERN is empty | ||
if ($options['config']) { | ||
// set environment variables so they're available in the CONFIG file | ||
$env = sprintf( | ||
'CONFIG_PATH=%s RULES=$\'%s\' EXCLUDE_PATTERNS=$\'%s\'', | ||
$options['path'], | ||
$rules, | ||
$excludePatterns, | ||
); | ||
// Run command using the --config flag | ||
$cmd = sprintf( | ||
'%s ~/.composer/vendor/bin/php-cs-fixer fix --config=%s %s', | ||
$env, | ||
$options['config'], | ||
$input->getOption('flags') | ||
); | ||
} else { | ||
// Run command using the --rules flag | ||
$cmd = sprintf( | ||
'~/.composer/vendor/bin/php-cs-fixer fix %s --rules=$\'%s\' %s', | ||
$options['path'], | ||
$rules, | ||
$input->getOption('flags') | ||
); | ||
} | ||
|
||
$output->writeln('Executing the following command: '); | ||
$output->writeln(''); | ||
$output->writeln("<info>\t" . $cmd . '</>'); | ||
$output->writeln(''); | ||
|
||
// @TODO use Symfony process component to run this | ||
passthru($cmd, $resultCode); | ||
|
||
return $resultCode; | ||
} | ||
} |