Skip to content

Commit

Permalink
enhance: update the group name match logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 13, 2022
1 parent a9d653e commit 7668d4d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 16 deletions.
22 changes: 17 additions & 5 deletions src/Changelog/ChangeLogUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,29 @@ class ChangeLogUtil
{
/**
* @param string $msg
* @param array{startWiths: list<string>, contains: list<string>} $rule
*
* @return bool
*/
public static function isFixMsg(string $msg): bool
public static function matchMsgByRule(string $msg, array $rule): bool
{
if (stripos($msg, 'bug') === 0 || stripos($msg, 'close') === 0 || stripos($msg, 'fix') === 0) {
return true;
if (isset($rule['startWiths'])) {
foreach ($rule['startWiths'] as $start) {
if (stripos($msg, $start) === 0) {
return true;
}
}
}

return stripos($msg, ' fix') > 0;
if (isset($rule['contains'])) {
foreach ($rule['contains'] as $sub) {
if (stripos($msg, $sub) > 0) {
return true;
}
}
}

return false;
}

/**
Expand All @@ -38,5 +51,4 @@ public static function mkdir(string $path, int $mode = 0775, bool $recursive = t
{
return (is_dir($path) || !(!@mkdir($path, $mode, $recursive) && !is_dir($path))) && is_writable($path);
}

}
76 changes: 65 additions & 11 deletions src/Changelog/Formatter/AbstractFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PhpGit\Changelog\ChangeLogUtil;
use PhpGit\Changelog\GitChangeLog;
use PhpGit\Changelog\ItemFormatterInterface;
use function stripos;
use function array_merge;

/**
* Class AbstractFormatter
Expand All @@ -15,22 +15,60 @@
abstract class AbstractFormatter implements ItemFormatterInterface
{
/**
* @param string $msg
* The group name match rules. key is the group name.
*
* @return string
* @var array<string, array<string, string[]>>
* @see defaultRules()
*/
public function matchGroup(string $msg): string
protected array $rules = [];

/**
* Class constructor.
*
* @param array $rules
*/
public function __construct(array $rules = [])
{
if (ChangeLogUtil::isFixMsg($msg)) {
return 'Fixed';
if ($rules) {
$this->rules = array_merge(self::defaultRules(), $rules);
} else {
$this->rules = self::defaultRules();
}
}

if (stripos($msg, 'up') === 0 || stripos($msg, 'add') === 0 || stripos($msg, 'create') === 0) {
return 'Update';
}
public static function defaultRules(): array
{
return [
'Refactor' => [
'startWiths' => ['break', 'refactor'],
'contains' => [],
],
'Update' => [
'startWiths' => ['up', 'add', 'create', 'prof', 'perf', 'enhance'],
'contains' => [],
],
'Feature' => [
'startWiths' => ['feat', 'support', 'new'],
'contains' => [],
],
'Fixed' => [
'startWiths' => ['bug', 'fix', 'close'],
'contains' => [' fix']
]
];
}

if (stripos($msg, 'feat') === 0 || stripos($msg, 'support') === 0 || stripos($msg, 'new') === 0) {
return 'Feature';
/**
* @param string $msg
*
* @return string
*/
public function matchGroup(string $msg): string
{
foreach ($this->rules as $group => $rule) {
if (ChangeLogUtil::matchMsgByRule($msg, $rule)) {
return $group;
}
}

return GitChangeLog::OTHER_GROUP;
Expand All @@ -42,4 +80,20 @@ public function matchGroup(string $msg): string
* @return string[] returns [group, line string]
*/
abstract public function format(array $item): array;

/**
* @return array[]
*/
public function getRules(): array
{
return $this->rules;
}

/**
* @param array[] $rules
*/
public function setRules(array $rules): void
{
$this->rules = $rules;
}
}

0 comments on commit 7668d4d

Please sign in to comment.