diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml index 11b979c..05453ba 100644 --- a/.github/workflows/continuous-integration.yml +++ b/.github/workflows/continuous-integration.yml @@ -22,6 +22,7 @@ env: COMPOSER_FLAGS: --ansi --no-interaction --no-progress COMPOSER_INSTALL_FLAGS: --prefer-dist COMPOSER_UPDATE_FLAGS: '' + GS_CI: 0 jobs: validation: diff --git a/README.md b/README.md index 8ac75c8..b624d90 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ composer config extra.gilbertsoft/typo3-core-patches.ignore-branch true When running `composer update` or `composer install`, the plugin detects changes that already exist in the version being installed and suggests removing them. If you run Composer with the `--no-interaction` option, the patches are always -removed. +preserved. This can be changed by the config `force-tidy-patches` see bellow. Errors may occur if you use the source-dist of packages, which can be solved by adding the `config.discard-changes` configuration option to your `composer.json`, @@ -144,6 +144,28 @@ see . Run e.g. `composer config discard-changes true` to add the configuration to your `composer.json`. +If a CI environment is detected, the detection of merged changes is skipped by +default. To change this behavior and enable the detection again, run: + +```bash +composer config extra.gilbertsoft/typo3-core-patches.force-tidy-patches true +``` + +To disable the detection of merged changes completely, run: + +```bash +composer config extra.gilbertsoft/typo3-core-patches.disable-tidy-patches true +``` + +## CI detection + +The plugin tries to detect CI environments and changes its default behavior +while running in a CI pipeline. It's possible to override the detection by +setting an environment variable: + +- Set `GS_CI=1` to force CI mode +- Set `GS_CI=0` to disable CI mode + ## Feedback / Bug reports / Contribution Bug reports, feature requests and pull requests are welcome in the [GitHub diff --git a/src/Config.php b/src/Config.php index ddc2468..13933c6 100644 --- a/src/Config.php +++ b/src/Config.php @@ -75,6 +75,16 @@ final class Config implements PersistenceInterface */ private const PLUGIN_IGNORE_BRANCH = 'ignore-branch'; + /** + * @var string + */ + private const PLUGIN_DISABLE_TIDY_PATCHES = 'disable-tidy-patches'; + + /** + * @var string + */ + private const PLUGIN_FORCE_TIDY_PATCHES = 'force-tidy-patches'; + private JsonFile $jsonFile; private ConfigSourceInterface $configSource; @@ -91,6 +101,10 @@ final class Config implements PersistenceInterface private bool $ignoreBranch = \false; + private bool $disableTidyPatches = \false; + + private bool $forceTidyPatches = \false; + public function __construct( ?JsonFile $jsonFile = null, ?ConfigSourceInterface $configSource = null @@ -152,6 +166,30 @@ public function getPatches(): Patches return $this->patches; } + public function getDisableTidyPatches(): bool + { + return $this->disableTidyPatches; + } + + public function setDisableTidyPatches(bool $disableTidyPatches): self + { + $this->disableTidyPatches = $disableTidyPatches; + + return $this; + } + + public function getForceTidyPatches(): bool + { + return $this->forceTidyPatches; + } + + public function setForceTidyPatches(bool $forceTidyPatches): self + { + $this->forceTidyPatches = $forceTidyPatches; + + return $this; + } + private function isEmpty(): bool { if (!$this->changes->isEmpty()) { @@ -166,7 +204,15 @@ private function isEmpty(): bool return false; } - return !$this->ignoreBranch; + if ($this->ignoreBranch) { + return false; + } + + if ($this->disableTidyPatches) { + return false; + } + + return !$this->forceTidyPatches; } public function load(): self @@ -259,6 +305,14 @@ public function jsonSerialize(): array $config[self::PLUGIN_IGNORE_BRANCH] = $this->ignoreBranch; } + if ($this->disableTidyPatches) { + $config[self::PLUGIN_DISABLE_TIDY_PATCHES] = $this->disableTidyPatches; + } + + if ($this->forceTidyPatches) { + $config[self::PLUGIN_FORCE_TIDY_PATCHES] = $this->forceTidyPatches; + } + return $config; } @@ -318,6 +372,18 @@ public function jsonUnserialize(array $json): self $this->ignoreBranch = $ignoreBranch; + if (!is_bool($disableTidyPatches = $packageConfig[self::PLUGIN_DISABLE_TIDY_PATCHES] ?? null)) { + $disableTidyPatches = false; + } + + $this->disableTidyPatches = $disableTidyPatches; + + if (!is_bool($forceTidyPatches = $packageConfig[self::PLUGIN_FORCE_TIDY_PATCHES] ?? null)) { + $forceTidyPatches = false; + } + + $this->forceTidyPatches = $forceTidyPatches; + return $this; } } diff --git a/src/Plugin.php b/src/Plugin.php index 85fefcf..e68a138 100644 --- a/src/Plugin.php +++ b/src/Plugin.php @@ -18,20 +18,25 @@ use Composer\DependencyResolver\Operation\UpdateOperation; use Composer\DependencyResolver\Transaction; use Composer\EventDispatcher\EventSubscriberInterface; +use Composer\Factory; use Composer\Installer\InstallerEvent; use Composer\Installer\InstallerEvents; use Composer\IO\IOInterface; +use Composer\Json\JsonFile; use Composer\Plugin\Capability\CommandProvider as ComposerCommandProvider; use Composer\Plugin\Capable; use Composer\Plugin\PluginInterface; use Composer\Script\Event; use Composer\Script\ScriptEvents; use GsTYPO3\CorePatches\Utility\ComposerUtils; +use GsTYPO3\CorePatches\Utility\Utils; final class Plugin implements PluginInterface, Capable, EventSubscriberInterface { private ComposerUtils $composerUtils; + private Config $config; + /** * @var array */ @@ -43,6 +48,10 @@ final class Plugin implements PluginInterface, Capable, EventSubscriberInterface public function activate(Composer $composer, IOInterface $io): void { $this->composerUtils = new ComposerUtils($composer, $io); + $this->config = new Config( + new JsonFile(Factory::getComposerFile(), Factory::createHttpDownloader($io, $composer->getConfig()), $io), + $composer->getConfig()->getConfigSource() + ); $composer->getConfig()->getConfigSource()->addConfigSetting('allow-plugins.cweagans/composer-patches', true); } @@ -100,6 +109,16 @@ public function checkForObsoletePatches(InstallerEvent $installerEvent): void return; } + $this->config->load(); + + if (Utils::isCI() && !$this->config->getForceTidyPatches()) { + return; + } + + if ($this->config->getDisableTidyPatches()) { + return; + } + $installerEvent->getIO()->write('Checking for obsolete patches, this may take a while...'); foreach ($installerEvent->getTransaction()->getOperations() as $operation) { diff --git a/src/Utility/ComposerUtils.php b/src/Utility/ComposerUtils.php index 6093b92..4c8af82 100644 --- a/src/Utility/ComposerUtils.php +++ b/src/Utility/ComposerUtils.php @@ -361,7 +361,7 @@ private function askRemoval(int $changeId): bool ' Should the patch for this change be removed? [Y,n] ', $changeId ), - true + $this->config->load()->getForceTidyPatches() ); } diff --git a/src/Utility/Utils.php b/src/Utility/Utils.php new file mode 100644 index 0000000..1c736b3 --- /dev/null +++ b/src/Utility/Utils.php @@ -0,0 +1,36 @@ +prophesize(JsonFile::class); $jsonFileProphecy->read()->willReturn($configuration); @@ -60,6 +62,8 @@ public function testLoadWorksProperly( self::assertCount($expectedPreferredInstallChangedCount, $config->getPreferredInstallChanged()); self::assertSame($expectedPatchDirectory, $config->getPatchDirectory()); self::assertSame($expectedIgnoreBranch, $config->getIgnoreBranch()); + self::assertSame($expectedDisableTidyPatches, $config->getDisableTidyPatches()); + self::assertSame($expectedForceTidyPatches, $config->getForceTidyPatches()); } /** @@ -70,7 +74,9 @@ public function testLoadWorksProperly( * expectedAppliedChangesCount: int, * expectedPreferredInstallChangedCount: int, * expectedPatchDirectory: string, - * expectedIgnoreBranch: bool + * expectedIgnoreBranch: bool, + * expectedDisableTidyPatches: bool, + * expectedForceTidyPatches: bool * }> */ public function configurationLoadProvider(): Iterator @@ -115,6 +121,8 @@ public function configurationLoadProvider(): Iterator 'preferred-install-changed' => ['package1', 'package2'], 'patch-directory' => 'patch-dir', 'ignore-branch' => true, + 'disable-tidy-patches' => true, + 'force-tidy-patches' => true, ], ], ], @@ -124,6 +132,8 @@ public function configurationLoadProvider(): Iterator 'expectedPreferredInstallChangedCount' => 2, 'expectedPatchDirectory' => 'patch-dir', 'expectedIgnoreBranch' => true, + 'expectedDisableTidyPatches' => true, + 'expectedForceTidyPatches' => true, ]; yield 'empty configuration' => [ 'configuration' => null, @@ -133,6 +143,8 @@ public function configurationLoadProvider(): Iterator 'expectedPreferredInstallChangedCount' => 0, 'expectedPatchDirectory' => 'patches', 'expectedIgnoreBranch' => false, + 'expectedDisableTidyPatches' => false, + 'expectedForceTidyPatches' => false, ]; yield 'empty extra' => [ 'configuration' => ['extra' => []], @@ -142,6 +154,8 @@ public function configurationLoadProvider(): Iterator 'expectedPreferredInstallChangedCount' => 0, 'expectedPatchDirectory' => 'patches', 'expectedIgnoreBranch' => false, + 'expectedDisableTidyPatches' => false, + 'expectedForceTidyPatches' => false, ]; } @@ -402,6 +416,8 @@ public function configurationSaveProvider(): Iterator ], 'patch-directory' => 'patch-dir', 'ignore-branch' => true, + 'disable-tidy-patches' => true, + 'force-tidy-patches' => true, ], ], ], @@ -447,6 +463,8 @@ public function configurationSaveProvider(): Iterator ], 'patch-directory' => 'patch-dir', 'ignore-branch' => true, + 'disable-tidy-patches' => true, + 'force-tidy-patches' => true, ], ], ], @@ -497,5 +515,13 @@ public function testGettersAndSetters(): void self::assertTrue($config->getIgnoreBranch()); self::assertSame($config->getPatches(), $config->getPatches()); + + self::assertFalse($config->getDisableTidyPatches()); + self::assertSame($config, $config->setDisableTidyPatches(true)); + self::assertTrue($config->getDisableTidyPatches()); + + self::assertFalse($config->getForceTidyPatches()); + self::assertSame($config, $config->setForceTidyPatches(true)); + self::assertTrue($config->getForceTidyPatches()); } } diff --git a/tests/Unit/Utility/ComposerUtilsTest.php b/tests/Unit/Utility/ComposerUtilsTest.php index 435182f..e98ec14 100644 --- a/tests/Unit/Utility/ComposerUtilsTest.php +++ b/tests/Unit/Utility/ComposerUtilsTest.php @@ -43,6 +43,7 @@ * @uses \GsTYPO3\CorePatches\Gerrit\Entity\IncludedInInfo * @uses \GsTYPO3\CorePatches\Gerrit\RestApi * @uses \GsTYPO3\CorePatches\Utility\PatchUtils + * @uses \GsTYPO3\CorePatches\Utility\Utils */ final class ComposerUtilsTest extends TestCase { diff --git a/tests/Unit/Utility/UtilsTest.php b/tests/Unit/Utility/UtilsTest.php new file mode 100644 index 0000000..a3178ec --- /dev/null +++ b/tests/Unit/Utility/UtilsTest.php @@ -0,0 +1,33 @@ +