Skip to content

Commit

Permalink
Merge pull request #843 from Benedikt-Brunner/allow-github-merge-comm…
Browse files Browse the repository at this point in the history
…its-in-repeated-commits-rule

Allow Github style merge commits in no repeated commits rule
  • Loading branch information
shyim committed Jun 24, 2024
2 parents 6e5fa8c + 93b7360 commit 7b4f2ff
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/Rule/DisallowRepeatedCommits.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Danger\Rule;

use Danger\Context;
use Danger\Platform\Github\Github;

class DisallowRepeatedCommits
{
Expand All @@ -15,6 +16,13 @@ public function __invoke(Context $context): void
{
$messages = $context->platform->pullRequest->getCommits()->getMessages();

if ($context->platform instanceof Github) {
$messages = array_filter(
$messages,
fn ($message) => !(preg_match('/^Merge branch .* into .*$/', $message) === 1),
);
}

if (\count($messages) !== \count(array_unique($messages))) {
$context->failure($this->message);
}
Expand Down
48 changes: 48 additions & 0 deletions tests/Rule/DisallowRepeatedCommitsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,52 @@ public function testRuleNotMatches(): void

static::assertFalse($context->hasFailures());
}

public function testRuleMatchesWithMergeCommits(): void
{
$commit = new Commit();
$commit->message = 'Test';

$secondCommit = new Commit();
$secondCommit->message = 'Test';

$thirdCommit = new Commit();
$thirdCommit->message = 'Merge branch master into feature';

$github = $this->createMock(Github::class);
$pr = $this->createMock(PullRequest::class);
$pr->method('getCommits')->willReturn(new CommitCollection([$commit, $secondCommit, $thirdCommit]));
$github->pullRequest = $pr;

$context = new Context($github);

$rule = new DisallowRepeatedCommits();
$rule($context);

static::assertTrue($context->hasFailures());
}

public function testRuleNotMatchesWithMultipleMergeCommits(): void
{
$commit = new Commit();
$commit->message = 'Test';

$secondCommit = new Commit();
$secondCommit->message = 'Merge branch master into features';

$thirdCommit = new Commit();
$thirdCommit->message = 'Merge branch master into feature';

$github = $this->createMock(Github::class);
$pr = $this->createMock(PullRequest::class);
$pr->method('getCommits')->willReturn(new CommitCollection([$commit, $secondCommit, $thirdCommit]));
$github->pullRequest = $pr;

$context = new Context($github);

$rule = new DisallowRepeatedCommits();
$rule($context);

static::assertFalse($context->hasFailures());
}
}

0 comments on commit 7b4f2ff

Please sign in to comment.