From 93b7360ad5f7ca9ed0d0f8adf266b2cc4d11f441 Mon Sep 17 00:00:00 2001 From: Benedikt Brunner Date: Mon, 24 Jun 2024 08:45:37 +0200 Subject: [PATCH] feat: allow Github style merge commits --- src/Rule/DisallowRepeatedCommits.php | 8 ++++ tests/Rule/DisallowRepeatedCommitsTest.php | 48 ++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/src/Rule/DisallowRepeatedCommits.php b/src/Rule/DisallowRepeatedCommits.php index 1c9b6b58..0fddefb2 100644 --- a/src/Rule/DisallowRepeatedCommits.php +++ b/src/Rule/DisallowRepeatedCommits.php @@ -4,6 +4,7 @@ namespace Danger\Rule; use Danger\Context; +use Danger\Platform\Github\Github; class DisallowRepeatedCommits { @@ -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); } diff --git a/tests/Rule/DisallowRepeatedCommitsTest.php b/tests/Rule/DisallowRepeatedCommitsTest.php index 2a809bac..1701bbbe 100644 --- a/tests/Rule/DisallowRepeatedCommitsTest.php +++ b/tests/Rule/DisallowRepeatedCommitsTest.php @@ -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()); + } }