Skip to content

Commit

Permalink
fix: breaking changes preserving the initial commit and customizable …
Browse files Browse the repository at this point in the history
…label
  • Loading branch information
Marco Cesarato committed Jan 21, 2021
1 parent b362bd5 commit 607f6e0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
33 changes: 23 additions & 10 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,22 +213,31 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
}

// Changes groups sorting
$changes = ['breaking_changes' => []];
foreach ($this->config->getTypes() as $key => $type) {
$changes[$key] = [];
$changes = [];
foreach ($this->config->getTypes(true) as $key => $type) {
$changes[$type] = [];
}

// Group all changes to lists by type
$types = $this->config->getTypes();
foreach ($commits as $commit) {
if (in_array($commit->getType(), $types)) {
$itemKey = strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '', $commit->getDescription()));
$itemKey = $this->getItemKey($commit->getDescription());
$breakingChanges = $commit->getBreakingChanges();
$type = (string)$commit->getType();
$scope = $commit->getScope()->toPrettyString();
$hash = $commit->getHash();
if (!empty($breakingChanges)) {
$type = 'breaking_changes';
foreach ($breakingChanges as $description) {
// Clone commit as breaking with different description message
$breakingCommit = new Commit\Conventional();
$breakingCommit->setType($type)
->setDescription($description)
->setScope($scope)
->setHash($hash);
$key = $this->getItemKey($description);
$changes['breaking_changes'][$scope][$key][$hash] = $breakingCommit;
}
}
$changes[$type][$scope][$itemKey][$hash] = $commit;
}
Expand Down Expand Up @@ -273,6 +282,14 @@ public function generate(InputInterface $input, SymfonyStyle $output): int
return Command::SUCCESS;
}

/**
* Generate item key for merge commit with similar description.
*/
protected function getItemKey(string $string): string
{
return strtolower(preg_replace('/[^a-zA-Z0-9_-]+/', '', $string));
}

/**
* Generate markdown from changes.
*
Expand All @@ -288,11 +305,7 @@ protected function getMarkdownChanges(array $changes): string
if (empty($list)) {
continue;
}
if ($type === 'breaking_changes') {
$label = '⚠ BREAKING CHANGES';
} else {
$label = $this->config->getTypeLabel($type);
}
$label = $this->config->getTypeLabel($type);
$changelog .= "\n### {$label}\n\n";
ksort($list);
foreach ($list as $scope => $items) {
Expand Down
12 changes: 9 additions & 3 deletions src/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Configuration
* @var string[][]
*/
public $types = [
'breaking_changes' => ['label' => '⚠ BREAKING CHANGES', 'description' => 'Code changes that potentially causes other components to fail'],
'feat' => ['label' => 'Features', 'description' => 'New features'],
'perf' => ['label' => 'Performance Improvements', 'description' => 'Code changes that improves performance'],
'fix' => ['label' => 'Bug Fixes', 'description' => 'Issues resolution'],
Expand Down Expand Up @@ -84,7 +85,7 @@ public function fromArray(array $array)

// Overwrite excluded types
if (!empty($array['excludedTypes'])) {
$params['excludedTypes'] = $array['excludedTypes'];
$params['excludedTypes'] = $array['excludeTypes'];
}

// Unset excluded types
Expand Down Expand Up @@ -136,9 +137,14 @@ public function getTypesInfo(): array
/**
* @return string[]
*/
public function getTypes(): array
public function getTypes(bool $breaking = false): array
{
return array_keys($this->types);
$types = $this->types;
if (!$breaking) {
unset($types['breaking_changes']);
}

return array_keys($types);
}

/**
Expand Down

0 comments on commit 607f6e0

Please sign in to comment.