Add application API and OpenAPI docs
This commit is contained in:
@@ -2,29 +2,23 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Actions\Git\Data\GitBranch;
|
||||
use App\Actions\Git\GitProviderManager;
|
||||
use App\Actions\Git\ValidateGitBranchName;
|
||||
use App\Actions\Permissions\PermissionService;
|
||||
use App\Actions\Tickets\CreateProjectTicketBranch;
|
||||
use App\Actions\Tickets\ValidateProjectTicketBranch;
|
||||
use App\Enums\Permission;
|
||||
use App\Http\Requests\StoreProjectTicketBranchRequest;
|
||||
use App\Http\Requests\ValidateProjectTicketBranchRequest;
|
||||
use App\Models\Project;
|
||||
use App\Models\RepositoryConnection;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\UserGitConnection;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Throwable;
|
||||
|
||||
class ProjectTicketBranchController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly GitProviderManager $gitProviderManager,
|
||||
private readonly ValidateGitBranchName $validateGitBranchName,
|
||||
private readonly CreateProjectTicketBranch $createProjectTicketBranch,
|
||||
private readonly PermissionService $permissionService,
|
||||
private readonly ValidateProjectTicketBranch $validateProjectTicketBranch,
|
||||
) {}
|
||||
|
||||
public function validate(
|
||||
@@ -39,65 +33,9 @@ class ProjectTicketBranchController extends Controller
|
||||
403,
|
||||
);
|
||||
|
||||
$validated = $request->validated();
|
||||
$repositoryConnectionId = Arr::get($validated, 'repository_connection_id');
|
||||
$branchName = trim((string) Arr::get($validated, 'branch_name'));
|
||||
|
||||
$repositoryConnection = $this->projectRepositoryConnection(
|
||||
$project,
|
||||
$repositoryConnectionId,
|
||||
return response()->json(
|
||||
$this->validateProjectTicketBranch->handle($project, $ticket, $user, $request->validated()),
|
||||
);
|
||||
$userGitConnection = $this->matchingUserGitConnection(
|
||||
$user->id,
|
||||
$repositoryConnection,
|
||||
);
|
||||
|
||||
if ($userGitConnection === null) {
|
||||
return response()->json([
|
||||
'is_valid' => false,
|
||||
'message' => 'Connect a matching Git connection in your user settings first.',
|
||||
]);
|
||||
}
|
||||
|
||||
$syntaxMessage = $this->validateGitBranchName->message($branchName);
|
||||
|
||||
if ($syntaxMessage !== null) {
|
||||
return response()->json([
|
||||
'is_valid' => false,
|
||||
'message' => $syntaxMessage,
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$branchAlreadyExists = collect(
|
||||
$this->gitProviderManager
|
||||
->client($userGitConnection->provider)
|
||||
->listBranches(
|
||||
$userGitConnection->api_token,
|
||||
$repositoryConnection->toGitRepository(),
|
||||
),
|
||||
)
|
||||
->contains(
|
||||
fn (GitBranch $branch): bool => $branch->name === $branchName,
|
||||
);
|
||||
} catch (Throwable) {
|
||||
return response()->json([
|
||||
'is_valid' => false,
|
||||
'message' => sprintf('%s could not validate this branch name right now.', config('app.name')),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($branchAlreadyExists) {
|
||||
return response()->json([
|
||||
'is_valid' => false,
|
||||
'message' => 'This branch name already exists in the selected repository.',
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'is_valid' => true,
|
||||
'message' => "Branch name is available for {$ticket->ticket_key}.",
|
||||
]);
|
||||
}
|
||||
|
||||
public function store(
|
||||
@@ -113,100 +51,12 @@ class ProjectTicketBranchController extends Controller
|
||||
403,
|
||||
);
|
||||
|
||||
$validated = $request->validated();
|
||||
$repositoryConnectionId = Arr::get($validated, 'repository_connection_id');
|
||||
$branchName = trim((string) Arr::get($validated, 'branch_name'));
|
||||
|
||||
$repositoryConnection = $this->projectRepositoryConnection(
|
||||
$project,
|
||||
$repositoryConnectionId,
|
||||
);
|
||||
$userGitConnection = $this->matchingUserGitConnection(
|
||||
$user->id,
|
||||
$repositoryConnection,
|
||||
);
|
||||
|
||||
if ($userGitConnection === null) {
|
||||
throw ValidationException::withMessages([
|
||||
'repository_connection_id' => 'Connect a matching Git connection in your user settings first.',
|
||||
]);
|
||||
}
|
||||
|
||||
$syntaxMessage = $this->validateGitBranchName->message($branchName);
|
||||
|
||||
if ($syntaxMessage !== null) {
|
||||
throw ValidationException::withMessages([
|
||||
'branch_name' => $syntaxMessage,
|
||||
]);
|
||||
}
|
||||
|
||||
$gitProvider = $this->gitProviderManager->client($userGitConnection->provider);
|
||||
|
||||
try {
|
||||
$branchAlreadyExists = collect(
|
||||
$gitProvider->listBranches(
|
||||
$userGitConnection->api_token,
|
||||
$repositoryConnection->toGitRepository(),
|
||||
),
|
||||
)
|
||||
->contains(
|
||||
fn (GitBranch $branch): bool => $branch->name === $branchName,
|
||||
);
|
||||
} catch (Throwable) {
|
||||
throw ValidationException::withMessages([
|
||||
'branch_name' => sprintf('%s could not validate this branch name right now.', config('app.name')),
|
||||
]);
|
||||
}
|
||||
|
||||
if ($branchAlreadyExists) {
|
||||
throw ValidationException::withMessages([
|
||||
'branch_name' => 'This branch name already exists in the selected repository.',
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$repository = $gitProvider->getRepository(
|
||||
$userGitConnection->base_url,
|
||||
$userGitConnection->api_token,
|
||||
$repositoryConnection->owner_name,
|
||||
$repositoryConnection->repository_name,
|
||||
);
|
||||
|
||||
$gitProvider->createBranch(
|
||||
$userGitConnection->base_url,
|
||||
$userGitConnection->api_token,
|
||||
$repositoryConnection->owner_name,
|
||||
$repositoryConnection->repository_name,
|
||||
$branchName,
|
||||
$repository->defaultBranch,
|
||||
);
|
||||
} catch (Throwable) {
|
||||
throw ValidationException::withMessages([
|
||||
'branch_name' => sprintf('%s could not create this branch right now.', config('app.name')),
|
||||
]);
|
||||
}
|
||||
$result = $this->createProjectTicketBranch->handle($project, $ticket, $user, $request->validated());
|
||||
|
||||
return to_route('projects.tickets.show', [$project, $ticket])
|
||||
->with(
|
||||
'status',
|
||||
"{$branchName} was created in {$repositoryConnection->repository_full_name}.",
|
||||
"{$result['branch_name']} was created in {$result['repository_full_name']}.",
|
||||
);
|
||||
}
|
||||
|
||||
private function projectRepositoryConnection(Project $project, int $repositoryConnectionId): RepositoryConnection
|
||||
{
|
||||
return $project->repositoryConnections()
|
||||
->findOrFail($repositoryConnectionId);
|
||||
}
|
||||
|
||||
private function matchingUserGitConnection(
|
||||
int $userId,
|
||||
RepositoryConnection $repositoryConnection,
|
||||
): ?UserGitConnection {
|
||||
return UserGitConnection::query()
|
||||
->where('user_id', $userId)
|
||||
->where('provider', $repositoryConnection->provider)
|
||||
->where('base_url', $repositoryConnection->base_url)
|
||||
->first();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user