Skip to content

Commit 164d159

Browse files
author
Pete Bishop
committed
Add GitHub Release Object with formatting for release notes
1 parent f146d73 commit 164d159

File tree

3 files changed

+93
-11
lines changed

3 files changed

+93
-11
lines changed

Diff for: app/Support/GitHub.php

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Support;
44

5+
use App\Support\GitHub\Release;
56
use Illuminate\Support\Collection;
67
use Illuminate\Support\Facades\Cache;
78
use Illuminate\Support\Facades\Http;
@@ -31,9 +32,7 @@ public function latestVersion()
3132
$version = Cache::remember(
3233
$this->getCacheKey('latest-version'),
3334
now()->addHour(),
34-
function () {
35-
return $this->fetchLatestVersion();
36-
}
35+
fn () => $this->fetchLatestVersion()
3736
);
3837

3938
return $version['name'] ?? 'Unknown';
@@ -44,9 +43,7 @@ public function releases(): Collection
4443
return Cache::remember(
4544
$this->getCacheKey('releases'),
4645
now()->addHour(),
47-
function () {
48-
return $this->fetchReleases();
49-
}
46+
fn () => $this->fetchReleases()
5047
);
5148
}
5249

@@ -75,9 +72,9 @@ private function fetchReleases(): ?Collection
7572

7673
// Check if the request was successful
7774
if ($response->failed()) {
78-
return null;
75+
return collect();
7976
}
8077

81-
return collect($response->json());
78+
return collect($response->json())->map(fn (array $release) => new Release($release));
8279
}
8380
}

Diff for: app/Support/GitHub/Release.php

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
namespace App\Support\GitHub;
4+
5+
/**
6+
* @property string $url
7+
* @property string $assets_url
8+
* @property string $upload_url
9+
* @property string $html_url
10+
* @property int $id
11+
* @property array $author
12+
* @property string $node_id
13+
* @property string $tag_name
14+
* @property string $target_commitish
15+
* @property string $name
16+
* @property bool $draft
17+
* @property bool $prerelease
18+
* @property string $created_at
19+
* @property string $published_at
20+
* @property array $assets
21+
* @property string $tarball_url
22+
* @property string $zipball_url
23+
* @property string $body
24+
* @property int $mentions_count
25+
*/
26+
class Release
27+
{
28+
private bool $withUserLinks = true;
29+
private bool $convertLinks = true;
30+
31+
public function __construct(
32+
private array $data
33+
) {
34+
//
35+
}
36+
37+
public function __get(string $name)
38+
{
39+
return $this->data[$name] ?? null;
40+
}
41+
42+
public function __isset(string $name): bool
43+
{
44+
return isset($this->data[$name]);
45+
}
46+
47+
public function getBodyForMarkdown(): string
48+
{
49+
$body = $this->body;
50+
51+
// Convert any URLs to Markdown links
52+
if ($this->convertLinks) {
53+
$body = preg_replace(
54+
'/(https?:\/\/[^\s]+)/',
55+
'[$1]($1)',
56+
$body
57+
);
58+
}
59+
60+
// Change any @ tags to markdown links to GitHub
61+
if ($this->withUserLinks) {
62+
$body = preg_replace(
63+
'/@([a-zA-Z0-9_]+)/',
64+
'[@$1](https://github.com/$1)',
65+
$body
66+
);
67+
}
68+
69+
return str_replace('#', '##', $body);
70+
}
71+
72+
public function withoutUserLinks(bool $withoutUserLinks = true): static
73+
{
74+
$this->withUserLinks = ! $withoutUserLinks;
75+
76+
return $this;
77+
}
78+
79+
public function withoutLinkConversion(bool $convertLinks = true): static
80+
{
81+
$this->convertLinks = ! $convertLinks;
82+
83+
return $this;
84+
}
85+
}

Diff for: resources/views/docs/desktop/1/getting-started/release-notes.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ order: 1100
44
---
55

66
@forelse (\App\Support\GitHub::electron()->releases() as $release)
7-
## {{ $release['name'] }}
8-
Released: {{ \Carbon\Carbon::parse($release['published_at'])->format('F j, Y') }}**
7+
## {{ $release->name }}
8+
**Released: {{ \Carbon\Carbon::parse($release->published_at)->format('F j, Y') }}**
99

10-
{{ str_replace('#', '##', $release['body']) }}
10+
{{ $release->getBodyForMarkdown() }}
1111
---
1212
@empty
1313
## We couldn't show you the latest release notes at this time.

0 commit comments

Comments
 (0)