Skip to content

Commit

Permalink
Merge branch 'master' into team-member-avatar-link
Browse files Browse the repository at this point in the history
  • Loading branch information
notbakaneko authored Jan 22, 2025
2 parents 312363c + 67103a6 commit d07d1c0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 43 deletions.
57 changes: 32 additions & 25 deletions app/Models/ChangelogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class ChangelogEntry extends Model

public static function convertLegacy($changelog)
{
$message = $changelog->message;
$splitMessage = static::splitMessage($message);
$title = $splitMessage[0];
$splitMessage = static::splitMessage($changelog->message);
$title = presence($splitMessage[0]);
$message = $splitMessage[1];

if ($title === null) {
$title = $splitMessage[1];
Expand All @@ -65,6 +65,13 @@ public static function convertLegacy($changelog)
]);
}

public static function getDisplayMessage(?string $origMessage): string
{
$split = static::splitMessage($origMessage);

return $split[1] === null ? '' : $split[0];
}

public static function guessCategory($data)
{
static $ignored = [
Expand Down Expand Up @@ -125,7 +132,7 @@ public static function importFromGithub($data)
'category' => static::guessCategory($data),
'created_at' => Carbon::parse($data['pull_request']['merged_at']),
'github_pull_request_id' => $data['pull_request']['number'],
'message' => $data['pull_request']['body'],
'message' => static::getDisplayMessage($data['pull_request']['body']),
'private' => static::isPrivate($data),
'title' => $data['pull_request']['title'],
'type' => static::guessType($data),
Expand Down Expand Up @@ -180,21 +187,30 @@ public static function placeholder()
]);
}

public static function splitMessage($message)
/**
* Returns array of message split by thematic break (`---`)
*
* The array length is always two.
* If the message is empty, both values will be null.
* If there's no thematic break, the second value will be null.
*/
public static function splitMessage($message): array
{
if (!present($message)) {
return [null, null];
}

static $separator = "\n\n---\n";
// prepended with \n\n just in case the message starts with ---\n (blank first part).
$message = "\n\n".trim(str_replace("\r\n", "\n", $message));
$splitPos = null_if_false(strpos($message, $separator)) ?? strlen($message);

return [
presence(trim(substr($message, 0, $splitPos))),
presence(trim(substr($message, $splitPos + strlen($separator)))),
];
// Surround with newlines to handle separator at the start/end.
$message = "\n\n".trim(strtr($message, ["\r\n" => "\n"]))."\n";
$splitPos = strpos($message, $separator);

return $splitPos === false
? [trim($message), null]
: [
trim(substr($message, 0, $splitPos)),
trim(substr($message, $splitPos + strlen($separator))),
];
}

public function builds()
Expand Down Expand Up @@ -253,21 +269,12 @@ public function githubUrl()
}
}

public function publicMessageHtml()
public function messageHtml(): ?string
{
return $this->memoize(__FUNCTION__, function () {
$message = $this->publicMessage();
$message = $this->message;

if ($message !== null) {
return markdown($message, 'changelog_entry');
}
});
}

public function publicMessage()
{
return $this->memoize(__FUNCTION__, function () {
return static::splitMessage($this->message)[1];
return present($message) ? markdown($message, 'changelog_entry') : null;
});
}
}
4 changes: 2 additions & 2 deletions app/Transformers/ChangelogEntryTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ public function includeGithubUser(ChangelogEntry $entry)

public function includeMessage(ChangelogEntry $entry)
{
return $this->primitive($entry->publicMessage());
return $this->primitive($entry->message);
}

public function includeMessageHtml(ChangelogEntry $entry)
{
return $this->primitive($entry->publicMessageHtml());
return $this->primitive($entry->messageHtml());
}
}
31 changes: 15 additions & 16 deletions tests/Models/ChangelogEntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
class ChangelogEntryTest extends TestCase
{
/**
* @dataProvider dataForPublicMessageHtmlVisibility
* @dataProvider dataForGetDisplayMessage
*/
public function testPublicMessageHtmlVisibility($message, $html)
public function testGetDisplayMessage($message, $html)
{
$entry = new ChangelogEntry(compact('message'));
$this->assertSame($html, $entry->publicMessageHtml());
$this->assertSame($html, ChangelogEntry::getDisplayMessage($message));
}

public function testConvertLegacyChangelogWithTitle()
Expand All @@ -26,7 +25,7 @@ public function testConvertLegacyChangelogWithTitle()
$legacy = new Changelog(['message' => $title]);
$converted = ChangelogEntry::convertLegacy($legacy);
$this->assertSame($title, $converted->title);
$this->assertNull($converted->publicMessageHtml());
$this->assertNull($converted->messageHtml());
}

public function testConvertLegacyChangelogWithTitleAndMessage()
Expand All @@ -36,7 +35,7 @@ public function testConvertLegacyChangelogWithTitleAndMessage()
$legacy = new Changelog(['message' => "{$title}\n\n---\n{$message}"]);
$converted = ChangelogEntry::convertLegacy($legacy);
$this->assertSame($title, $converted->title);
$this->assertSame("<div class='changelog-md'><p class=\"changelog-md__paragraph\">{$message}</p>\n</div>", $converted->publicMessageHtml());
$this->assertSame("<div class='changelog-md'><p class=\"changelog-md__paragraph\">{$message}</p>\n</div>", $converted->messageHtml());
}

public function testConvertLegacyChangelogWithMessage()
Expand All @@ -45,7 +44,7 @@ public function testConvertLegacyChangelogWithMessage()
$legacy = new Changelog(['message' => "---\n{$message}"]);
$converted = ChangelogEntry::convertLegacy($legacy);
$this->assertSame($message, $converted->title);
$this->assertNull($converted->publicMessageHtml());
$this->assertNull($converted->messageHtml());
}

public function testGuessCategoryCapitalise()
Expand Down Expand Up @@ -131,17 +130,17 @@ public function testIsPrivate()
$this->assertTrue(ChangelogEntry::isPrivate($data));
}

public static function dataForPublicMessageHtmlVisibility()
public static function dataForGetDisplayMessage()
{
return [
['Hidden', null],
["---\nVisible", "<div class='changelog-md'><p class=\"changelog-md__paragraph\">Visible</p>\n</div>"],
["Hidden\n---", null],
["Hidden\n\n---", null],
["Hidden\n\n---Still hidden", null],
["Hidden\n---\n\nStill hidden", null],
["Hidden\n---\nStill hidden", null],
["Hidden\n\n---\nVisible", "<div class='changelog-md'><p class=\"changelog-md__paragraph\">Visible</p>\n</div>"],
['Hidden', ''],
["Visible\n\n---", 'Visible'],
["---\nHidden", ''],
['---Hidden', ''],
['Still hidden---', ''],
["Hidden\n---\n\nStill hidden", ''],
["Hidden\n---\nStill hidden", ''],
["Visible\n\n---\nHidden", 'Visible'],
];
}
}

0 comments on commit d07d1c0

Please sign in to comment.