Skip to content

Commit

Permalink
fix: breaking changes indicated by a ! and ignore duplicated or empty…
Browse files Browse the repository at this point in the history
… message

Refs: #1
  • Loading branch information
Marco Cesarato committed Feb 12, 2021
1 parent f69c383 commit f3ebeee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 39 deletions.
22 changes: 11 additions & 11 deletions src/Changelog.php
Expand Up @@ -285,23 +285,28 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
// Group all changes to lists by type
$types = $this->config->getAllowedTypes();
foreach ($commits as $commit) {
if (in_array($commit->getType(), $types)) {
if (in_array($commit->getType(), $types) || $commit->isBreakingChange()) {
$itemKey = $this->getItemKey($commit->getDescription());
$breakingChanges = $commit->getBreakingChanges();
$type = (string)$commit->getType();
$scope = $commit->getScope()->toPrettyString();
$hash = $commit->getHash();
if (!empty($breakingChanges)) {
foreach ($breakingChanges as $description) {
$breakingType = Configuration::$breakingChangesType;
$key = $this->getItemKey($description);
if (empty($description) || $itemKey === $key) {
$commit->setBreakingChange(true);
continue;
}
// Clone commit as breaking with different description message
$breakingCommit = new ConventionalCommit();
$breakingCommit->setType($type)
$breakingCommit->setType($breakingType)
->setDescription($description)
->setScope($scope)
->setHash($hash);
$key = $this->getItemKey($description);
$changes['breaking_changes'][$scope][$key][$hash] = $breakingCommit;
$summary['breaking_changes']++;
$changes[$breakingType][$scope][$key][$hash] = $breakingCommit;
$summary[$breakingType]++;
}
}
$changes[$type][$scope][$itemKey][$hash] = $commit;
Expand Down Expand Up @@ -408,7 +413,6 @@ protected function getMarkdownChanges(array $changes): string
$changelog .= "\n##### {$scope}\n\n";
}
foreach ($items as $itemsList) {
$important = '';
$description = '';
$sha = '';
$references = '';
Expand All @@ -418,10 +422,6 @@ protected function getMarkdownChanges(array $changes): string
$description = ucfirst($item->getDescription());
$refs = $item->getReferences();

if ($item->isImportant()) {
$important = '**';
}

if (!empty($refs)) {
foreach ($refs as $ref) {
$url = $this->getIssueUrl($ref);
Expand All @@ -439,7 +439,7 @@ protected function getMarkdownChanges(array $changes): string
if (!empty($shaGroup)) {
$sha = '(' . implode(', ', $shaGroup) . ')';
}
$changelog .= Formatter::clean("* {$important}{$description}{$important} {$references} {$sha}");
$changelog .= Formatter::clean("* {$description} {$references} {$sha}");
$changelog .= PHP_EOL;
}
}
Expand Down
29 changes: 12 additions & 17 deletions src/Configuration.php
Expand Up @@ -52,14 +52,19 @@ class Configuration
'revert' => ['label' => 'Reverts', 'description' => 'Reverts a previous commit'],
];

/**
* Key of breaking changes.
*
* @var string
*/
public static $breakingChangesType = 'breaking_changes';

/**
* Preset of breaking changes.
*
* @var string[][]
*/
protected $breakingPreset = [
'breaking_changes' => ['label' => '⚠ BREAKING CHANGES', 'description' => 'Code changes that potentially causes other components to fail'],
];
protected $breakingChangesPreset = ['label' => '⚠ BREAKING CHANGES', 'description' => 'Code changes that potentially causes other components to fail'];

/**
* Types allowed on changelog.
Expand Down Expand Up @@ -235,7 +240,7 @@ public function fromArray(array $array)
}

// Add breaking changes
$params['preset'] = array_merge($this->breakingPreset, $params['preset']);
$params['preset'] = array_merge($this->getBreakingChangesPreset(), $params['preset']);

// Paths
$this->setRoot($params['root']);
Expand Down Expand Up @@ -389,7 +394,7 @@ public function setIgnorePatterns(array $ignorePatterns): Configuration
*/
public function getPreset(): array
{
return array_merge($this->breakingPreset, $this->preset);
return array_merge($this->getBreakingChangesPreset(), $this->preset);
}

/**
Expand Down Expand Up @@ -498,19 +503,9 @@ public function setSkipVerify(bool $skipVerify): Configuration
/**
* @return \string[][]
*/
public function getBreakingPreset(): array
public function getBreakingChangesPreset(): array
{
return $this->breakingPreset;
}

/**
* @param \string[][] $breakingPreset
*/
public function setBreakingPreset(array $breakingPreset): Configuration
{
$this->breakingPreset = $breakingPreset;

return $this;
return [self::$breakingChangesType => $this->breakingChangesPreset];
}

public function getCommitUrlFormat(): string
Expand Down
28 changes: 17 additions & 11 deletions src/Git/ConventionalCommit.php
Expand Up @@ -2,6 +2,7 @@

namespace ConventionalChangelog\Git;

use ConventionalChangelog\Configuration;
use ConventionalChangelog\Git\Commit\Description;
use ConventionalChangelog\Git\Commit\Footer;
use ConventionalChangelog\Git\Commit\Scope;
Expand All @@ -10,8 +11,8 @@

class ConventionalCommit extends Commit
{
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";
protected const PATTERN_HEADER = "/^(?<type>[a-z]+)(?<breaking_before>[!]?)(\((?<scope>.+)\))?(?<breaking_after>[!]?)[:][[:blank:]](?<description>.+)/iums";
protected const PATTERN_FOOTER = "/(?<token>^([a-z0-9_-]+|BREAKING[[:blank:]]CHANGES?))(?<value>([:][[:blank:]]|[:]?[[:blank:]][#](?=\w)).*?)$/iums";

/**
* Sha hash.
Expand All @@ -35,11 +36,11 @@ class ConventionalCommit extends Commit
protected $scope;

/**
* Important.
* Is breaking change.
*
* @var bool
*/
protected $important = false;
protected $isBreakingChange = false;

/**
* Description.
Expand Down Expand Up @@ -69,7 +70,7 @@ protected function parseHeader(string $header)
preg_match(self::PATTERN_HEADER, $header, $matches);
$this->setType((string)$matches['type'])
->setScope((string)$matches['scope'])
->setImportant(!empty($matches['important']) ? true : false)
->setBreakingChange(!empty($matches['breaking_before'] || !empty($matches['breaking_after'])) ? true : false)
->setDescription((string)$matches['description']);
}

Expand Down Expand Up @@ -115,7 +116,12 @@ public function isValid(): bool

public function getType(): Type
{
return $this->type;
$type = $this->type;
if ($this->isBreakingChange()) {
$type = new Type(Configuration::$breakingChangesType);
}

return $type;
}

public function getScope(): Scope
Expand All @@ -128,9 +134,9 @@ public function hasScope(): bool
return !empty((string)$this->scope);
}

public function isImportant(): bool
public function isBreakingChange(): bool
{
return $this->important;
return $this->isBreakingChange;
}

public function getDescription(): Description
Expand Down Expand Up @@ -177,7 +183,7 @@ public function getHeader(): string
if ($this->hasScope()) {
$header .= '(' . $this->scope . ')';
}
if ($this->important) {
if ($this->isBreakingChange) {
$header .= '!';
}
$header .= ': ' . $this->description;
Expand Down Expand Up @@ -206,9 +212,9 @@ public function setScope(?string $scope): self
return $this;
}

public function setImportant(bool $important): self
public function setBreakingChange(bool $isBreakingChange): self
{
$this->important = $important;
$this->isBreakingChange = $isBreakingChange;

return $this;
}
Expand Down

0 comments on commit f3ebeee

Please sign in to comment.