Add ticket-driven branch creation flow
This commit is contained in:
@@ -58,6 +58,7 @@ class GiteaProviderTest extends TestCase
|
||||
'name' => 'kanbangit',
|
||||
'full_name' => 'nico/kanbangit',
|
||||
'html_url' => 'https://git.example.test/nico/kanbangit',
|
||||
'default_branch' => 'main',
|
||||
'owner' => [
|
||||
'login' => 'nico',
|
||||
],
|
||||
@@ -75,6 +76,7 @@ class GiteaProviderTest extends TestCase
|
||||
$this->assertContainsOnlyInstancesOf(GitRepository::class, $repositories);
|
||||
$this->assertSame('501', $repositories[0]->providerRepositoryId);
|
||||
$this->assertSame('nico/kanbangit', $repositories[0]->fullName);
|
||||
$this->assertSame('main', $repositories[0]->defaultBranch);
|
||||
$this->assertFalse($repositories[0]->isPrivate);
|
||||
|
||||
Http::assertSent(function ($request): bool {
|
||||
@@ -83,6 +85,41 @@ class GiteaProviderTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the provider can fetch a single repository with its default branch.
|
||||
*/
|
||||
public function test_provider_can_fetch_a_single_repository(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/repos/core-team/kanbangit' => Http::response([
|
||||
'id' => 501,
|
||||
'name' => 'kanbangit',
|
||||
'full_name' => 'core-team/kanbangit',
|
||||
'html_url' => 'https://git.example.test/core-team/kanbangit',
|
||||
'default_branch' => 'main',
|
||||
'owner' => [
|
||||
'login' => 'core-team',
|
||||
],
|
||||
'private' => true,
|
||||
]),
|
||||
]);
|
||||
|
||||
$repository = app(GiteaProviderClient::class)->getRepository(
|
||||
'https://git.example.test',
|
||||
'repo-token',
|
||||
'core-team',
|
||||
'kanbangit',
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(GitRepository::class, $repository);
|
||||
$this->assertSame('501', $repository->providerRepositoryId);
|
||||
$this->assertSame('core-team', $repository->owner);
|
||||
$this->assertSame('kanbangit', $repository->name);
|
||||
$this->assertSame('main', $repository->defaultBranch);
|
||||
$this->assertTrue($repository->isPrivate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the provider lists branches for a specific repository.
|
||||
*/
|
||||
@@ -119,6 +156,38 @@ class GiteaProviderTest extends TestCase
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the provider can create branches from a base reference.
|
||||
*/
|
||||
public function test_provider_can_create_a_repository_branch(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches' => Http::response([
|
||||
'name' => 'core-1-login-form',
|
||||
], 201),
|
||||
]);
|
||||
|
||||
$branch = app(GiteaProviderClient::class)->createBranch(
|
||||
'https://git.example.test',
|
||||
'branch-token',
|
||||
'core-team',
|
||||
'kanbangit',
|
||||
'core-1-login-form',
|
||||
'main',
|
||||
);
|
||||
|
||||
$this->assertInstanceOf(GitBranch::class, $branch);
|
||||
$this->assertSame('core-1-login-form', $branch->name);
|
||||
|
||||
Http::assertSent(function ($request): bool {
|
||||
return $request->url() === 'https://git.example.test/api/v1/repos/core-team/kanbangit/branches'
|
||||
&& $request['new_branch_name'] === 'core-1-login-form'
|
||||
&& $request['old_ref_name'] === 'main'
|
||||
&& $request->hasHeader('Authorization', 'token branch-token');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the provider lists pull requests for a specific repository.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Git;
|
||||
|
||||
use App\Actions\Git\TicketKeyReferenceMatcher;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TicketKeyReferenceMatcherTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure a reference must start with the exact ticket key.
|
||||
*/
|
||||
public function test_matcher_accepts_exact_ticket_key_prefixes(): void
|
||||
{
|
||||
$matcher = app(TicketKeyReferenceMatcher::class);
|
||||
|
||||
$this->assertTrue($matcher->matches('CORE-1', 'CORE-1-login-form'));
|
||||
$this->assertTrue($matcher->matches('CORE-1', 'CORE-1/fix-oauth'));
|
||||
$this->assertTrue($matcher->matches('CORE-1', 'CORE-1_hotfix'));
|
||||
$this->assertTrue($matcher->matches('CORE-1', 'CORE-1'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure similar ticket keys are not treated as matches.
|
||||
*/
|
||||
public function test_matcher_rejects_other_ticket_prefixes(): void
|
||||
{
|
||||
$matcher = app(TicketKeyReferenceMatcher::class);
|
||||
|
||||
$this->assertFalse($matcher->matches('CORE-1', 'CORE-10-login-form'));
|
||||
$this->assertFalse($matcher->matches('CORE-1', 'MY-CORE-1-login-form'));
|
||||
$this->assertFalse($matcher->matches('CORE-1', 'core-1-login-form'));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Projects;
|
||||
|
||||
use App\Actions\Git\GitProviderKey;
|
||||
use App\Actions\ProjectPermissions\ProjectPermissionService;
|
||||
use App\Actions\ProjectTicketMetadata\InitializeProjectTicketMetadata;
|
||||
use App\Models\Project;
|
||||
use App\Models\RepositoryConnection;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\TicketPriority;
|
||||
use App\Models\User;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ProjectTicketBranchControllerTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
/**
|
||||
* Ensure authorized users can validate available branch names.
|
||||
*/
|
||||
public function test_users_with_edit_permission_can_validate_available_branch_names(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches*' => Http::response([
|
||||
['name' => 'main'],
|
||||
['name' => 'release/1.0'],
|
||||
]),
|
||||
]);
|
||||
|
||||
[$user, $project, $ticket] = $this->ticketContext();
|
||||
|
||||
RepositoryConnection::factory()->for($project)->create([
|
||||
'base_url' => 'https://git.example.test',
|
||||
'owner_name' => 'core-team',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
'provider_repository_id' => '501',
|
||||
'repository_full_name' => 'core-team/kanbangit',
|
||||
'repository_name' => 'kanbangit',
|
||||
'repository_url' => 'https://git.example.test/core-team/kanbangit',
|
||||
]);
|
||||
UserGitConnection::factory()->for($user)->create([
|
||||
'base_url' => 'https://git.example.test',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
]);
|
||||
|
||||
app(ProjectPermissionService::class)->syncUserPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson(route('projects.tickets.branches.validate', [
|
||||
'project' => $project,
|
||||
'ticket' => $ticket,
|
||||
'branch_name' => 'core-1-login-form',
|
||||
'repository_connection_id' => $project->repositoryConnections()->firstOrFail()->id,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'is_valid' => true,
|
||||
'message' => 'Branch name is available for CORE-1.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure branch validation reports existing branch names.
|
||||
*/
|
||||
public function test_branch_validation_rejects_existing_branch_names(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches*' => Http::response([
|
||||
['name' => 'core-1-login-form'],
|
||||
]),
|
||||
]);
|
||||
|
||||
[$user, $project, $ticket] = $this->ticketContext();
|
||||
|
||||
RepositoryConnection::factory()->for($project)->create([
|
||||
'base_url' => 'https://git.example.test',
|
||||
'owner_name' => 'core-team',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
'provider_repository_id' => '501',
|
||||
'repository_full_name' => 'core-team/kanbangit',
|
||||
'repository_name' => 'kanbangit',
|
||||
'repository_url' => 'https://git.example.test/core-team/kanbangit',
|
||||
]);
|
||||
UserGitConnection::factory()->for($user)->create([
|
||||
'base_url' => 'https://git.example.test',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
]);
|
||||
|
||||
app(ProjectPermissionService::class)->syncUserPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson(route('projects.tickets.branches.validate', [
|
||||
'project' => $project,
|
||||
'ticket' => $ticket,
|
||||
'branch_name' => 'core-1-login-form',
|
||||
'repository_connection_id' => $project->repositoryConnections()->firstOrFail()->id,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'is_valid' => false,
|
||||
'message' => 'This branch name already exists in the selected repository.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure inaccessible repositories remain visible but cannot be used for branch creation.
|
||||
*/
|
||||
public function test_branch_validation_requires_a_matching_personal_git_connection(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake();
|
||||
|
||||
[$user, $project, $ticket] = $this->ticketContext();
|
||||
|
||||
RepositoryConnection::factory()->for($project)->create([
|
||||
'base_url' => 'https://git.example.test',
|
||||
'owner_name' => 'core-team',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
'provider_repository_id' => '501',
|
||||
'repository_full_name' => 'core-team/kanbangit',
|
||||
'repository_name' => 'kanbangit',
|
||||
'repository_url' => 'https://git.example.test/core-team/kanbangit',
|
||||
]);
|
||||
|
||||
app(ProjectPermissionService::class)->syncUserPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->getJson(route('projects.tickets.branches.validate', [
|
||||
'project' => $project,
|
||||
'ticket' => $ticket,
|
||||
'branch_name' => 'core-1-login-form',
|
||||
'repository_connection_id' => $project->repositoryConnections()->firstOrFail()->id,
|
||||
]))
|
||||
->assertOk()
|
||||
->assertExactJson([
|
||||
'is_valid' => false,
|
||||
'message' => 'Connect a matching Git connection in your user settings first.',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure users with edit permission can create branches from the ticket detail flow.
|
||||
*/
|
||||
public function test_users_with_edit_permission_can_create_ticket_branches(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake([
|
||||
'https://git.example.test/api/v1/repos/core-team/kanbangit/branches*' => Http::sequence()
|
||||
->push([
|
||||
['name' => 'main'],
|
||||
], 200)
|
||||
->push([
|
||||
'name' => 'core-1-login-form',
|
||||
], 201),
|
||||
'https://git.example.test/api/v1/repos/core-team/kanbangit' => Http::response([
|
||||
'id' => 501,
|
||||
'name' => 'kanbangit',
|
||||
'full_name' => 'core-team/kanbangit',
|
||||
'html_url' => 'https://git.example.test/core-team/kanbangit',
|
||||
'default_branch' => 'main',
|
||||
'owner' => [
|
||||
'login' => 'core-team',
|
||||
],
|
||||
'private' => false,
|
||||
]),
|
||||
]);
|
||||
|
||||
[$user, $project, $ticket] = $this->ticketContext();
|
||||
|
||||
$repositoryConnection = RepositoryConnection::factory()->for($project)->create([
|
||||
'base_url' => 'https://git.example.test',
|
||||
'owner_name' => 'core-team',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
'provider_repository_id' => '501',
|
||||
'repository_full_name' => 'core-team/kanbangit',
|
||||
'repository_name' => 'kanbangit',
|
||||
'repository_url' => 'https://git.example.test/core-team/kanbangit',
|
||||
]);
|
||||
UserGitConnection::factory()->for($user)->create([
|
||||
'api_token' => 'branch-token',
|
||||
'base_url' => 'https://git.example.test',
|
||||
'provider' => GitProviderKey::Gitea,
|
||||
]);
|
||||
|
||||
app(ProjectPermissionService::class)->syncUserPermissions($project, $user, ['edit', 'view']);
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('projects.tickets.branches.store', [$project, $ticket]), [
|
||||
'branch_name' => ' core-1-login-form ',
|
||||
'repository_connection_id' => $repositoryConnection->id,
|
||||
])
|
||||
->assertRedirect(route('projects.tickets.show', [$project, $ticket]))
|
||||
->assertSessionHas('status', 'core-1-login-form was created in core-team/kanbangit.');
|
||||
|
||||
Http::assertSent(function ($request): bool {
|
||||
return $request->url() === 'https://git.example.test/api/v1/repos/core-team/kanbangit/branches'
|
||||
&& $request['new_branch_name'] === 'core-1-login-form'
|
||||
&& $request['old_ref_name'] === 'main'
|
||||
&& $request->hasHeader('Authorization', 'token branch-token');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure project edit permission is required for branch creation.
|
||||
*/
|
||||
public function test_branch_creation_requires_the_project_edit_permission(): void
|
||||
{
|
||||
Http::preventStrayRequests();
|
||||
Http::fake();
|
||||
|
||||
[$user, $project, $ticket] = $this->ticketContext();
|
||||
|
||||
$repositoryConnection = RepositoryConnection::factory()->for($project)->create();
|
||||
|
||||
$this->actingAs($user)
|
||||
->post(route('projects.tickets.branches.store', [$project, $ticket]), [
|
||||
'branch_name' => 'core-1-login-form',
|
||||
'repository_connection_id' => $repositoryConnection->id,
|
||||
])
|
||||
->assertForbidden();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array{0: User, 1: Project, 2: Ticket}
|
||||
*/
|
||||
private function ticketContext(): array
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
$project = Project::factory()->create([
|
||||
'key' => 'CORE',
|
||||
]);
|
||||
|
||||
app(InitializeProjectTicketMetadata::class)->handle($project);
|
||||
|
||||
$priority = TicketPriority::factory()->for($project)->create([
|
||||
'name' => 'Medium',
|
||||
'position' => 1,
|
||||
]);
|
||||
$taskType = $project->ticketTypes()->where('system_key', 'task')->firstOrFail();
|
||||
|
||||
$ticket = Ticket::factory()->create([
|
||||
'project_id' => $project->id,
|
||||
'sequence_number' => 1,
|
||||
'ticket_key' => 'CORE-1',
|
||||
'ticket_priority_id' => $priority->id,
|
||||
'ticket_type_id' => $taskType->id,
|
||||
'title' => 'Login form',
|
||||
]);
|
||||
|
||||
return [$user, $project, $ticket];
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,7 @@ class ProjectTicketControllerTest extends TestCase
|
||||
->component('projects/Ticket')
|
||||
->where('project.id', $project->id)
|
||||
->where('ticket.ticket_key', 'CORE-1')
|
||||
->where('suggestedBranchName', 'core-1-ticket-detail-test')
|
||||
->where('ticket.field_values.0.ticket_field_definition.label', 'Story Points')
|
||||
->where('ticket.field_values.0.value_number', 5)
|
||||
->where('ticket.worklog_entries.0.actor.name', $viewer->name)
|
||||
@@ -202,6 +203,7 @@ class ProjectTicketControllerTest extends TestCase
|
||||
->assertOk()
|
||||
->assertInertia(fn (Assert $page) => $page
|
||||
->component('projects/Ticket')
|
||||
->where('suggestedBranchName', 'core-1-branch-detail-test')
|
||||
->where('repositoryBranches.0.repository_connection.repository_full_name', 'core-team/kanbangit')
|
||||
->has('repositoryBranches.0.branches', 2)
|
||||
->where('repositoryBranches.0.branches.0.name', 'CORE-1-login-form')
|
||||
|
||||
Reference in New Issue
Block a user