Skip to content

Commit 53fa526

Browse files
authored
Dev/GitHub release files (#389)
1 parent b765bd1 commit 53fa526

File tree

8 files changed

+839
-8
lines changed

8 files changed

+839
-8
lines changed

README.md

+27
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,33 @@ file.
150150

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

153+
#### Tag-based updates with assets/package file
154+
155+
If you have pre-packaged _tag based_ releases, you can use the `'repository_types.github.package_file_name'` key in your
156+
`config/self-update.php` file or update the `SELF_UPDATER_PACKAGE_FILE_NAME` `.env` var to specify the asset name to
157+
download.
158+
159+
If you prefix the file name with `regex:` the package will try to find the latest asset matching the given regex. Note
160+
however to use only the regex and not the PHP regex `/` prefix and suffixes. For example, an acceptable value would be
161+
`regex:.releaseV*\.zip`. This will match all assets starting with `releaseV` and ending with `.zip`.
162+
163+
An invalid value would be `regex:/releaseV*\.zip/`. Note the `/` prefix and suffix.
164+
165+
```php
166+
// ...
167+
'repository_types' => [
168+
'github' => [
169+
'type' => 'github',
170+
'repository_vendor' => env('SELF_UPDATER_REPO_VENDOR', ''),
171+
'repository_name' => env('SELF_UPDATER_REPO_NAME', ''),
172+
// ...
173+
'package_file_name' => 'release.zip', // Package file name to download
174+
'package_file_name' => 'regex:releaseV.*\.zip', // REGEX Package file name to download
175+
],
176+
// ...
177+
];
178+
```
179+
153180
#### Branch-based updates
154181

155182
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).

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
}
7575
},
7676
"scripts": {
77-
"phpstan": "./vendor/bin/phpstan",
77+
"phpstan": "./vendor/bin/phpstan --memory-limit=1G",
7878
"test": "./vendor/bin/phpunit",
7979
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html=build/coverage-html"
8080
}

config/self-update.php

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
4949
'private_access_token' => env('SELF_UPDATER_GITHUB_PRIVATE_ACCESS_TOKEN', ''),
5050
'use_branch' => env('SELF_UPDATER_USE_BRANCH', ''),
51+
'package_file_name' => env('SELF_UPDATER_PACKAGE_FILE_NAME'),
5152
],
5253
'gitlab' => [
5354
'base_url' => '',

src/SourceRepositoryTypes/GithubRepositoryTypes/GithubTagType.php

+23-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use GuzzleHttp\Exception\InvalidArgumentException;
1414
use GuzzleHttp\Utils;
1515
use Illuminate\Http\Client\Response;
16+
use Illuminate\Support\Arr;
1617
use Illuminate\Support\Collection;
1718
use Illuminate\Support\Facades\Http;
1819
use Illuminate\Support\Facades\Log;
@@ -77,10 +78,30 @@ public function fetch(string $version = ''): Release
7778

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

81+
$packageName = $this->config['package_file_name'] ?? null;
82+
if ($packageName) {
83+
$asset = Arr::first($release->assets, static function ($asset) use ($packageName) {
84+
if (Str::startsWith($packageName, 'regex:')) {
85+
// The package is a regex, so do a regex search
86+
return Str::match('/'.Str::after($packageName, 'regex:').'/', $asset->name);
87+
}
88+
89+
return Str::contains($asset->name, $packageName);
90+
});
91+
if (!$asset) {
92+
throw ReleaseException::archiveFileNotFound($version);
93+
}
94+
$downloadUrl = $asset->browser_download_url;
95+
$fileName = $asset->name;
96+
} else {
97+
$downloadUrl = $release->zipball_url;
98+
$fileName = $release->tag_name.'.zip';
99+
}
100+
80101
$this->release->setVersion($release->tag_name)
81-
->setRelease($release->tag_name.'.zip')
102+
->setRelease($fileName)
82103
->updateStoragePath()
83-
->setDownloadUrl($release->zipball_url);
104+
->setDownloadUrl($downloadUrl);
84105

85106
if (!$this->release->isSourceAlreadyFetched()) {
86107
$this->release->download();

tests/Data/releases-tag_asset.json

+372
Large diffs are not rendered by default.

tests/Data/releases-tag_regex_asset.json

+372
Large diffs are not rendered by default.

tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php

+36
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,42 @@ public function it_can_fetch_github_tag_releases_and_takes_latest_if_version_not
224224
$this->assertEquals('2.6.1.zip', $release->getRelease());
225225
}
226226

227+
/** @test */
228+
public function it_can_fetch_github_tag_asset_latest_release(): void
229+
{
230+
config(['self-update.repository_types.github.package_file_name' => 'release.zip']);
231+
232+
/** @var GithubTagType $github */
233+
$github = (resolve(GithubRepositoryType::class))->create();
234+
235+
Http::fakeSequence()
236+
->pushResponse($this->getResponse200Type('tag_asset'))
237+
->pushResponse($this->getResponse200ZipFile());
238+
239+
$release = $github->fetch();
240+
$this->assertInstanceOf(Release::class, $release);
241+
$this->assertEquals('v0.0.9', $release->getVersion());
242+
$this->assertEquals('release.zip', $release->getRelease());
243+
}
244+
245+
/** @test */
246+
public function it_can_fetch_github_tag_regex_asset_latest_release(): void
247+
{
248+
config(['self-update.repository_types.github.package_file_name' => 'regex:releaseV\d+\.\d+\.\d+\.zip']);
249+
250+
/** @var GithubTagType $github */
251+
$github = (resolve(GithubRepositoryType::class))->create();
252+
253+
Http::fakeSequence()
254+
->pushResponse($this->getResponse200Type('tag_regex_asset'))
255+
->pushResponse($this->getResponse200ZipFile());
256+
257+
$release = $github->fetch();
258+
$this->assertInstanceOf(Release::class, $release);
259+
$this->assertEquals('v0.0.9', $release->getVersion());
260+
$this->assertEquals('releaseV0.0.9.zip', $release->getRelease());
261+
}
262+
227263
/** @test */
228264
public function it_can_fetch_github_branch_releases_latest(): void
229265
{

tests/TestCase.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ abstract class TestCase extends Orchestra
1919

2020
/** @var array<string, string> */
2121
protected array $mockedResponses = [
22-
'tag' => 'releases-tag.json',
23-
'branch' => 'releases-branch.json',
24-
'http' => 'releases-http_gh.json',
25-
'gitlab' => 'releases-gitlab.json',
26-
'gitea' => 'releases-gitea.json',
22+
'tag' => 'releases-tag.json',
23+
'tag_asset' => 'releases-tag_asset.json',
24+
'tag_regex_asset' => 'releases-tag_regex_asset.json',
25+
'branch' => 'releases-branch.json',
26+
'http' => 'releases-http_gh.json',
27+
'gitlab' => 'releases-gitlab.json',
28+
'gitea' => 'releases-gitea.json',
2729
];
2830

2931
/**

0 commit comments

Comments
 (0)