Skip to content
Open
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
2 changes: 2 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ parameters:
- 'App\Subscriber\RewriteUnwantedPhrasesSubscriber'
- 'App\Subscriber\AllowEditFromMaintainerSubscriber'
secret: '%env(SYMFONY_AI_SECRET)%'
ignored_labels:
- 'Run examples'

symfony/ux:
subscribers:
Expand Down
12 changes: 11 additions & 1 deletion src/Model/Repository.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check warning on line 1 in src/Model/Repository.php

View workflow job for this annotation

GitHub Actions / PHP-CS-Fixer

Found violation(s) of type: phpdoc_align

namespace App\Model;

Expand All @@ -10,12 +10,14 @@
class Repository
{
/**
* @param string|null $secret the webhook secret used by GitHub
* @param string|null $secret the webhook secret used by GitHub
* @param list<string> $ignoredLabels labels that should not be auto-applied from PR/issue titles
*/
public function __construct(
private readonly string $vendor,
private readonly string $name,
private readonly ?string $secret = null,
private readonly array $ignoredLabels = [],
) {
}

Expand Down Expand Up @@ -44,6 +46,14 @@
);
}

/**
* @return list<string>
*/
public function getIgnoredLabels(): array
{
return $this->ignoredLabels;
}

public function getFullName(): string
{
return sprintf('%s/%s', $this->getVendor(), $this->getName());
Expand Down
3 changes: 3 additions & 0 deletions src/Service/LabelNameExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ public function extractLabels(string $title, Repository $repository): array
}
}

$ignoredLabels = array_map('strtolower', $repository->getIgnoredLabels());
$labels = array_values(array_filter($labels, static fn (string $label): bool => !\in_array(strtolower($label), $ignoredLabels, true)));

$this->logger->debug('Searched for labels in title', ['title' => $title, 'labels' => \json_encode($labels, \JSON_THROW_ON_ERROR)]);

return $labels;
Expand Down
5 changes: 3 additions & 2 deletions src/Service/RepositoryProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RepositoryProvider
private array $repositories = [];

/**
* @param array<string, array{secret?: string}> $repositories
* @param array<string, array{secret?: string, ignored_labels?: list<string>}> $repositories
*/
public function __construct(array $repositories)
{
Expand All @@ -29,7 +29,8 @@ public function __construct(array $repositories)
$this->addRepository(new Repository(
$vendorName,
$repositoryName,
$repositoryData['secret'] ?? null
$repositoryData['secret'] ?? null,
$repositoryData['ignored_labels'] ?? [],
));
}
}
Expand Down
24 changes: 24 additions & 0 deletions tests/Service/LabelNameExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,28 @@ public static function provideLabels(): iterable
yield [['Messenger', 'Mime'], '[Messenger] [Mime] Foobar'];
yield [['Messenger', 'Mime'], '[Messenger] Foobar [Mime]'];
}

public function testIgnoredLabelsAreFilteredOut(): void
{
$extractor = new LabelNameExtractor(new StaticLabelApi(), new NullLogger());
$repo = new Repository('carsonbot-playground', 'symfony', null, ['Messenger']);

$this->assertSame([], $extractor->extractLabels('[Messenger] Foobar', $repo));
}

public function testIgnoredLabelsAreCaseInsensitive(): void
{
$extractor = new LabelNameExtractor(new StaticLabelApi(), new NullLogger());
$repo = new Repository('carsonbot-playground', 'symfony', null, ['messenger']);

$this->assertSame([], $extractor->extractLabels('[Messenger] Foobar', $repo));
}

public function testIgnoredLabelsOnlyFilterConfiguredOnes(): void
{
$extractor = new LabelNameExtractor(new StaticLabelApi(), new NullLogger());
$repo = new Repository('carsonbot-playground', 'symfony', null, ['Messenger']);

$this->assertSame(['Mime'], $extractor->extractLabels('[Messenger] [Mime] Foobar', $repo));
}
}
35 changes: 35 additions & 0 deletions tests/Subscriber/AutoLabelFromContentSubscriberTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,39 @@ public static function getPRTests(): array

return $tests;
}

public function testIgnoredLabelsAreNotAppliedFromPRTitle(): void
{
$labelsApi = $this->getMockBuilder(StaticLabelApi::class)
->disableOriginalConstructor()
->onlyMethods(['addIssueLabels'])
->getMock();

$subscriber = new AutoLabelFromContentSubscriber($labelsApi, new LabelNameExtractor($labelsApi, new NullLogger()));
$repository = new Repository('weaverryan', 'symfony', null, ['Bug']);

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber($subscriber);

$labelsApi->expects($this->once())
->method('addIssueLabels')
->with(1234, ['Asset', 'BC Break'], $repository);

$event = new GitHubEvent([
'action' => 'opened',
'pull_request' => [
'number' => 1234,
'title' => '[Asset][bc Break][Bug] Some PR title',
'body' => '',
'draft' => false,
],
], $repository);

$dispatcher->dispatch($event, GitHubEvents::PULL_REQUEST);

$responseData = $event->getResponseData();

$this->assertSame(1234, $responseData['pull_request']);
$this->assertSame(['Asset', 'BC Break'], $responseData['pr_labels']);
}
}
Loading