From 3e899655c82737ca363995530ec300d4215c2a64 Mon Sep 17 00:00:00 2001 From: Marco Cesarato Date: Wed, 20 Jan 2021 12:50:15 +0100 Subject: [PATCH] fix: add hash check and rename parser to conventional --- src/Changelog.php | 6 +++--- src/Commit/{Parser.php => Conventional.php} | 20 +++++++++++++++++--- 2 files changed, 20 insertions(+), 6 deletions(-) rename src/Commit/{Parser.php => Conventional.php} (91%) diff --git a/src/Changelog.php b/src/Changelog.php index b5d315d..c2096f7 100644 --- a/src/Changelog.php +++ b/src/Changelog.php @@ -191,7 +191,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int // Get all commits information $commits = []; foreach ($commitsRaw as $commitRaw) { - $commit = new Commit\Parser($commitRaw); + $commit = new Commit\Conventional($commitRaw); // Not a conventional commit if (!$commit->isValid()) { @@ -208,7 +208,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int } // Add commit if (!$ignore) { - $commits[] = new Commit\Parser($commit); + $commits[] = new Commit\Conventional($commit); } } @@ -276,7 +276,7 @@ public function generate(InputInterface $input, SymfonyStyle $output): int /** * Generate markdown from changes. * - * @param Commit\Parser[][][][] $changes + * @param Commit\Conventional[][][][] $changes */ protected function getMarkdownChanges(array $changes): string { diff --git a/src/Commit/Parser.php b/src/Commit/Conventional.php similarity index 91% rename from src/Commit/Parser.php rename to src/Commit/Conventional.php index 82aab6c..24ebee9 100644 --- a/src/Commit/Parser.php +++ b/src/Commit/Conventional.php @@ -5,7 +5,7 @@ use ConventionalChangelog\Helper\Format; use ConventionalChangelog\Type\Stringable; -class Parser implements Stringable +class Conventional implements Stringable { protected const PATTERN_HEADER = "/^(?'type'[a-z]+)(\((?'scope'.+)\))?(?'important'[!]?)[:][[:blank:]](?'description'.+)/iums"; protected const PATTERN_FOOTER = "/(?'token'^([a-z0-9_-]+|BREAKING[[:blank:]]CHANGES?))(?'value'([:][[:blank:]]|[:]?[[:blank:]][#](?=\w)).*?)$/iums"; @@ -75,7 +75,10 @@ public function __construct(string $commit) $rows = explode("\n", $commit); $count = count($rows); // Commit info - $this->hash = $rows[$count - 1]; + $hash = trim($rows[$count - 1]); + if ($this->isValidHash($hash)) { + $this->hash = $hash; + } $header = $rows[0]; $message = ''; // Get message @@ -119,9 +122,20 @@ protected function parseMessage(string $message) $this->body = new Body($body); } + /** + * Check if is valid SHA-1. + */ + protected function isValidHash(string $hash) + { + return (bool)preg_match('/^[0-9a-f]{40}$/i', $hash); + } + + /** + * Check if is valid conventional commit. + */ public function isValid(): bool { - return preg_match(self::PATTERN_HEADER, $this->raw); + return (bool)preg_match(self::PATTERN_HEADER, $this->raw); } public function getRaw(): string