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

UHF-10713 #44

Merged
merged 6 commits into from
Dec 17, 2024
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ $fileManager->updateFiles($options, [
```
## Checking composer package versions

Checks whether HELfi packages are up-to-date. This is used as a part of our [Automatic update workflow](https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/documentation/automatic-updates.md) to warn if composer was unable to update any of our dependencies.
Checks that packages are up-to-date. This is used as a part of our [Automatic update workflow](https://github.com/City-of-Helsinki/drupal-helfi-platform/blob/main/documentation/automatic-updates.md) to warn if composer was unable to update any of our dependencies.

Run `drush helfi:tools:check-composer-versions /path/to/composer.lock`.

Expand Down
44 changes: 21 additions & 23 deletions src/Drush/Commands/PackageScannerDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

namespace DrupalTools\Drush\Commands;

use ComposerLockParser\ComposerInfo;
use Consolidation\AnnotatedCommand\CommandResult;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Drupal\Component\DependencyInjection\ContainerInterface;
use Drupal\helfi_api_base\Package\VersionChecker;
use DrupalTools\OutputFormatters\MarkdownTableFormatter;
use DrupalTools\Package\ComposerOutdatedProcess;
use DrupalTools\Package\VersionChecker;
use Drush\Attributes\Argument;
use Drush\Attributes\Bootstrap;
use Drush\Attributes\Command;
use Drush\Attributes\FieldLabels;
use Drush\Boot\DrupalBootLevels;
use Drush\Commands\DrushCommands;
use Drush\Drush;
use Psr\Container\ContainerInterface as DrushContainer;

/**
Expand All @@ -25,36 +25,38 @@ final class PackageScannerDrushCommands extends DrushCommands {
/**
* Constructs a new instance.
*
* @param \Drupal\helfi_api_base\Package\VersionChecker $versionChecker
* @param \DrupalTools\Package\VersionChecker $versionChecker
* The version checker service.
*/
public function __construct(
private readonly VersionChecker $versionChecker,
) {
parent::__construct();
}

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, ?DrushContainer $drush = NULL): self {
if (!$drush && Drush::hasContainer()) {
$drush = Drush::getContainer();
}
public static function create(DrushContainer $drush): self {
/** @var \Drush\Formatters\DrushFormatterManager $formatterManager */
$formatterManager = $drush->get('formatterManager');

// @todo Figure out if there's a better way to inject this service.
if (!$formatterManager->hasFormatter('markdown_table')) {
$formatterManager->addFormatter('markdown_table', new MarkdownTableFormatter());
}
return new self(
$container->get('helfi_api_base.package_version_checker'),
);

$process = new ComposerOutdatedProcess();
$versionChecker = new VersionChecker($process);

return new self($versionChecker);
}

/**
* Checks whether Composer packages are up-to-date.
*
* Wrapper for `composer outdated` command.
*
* @param string $file
* The path to composer.lock file.
* @param array $options
Expand All @@ -64,28 +66,24 @@ public static function create(ContainerInterface $container, ?DrushContainer $dr
* The result.
*/
#[Command(name: 'helfi:tools:check-composer-versions')]
#[Bootstrap(level: DrupalBootLevels::NONE)]
#[Argument(name: 'file', description: 'Path to composer.lock file')]
#[FieldLabels(labels: [
'name' => 'Name',
'version' => 'Current version',
'latest' => 'Latest version',
])]
public function checkVersions(string $file, array $options = ['format' => 'table']) : CommandResult {
$info = new ComposerInfo($file);

$rows = [];
/** @var \Composer\Package\Package $package */
foreach (iterator_to_array($info->getPackages()) as $package) {
$version = $this->versionChecker->get($package->getName(), $package->getVersion());

// Skip dev versions since we can't easily verify the latest
// version.
if (!$version || $version->isLatest || str_starts_with($package->getVersion(), 'dev-')) {
foreach ($this->versionChecker->getOutdated($file) as $version) {
hyrsky marked this conversation as resolved.
Show resolved Hide resolved
// Skip dev versions since we can't easily verify the latest version.
if (str_starts_with($version->version, 'dev-')) {
continue;
}

$rows[] = [
'name' => $package->getName(),
'version' => $package->getVersion(),
'name' => $version->name,
'version' => $version->version,
'latest' => $version->latestVersion,
];
}
Expand Down
5 changes: 4 additions & 1 deletion src/Drush/Commands/UpdateDrushCommands.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#[Bootstrap(DrupalBootLevels::NONE)]
final class UpdateDrushCommands extends DrushCommands {

private const BASE_URL = 'https://raw.githubusercontent.com/City-of-Helsinki/drupal-helfi-platform/main/';
private const BASE_URL = 'https://raw.githubusercontent.com/City-of-Helsinki/drupal-helfi-platform/';

/**
* The git root.
Expand All @@ -37,6 +37,7 @@ final class UpdateDrushCommands extends DrushCommands {
private ?string $gitRoot = NULL;

public const DEFAULT_OPTIONS = [
'branch' => 'main',
'ignore-files' => TRUE,
'update-external-packages' => TRUE,
'self-update' => TRUE,
Expand Down Expand Up @@ -124,6 +125,7 @@ private function parseOptions(array $options, string $root) : UpdateOptions {

return new UpdateOptions(
ignoreFiles: $ignoreFiles,
branch: $options['branch'],
updateExternalPackages: $options['update-external-packages'],
selfUpdate: $options['self-update'],
runMigrations: $options['run-migrations'],
Expand Down Expand Up @@ -266,6 +268,7 @@ private function updateDefaultFiles(UpdateOptions $options) : self {
'tests/dtt/src/ExistingSite/ModuleEnabledTest.php',
])
->updateFiles($options, [
'.github/dependabot.yml.dist' => '.github/dependabot.yml',
'.github/workflows/test.yml.dist' => '.github/workflows/test.yml',
'.github/workflows/artifact.yml.dist' => '.github/workflows/artifact.yml',
'.github/workflows/update-config.yml.dist' => '.github/workflows/update-config.yml',
Expand Down
9 changes: 6 additions & 3 deletions src/HttpFileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace DrupalTools;

use DrupalTools\Update\UpdateOptions;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Utils;

Expand All @@ -18,7 +19,7 @@ class HttpFileManager {
* @param \GuzzleHttp\ClientInterface $client
* The HTTP client.
*/
public function __construct(private ClientInterface $client) {
public function __construct(private readonly ClientInterface $client) {
}

/**
Expand All @@ -28,12 +29,14 @@ public function __construct(private ClientInterface $client) {
* The source.
* @param string $destination
* The file destination.
* @param \DrupalTools\Update\UpdateOptions $options
* The update options.
*
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function copyFile(string $source, string $destination) : void {
public function copyFile(string $source, string $destination, UpdateOptions $options) : void {
$resource = Utils::tryFopen($destination, 'w');
$this->client->request('GET', $source, ['sink' => $resource]);
$this->client->request('GET', $options->branch . '/' . $source, ['sink' => $resource]);
}

}
30 changes: 30 additions & 0 deletions src/Package/ComposerOutdatedProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace DrupalTools\Package;

use Symfony\Component\Process\Process;

/**
* Process for `composer outdated` command.
*/
class ComposerOutdatedProcess {

/**
* Runs `composer outdated`.
*
* @return array
* Decoded JSON from `composer outdated`.
*
* @throws \Symfony\Component\Process\Exception\ProcessFailedException
*/
public function run(string $workingDir): array {
$process = new Process([
'composer', 'outdated', '--direct', '--format=json', '--working-dir=' . $workingDir,
]);
$process->mustRun();
return json_decode($process->getOutput(), TRUE);
}

}
11 changes: 11 additions & 0 deletions src/Package/Exception/VersionCheckException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace DrupalTools\Package\Exception;

/**
* Indicated failure to check versions.
*/
class VersionCheckException extends \RuntimeException {
}
29 changes: 29 additions & 0 deletions src/Package/Version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace DrupalTools\Package;

/**
* A value object to store package version data.
*/
final class Version {

/**
* Constructs a new instance.
*
* @param string $name
* The package name.
* @param string $latestVersion
* The latest version.
* @param string $version
* The current version.
*/
public function __construct(
public string $name,
public string $latestVersion,
public string $version,
) {
}

}
74 changes: 74 additions & 0 deletions src/Package/VersionChecker.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace DrupalTools\Package;

use DrupalTools\Package\Exception\VersionCheckException;
use Symfony\Component\Process\Exception\ProcessFailedException;

/**
* Provides a package version checker.
*/
class VersionChecker {

/**
* Constructs a new instance.
*/
public function __construct(
private readonly ComposerOutdatedProcess $process,
) {
}

/**
* Gets outdated package versions.
*
* @param string $composerLockFile
* Path to composer lock file.
*
* @return \DrupalTools\Package\Version[]
* Outdated packages.
*
* @throws \DrupalTools\Package\Exception\VersionCheckException
*/
public function getOutdated(string $composerLockFile) : array {
$packages = $this->getPackages($composerLockFile);
$versions = [];

foreach ($packages as $packageName => $package) {
$versions[] = new Version($packageName, $package['latest'], $package['version']);
}

return $versions;
}

/**
* Get outdated packages.
*
* @throws \DrupalTools\Package\Exception\VersionCheckException
*/
private function getPackages(string $composerLockFile): array {
if (!$composerLockFile = realpath($composerLockFile)) {
throw new VersionCheckException('Composer lock file not found');
}

$workingDir = dirname($composerLockFile);
try {
$packages = $this->process->run($workingDir);
$packages = $packages['installed'] ?? [];
}
catch (ProcessFailedException) {
throw new VersionCheckException("Composer process failed");
}

$result = [];

// Key with package name.
foreach ($packages as $package) {
$result[$package['name']] = $package;
}

return $result;
}

}
10 changes: 6 additions & 4 deletions src/Update/FileManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function updateFiles(UpdateOptions $options, array $map) : self {
$destination = $source;
}
}
$this->copyFile($source, $destination);
$this->copyFile($source, $destination, $options);
}

return $this;
Expand Down Expand Up @@ -136,13 +136,15 @@ private function ensureFolder(string $destination) : void {
* The source.
* @param string $destination
* The destination.
* @param \DrupalTools\Update\UpdateOptions $options
* The update options.
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Symfony\Component\Filesystem\Exception\IOException
*/
private function copyFile(string $source, string $destination) : void {
private function copyFile(string $source, string $destination, UpdateOptions $options) : void {
$this->ensureFolder($source);
$this->httpFileManager->copyFile($source, $destination);
$this->httpFileManager->copyFile($source, $destination, $options);
}

/**
Expand Down Expand Up @@ -235,7 +237,7 @@ public function addFiles(UpdateOptions $options, array $map) : self {
if ($this->filesystem->exists($destination)) {
continue;
}
$this->copyFile($source, $destination);
$this->copyFile($source, $destination, $options);

continue;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Update/UpdateOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ final class UpdateOptions {
*
* @param array $ignoreFiles
* Files to ignore. Leave empty to never ignore anything.
* @param string $branch
* Helfi platform branch for file updates.
* @param bool $updateExternalPackages
* Whether to update external packages.
* @param bool $selfUpdate
Expand All @@ -25,6 +27,7 @@ final class UpdateOptions {
*/
public function __construct(
public array $ignoreFiles = [],
public string $branch = 'main',
public bool $updateExternalPackages = TRUE,
public bool $selfUpdate = TRUE,
public bool $runMigrations = TRUE,
Expand Down
Loading
Loading