From 214c75f84874ef4a92fd869230762c257d97c638 Mon Sep 17 00:00:00 2001 From: Marco Cesarato Date: Fri, 12 Feb 2021 18:06:30 +0100 Subject: [PATCH] feat: add user mentions --- src/Changelog.php | 47 +++++++++++++++++++++++++--------- src/Git/ConventionalCommit.php | 15 +++++++++++ 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/Changelog.php b/src/Changelog.php index d05eebe..1efb055 100644 --- a/src/Changelog.php +++ b/src/Changelog.php @@ -419,30 +419,53 @@ protected function getMarkdownChanges(array $changes): string $description = ''; $sha = ''; $references = ''; + $mentions = ''; $shaGroup = []; $refsGroup = []; + $mentionsGroup = []; foreach ($itemsList as $item) { $description = ucfirst($item->getDescription()); - $refs = $item->getReferences(); - - if (!empty($refs)) { - foreach ($refs as $ref) { - $url = $this->getIssueUrl($ref); - $refsGroup[] = '[#' . $ref . "]({$url})"; + // Hashes + if (!$this->config->isHiddenHash()) { + if (!empty($item->getHash())) { + $commitUrl = $this->getCommitUrl($item->getHash()); + $shaGroup[] = '[' . $item->getShortHash() . "]({$commitUrl})"; } } - if (!empty($item->getHash())) { - $url = $this->getCommitUrl($item->getHash()); - $shaGroup[] = '[' . $item->getShortHash() . "]({$url})"; + // References + if (!$this->config->isHiddenReferences()) { + $refs = $item->getReferences(); + if (!empty($refs)) { + foreach ($refs as $ref) { + $refUrl = $this->getIssueUrl($ref); + $refsGroup[] = '[#' . $ref . "]({$refUrl})"; + } + } } + // Mentions + $commitMentions = $item->getMentions(); + foreach ($commitMentions as $user) { + $mention = "@{$user}"; + $userUrl = $this->getUserUrl($user); + $text = "[*{$mention}*]({$userUrl})"; + if (strpos($description, $mention) !== false) { + $description = str_replace($mention, $text, $description); + } elseif (!$this->config->isHiddenMentions()) { + $mentionsGroup[] = $text; + } + } + } + + if (!$this->config->isHiddenHash() && !empty($shaGroup)) { + $sha = '(' . implode(', ', $shaGroup) . ')'; } if (!$this->config->isHiddenReferences() && !empty($refsGroup)) { $references = implode(', ', $refsGroup); } - if (!$this->config->isHiddenHash() && !empty($shaGroup)) { - $sha = '(' . implode(', ', $shaGroup) . ')'; + if (!$this->config->isHiddenMentions() && !empty($mentionsGroup)) { + $mentions = '*[*' . implode(', ', $mentionsGroup) . '*]*'; } - $changelog .= Formatter::clean("* {$description} {$references} {$sha}"); + $changelog .= Formatter::clean("* {$description} {$references} {$sha} {$mentions}"); $changelog .= PHP_EOL; } } diff --git a/src/Git/ConventionalCommit.php b/src/Git/ConventionalCommit.php index 76488fc..2686448 100644 --- a/src/Git/ConventionalCommit.php +++ b/src/Git/ConventionalCommit.php @@ -177,6 +177,21 @@ public function getReferences(): array return array_unique($refs); } + /** + * Get mentions. + */ + public function getMentions(): array + { + $mentions = []; + if (preg_match_all('/(?:^|\s+)(?@(?[a-z\d](?:[a-z\d]|-(?=[a-z\d])){0,38}))(?:$|\s+)/smi', $this->raw, $matches)) { + foreach ($matches['user'] as $match) { + $mentions[] = $match; + } + } + + return array_unique($mentions); + } + public function getHeader(): string { $header = $this->type;