Skip to content
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
16 changes: 14 additions & 2 deletions src/Api/Action/GitLab/ListGitLabIssuesAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace mglaman\DrupalOrg\Action\GitLab;

use mglaman\DrupalOrg\Enum\ProjectIssueCategory;
use mglaman\DrupalOrg\Enum\ProjectIssueType;
use mglaman\DrupalOrg\GitLab\Client as GitLabClient;
use mglaman\DrupalOrg\GitLab\Entity\GitLabIssue;
use mglaman\DrupalOrg\Result\GitLab\GitLabIssuesResult;
Expand All @@ -14,15 +16,25 @@ public function __construct(private readonly GitLabClient $gitLabClient)
{
}

public function __invoke(string $projectMachineName, string $state = 'opened', int $limit = 25): GitLabIssuesResult
{
public function __invoke(
string $projectMachineName,
string $state = 'opened',
int $limit = 25,
ProjectIssueType $type = ProjectIssueType::All,
?ProjectIssueCategory $category = null,
): GitLabIssuesResult {
$params = [
'state' => $state,
'per_page' => $limit,
'order_by' => 'created_at',
'sort' => 'desc',
];

$labels = array_filter([$type->gitLabLabel(), $category?->gitLabLabel()]);
if ($labels !== []) {
$params['labels'] = implode(',', $labels);
}

$data = $this->gitLabClient->getIssues('project/' . $projectMachineName, $params);
$issues = array_map(
static fn(\stdClass $item) => GitLabIssue::fromStdClass($item),
Expand Down
5 changes: 5 additions & 0 deletions src/Api/Enum/ProjectIssueCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function categoryId(): int
ProjectIssueCategory::Plan => 5,
};
}

public function gitLabLabel(): string
{
return 'category::' . $this->value;
}
}
9 changes: 9 additions & 0 deletions src/Api/Enum/ProjectIssueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ enum ProjectIssueType: string
case All = 'all';
case Rtbc = 'rtbc';
case Review = 'review';

public function gitLabLabel(): ?string
{
return match ($this) {
ProjectIssueType::All => null,
ProjectIssueType::Rtbc => 'state::rtbc',
ProjectIssueType::Review => 'state::needsReview',
};
}
}
9 changes: 7 additions & 2 deletions src/Api/Mcp/ToolRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,13 @@ public function gitlabProjectIssues(
#[Schema(description: 'Issue state filter.', enum: ['opened', 'closed', 'all'])]
string $state = 'opened',
#[Schema(description: 'Maximum number of issues to return.', minimum: 1, maximum: 100)]
int $limit = 25
int $limit = 25,
#[Schema(description: 'Filter issues by status type.', enum: ['all', 'rtbc', 'review'])]
string $type = 'all',
#[Schema(description: 'Filter issues by category.', enum: ['bug', 'task', 'feature', 'support', 'plan'])]
?string $category = null
): mixed {
return (new ListGitLabIssuesAction(new GitLabClient()))($machineName, $state, $limit)->jsonSerialize();
$issueCategory = $category !== null ? ProjectIssueCategory::from($category) : null;
return (new ListGitLabIssuesAction(new GitLabClient()))($machineName, $state, $limit, ProjectIssueType::from($type), $issueCategory)->jsonSerialize();
}
}
14 changes: 10 additions & 4 deletions src/Cli/Command/Project/ProjectIssues.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,18 @@ protected function execute(
): int {
$format = (string) $this->stdIn->getOption('format');
$limit = (int) $this->stdIn->getOption('limit');
$type = ProjectIssueType::from((string) $this->stdIn->getArgument('type'));
$categoryOption = $this->stdIn->getOption('category');
$category = $categoryOption !== null ? ProjectIssueCategory::from((string) $categoryOption) : null;

if (!$this->projectData->hasIssueQueue) {
$this->stdErr->writeln('<comment>Project uses GitLab work items (no Drupal.org issue queue).</comment>');
$result = (new ListGitLabIssuesAction(new GitLabClient()))($this->projectData->machineName, limit: $limit);
$result = (new ListGitLabIssuesAction(new GitLabClient()))(
$this->projectData->machineName,
limit: $limit,
type: $type,
category: $category,
);
if ($this->writeFormatted($result, $format)) {
return 0;
}
Expand All @@ -98,11 +106,9 @@ protected function execute(
}

$action = new GetProjectIssuesAction($this->client);
$categoryOption = $this->stdIn->getOption('category');
$category = $categoryOption !== null ? ProjectIssueCategory::from((string) $categoryOption) : null;
$result = $action(
$this->projectData,
ProjectIssueType::from((string) $this->stdIn->getArgument('type')),
$type,
(string) $this->stdIn->getOption('core'),
$limit,
$category
Expand Down
66 changes: 66 additions & 0 deletions tests/src/Action/GitLab/ListGitLabIssuesActionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace mglaman\DrupalOrg\Tests\Action\GitLab;

use mglaman\DrupalOrg\Action\GitLab\ListGitLabIssuesAction;
use mglaman\DrupalOrg\Enum\ProjectIssueCategory;
use mglaman\DrupalOrg\Enum\ProjectIssueType;
use mglaman\DrupalOrg\GitLab\Client as GitLabClient;
use mglaman\DrupalOrg\Result\GitLab\GitLabIssuesResult;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;

#[CoversClass(ListGitLabIssuesAction::class)]
#[CoversClass(GitLabIssuesResult::class)]
#[CoversClass(ProjectIssueType::class)]
#[CoversClass(ProjectIssueCategory::class)]
class ListGitLabIssuesActionTest extends TestCase
{
/**
* @return iterable<string, array{ProjectIssueType, ?ProjectIssueCategory, ?string}>
*/
public static function filterProvider(): iterable
{
yield 'no filters' => [ProjectIssueType::All, null, null];
yield 'rtbc' => [ProjectIssueType::Rtbc, null, 'state::rtbc'];
yield 'review' => [ProjectIssueType::Review, null, 'state::needsReview'];
yield 'category only' => [ProjectIssueType::All, ProjectIssueCategory::Bug, 'category::bug'];
yield 'type and category' => [ProjectIssueType::Rtbc, ProjectIssueCategory::Task, 'state::rtbc,category::task'];
}

#[DataProvider('filterProvider')]
public function testFiltersMapToGitLabLabels(
ProjectIssueType $type,
?ProjectIssueCategory $category,
?string $expectedLabels,
): void {
$expectedParams = [
'state' => 'opened',
'per_page' => 10,
'order_by' => 'created_at',
'sort' => 'desc',
];
if ($expectedLabels !== null) {
$expectedParams['labels'] = $expectedLabels;
}

$gitLabClient = $this->createMock(GitLabClient::class);
$gitLabClient->expects(self::once())
->method('getIssues')
->with('project/smart_content', $expectedParams)
->willReturn([]);

$result = (new ListGitLabIssuesAction($gitLabClient))(
'smart_content',
limit: 10,
type: $type,
category: $category,
);

self::assertSame('smart_content', $result->projectMachineName);
self::assertSame([], $result->issues);
}
}
Loading