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

Prints error message when rejecting commit #1108

Merged
merged 9 commits into from
Sep 19, 2023
36 changes: 24 additions & 12 deletions src/Task/Git/CommitMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

class CommitMessage implements TaskInterface
{
const MERGE_COMMIT_REGEX =
public const MERGE_COMMIT_REGEX =
'(Merge branch|tag \'.+\'(?:\s.+)?|Merge remote-tracking branch \'.+\'|Merge pull request #\d+\s.+)';

/**
Expand Down Expand Up @@ -93,6 +93,18 @@ public function canRunInContext(ContextInterface $context): bool
return $context instanceof GitCommitMsgContext;
}

private static function withCommitMessage(string $errorMessage, string $commitMessage): string
{
return sprintf(
"%s%sOriginal commit message:%s%s",
EwenQuim marked this conversation as resolved.
Show resolved Hide resolved
$errorMessage,
PHP_EOL,
PHP_EOL,
$commitMessage
);
}


public function run(ContextInterface $context): TaskResultInterface
{
assert($context instanceof GitCommitMsgContext);
Expand All @@ -110,39 +122,39 @@ public function run(ContextInterface $context): TaskResultInterface
return TaskResult::createFailed(
$this,
$context,
'Commit message should not be empty.'
self::withCommitMessage('Commit message should not be empty.', $commitMessage),
EwenQuim marked this conversation as resolved.
Show resolved Hide resolved
);
}

if ((bool) $config['enforce_capitalized_subject'] && !$this->subjectIsCapitalized($context)) {
return TaskResult::createFailed(
$this,
$context,
'Subject should start with a capital letter.'
self::withCommitMessage('Subject should start with a capital letter.', $commitMessage)
);
}

if ((bool) $config['enforce_single_lined_subject'] && !$this->subjectIsSingleLined($context)) {
return TaskResult::createFailed(
$this,
$context,
'Subject should be one line and followed by a blank line.'
self::withCommitMessage('Subject should be one line and followed by a blank line.', $commitMessage)
);
}

if ((bool) $config['enforce_no_subject_punctuations'] && $this->subjectHasPunctuations($context)) {
return TaskResult::createFailed(
$this,
$context,
'Please omit all punctuations from commit message subject.'
self::withCommitMessage('Please omit all punctuations from commit message subject.', $commitMessage)
);
}

if ((bool) $config['enforce_no_subject_trailing_period'] && $this->subjectHasTrailingPeriod($context)) {
return TaskResult::createFailed(
$this,
$context,
'Please omit trailing period from commit message subject.'
self::withCommitMessage('Please omit trailing period from commit message subject.', $commitMessage)
);
}

Expand All @@ -166,11 +178,7 @@ public function run(ContextInterface $context): TaskResultInterface
return TaskResult::createFailed(
$this,
$context,
implode(PHP_EOL, $exceptions).PHP_EOL.sprintf(
'Original commit message: %s%s',
PHP_EOL,
$commitMessage
)
self::withCommitMessage(implode(PHP_EOL, $exceptions), $commitMessage)
);
}

Expand Down Expand Up @@ -211,7 +219,11 @@ private function enforceTextWidth(GitCommitMsgContext $context): TaskResult
}

if (\count($errors)) {
return TaskResult::createFailed($this, $context, implode(PHP_EOL, $errors));
return TaskResult::createFailed(
$this,
$context,
self::withCommitMessage(implode(PHP_EOL, $errors), $commitMessage)
);
}

return TaskResult::createPassed($this, $context);
Expand Down
32 changes: 29 additions & 3 deletions test/Unit/Task/Git/CommitMessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,32 @@ function () {
'Line 5 of commit message has > 10 characters.'
)
];
yield 'enforce_text_with_regular_gives_back_message' => [
veewee marked this conversation as resolved.
Show resolved Hide resolved
[
'enforce_single_lined_subject' => false,
'max_subject_width' => 10,
'max_body_width' => 10,
],
$this->mockCommitMsgContext($this->buildMessage(
'Subject 1234567891011',
'Body 1234567891011',
'Body ok',
'Body 1110987654321'
)),
function () {
},
$this->buildMultiLineString(
'Please keep the subject <= 10 characters.',
'Line 3 of commit message has > 10 characters.',
'Line 5 of commit message has > 10 characters.',
'Original commit message:',
'Subject 1234567891011',
'',
'Body 1234567891011',
'Body ok',
'Body 1110987654321'
)
];
yield 'enforce_text_with_long_comments' => [
[
'enforce_single_lined_subject' => false,
Expand Down Expand Up @@ -309,7 +335,7 @@ function () {
'Rule not matched: "4" /(.*)st$/',
'Rule not matched: "5" /t(e|a)st/',
'Rule not matched: "6" TEST',
'Original commit message: ',
'Original commit message:',
'invalid'
)
];
Expand All @@ -327,7 +353,7 @@ function () {
$this->buildMultiLineString(
'Rule not matched: "full" test',
'Rule not matched: "partial" *es*',
'Original commit message: ',
'Original commit message:',
'invalid'
)
];
Expand All @@ -342,7 +368,7 @@ function () {
},
$this->buildMultiLineString(
'Rule not matched: "0" /TEST/',
'Original commit message: ',
'Original commit message:',
'test'
)
];
Expand Down