Skip to content

Commit

Permalink
fix: add hash check and rename parser to conventional
Browse files Browse the repository at this point in the history
  • Loading branch information
Marco Cesarato committed Jan 20, 2021
1 parent 79b0f95 commit 3e89965
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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
{
Expand Down
20 changes: 17 additions & 3 deletions src/Commit/Parser.php → src/Commit/Conventional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3e89965

Please sign in to comment.