Skip to content

Commit

Permalink
#443 Set Gitlab base url (#445)
Browse files Browse the repository at this point in the history
  • Loading branch information
codedge authored Apr 5, 2023
1 parent 6f05b66 commit a211b11
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 43 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/markdown-normalize.yml

This file was deleted.

2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php: ['8.1']
php: ['8.1', '8.2']
laravel: [10.*]
dependency-version: [prefer-stable]
include:
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ Configure Gitlab either via the `config/self-updater.php` or use the appropriate
// ...
'repository_types' => [
'gitlab' => [
'base_url' => '',
'type' => 'gitlab',
'repository_id' => env('SELF_UPDATER_REPO_URL', ''),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
Expand All @@ -187,6 +188,8 @@ Configure Gitlab either via the `config/self-updater.php` or use the appropriate

ℹ Although the environment variable is named `SELF_UPDATER_REPO_URL`, only specify your repository id.

For self-hosted Gitlab instances you can set the `base_url` variable to a domain where the instance is hosted at, f. ex. `http://gitlab.acme.local`.

### Using HTTP archives

The package comes with an _HTTP_ source repository type to fetch
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dg/bypass-finals": "^1.4",
"mikey179/vfsstream": "^1.6",
"mockery/mockery": "^1.5",
"orchestra/testbench": "^8.0",
"orchestra/testbench": "^8.1",
"phpunit/phpunit": "^9.5.26"
},
"minimum-stability": "dev",
Expand Down
1 change: 1 addition & 0 deletions config/self-update.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
'use_branch' => env('SELF_UPDATER_USE_BRANCH', ''),
],
'gitlab' => [
'base_url' => '',
'type' => 'gitlab',
'repository_id' => env('SELF_UPDATER_REPO_URL', ''),
'download_path' => env('SELF_UPDATER_DOWNLOAD_PATH', '/tmp'),
Expand Down
17 changes: 14 additions & 3 deletions src/SourceRepositoryTypes/GitlabRepositoryType.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,19 @@ public function selectRelease(Collection $collection, string $version)
return $release;
}

final public function getReleases(): Response
/**
* @return array{base_url:string, url:string}
*/
final public function getReleaseUrl(): array
{
$url = '/api/v4/projects/'.$this->config['repository_id'].'/releases';
return [
'base_url' => $this->config['base_url'] ?? self::BASE_URL,
'url' => '/api/v4/projects/'.$this->config['repository_id'].'/releases',
];
}

final public function getReleases(): Response
{
$headers = [];

if ($this->release->hasAccessToken()) {
Expand All @@ -156,6 +165,8 @@ final public function getReleases(): Response
];
}

return Http::withHeaders($headers)->baseUrl(self::BASE_URL)->get($url);
$urls = $this->getReleaseUrl();

return Http::withHeaders($headers)->baseUrl($urls['base_url'])->get($urls['url']);
}
}
4 changes: 2 additions & 2 deletions tests/Commands/CheckUpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function it_can_run_check_update_command_without_new_version_available():
->pushResponse($this->getResponse200ZipFile());

/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

$github->deleteVersionFile();

Expand All @@ -41,7 +41,7 @@ public function it_can_run_check_update_command_without_new_version_available():
public function it_can_run_check_update_command_with_new_version_available(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$github->setVersionFile('v3.5');

config(['self-update.version_installed' => 'v1.0']);
Expand Down
34 changes: 17 additions & 17 deletions tests/SourceRepositoryTypes/GithubRepositoryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected function setUp(): void
public function it_can_instantiate(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

$this->assertInstanceOf(GithubTagType::class, $github);
}
Expand All @@ -39,7 +39,7 @@ public function it_can_instantiate_branch_type(): void
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

$this->assertInstanceOf(GithubBranchType::class, $github);
}
Expand All @@ -52,14 +52,14 @@ public function it_cannot_instantiate_and_fails_with_exception(): void
$this->expectException(\Exception::class);

/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
}

