-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added milestone dispatch feature
- Loading branch information
Showing
10 changed files
with
215 additions
and
11 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Jobs\MilestoneRelease; | ||
use App\Models\Repository; | ||
use Illuminate\Console\Command; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Throwable; | ||
|
||
class MilestoneRepository extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'repository:milestone {repository}'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Dispatch a milestone reset job for the given repository.'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @throws Throwable | ||
*/ | ||
public function handle(): void | ||
{ | ||
try { | ||
$repository = Repository::where('name', $this->argument('repository'))->firstOrFail(); | ||
MilestoneRelease::dispatch($repository); | ||
$this->info("Milestone release for $repository->name dispatched."); | ||
} catch (ModelNotFoundException) { | ||
$this->fail("Repository '{$this->argument('repository')}' not found."); | ||
} | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException; | ||
|
||
class MilestoneAuth | ||
{ | ||
/** | ||
* Using the Authorization header to authenticate the user, the token is passed as a Bearer token. | ||
* Check against the configuration milestone_token to allow the request. | ||
* | ||
* @param Closure(Request): (Response) $next | ||
*/ | ||
public function handle(Request $request, Closure $next): Response | ||
{ | ||
if ($request->header('Authorization') !== 'Bearer '.config('repositories.milestone_token')) { | ||
throw new UnauthorizedHttpException('Bearer', 'Unauthenticated'); | ||
} | ||
|
||
return $next($request); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
// | ||
// Copyright (C) 2024 Nethesis S.r.l. | ||
// SPDX-License-Identifier: AGPL-3.0-or-later | ||
// | ||
|
||
namespace App\Jobs; | ||
|
||
use App\Models\Repository; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Foundation\Queue\Queueable; | ||
use Illuminate\Support\Facades\Log; | ||
use Illuminate\Support\Facades\Storage; | ||
|
||
class MilestoneRelease implements ShouldQueue | ||
{ | ||
use Queueable; | ||
|
||
/** | ||
* Sync the milestone release. | ||
*/ | ||
public function __construct(public readonly Repository $repository) {} | ||
|
||
/** | ||
* Execute the job. | ||
*/ | ||
public function handle(): void | ||
{ | ||
Log::debug("Releasing milestone for {$this->repository->name}."); | ||
$toPurge = Storage::directories($this->repository->snapshotDir()); | ||
SyncRepository::dispatchSync($this->repository); | ||
foreach ($toPurge as $dir) { | ||
Log::debug("Purging directory $dir."); | ||
Storage::deleteDirectory($dir); | ||
} | ||
Log::debug("Milestone released for {$this->repository->name}."); | ||
} | ||
} |
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
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,27 @@ | ||
<?php | ||
|
||
use App\Jobs\MilestoneRelease; | ||
use App\Models\Repository; | ||
|
||
use function Pest\Laravel\artisan; | ||
|
||
it('cannot release milestone without a name', function () { | ||
artisan('repository:milestone') | ||
->assertFailed(); | ||
})->throws(RuntimeException::class); | ||
|
||
it('cannot release milestone for a non-existing repository', function () { | ||
artisan('repository:milestone', ['repository' => 'non-existing-repo']) | ||
->assertFailed(); | ||
}); | ||
|
||
it('ensure MilestoneRelease job is dispatched', function () { | ||
$repo = Repository::factory()->create(); | ||
Queue::fake(); | ||
artisan('repository:milestone', ['repository' => $repo->name]) | ||
->expectsOutput("Milestone release for $repo->name dispatched.") | ||
->assertExitCode(0); | ||
Queue::assertPushed(function (MilestoneRelease $job) use ($repo): bool { | ||
return $job->repository->is($repo); | ||
}); | ||
}); |
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,34 @@ | ||
<?php | ||
|
||
use App\Jobs\MilestoneRelease; | ||
use App\Models\Repository; | ||
|
||
use function Pest\Laravel\post; | ||
use function Pest\Laravel\withToken; | ||
|
||
it('cannot access milestone route without auth', function () { | ||
$repo = Repository::factory()->create(); | ||
post("repository/$repo->name/milestone") | ||
->assertUnauthorized() | ||
->assertHeader('WWW-Authenticate', 'Bearer'); | ||
withToken('random')->post("repository/$repo->name/milestone") | ||
->assertUnauthorized() | ||
->assertHeader('WWW-Authenticate', 'Bearer'); | ||
}); | ||
|
||
it('will dispatch a MilestoneRelease job', function () { | ||
$repo = Repository::factory()->create(); | ||
Queue::fake(); | ||
withToken(config('repositories.milestone_token')) | ||
->post("repository/$repo->name/milestone") | ||
->assertOk(); | ||
Queue::assertPushed(function (MilestoneRelease $job) use ($repo): bool { | ||
return $job->repository->is($repo); | ||
}); | ||
}); | ||
|
||
it('cannot dispatch a MilestoneRelease job for a non-existent repository', function () { | ||
withToken(config('repositories.milestone_token')) | ||
->post('repository/non-existent/milestone') | ||
->assertNotFound(); | ||
}); |