Skip to content

Commit

Permalink
feat: add user mentions
Browse files Browse the repository at this point in the history
  • Loading branch information
marcocesarato committed Feb 12, 2021
1 parent 7fcca82 commit 214c75f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
47 changes: 35 additions & 12 deletions src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/Git/ConventionalCommit.php
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,21 @@ public function getReferences(): array
return array_unique($refs);
}

/**
* Get mentions.
*/
public function getMentions(): array
{
$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[] = $match;
}
}

return array_unique($mentions);
}

public function getHeader(): string
{
$header = $this->type;
Expand Down

0 comments on commit 214c75f

Please sign in to comment.