Skip to content

#42 private repo access #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ Usually you need this when distributing a self-hosted Laravel application
that needs some updating mechanism, as you do not want to bother your
lovely users with Git and/or Composer commands ;-)

## Compatibility

* PHP:
* 7.2
* 7.3
* 7.4
* Laravel:
* 5.8
* 6.x

## Install with Composer

To install the latest version from the master using [Composer](https://getcomposer.org/):
Expand Down Expand Up @@ -125,6 +135,13 @@ You can specify these values by adding `SELF_UPDATER_MAILTO_NAME` and
| SELF_UPDATER_MAILTO_UPDATE_AVAILABLE_SUBJECT | Subject of update available email |
| SELF_UPDATER_MAILTO_UPDATE_SUCCEEDED_SUBJECT | Subject of update succeeded email |

### Private repositories

Private repositories can be accessed via (Bearer) tokens. Each repository inside the config file should have
a `private_access_token` field, where you can set the token.

**Note:** Do not prefix the token with `Bearer `. This is done automatically.

## Usage
To start an update process, i. e. in a controller, just use:
```php
Expand Down
2 changes: 2 additions & 0 deletions config/self-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@
'repository_name' => env('SELF_UPDATER_REPO_NAME', ''),
'repository_url' => '',
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_GITHUB_PRIVATE_ACCESS_TOKEN', ''),
],
'http' => [
'type' => 'http',
'repository_url' => env('SELF_UPDATER_REPO_URL', ''),
'pkg_filename_format' => env('SELF_UPDATER_PKG_FILENAME_FORMAT', 'v_VERSION_'),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => env('SELF_UPDATER_HTTP_PRIVATE_ACCESS_TOKEN', ''),
],
],

Expand Down
60 changes: 59 additions & 1 deletion src/AbstractRepositoryType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater;

use Codedge\Updater\Events\HasWrongPermissions;
Expand All @@ -15,11 +17,18 @@
*/
abstract class AbstractRepositoryType
{
const ACCESS_TOKEN_PREFIX = 'Bearer ';

/**
* @var array
*/
protected $config;

/**
* Access token for private repository access.
*/
private $accessToken = '';

/**
* @var Finder|SplFileInfo[]
*/
Expand Down Expand Up @@ -99,8 +108,21 @@ protected function hasCorrectPermissionForUpdate() : bool
*/
protected function downloadRelease(Client $client, $source, $storagePath)
{
$headers = [];

if ($this->hasAccessToken()) {
$headers = [
'Authorization' => $this->getAccessToken(),
];
}

return $client->request(
'GET', $source, ['sink' => $storagePath]
'GET',
$source,
[
'sink' => $storagePath,
'headers' => $headers,
]
);
}

Expand Down Expand Up @@ -159,4 +181,40 @@ public function createReleaseFolder($storagePath, $releaseName)

File::deleteDirectory($subDirName[0]);
}

/**
* Get the access token.
*
* @param bool $withPrefix
*
* @return string
*/
public function getAccessToken($withPrefix = true): string
{
if ($withPrefix) {
return self::ACCESS_TOKEN_PREFIX.$this->accessToken;
}

return $this->accessToken;
}

/**
* Set access token.
*
* @param string $token
*/
public function setAccessToken(string $token): void
{
$this->accessToken = $token;
}

/**
* Check if an access token has been set.
*
* @return bool
*/
public function hasAccessToken(): bool
{
return ! empty($this->accessToken);
}
}
17 changes: 16 additions & 1 deletion src/SourceRepositoryTypes/GithubRepositoryType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\SourceRepositoryTypes;

use Codedge\Updater\AbstractRepositoryType;
Expand Down Expand Up @@ -40,6 +42,8 @@ public function __construct(Client $client, array $config)
$this->config = $config;
$this->config['version_installed'] = config('self-update.version_installed');
$this->config['exclude_folders'] = config('self-update.exclude_folders');

$this->setAccessToken($config['private_access_token']);
}

/**
Expand Down Expand Up @@ -214,9 +218,20 @@ protected function getRepositoryReleases()
throw new \Exception('No repository specified. Please enter a valid Github repository owner and name in your config.');
}

$headers = [];

if ($this->hasAccessToken()) {
$headers = [
'Authorization' => $this->getAccessToken(),
];
}

return $this->client->request(
'GET',
self::GITHUB_API_URL.'/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/tags'
self::GITHUB_API_URL.'/repos/'.$this->config['repository_vendor'].'/'.$this->config['repository_name'].'/tags',
[
'headers' => $headers,
]
);
}

Expand Down
13 changes: 10 additions & 3 deletions src/SourceRepositoryTypes/HttpRepositoryType.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Codedge\Updater\SourceRepositoryTypes;

use Codedge\Updater\AbstractRepositoryType;
Expand All @@ -10,6 +12,7 @@
use File;
use GuzzleHttp\Client;
use Illuminate\Database\Eloquent\Collection;
use Psr\Http\Message\ResponseInterface;
use Storage;
use Symfony\Component\Finder\Finder;

Expand All @@ -29,7 +32,7 @@ class HttpRepositoryType extends AbstractRepositoryType implements SourceReposit
protected $client;

/**
* @var Version prepand string
* @var Version prepend string
*/
protected $prepend;

Expand All @@ -53,6 +56,8 @@ public function __construct(Client $client, array $config)
// Get prepend and append strings
$this->prepend = preg_replace('/_VERSION_.*$/', '', $this->config['pkg_filename_format']);
$this->append = preg_replace('/^.*_VERSION_/', '', $this->config['pkg_filename_format']);

$this->setAccessToken($config['private_access_token']);
}

/**
Expand Down Expand Up @@ -196,6 +201,8 @@ public function getVersionInstalled($prepend = '', $append = '') : string
* @param string $prepend Prepend a string to the latest version
* @param string $append Append a string to the latest version
*
* @throws \Exception
*
* @return string
*/
public function getVersionAvailable($prepend = '', $append = '') : string
Expand All @@ -217,9 +224,9 @@ public function getVersionAvailable($prepend = '', $append = '') : string
/**
* Retrieve html body with list of all releases from archive URL.
*
* @throws \Exception
*@throws \Exception
*
* @return mixed|\Psr\Http\Message\ResponseInterface
* @return mixed|ResponseInterface
*/
protected function getPackageReleases()
{
Expand Down
19 changes: 18 additions & 1 deletion tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php
<?php declare(strict_types=1);

namespace Codedge\Updater\Tests\SourceRepositoryTypes;

Expand Down Expand Up @@ -106,4 +106,21 @@ public function testFetchingFailsWithException()
$this->expectException(Exception::class);
$class->fetch();
}

public function testHasAccessTokenSet()
{
$config = $this->config;
$config['private_access_token'] = 'abc123';

$class = new GithubRepositoryType($this->client, $config);
$this->assertTrue($class->hasAccessToken());
$this->assertEquals($class->getAccessToken(), 'Bearer abc123');
}

public function testHasAccessTokenNotSet()
{
$class = new GithubRepositoryType($this->client, $this->config);
$this->assertFalse($class->hasAccessToken());
$this->assertEquals($class->getAccessToken(), 'Bearer ');
}
}
2 changes: 2 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ protected function getEnvironmentSetUp($app)
'repository_name' => 'laravel',
'repository_url' => '',
'download_path' => '/tmp',
'private_access_token' => '',
],
'http' => [
'type' => 'http',
'repository_url' => env('SELF_UPDATER_REPO_URL', ''),
'pkg_filename_format' => env('SELF_UPDATER_PKG_FILENAME_FORMAT', 'v_VERSION_'),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
'private_access_token' => '',
],
],
'log_events' => false,
Expand Down