Skip to content

Commit

Permalink
Make it possible to check for several packages that has updated
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriksm committed Oct 16, 2023
1 parent 0708f64 commit 4bc7cc8
Showing 1 changed file with 61 additions and 29 deletions.
90 changes: 61 additions & 29 deletions src/Updater.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ class Updater
protected $shouldThrowOnUnupdated = true;

/**
* @var string
* @var array
*/
protected $packageToCheck;
protected $packagesToCheck = [];

/**
* @return bool
Expand All @@ -94,12 +94,19 @@ public function setShouldThrowOnUnupdated(bool $shouldThrowOnUnupdated)
$this->shouldThrowOnUnupdated = $shouldThrowOnUnupdated;
}

/**
* @deprecated This method should not be used, and instead we should use the
* one that accepts an array of packages. @see ::setPackagesToCheckHasUpdated
*/
public function setPackageToCheckHasUpdated($package)
{
$this->packageToCheck = $package;
$this->setPackagesToCheckHasUpdated([$package]);
}


public function setPackagesToCheckHasUpdated(array $packages)
{
$this->packagesToCheck = $packages;
}

/**
* @return bool
Expand Down Expand Up @@ -175,7 +182,7 @@ public function __construct($cwd, $package)
{
$this->cwd = $cwd;
$this->package = $package;
$this->packageToCheck = $package;
$this->setPackagesToCheckHasUpdated([$package]);
}

/**
Expand Down Expand Up @@ -264,10 +271,17 @@ public function setDevPackage($devPackage)
$this->devPackage = $devPackage;
}

public function executeRequire($new_version)
protected function getPreUpdataData() : array
{
$pre_update_lock = ComposerLockData::createFromFile($this->cwd . '/composer.lock');
$pre_update_data = $pre_update_lock->getPackageData($this->packageToCheck);
return array_map(function($package) use ($pre_update_lock) {
return $pre_update_lock->getPackageData($package);
}, $this->packagesToCheck);
}

public function executeRequire($new_version)
{
$pre_update_data = $this->getPreUpdateData();

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 7.4)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 8.1)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 7.2)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 8.1)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 8.0)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 7.3)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 7.3)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 7.2)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 8.1)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 8.0)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (2, 7.4)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 8.0)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 7.3)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 8.0)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 8.1)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 7.2)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 7.2)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 7.4)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 7.3)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().

Check failure on line 284 in src/Updater.php

View workflow job for this annotation

GitHub Actions / Run tests (1, 7.4)

Call to an undefined method Violinist\ComposerUpdater\Updater::getPreUpdateData().
$commands = $this->getRequireRecipes($new_version);
$exception = null;
$success = false;
Expand Down Expand Up @@ -313,8 +327,7 @@ public function executeRequire($new_version)
*/
public function executeUpdate()
{
$pre_update_lock = ComposerLockData::createFromFile($this->cwd . '/composer.lock');
$pre_update_data = $pre_update_lock->getPackageData($this->packageToCheck);
$pre_update_data = $this->getPreUpdataData();
$commands = $this->getUpdateRecipies();
$exception = null;
$success = false;
Expand Down Expand Up @@ -354,7 +367,7 @@ public function executeUpdate()
}
}

protected function handlePostComposerCommand($pre_update_data, Process $process)
protected function handlePostComposerCommand(array $pre_update_data_array, Process $process)
{
$new_lock_data = @json_decode(@file_get_contents(sprintf('%s/composer.lock', $this->cwd)));
if (!$new_lock_data) {
Expand All @@ -366,27 +379,36 @@ protected function handlePostComposerCommand($pre_update_data, Process $process)
$this->log($process->getErrorOutput());
throw new \Exception($message);
}
$post_update_data = ComposerLockData::createFromString(json_encode($new_lock_data))->getPackageData($this->packageToCheck);
$version_to = $post_update_data->version;
$version_from = $pre_update_data->version;
if (isset($post_update_data->source) && $post_update_data->source->type == 'git' && isset($pre_update_data->source)) {
$version_from = $pre_update_data->source->reference;
$version_to = $post_update_data->source->reference;
$has_updated_at_least_one_package = false;
foreach ($this->packagesToCheck as $package) {
$pre_update_data = $this->getPreUpdataDataForPackageFromArray($pre_update_data_array, $package);
$post_update_data = ComposerLockData::createFromString(json_encode($new_lock_data))->getPackageData($package);
$version_to = $post_update_data->version;
$version_from = $pre_update_data->version;
if (isset($post_update_data->source) && $post_update_data->source->type == 'git' && isset($pre_update_data->source)) {
$version_from = $pre_update_data->source->reference;
$version_to = $post_update_data->source->reference;
}
if ($version_from === $version_to) {
// In theory though, the reference sources can be the same (the same commit), but the
// version is different. In which case it does not really matter much to update, but it
// can be frustrating to get an error. So let's not give an error.
if ($post_update_data->version === $pre_update_data->version) {
$this->log($process->getErrorOutput(), [
'package' => $this->package,
]);
} else {
$has_updated_at_least_one_package = true;
}
} else {
$has_updated_at_least_one_package = true;
}
}
if ($this->shouldThrowOnUnupdated && $version_to === $version_from) {
if ($this->shouldThrowOnUnupdated && !$has_updated_at_least_one_package) {
// Nothing has happened here. Although that can be alright (like we
// have updated some dependencies of this package) this is not what
// this service does, currently, and also the title of the PR would be
// wrong.
// In theory though, the reference sources can be the same (the same commit), but the
// version is different. In which case it does not really matter much to update, but it
// can be frustrating to get an error. So let's not give an error.
if ($post_update_data->version === $pre_update_data->version) {
$this->log($process->getErrorOutput(), [
'package' => $this->package,
]);
throw new NotUpdatedException('The version installed is still the same after trying to update.');
}
// have updated some dependencies of this package), we have at least
// not updated any of the expected dependencies at this point.
throw new NotUpdatedException('The version installed is still the same after trying to update.');
}
// We still want the post update data to be from the actual package though, no matter if we were actually
// checking if a dependency was updated or not.
Expand All @@ -397,6 +419,16 @@ protected function handlePostComposerCommand($pre_update_data, Process $process)
$this->log($process->getErrorOutput());
}

protected function getPreUpdataDataForPackageFromArray(array $pre_update_data_array, $package) : ?\stdClass
{
foreach ($pre_update_data_array as $item) {
if ($item->name === $package) {
return $item;
}
}
return null;
}

/**
* @return \stdClass
*/
Expand Down

0 comments on commit 4bc7cc8

Please sign in to comment.