/** @test */
public function it_can_run_update(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fake([
'*' => $this->getResponse200ZipFile(),
Expand Down Expand Up @@ -89,7 +89,7 @@ public function it_can_run_update(): void
public function it_can_get_the_version_installed(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$this->assertEmpty($github->getVersionInstalled());

config(['self-update.version_installed' => '1.0']);
Expand All @@ -103,15 +103,15 @@ public function it_cannot_get_new_version_available_and_fails_with_exception():
$this->expectExceptionMessage('Version installed not found.');

/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$github->isNewVersionAvailable();
}

/** @test */
public function it_can_get_new_version_available_from_type_tag_without_version_file(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$github->deleteVersionFile();

Event::fake();
Expand All @@ -132,7 +132,7 @@ public function it_can_get_new_version_available_from_type_tag_without_version_f
public function it_can_get_new_version_available_from_type_tag_with_version_file(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$github->setVersionFile('v2.7');

$this->assertFalse($github->isNewVersionAvailable('v2.7'));
Expand All @@ -149,7 +149,7 @@ public function it_can_get_new_version_available_from_type_branch_without_versio
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$github->deleteVersionFile();

Http::fake([
Expand All @@ -166,7 +166,7 @@ public function it_can_get_new_version_available_from_type_branch_with_version_f
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();
$github->setVersionFile('2020-02-07T21:09:15Z');

$this->assertFalse($github->isNewVersionAvailable('2020-02-08T21:09:15Z'));
Expand All @@ -177,7 +177,7 @@ public function it_can_get_new_version_available_from_type_branch_with_version_f
public function it_can_fetch_github_tag_releases_latest(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('tag'))
Expand All @@ -194,7 +194,7 @@ public function it_can_fetch_github_tag_releases_latest(): void
public function it_can_fetch_github_tag_releases_specific_version(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('tag'))
Expand All @@ -211,7 +211,7 @@ public function it_can_fetch_github_tag_releases_specific_version(): void
public function it_can_fetch_github_tag_releases_and_takes_latest_if_version_not_available(): void
{
/** @var GithubTagType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('tag'))
Expand All @@ -230,7 +230,7 @@ public function it_can_fetch_github_branch_releases_latest(): void
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('branch'))
Expand All @@ -249,7 +249,7 @@ public function it_can_fetch_github_branch_releases_specific_version(): void
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('branch'))
Expand All @@ -268,7 +268,7 @@ public function it_can_fetch_github_branch_releases_and_takes_latest_if_version_
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fakeSequence()
->pushResponse($this->getResponse200Type('branch'))
Expand All @@ -287,7 +287,7 @@ public function it_cannot_fetch_github_branch_releases_if_response_empty(): void
config(['self-update.repository_types.github.use_branch' => 'v2']);

/** @var GithubBranchType $github */
$github = (resolve(GithubRepositoryType::class))->create();
$github = resolve(GithubRepositoryType::class)->create();

Http::fake([
'*' => $this->getResponseEmpty(),
Expand Down
22 changes: 22 additions & 0 deletions tests/SourceRepositoryTypes/GitlabRepositoryTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,26 @@ public function it_takes_latest_release_if_no_other_found(): void

$this->assertEquals('1.3', $gitlab->selectRelease(collect($items), '1.7')['tag_name']);
}

/** @test */
public function it_can_use_default_base_url(): void
{
/** @var GitlabRepositoryType $gitlab */
$gitlab = resolve(GitlabRepositoryType::class);
$urls = $gitlab->getReleaseUrl();

$this->assertEquals('https://gitlab.com', $urls['base_url']);
}

/** @test */
public function it_can_use_base_url_from_config(): void
{
config(['self-update.repository_types.gitlab.base_url' => 'https://example.local']);

/** @var GitlabRepositoryType $gitlab */
$gitlab = resolve(GitlabRepositoryType::class);
$urls = $gitlab->getReleaseUrl();

$this->assertEquals('https://example.local', $urls['base_url']);
}
}

0 comments on commit a211b11

Please sign in to comment.