Skip to content

Commit

Permalink
refactor(git): move mentions to parent commit class
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Feb 13, 2021
1 parent 14cfa35 commit 81dd44d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 85 deletions.
51 changes: 47 additions & 4 deletions src/Git/Commit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use ConventionalChangelog\Git\Commit\Body;
use ConventionalChangelog\Git\Commit\Footer;
use ConventionalChangelog\Git\Commit\Mention;
use ConventionalChangelog\Git\Commit\Subject;
use ConventionalChangelog\Helper\Formatter;
use ConventionalChangelog\Type\Stringable;
Expand All @@ -12,6 +13,7 @@
class Commit implements Stringable
{
protected const PATTERN_FOOTER = "/(?<token>^([a-z0-9_-]+|BREAKING[[:blank:]]CHANGES?))(?<value>([:][[:blank:]]|[:]?[[:blank:]][#](?=\w)).*?)$/iums";
protected const PATTERN_MENTION = "/(?:^|\s+)(?<mention>@(?<user>[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}))(?:$|\s+)/smi";

/**
* Raw content.
Expand Down Expand Up @@ -41,6 +43,13 @@ class Commit implements Stringable
*/
protected $footers = [];

/**
* User Mentions.
*
* @var Mention[]
*/
protected $mentions = [];

/**
* Sha hash.
*
Expand Down Expand Up @@ -246,6 +255,7 @@ public function getBody(): Body

public function setBody(string $body): self
{
$body = Formatter::clean($body);
$footers = [];
if (preg_match_all(self::PATTERN_FOOTER, $body, $matches, PREG_SET_ORDER, 0)) {
foreach ($matches as $match) {
Expand All @@ -257,6 +267,8 @@ public function setBody(string $body): self
}
}
$this->setFooters($footers);

$body = Formatter::clean($body);
$this->body = new Body($body);

return $this;
Expand Down Expand Up @@ -289,6 +301,30 @@ public function getFooters(): array
return $this->footers;
}

/**
* Set mentions.
*
* @param Mention[] $mentions
*
* @return $this
*/
public function setMentions(array $mentions): self
{
$this->mentions = array_unique($mentions);

return $this;
}

/**
* Get mentions.
*
* @return Mention[]
*/
public function getMentions(): array
{
return $this->mentions;
}

/**
* Parse raw commit.
*/
Expand All @@ -297,16 +333,23 @@ protected function parse()
$rows = explode("\n", $this->raw);

$subject = $rows[0];
$this->setSubject($subject);

$body = '';
// Get message
foreach ($rows as $i => $row) {
if ($i !== 0) {
$body .= $row . "\n";
}
}
$this->setBody($body);

$mentions = [];
if (preg_match_all(self::PATTERN_MENTION, $this->raw, $matches)) {
foreach ($matches['user'] as $match) {
$mentions[] = new Mention($match);
}
}

$this->setSubject($subject)
->setBody($body)
->setMentions($mentions);
}

public function __wakeup()
Expand Down
93 changes: 12 additions & 81 deletions src/Git/ConventionalCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,14 @@

use ConventionalChangelog\Configuration;
use ConventionalChangelog\Git\Commit\Description;
use ConventionalChangelog\Git\Commit\Mention;
use ConventionalChangelog\Git\Commit\Reference;
use ConventionalChangelog\Git\Commit\Scope;
use ConventionalChangelog\Git\Commit\Type;
use ConventionalChangelog\Helper\Formatter;

class ConventionalCommit extends Commit
{
protected const PATTERN_HEADER = "/^(?<type>[a-z]+)(?<breaking_before>[!]?)(\((?<scope>.+)\))?(?<breaking_after>[!]?)[:][[:blank:]](?<description>.+)/iums";

/**
* Sha hash.
*
* @var string
*/
protected $hash;

/**
* Type.
*
Expand Down Expand Up @@ -49,57 +40,12 @@ class ConventionalCommit extends Commit
*/
protected $description;

/**
* User Mentions.
*
* @var Mention[]
*/
protected $mentions = [];

public function __construct(?string $commit = null)
{
parent::__construct($commit);
$this->parse();
}

/**
* Parse header.
*/
protected function parseHeader(string $header)
{
preg_match(self::PATTERN_HEADER, $header, $matches);
$this->setType((string)$matches['type'])
->setScope((string)$matches['scope'])
->setBreakingChange(!empty($matches['breaking_before'] || !empty($matches['breaking_after'])) ? true : false)
->setDescription((string)$matches['description']);
}

/**
* Parse message.
*/
protected function parseMessage(string $message)
{
$body = Formatter::clean($message);

$mentions = [];
if (preg_match_all('/(?:^|\s+)(?<mention>@(?<user>[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}))(?:$|\s+)/smi', $this->raw, $matches)) {
foreach ($matches['user'] as $match) {
$mentions[] = new Mention($match);
}
}

if (preg_match_all(self::PATTERN_FOOTER, $body, $matches, PREG_SET_ORDER, 0)) {
foreach ($matches as $match) {
$footer = $match[0];
$body = str_replace($footer, '', $body);
}
}

$body = Formatter::clean($body);
$this->setBody($body)
->setMentions($mentions);
}

/**
* From commit.
*/
Expand Down Expand Up @@ -176,30 +122,6 @@ public function getReferences(): array
return array_unique($refs);
}

/**
* Set mentions.
*
* @param Mention[] $mentions
*
* @return $this
*/
public function setMentions(array $mentions): self
{
$this->mentions = array_unique($mentions);

return $this;
}

/**
* Get mentions.
*
* @return Mention[]
*/
public function getMentions(): array
{
return $this->mentions;
}

public function getHeader(): string
{
$header = $this->type;
Expand Down Expand Up @@ -265,11 +187,20 @@ protected function parse()
if (!$this->isValid()) {
return;
}

$header = $this->getSubject();
$message = $this->getBody();

$this->parseHeader($header);
$this->parseMessage($message);
}

/**
* Parse header.
*/
protected function parseHeader(string $header)
{
preg_match(self::PATTERN_HEADER, $header, $matches);
$this->setType((string)$matches['type'])
->setScope((string)$matches['scope'])
->setBreakingChange(!empty($matches['breaking_before'] || !empty($matches['breaking_after'])) ? true : false)
->setDescription((string)$matches['description']);
}
}

0 comments on commit 81dd44d

Please sign in to comment.