Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[DX] Add "setup-ci" command to ease integration to CI (#3425)
- Loading branch information
1 parent
8ef705e
commit 8a0b838
Showing
4 changed files
with
147 additions
and
25 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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Core\Console\Command; | ||
|
||
use Nette\Utils\FileSystem; | ||
use Nette\Utils\Strings; | ||
use OndraM\CiDetector\CiDetector; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Symfony\Component\Process\Process; | ||
|
||
final class SetupCICommand extends Command | ||
{ | ||
/** | ||
* @var string | ||
* @see https://regex101.com/r/etcmog/1 | ||
*/ | ||
private const GITHUB_REPOSITORY_REGEX = '#github\.com:(?<repository_name>.*?)\.git#'; | ||
|
||
public function __construct( | ||
private readonly SymfonyStyle $symfonyStyle | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->setName('setup-ci'); | ||
$this->setDescription('Add CI workflow to let Rector work for you'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
// detect current CI | ||
$ci = $this->resolveCurrentCI(); | ||
if ($ci === null) { | ||
$this->symfonyStyle->error('No CI detected'); | ||
return self::FAILURE; | ||
} | ||
|
||
if ($ci === CiDetector::CI_GITHUB_ACTIONS) { | ||
$rectorWorkflowFilePath = getcwd() . '/.github/workflows/rector.yaml'; | ||
if (file_exists($rectorWorkflowFilePath)) { | ||
$this->symfonyStyle->warning('The "rector.yaml" workflow already exists'); | ||
return self::SUCCESS; | ||
} | ||
|
||
$currentRepository = $this->resolveCurrentRepositoryName(getcwd()); | ||
if ($currentRepository === null) { | ||
$this->symfonyStyle->error('Current repository name could not be resolved'); | ||
|
||
return self::FAILURE; | ||
} | ||
|
||
$workflowTemplate = FileSystem::read(__DIR__ . '/../../../templates/rector-github-action-check.yaml'); | ||
|
||
$workflowContents = strtr($workflowTemplate, [ | ||
'__CURRENT_REPOSITORY__' => $currentRepository, | ||
]); | ||
|
||
FileSystem::write($rectorWorkflowFilePath, $workflowContents); | ||
$this->symfonyStyle->success('The "rector.yaml" workflow was added'); | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
/** | ||
* @return CiDetector::CI_*|null | ||
*/ | ||
private function resolveCurrentCI(): ?string | ||
{ | ||
if (file_exists(getcwd() . '/.github')) { | ||
return CiDetector::CI_GITHUB_ACTIONS; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private function resolveCurrentRepositoryName(string $currentDirectory): ?string | ||
{ | ||
// resolve current repository name | ||
$process = new Process(['git', 'remote', 'get-url', 'origin'], $currentDirectory, null, null, null); | ||
$process->run(); | ||
|
||
$output = $process->getOutput(); | ||
|
||
$match = Strings::match($output, self::GITHUB_REPOSITORY_REGEX); | ||
return $match['repository_name'] ?? null; | ||
} | ||
} |
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,48 @@ | ||
# github action that checks code with Rector | ||
name: Rector | ||
|
||
on: | ||
pull_request: null | ||
|
||
|
||
jobs: | ||
rector: | ||
# Don't run on forks. | ||
if: github.repository == '__CURRENT_REPOSITORY__' | ||
|
||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- | ||
uses: actions/checkout@v3 | ||
with: | ||
# needed to trigger workflow after push | ||
token: ${{ secrets.ACCESS_TOKEN }} | ||
|
||
- | ||
uses: shivammathur/setup-php@v2 | ||
with: | ||
php-version: 8.1 | ||
coverage: none | ||
|
||
- uses: "ramsey/composer-install@v2" | ||
|
||
## First run Rector - here can't be --dry-run !!! it would stop the job with it and not commit anything in the future | ||
- run: vendor/bin/rector --ansi | ||
|
||
- run: vendor/bin/ecs check --fix --ansi | ||
|
||
# see https://github.com/EndBug/add-and-commit | ||
- | ||
# commit only to core contributors who have repository access | ||
if: github.event.pull_request.head.repo.full_name == github.repository | ||
uses: EndBug/add-and-commit@v7.5.0 | ||
with: | ||
# The arguments for the `git add` command (see the paragraph below for more info) | ||
add: . | ||
message: "[rector] Rector fixes" | ||
author_name: "GitHub Action" | ||
author_email: "action@github.com" | ||
env: | ||
# to get push access | ||
GITHUB_TOKEN: ${{ secrets.ACCESS_TOKEN }} |