Skip to content

Commit

Permalink
Gitea Support (#381)
Browse files Browse the repository at this point in the history
* 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 <phillopp@users.noreply.github.com>
  • Loading branch information
phillopp and phillopp committed Aug 23, 2022
1 parent 238f9b0 commit c27e4e4
Show file tree
Hide file tree
Showing 7 changed files with 461 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ functionality for your Laravel application.

- GitHub
- Gitlab
- Gitea
- Http-based archives

Usually you need this when distributing a self-hosted Laravel application
Expand Down Expand Up @@ -206,6 +207,21 @@ The archive URL should contain nothing more than a simple directory listing with

The target archive files must be zip archives and should contain all files on root level, not within an additional folder named like the archive itself.

### Using Gitea

With _Gitea_ you can use your own Gitea-Instance with tag-releases.

To use it, use the following settings in your `.env` file:

| Config name | Value / Description |
| --------------------------------------- | --------------------------------------- |
| SELF_UPDATER_SOURCE | `gitea` |
| SELF_UPDATER_GITEA_URL | URL of Gitea Server |
| SELF_UPDATER_REPO_VENDOR | Repo Vendor Name |
| SELF_UPDATER_REPO_NAME | Repo Name |
| SELF_UPDATER_GITEA_PRIVATE_ACCESS_TOKEN | Access Token from Gitea |
| SELF_UPDATER_DOWNLOAD_PATH | Download path on the webapp host server |

## Contributing

Please see the [contributing guide](CONTRIBUTING.md).
Expand Down
8 changes: 8 additions & 0 deletions config/self-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_HTTP_PRIVATE_ACCESS_TOKEN', ''),
],
'gitea' => [
'type' => 'gitea',
'repository_vendor' => env('SELF_UPDATER_REPO_VENDOR', ''),
'gitea_url' => env('SELF_UPDATER_GITEA_URL', ''),
'repository_name' => env('SELF_UPDATER_REPO_NAME', ''),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_GITEA_PRIVATE_ACCESS_TOKEN', ''),
],
],

/*
Expand Down
165 changes: 165 additions & 0 deletions src/SourceRepositoryTypes/GiteaRepositoryType.php
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);
}
}
6 changes: 6 additions & 0 deletions src/UpdaterManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Codedge\Updater\Contracts\SourceRepositoryTypeContract;
use Codedge\Updater\Contracts\UpdaterContract;
use Codedge\Updater\Models\UpdateExecutor;
use Codedge\Updater\SourceRepositoryTypes\GiteaRepositoryType;
use Codedge\Updater\SourceRepositoryTypes\GithubRepositoryType;
use Codedge\Updater\SourceRepositoryTypes\GitlabRepositoryType;
use Codedge\Updater\SourceRepositoryTypes\HttpRepositoryType;
Expand Down Expand Up @@ -106,4 +107,9 @@ protected function createHttpRepository(): SourceRepositoryTypeContract
{
return $this->sourceRepository($this->app->make(HttpRepositoryType::class));
}

protected function createGiteaRepository(): SourceRepositoryTypeContract
{
return $this->sourceRepository($this->app->make(GiteaRepositoryType::class));
}
}
80 changes: 80 additions & 0 deletions tests/Data/releases-gitea.json
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": "phillopp@noreply.try.gitea.io",
"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": "phillopp@noreply.try.gitea.io",
"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": []
}
]

0 comments on commit c27e4e4

Please sign in to comment.