Skip to content

Commit

Permalink
Dev/GitHub release files (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelated-au authored Jun 28, 2024
1 parent b765bd1 commit 53fa526
Show file tree
Hide file tree
Showing 8 changed files with 839 additions and 8 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,33 @@ file.

This is the default. Updates will be fetched by using a tagged commit, aka release.

#### Tag-based updates with assets/package file

If you have pre-packaged _tag based_ releases, you can use the `'repository_types.github.package_file_name'` key in your
`config/self-update.php` file or update the `SELF_UPDATER_PACKAGE_FILE_NAME` `.env` var to specify the asset name to
download.

If you prefix the file name with `regex:` the package will try to find the latest asset matching the given regex. Note
however to use only the regex and not the PHP regex `/` prefix and suffixes. For example, an acceptable value would be
`regex:.releaseV*\.zip`. This will match all assets starting with `releaseV` and ending with `.zip`.

An invalid value would be `regex:/releaseV*\.zip/`. Note the `/` prefix and suffix.

```php
// ...
'repository_types' => [
'github' => [
'type' => 'github',
'repository_vendor' => env('SELF_UPDATER_REPO_VENDOR', ''),
'repository_name' => env('SELF_UPDATER_REPO_NAME', ''),
// ...
'package_file_name' => 'release.zip', // Package file name to download
'package_file_name' => 'regex:releaseV.*\.zip', // REGEX Package file name to download
],
// ...
];
```

#### Branch-based updates

Select the branch that should be used via the `use_branch` setting [inside the configuration](https://github.com/codedge/laravel-selfupdater/blob/master/config/self-update.php).
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
}
},
"scripts": {
"phpstan": "./vendor/bin/phpstan",
"phpstan": "./vendor/bin/phpstan --memory-limit=1G",
"test": "./vendor/bin/phpunit",
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html=build/coverage-html"
}
Expand Down
1 change: 1 addition & 0 deletions config/self-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_GITHUB_PRIVATE_ACCESS_TOKEN', ''),
'use_branch' => env('SELF_UPDATER_USE_BRANCH', ''),
'package_file_name' => env('SELF_UPDATER_PACKAGE_FILE_NAME'),
],
'gitlab' => [
'base_url' => '',
Expand Down
25 changes: 23 additions & 2 deletions src/SourceRepositoryTypes/GithubRepositoryTypes/GithubTagType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use GuzzleHttp\Exception\InvalidArgumentException;
use GuzzleHttp\Utils;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -77,10 +78,30 @@ public function fetch(string $version = ''): Release

$release = $this->selectRelease($releases, $version);

$packageName = $this->config['package_file_name'] ?? null;
if ($packageName) {
$asset = Arr::first($release->assets, static function ($asset) use ($packageName) {
if (Str::startsWith($packageName, 'regex:')) {
// The package is a regex, so do a regex search
return Str::match('/'.Str::after($packageName, 'regex:').'/', $asset->name);
}

return Str::contains($asset->name, $packageName);
});
if (!$asset) {
throw ReleaseException::archiveFileNotFound($version);
}
$downloadUrl = $asset->browser_download_url;
$fileName = $asset->name;
} else {
$downloadUrl = $release->zipball_url;
$fileName = $release->tag_name.'.zip';
}

$this->release->setVersion($release->tag_name)
->setRelease($release->tag_name.'.zip')
->setRelease($fileName)
->updateStoragePath()
->setDownloadUrl($release->zipball_url);
->setDownloadUrl($downloadUrl);

if (!$this->release->isSourceAlreadyFetched()) {
$this->release->download();
Expand Down
372 changes: 372 additions & 0 deletions tests/Data/releases-tag_asset.json

Large diffs are not rendered by default.

372 changes: 372 additions & 0 deletions tests/Data/releases-tag_regex_asset.json

Large diffs are not rendered by default.

36 changes: 36 additions & 0 deletions tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,42 @@ public function it_can_fetch_github_tag_releases_and_takes_latest_if_version_not
$this->assertEquals('2.6.1.zip', $release->getRelease());
}

/** @test */
public function it_can_fetch_github_tag_asset_latest_release(): void
{
config(['self-update.repository_types.github.package_file_name' => 'release.zip']);

/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('tag_asset'))
->pushResponse($this->getResponse200ZipFile());

$release = $github->fetch();
$this->assertInstanceOf(Release::class, $release);
$this->assertEquals('v0.0.9', $release->getVersion());
$this->assertEquals('release.zip', $release->getRelease());
}

/** @test */
public function it_can_fetch_github_tag_regex_asset_latest_release(): void
{
config(['self-update.repository_types.github.package_file_name' => 'regex:releaseV\d+\.\d+\.\d+\.zip']);

/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('tag_regex_asset'))
->pushResponse($this->getResponse200ZipFile());

$release = $github->fetch();
$this->assertInstanceOf(Release::class, $release);
$this->assertEquals('v0.0.9', $release->getVersion());
$this->assertEquals('releaseV0.0.9.zip', $release->getRelease());
}

/** @test */
public function it_can_fetch_github_branch_releases_latest(): void
{
Expand Down
12 changes: 7 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ abstract class TestCase extends Orchestra

/** @var array<string, string> */
protected array $mockedResponses = [
'tag' => 'releases-tag.json',
'branch' => 'releases-branch.json',
'http' => 'releases-http_gh.json',
'gitlab' => 'releases-gitlab.json',
'gitea' => 'releases-gitea.json',
'tag' => 'releases-tag.json',
'tag_asset' => 'releases-tag_asset.json',
'tag_regex_asset' => 'releases-tag_regex_asset.json',
'branch' => 'releases-branch.json',
'http' => 'releases-http_gh.json',
'gitlab' => 'releases-gitlab.json',
'gitea' => 'releases-gitea.json',
];

/**
Expand Down

0 comments on commit 53fa526

Please sign in to comment.