-
-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add Gitea Repository Type * Update Readme with gitea configs * Add Gitea Configs and createGiteaRepo function * copied test from gitlab, changed values/variable * Prettified Code! * Update README.md * update test configs * bug fix * add release gitea.json for testing * rename * append mockedersponses * test * update readme * style update * style update Co-authored-by: phillopp <[email protected]>
- Loading branch information
Showing
7 changed files
with
461 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codedge\Updater\SourceRepositoryTypes; | ||
|
||
use Codedge\Updater\Contracts\SourceRepositoryTypeContract; | ||
use Codedge\Updater\Events\UpdateAvailable; | ||
use Codedge\Updater\Exceptions\ReleaseException; | ||
use Codedge\Updater\Exceptions\VersionException; | ||
use Codedge\Updater\Models\Release; | ||
use Codedge\Updater\Models\UpdateExecutor; | ||
use Codedge\Updater\Traits\UseVersionFile; | ||
use Exception; | ||
use GuzzleHttp\Exception\InvalidArgumentException; | ||
use GuzzleHttp\Utils; | ||
use Illuminate\Http\Client\Response; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Http; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Str; | ||
|
||
class GiteaRepositoryType implements SourceRepositoryTypeContract | ||
{ | ||
use UseVersionFile; | ||
|
||
const BASE_URL = 'https://gitlab.com'; | ||
|
||
protected array $config; | ||
protected Release $release; | ||
protected UpdateExecutor $updateExecutor; | ||
|
||
public function __construct(UpdateExecutor $updateExecutor) | ||
{ | ||
$this->config = config('self-update.repository_types.gitea'); | ||
|
||
$this->release = resolve(Release::class); | ||
$this->release->setStoragePath(Str::finish($this->config['download_path'], DIRECTORY_SEPARATOR)) | ||
->setUpdatePath(base_path(), config('self-update.exclude_folders')) | ||
->setAccessToken($this->config['private_access_token']); | ||
$this->release->setAccessTokenPrefix('token '); | ||
|
||
$this->updateExecutor = $updateExecutor; | ||
} | ||
|
||
public function update(Release $release): bool | ||
{ | ||
return $this->updateExecutor->run($release); | ||
} | ||
|
||
public function isNewVersionAvailable(string $currentVersion = ''): bool | ||
{ | ||
$version = $currentVersion ?: $this->getVersionInstalled(); | ||
|
||
if (!$version) { | ||
throw VersionException::versionInstalledNotFound(); | ||
} | ||
|
||
$versionAvailable = $this->getVersionAvailable(); | ||
|
||
if (version_compare($version, $versionAvailable, '<')) { | ||
if (!$this->versionFileExists()) { | ||
$this->setVersionFile($versionAvailable); | ||
} | ||
event(new UpdateAvailable($versionAvailable)); | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function getVersionInstalled(): string | ||
{ | ||
return (string) config('self-update.version_installed'); | ||
} | ||
|
||
/** | ||
* Get the latest version that has been published in a certain repository. | ||
* Example: 2.6.5 or v2.6.5. | ||
* | ||
* @param string $prepend Prepend a string to the latest version | ||
* @param string $append Append a string to the latest version | ||
* | ||
* @throws Exception | ||
*/ | ||
public function getVersionAvailable(string $prepend = '', string $append = ''): string | ||
{ | ||
if ($this->versionFileExists()) { | ||
$version = $prepend.$this->getVersionFile().$append; | ||
} else { | ||
$response = $this->getReleases(); | ||
|
||
$releaseCollection = collect(json_decode($response->body())); | ||
$version = $prepend.$releaseCollection->first()->tag_name.$append; | ||
} | ||
|
||
return $version; | ||
} | ||
|
||
/** | ||
* @throws ReleaseException | ||
*/ | ||
public function fetch(string $version = ''): Release | ||
{ | ||
$response = $this->getReleases(); | ||
|
||
try { | ||
$releases = collect(Utils::jsonDecode($response->body())); | ||
} catch (InvalidArgumentException $e) { | ||
throw ReleaseException::noReleaseFound($version); | ||
} | ||
|
||
if ($releases->isEmpty()) { | ||
throw ReleaseException::noReleaseFound($version); | ||
} | ||
|
||
$release = $this->selectRelease($releases, $version); | ||
|
||
$url = '/api/v1/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/archive/'.$release->tag_name.'.zip'; | ||
$downloadUrl = $this->config['gitea_url'].$url; | ||
|
||
$this->release->setVersion($release->tag_name) | ||
->setRelease($release->tag_name.'.zip') | ||
->updateStoragePath() | ||
->setDownloadUrl($downloadUrl); | ||
|
||
if (!$this->release->isSourceAlreadyFetched()) { | ||
$this->release->download(); | ||
$this->release->extract(); | ||
} | ||
|
||
return $this->release; | ||
} | ||
|
||
public function selectRelease(Collection $collection, string $version) | ||
{ | ||
$release = $collection->first(); | ||
|
||
if (!empty($version)) { | ||
if ($collection->contains('tag_name', $version)) { | ||
$release = $collection->where('tag_name', $version)->first(); | ||
} else { | ||
Log::info('No release for version "'.$version.'" found. Selecting latest.'); | ||
} | ||
} | ||
|
||
return $release; | ||
} | ||
|
||
final public function getReleases(): Response | ||
{ | ||
$url = '/api/v1/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/releases'; | ||
|
||
$headers = []; | ||
|
||
if ($this->release->hasAccessToken()) { | ||
$headers = [ | ||
'Authorization' => $this->release->getAccessToken(), | ||
]; | ||
} | ||
|
||
return Http::withHeaders($headers)->get($this->config['gitea_url'].$url); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
[ | ||
{ | ||
"id": 5801852, | ||
"tag_name": "0.0.2", | ||
"target_commitish": "master", | ||
"name": "Testrelease", | ||
"body": "", | ||
"url": "https://try.gitea.io/api/v1/repos/phillopp/emptyRepo/releases/5801851", | ||
"html_url": "https://try.gitea.io/phillopp/emptyRepo/releases/tag/0.0.2", | ||
"tarball_url": "https://try.gitea.io/phillopp/emptyRepo/archive/0.0.2.tar.gz", | ||
"zipball_url": "https://try.gitea.io/phillopp/emptyRepo/archive/0.0.2.zip", | ||
"draft": false, | ||
"prerelease": false, | ||
"created_at": "2022-08-18T09:34:20Z", | ||
"published_at": "2022-08-18T09:34:20Z", | ||
"author": { | ||
"id": 515025, | ||
"login": "phillopp", | ||
"login_name": "", | ||
"full_name": "", | ||
"email": "[email protected]", | ||
"avatar_url": "https://try.gitea.io/avatar/330ff1fe1bb56077547730562f9bb038", | ||
"language": "", | ||
"is_admin": false, | ||
"last_login": "0001-01-01T00:00:00Z", | ||
"created": "2022-08-18T09:21:28Z", | ||
"restricted": false, | ||
"active": false, | ||
"prohibit_login": false, | ||
"location": "", | ||
"website": "", | ||
"description": "", | ||
"visibility": "public", | ||
"followers_count": 0, | ||
"following_count": 0, | ||
"starred_repos_count": 0, | ||
"username": "phillopp" | ||
}, | ||
"assets": [] | ||
}, | ||
{ | ||
"id": 5801851, | ||
"tag_name": "0.0.1", | ||
"target_commitish": "master", | ||
"name": "Testrelease", | ||
"body": "", | ||
"url": "https://try.gitea.io/api/v1/repos/phillopp/emptyRepo/releases/5801851", | ||
"html_url": "https://try.gitea.io/phillopp/emptyRepo/releases/tag/0.0.1", | ||
"tarball_url": "https://try.gitea.io/phillopp/emptyRepo/archive/0.0.1.tar.gz", | ||
"zipball_url": "https://try.gitea.io/phillopp/emptyRepo/archive/0.0.1.zip", | ||
"draft": false, | ||
"prerelease": false, | ||
"created_at": "2022-08-17T09:34:20Z", | ||
"published_at": "2022-08-17T09:34:20Z", | ||
"author": { | ||
"id": 515025, | ||
"login": "phillopp", | ||
"login_name": "", | ||
"full_name": "", | ||
"email": "[email protected]", | ||
"avatar_url": "https://try.gitea.io/avatar/330ff1fe1bb56077547730562f9bb038", | ||
"language": "", | ||
"is_admin": false, | ||
"last_login": "0001-01-01T00:00:00Z", | ||
"created": "2022-08-18T09:21:28Z", | ||
"restricted": false, | ||
"active": false, | ||
"prohibit_login": false, | ||
"location": "", | ||
"website": "", | ||
"description": "", | ||
"visibility": "public", | ||
"followers_count": 0, | ||
"following_count": 0, | ||
"starred_repos_count": 0, | ||
"username": "phillopp" | ||
}, | ||
"assets": [] | ||
} | ||
] |
Oops, something went wrong.