Skip to content
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

Add search to repository branch list #343

Merged
merged 3 commits into from
Jul 21, 2023
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
455 changes: 245 additions & 210 deletions composer.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docker/nodejs/run/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
set -e

npm install
npm install --no-save

if [ "${APP_ENV}" == "dev" ]; then
npm run watch &
Expand Down
745 changes: 312 additions & 433 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 15 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@
"axios": "^1.4.0",
"bootstrap": "^5.3.0-alpha3",
"bootstrap-icons": "^1.10.5",
"highlight.js": "^11.7.0",
"highlight.js": "^11.8.0",
"stimulus-use": "^0.52.0"
},
"devDependencies": {
"@babel/core": "^7.20.7",
"@babel/preset-env": "^7.20.2",
"@babel/core": "^7.22.9",
"@babel/preset-env": "^7.22.9",
"@hotwired/stimulus": "^3.2.1",
"@symfony/stimulus-bridge": "^3.2.1",
"@symfony/stimulus-bridge": "^3.2.2",
"@symfony/stimulus-bundle": "file:vendor/symfony/stimulus-bundle/assets",
"@symfony/webpack-encore": "^4.3.0",
"@symfony/webpack-encore": "^4.4.0",
"@typescript-eslint/eslint-plugin": "^5.59.2",
"core-js": "^3.30.1",
"eslint": "^8.40.0",
"core-js": "^3.31.1",
"eslint": "^8.45.0",
"eslint-config-standard-with-typescript": "^36.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^16.0.0",
"eslint-plugin-n": "^16.0.1",
"eslint-plugin-promise": "^6.1.1",
"regenerator-runtime": "^0.13.11",
"sass": "^1.63.6",
"sass-loader": "^13.2.2",
"stylelint": "^15.6.1",
"sass": "^1.64.0",
"sass-loader": "^13.3.2",
"stylelint": "^15.10.2",
"stylelint-config-standard-scss": "^10.0.0",
"stylelint-scss": "^5.0.0",
"ts-loader": "^9.4.2",
"stylelint-scss": "^5.0.1",
"ts-loader": "^9.4.4",
"ts-node": "^10.9.1",
"typescript": "^5.0.4",
"webpack": "^5.88.0",
"typescript": "^5.1.6",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-notifier": "^1.15.0"
},
Expand Down
5 changes: 3 additions & 2 deletions src/Controller/App/Project/ProjectBranchesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DR\Review\Controller\AbstractController;
use DR\Review\Entity\Repository\Repository;
use DR\Review\Exception\RepositoryException;
use DR\Review\Request\Project\ProjectBranchRequest;
use DR\Review\Security\Role\Roles;
use DR\Review\ViewModel\App\Project\ProjectBranchesViewModel;
use DR\Review\ViewModelProvider\ProjectBranchesViewModelProvider;
Expand All @@ -30,11 +31,11 @@ public function __construct(
#[Route('app/projects/{id<\d+>}/branches', name: self::class, methods: 'GET')]
#[Template('app/repository/branches.html.twig')]
#[IsGranted(Roles::ROLE_USER)]
public function __invoke(#[MapEntity] Repository $repository): array
public function __invoke(ProjectBranchRequest $request, #[MapEntity] Repository $repository): array
{
return [
'page_title' => $this->translator->trans('branches'),
'branchesViewModel' => $this->viewModelProvider->getProjectBranchesViewModel($repository)
'branchesViewModel' => $this->viewModelProvider->getProjectBranchesViewModel($repository, $request->getSearchQuery())
];
}
}
28 changes: 28 additions & 0 deletions src/Request/Project/ProjectBranchRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);

namespace DR\Review\Request\Project;

use DigitalRevolution\SymfonyRequestValidation\AbstractValidatedRequest;
use DigitalRevolution\SymfonyRequestValidation\ValidationRules;

class ProjectBranchRequest extends AbstractValidatedRequest
{
public function getSearchQuery(): ?string
{
$value = trim($this->request->query->get('search', ''));

return trim($value) === '' ? null : $value;
}

protected function getValidationRules(): ?ValidationRules
{
return new ValidationRules(
[
'query' => [
'search' => 'string'
]
]
);
}
}
1 change: 1 addition & 0 deletions src/ViewModel/App/Project/ProjectBranchesViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class ProjectBranchesViewModel
*/
public function __construct(
public readonly Repository $repository,
public readonly ?string $searchQuery,
public readonly array $branches,
public readonly array $mergedBranches,
private readonly array $reviews
Expand Down
9 changes: 7 additions & 2 deletions src/ViewModelProvider/ProjectBranchesViewModelProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,19 @@ public function __construct(private readonly GitBranchService $branchService, pr
/**
* @throws RepositoryException
*/
public function getProjectBranchesViewModel(Repository $repository): ProjectBranchesViewModel
public function getProjectBranchesViewModel(Repository $repository, ?string $searchQuery): ProjectBranchesViewModel
{
$branches = $this->branchService->getRemoteBranches($repository);
$mergedBranches = $this->branchService->getRemoteBranches($repository, true);

// filter branches based on searchQuery
if ($searchQuery !== null) {
$branches = array_filter($branches, static fn(string $branch): bool => stripos($branch, $searchQuery) !== false);
}

$branchReviews = $this->reviewRepository->findBy(['repository' => $repository, 'type' => CodeReviewType::BRANCH, 'referenceId' => $branches]);
$branchReviews = Arrays::reindex($branchReviews, static fn($review) => (string)$review->getReferenceId());

return new ProjectBranchesViewModel($repository, $branches, $mergedBranches, $branchReviews);
return new ProjectBranchesViewModel($repository, $searchQuery, $branches, $mergedBranches, $branchReviews);
}
}
14 changes: 13 additions & 1 deletion templates/app/repository/branches.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,19 @@
</ul>
</div>

<table class="table table-bordered table-hover table-sm repository-branch-list">
<form method="get">
<div class="input-group">
<input type="search" name="search" value="{{ branchesViewModel.searchQuery }}" class="form-control"
{{ stimulus_controller('search-field') }}
placeholder="{{ 'search'|trans }}"/>

{% if branchesViewModel.searchQuery != '' %}
<a href="?search=" class="btn btn-secondary">&times;</a>
{% endif %}
</div>
</form>

<table class="table table-bordered table-hover table-sm repository-branch-list mt-3">
<thead>
<tr>
<th scope="col">{{ 'branch'|trans }}</th>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use DR\Review\Controller\AbstractController;
use DR\Review\Controller\App\Project\ProjectBranchesController;
use DR\Review\Entity\Repository\Repository;
use DR\Review\Request\Project\ProjectBranchRequest;
use DR\Review\Tests\AbstractControllerTestCase;
use DR\Review\ViewModel\App\Project\ProjectBranchesViewModel;
use DR\Review\ViewModelProvider\ProjectBranchesViewModelProvider;
Expand All @@ -31,10 +32,13 @@ public function testInvoke(): void
$repository = new Repository();
$viewModel = $this->createMock(ProjectBranchesViewModel::class);

$this->viewModelProvider->expects(self::once())->method('getProjectBranchesViewModel')->with($repository)->willReturn($viewModel);
$request = $this->createMock(ProjectBranchRequest::class);
$request->expects(self::once())->method('getSearchQuery')->willReturn('search');

$this->viewModelProvider->expects(self::once())->method('getProjectBranchesViewModel')->with($repository, 'search')->willReturn($viewModel);
$this->translator->expects(self::once())->method('trans')->with('branches')->willReturn('Branches');

$result = ($this->controller)($repository);
$result = ($this->controller)($request, $repository);
static::assertSame('Branches', $result['page_title']);
static::assertSame($viewModel, $result['branchesViewModel']);
}
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Request/Project/ProjectBranchRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
declare(strict_types=1);

namespace DR\Review\Tests\Unit\Request\Project;

use DigitalRevolution\SymfonyRequestValidation\ValidationRules;
use DigitalRevolution\SymfonyValidationShorthand\Rule\InvalidRuleException;
use DR\Review\Request\Project\ProjectBranchRequest;
use DR\Review\Tests\Unit\Request\AbstractRequestTestCase;
use PHPUnit\Framework\Attributes\CoversClass;

/**
* @extends AbstractRequestTestCase<ProjectBranchRequest>
*/
#[CoversClass(ProjectBranchRequest::class)]
class ProjectBranchRequestTest extends AbstractRequestTestCase
{
public function testGetSearchQuery(): void
{
static::assertNull($this->validatedRequest->getSearchQuery());

$this->request->query->set('search', ' search ');
static::assertSame('search', $this->validatedRequest->getSearchQuery());
}

/**
* @throws InvalidRuleException
*/
public function testGetValidationRules(): void
{
$expected = new ValidationRules(
[
'query' => [
'search' => 'string'
]
]
);
$this->expectGetValidationRules($expected);

$this->validatedRequest->validate();
}

protected static function getClassToTest(): string
{
return ProjectBranchRequest::class;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public function testGetReview(): void
{
$review = new CodeReview();
$repository = new Repository();
$model = new ProjectBranchesViewModel($repository, [], [], ['branch' => $review]);
$model = new ProjectBranchesViewModel($repository, null, [], [], ['branch' => $review]);

static::assertNull($model->getReview('foobar'));
static::assertSame($review, $model->getReview('branch'));
Expand All @@ -25,7 +25,7 @@ public function testGetReview(): void
public function testIsMerged(): void
{
$repository = new Repository();
$model = new ProjectBranchesViewModel($repository, ['branch A', 'branch B'], ['branch B'], []);
$model = new ProjectBranchesViewModel($repository, null, ['branch A', 'branch B'], ['branch B'], []);

static::assertFalse($model->isMerged('branch A'));
static::assertTrue($model->isMerged('branch B'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ public function testGetProjectBranchesViewModel(): void
$this->branchService->expects(self::exactly(2))
->method('getRemoteBranches')
->with($repository)
->willReturn(['branchA', 'mergedB'], ['mergedB']);
->willReturn(['branchA', 'mergedBranchB', 'foobar'], ['mergedBranchB']);
$this->reviewRepository->expects(self::once())
->method('findBy')
->with(['repository' => $repository, 'type' => CodeReviewType::BRANCH, 'referenceId' => ['branchA', 'mergedB']])
->with(['repository' => $repository, 'type' => CodeReviewType::BRANCH, 'referenceId' => ['branchA', 'mergedBranchB']])
->willReturn([$review]);

$model = $this->provider->getProjectBranchesViewModel($repository);
$model = $this->provider->getProjectBranchesViewModel($repository, 'branch');
static::assertSame($repository, $model->repository);
static::assertSame(['branchA', 'mergedB'], $model->branches);
static::assertSame(['mergedB'], $model->mergedBranches);
static::assertSame(['branchA', 'mergedBranchB'], $model->branches);
static::assertSame(['mergedBranchB'], $model->mergedBranches);
static::assertSame($review, $model->getReview('branch'));
}
}