Skip to content

Commit

Permalink
Merge pull request #342 from codedge/#111-gitlab
Browse files Browse the repository at this point in the history
Add support for Gitlab
  • Loading branch information
codedge committed Apr 27, 2022
2 parents e96d39f + 7d88ba8 commit 7a08e9a
Show file tree
Hide file tree
Showing 36 changed files with 744 additions and 437 deletions.
4 changes: 4 additions & 0 deletions .styleci.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
preset: recommended
risky: true
version: 8.0
enabled:
- declare_strict_types
- no_superfluous_phpdoc_tags
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
[![codecov](https://codecov.io/gh/codedge/laravel-selfupdater/branch/master/graph/badge.svg)](https://codecov.io/gh/codedge/laravel-selfupdater)

This package provides some basic methods to implement a self updating
functionality for your Laravel 5 application. Already bundled are some
methods to provide a self-update mechanism via Github.
functionality for your Laravel application.

**Supported update provider:**

- GitHub
- Gitlab
- Http-based archives

Usually you need this when distributing a self-hosted Laravel application
that needs some updating mechanism without [Composer](https://getcomposer.org/).
Expand Down Expand Up @@ -43,7 +48,7 @@ Set the installed version to one of the tags set for a release.

#### `branch`-based updates

Set the installed version to to a datetime of one of the latest commits.
Set the installed version to a datetime of one of the latest commits.
A valid version would be: `2020-04-19T22:35:48Z`

### Running artisan commands
Expand Down Expand Up @@ -132,10 +137,10 @@ Route::get('/', function (\Codedge\Updater\UpdaterManager $updater) {
Currently, the fetching of the source is a _synchronous_ process.
It is not run in background.

### Using Github
### Using GitHub

The package comes with a _Github_ source repository type to fetch
releases from Github - basically use Github to pull the latest version
The package comes with a _GitHub_ source repository type to fetch
releases from GitHub - basically use GitHub to pull the latest version
of your software.

Just make sure you set the proper repository in your `config/self-updater.php`
Expand Down Expand Up @@ -163,6 +168,21 @@ Select the branch that should be used via the `use_branch` setting [inside the c
];
```

### Using Gitlab

```php
// ...
'repository_types' => [
'gitlab' => [
'type' => 'gitlab',
'repository_id' => env('SELF_UPDATER_REPO_URL', ''),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_GITLAB_PRIVATE_ACCESS_TOKEN', ''),
],
// ...
];
```

### Using Http archives

The package comes with an _Http_ source repository type to fetch
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"ext-json": "*",
"ext-zip": "*",
"guzzlehttp/guzzle": "^7.4",
"laravel/framework": "^8.83.9",
"laravel/framework": "^8.83.8 || ^9.2",
"phpstan/extension-installer": "^1.1",
"phpstan/phpstan-phpunit": "^1.1"
},
Expand Down Expand Up @@ -73,6 +73,7 @@
},
"scripts": {
"phpstan": "./vendor/bin/phpstan",
"test": "./vendor/bin/phpunit"
"test": "./vendor/bin/phpunit",
"test-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --coverage-html=build/coverage-html"
}
}
9 changes: 9 additions & 0 deletions config/self-update.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return [

/*
Expand Down Expand Up @@ -32,6 +34,7 @@
| A repository can be of different types, which can be specified here.
| Current options:
| - github
| - gitlab
| - http
|
*/
Expand All @@ -46,6 +49,12 @@
'private_access_token' => env('SELF_UPDATER_GITHUB_PRIVATE_ACCESS_TOKEN', ''),
'use_branch' => env('SELF_UPDATER_USE_BRANCH', ''),
],
'gitlab' => [
'type' => 'gitlab',
'repository_id' => env('SELF_UPDATER_REPO_URL', ''),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_GITLAB_PRIVATE_ACCESS_TOKEN', ''),
],
'http' => [
'type' => 'http',
'repository_url' => env('SELF_UPDATER_REPO_URL', ''),
Expand Down
2 changes: 2 additions & 0 deletions src/Commands/CheckForUpdate.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Commands;

use Codedge\Updater\UpdaterManager;
Expand Down
13 changes: 3 additions & 10 deletions src/Contracts/SourceRepositoryTypeContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Codedge\Updater\Contracts;

use Codedge\Updater\Models\Release;
use Illuminate\Http\Client\Response;

interface SourceRepositoryTypeContract
{
Expand All @@ -15,21 +16,15 @@ public function fetch(string $version = ''): Release;

/**
* Perform the actual update process.
*
* @param Release $release
*
* @return bool
*/
public function update(Release $release): bool;

public function getReleases(): Response;

/**
* Check repository if a newer version than the installed one is available.
* Caution: v.1.1 compared to 1.1 is not the same. Check to actually compare correct version, including letters
* before or after.
*
* @param string $currentVersion
*
* @return bool
*/
public function isNewVersionAvailable(string $currentVersion = ''): bool;

Expand All @@ -44,8 +39,6 @@ public function getVersionInstalled(): string;
*
* @param string $prepend Prepend a string to the latest version
* @param string $append Append a string to the latest version
*
* @return string
*/
public function getVersionAvailable(string $prepend = '', string $append = ''): string;
}
8 changes: 3 additions & 5 deletions src/Contracts/UpdaterContract.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Contracts;

interface UpdaterContract
{
/**
* Get a source type instance.
*
* @param string $name
*
* @return mixed
*/
public function source(string $name = '');
public function source(string $name = ''): SourceRepositoryTypeContract;
}
2 changes: 2 additions & 0 deletions src/Events/UpdateAvailable.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Events;

class UpdateAvailable
Expand Down
2 changes: 2 additions & 0 deletions src/Events/UpdateFailed.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Events;

use Codedge\Updater\Models\Release;
Expand Down
2 changes: 2 additions & 0 deletions src/Events/UpdateSucceeded.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Events;

use Codedge\Updater\Models\Release;
Expand Down
15 changes: 15 additions & 0 deletions src/Exceptions/ReleaseException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Exceptions;

final class ReleaseException extends \Exception
{
public static function noReleaseFound(string $version): self
{
$version = $version !== '' ? $version : 'latest version';

return new self(sprintf('No release found for version "%s". Please check the repository you\'re pulling from', $version));
}
}
13 changes: 13 additions & 0 deletions src/Exceptions/VersionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\Exceptions;

final class VersionException extends \Exception
{
public static function versionInstalledNotFound(): self
{
return new self('Version installed not found.');
}
}
29 changes: 17 additions & 12 deletions src/Models/Release.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use Codedge\Updater\Traits\SupportPrivateAccessToken;
use Exception;
use GuzzleHttp\ClientInterface;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;
use Psr\Http\Message\ResponseInterface;
use Symfony\Component\Finder\Finder;

final class Release
Expand Down Expand Up @@ -155,7 +155,15 @@ public function extract(bool $deleteSource = true): bool
protected function extractZip(string $extractTo): bool
{
$zip = new \ZipArchive();
$res = $zip->open($this->getStoragePath());

/*
* @see https://bugs.php.net/bug.php?id=79296
*/
if (filesize($this->getStoragePath()) === 0) {
$res = $zip->open($this->getStoragePath(), \ZipArchive::OVERWRITE);
} else {
$res = $zip->open($this->getStoragePath());
}

if ($res !== true) {
throw new Exception("Cannot open zip archive [{$this->getStoragePath()}]. Error: $res");
Expand All @@ -167,7 +175,7 @@ protected function extractZip(string $extractTo): bool
return $extracted;
}

public function download(ClientInterface $client): ResponseInterface
public function download(): Response
{
if (empty($this->getStoragePath())) {
throw new Exception('No storage path set.');
Expand All @@ -181,14 +189,11 @@ public function download(ClientInterface $client): ResponseInterface
];
}

return $client->request(
'GET',
$this->getDownloadUrl(),
[
'sink' => $this->getStoragePath(),
'headers' => $headers,
]
);
return Http::withHeaders($headers)
->withOptions([
'sink' => $this->getStoragePath(),
])
->get($this->getDownloadUrl());
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/Models/UpdateExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __construct()
* Use the base_path() function to determine the project root folder.
* This might be not good when running unit tests.
*
* @param string $path
*
* @return $this
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Notifications/BaseNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

abstract class BaseNotification extends Notification
{
/**
* @return array<string>
*/
public function via(): array
{
$notificationChannels = config('self-update.notifications.notifications.'.static::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Notifications/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(Repository $config)
$this->config = $config;
}

public function subscribe(Dispatcher $events)
public function subscribe(Dispatcher $events): void
{
$events->listen($this->allEventClasses(), function ($event) {
$notification = $this->determineNotification($event);
Expand Down
14 changes: 8 additions & 6 deletions src/SourceRepository.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater;

use Codedge\Updater\Contracts\SourceRepositoryTypeContract;
use Codedge\Updater\Models\Release;
use Codedge\Updater\Models\UpdateExecutor;
use Codedge\Updater\Traits\SupportPrivateAccessToken;
use Codedge\Updater\Traits\UseVersionFile;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Artisan;

/**
Expand Down Expand Up @@ -40,17 +43,18 @@ public function fetch(string $version = ''): Release
}

/**
* @param Release $release
*
* @throws \Exception
*
* @return bool
*/
public function update(Release $release): bool
{
return $this->updateExecutor->run($release);
}

public function getReleases(): Response
{
return $this->sourceRepository->getReleases();
}

/**
* Check repository if a newer version than the installed one is available.
*/
Expand All @@ -75,8 +79,6 @@ public function getVersionInstalled(string $prepend = '', string $append = ''):
*
* @param string $prepend Prepend a string to the latest version
* @param string $append Append a string to the latest version
*
* @return string
*/
public function getVersionAvailable(string $prepend = '', string $append = ''): string
{
Expand Down

0 comments on commit 7a08e9a

Please sign in to comment.