Skip to content

Commit

Permalink
Add feature for getting partial versions
Browse files Browse the repository at this point in the history
  • Loading branch information
jdambacher committed Oct 19, 2022
1 parent af90518 commit 83be010
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 33 deletions.
7 changes: 5 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.1.0] - 2022-10-20
### Added
- Get the latest version of a partial version string (e.g. get version `6.4.15.2` if you request version `6.4.15`)

### Changed
- Return the real Amazon S3 download url instead of the redirect url from Shopware

## [1.0.0] - 2022-10-19
### Added
- Controller for getting install urls for specific Shopware 6 versions or the latest version

[Unreleased]: https://github.com/jdambacher/shopware-versions/compare/1.0.0...HEAD
[1.1.0]: https://github.com/jdambacher/shopware-versions/compare/1.0.0...1.1.0
[1.0.0]: https://github.com/jdambacher/shopware-versions/releases/tag/1.0.0
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,28 @@ curl -s https://sw-version.dambacher.net/version
```
or
```curl
curl -s https://sw-version.dambacher.net/version/latest
curl -s https://sw-version.dambacher.net/version/6
```

### For partial version strings
Get the download url for the latest version of a partial version string (e.g. use `6.4.16` to get the latest patch version of `6.4.16`).

If `6.4.16.3` is the last patch version in this minor version, this will give you the download url for Shopware 6.4.16.3:

```curl
curl -s https://sw-version.dambacher.net/version/6.4.16
```

You can also request the latest version of a major version (e.g. `6.4`):

```curl
curl -s https://sw-version.dambacher.net/version/6.4
```


## Future ideas
Here's a list of ideas for future releases:
- Cache the changelog page to reduce the load on Shopware's servers and maintain a download link even if the changelog page is not accessible
- Add a parameter to get the download url for the latest minor or patch version of a specific major version
- Listen for the `Accept` header and return the download url as plain text, json or xml
- Collect more data from the changelog page (e.g. hash, release date, bugfixes etc.)
- Enable to use this package via composer to get the download url in your own project without rely on the webservice
27 changes: 6 additions & 21 deletions src/Controller/VersionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,14 @@ public function __construct(
}

#[Route('/version/{versionNo}', name: 'version')]
public function version(string $versionNo = 'latest'): Response
public function version(string $versionNo = '6'): Response
{
$versions = $this->versionLoader->getChangelog();

// Simply return latest version if version no. is not given in request
if (!$versionNo || $versionNo === 'latest') {
return new RedirectResponse($versions[0]->getInstallUrl());
}

// Easy solution - TODO: Refacator later
/** @var Version $version */
foreach ($versions as $version) {
if ($version->getVersionNo() === $versionNo) {
return new RedirectResponse($version->getInstallUrl());
}
try {
$version = $this->versionLoader->getSpecificVersion($versionNo);
} catch (\Exception $e) {
return new Response('Version not found', Response::HTTP_NOT_FOUND);
}

return new Response('Version not found', Response::HTTP_NOT_FOUND);
}

#[Route('/version', name: 'latest-version')]
public function latestVersion(): Response
{
return $this->redirectToRoute('version', ['versionNo' => 'latest']);
return new RedirectResponse($version->getInstallUrl());
}
}
67 changes: 59 additions & 8 deletions src/VersionLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace App;

use App\Entity\Version;
use App\Factory\VersionFactory;
use Symfony\Component\DomCrawler\Crawler;

class VersionLoader
Expand All @@ -18,13 +17,6 @@ public function getChangelog(): array

$crawler = new Crawler($html);

/*$crawler
->filter('.release-details--cta > a')
->reduce(function (Crawler $node, $i) {
//$versions[] = $node->attr('href'));
VersionFactory::create($node->attr('href'));
});*/

$accordions = $crawler
->filter('.accordion--column > .accordion');

Expand All @@ -40,6 +32,39 @@ public function getChangelog(): array
return $versions;
}

public function getSpecificVersion(string $versionNo): Version
{
$versions = $this->splitChangelog($this->getChangelog());
$splittedVersionNo = $this->splitVersionNo($versionNo);

// TODO: This is ugly, but works. @future me: Please refactor this.
if (!$splittedVersionNo['base']) {
$base = array_shift($versions);
} else {
$base = $versions[$splittedVersionNo['base']];
}

if (!$splittedVersionNo['major']) {
$major = array_shift($base);
} else {
$major = $base[$splittedVersionNo['major']];
}

if (!$splittedVersionNo['minor']) {
$minor = array_shift($major);
} else {
$minor = $major[$splittedVersionNo['minor']];
}

if (!$splittedVersionNo['patch']) {
$patch = array_shift($minor);
} else {
$patch = $minor[$splittedVersionNo['patch']];
}

return $patch;
}

/*
* Returns the real url at Amazon S3 after the redirect
*/
Expand All @@ -54,4 +79,30 @@ private function getRealUrl(?string $url): ?string

return self::S3_BASE_URL . '/' . $url;
}

private function splitChangelog(array $changelog): array
{
$splittedVersions = [];

/** @var Version $version */
foreach ($changelog as $version) {
$splittedVersionNo = $this->splitVersionNo($version->getVersionNo());
$splittedVersions[$splittedVersionNo['base']][$splittedVersionNo['major']][$splittedVersionNo['minor']][$splittedVersionNo['patch']] = $version;
}

return $splittedVersions;
}

private function splitVersionNo(string $versionNo): array
{
// TODO: Use regex here
$versionNo = explode('.', $versionNo);

return [
'base' => $versionNo[0] ?? null,
'major' => $versionNo[1] ?? null,
'minor' => $versionNo[2] ?? null,
'patch' => $versionNo[3] ?? null,
];
}
}

0 comments on commit 83be010

Please sign in to comment.