Skip to content

Commit 0c4a75d

Browse files
refactor: fetch github data at build time (#77)
Co-authored-by: Enzo Innocenzi <[email protected]>
1 parent 957084f commit 0c4a75d

File tree

3 files changed

+38
-32
lines changed

3 files changed

+38
-32
lines changed

.github/workflows/deploy.yml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,21 @@ jobs:
3434
- name: List Installed Dependencies
3535
run: composer show -D
3636

37-
- name: Deploy
38-
run: php ./tempest deploy
37+
- name: Get latest GitHub statistics
38+
id: stats
3939
env:
4040
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
41+
run: |
42+
set -e
43+
STARS=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework | jq '.stargazers_count // empty')
44+
TAG=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework/releases/latest | jq -r '.tag_name // empty')
45+
echo "Stars: $STARS"
46+
echo "Latest version: $TAG"
47+
echo "stars=$STARS" >> "$GITHUB_OUTPUT"
48+
echo "latest_tag=$TAG" >> "$GITHUB_OUTPUT"
4149
42-
43-
50+
- name: Deploy
51+
run: php ./tempest deploy
52+
env:
53+
TEMPEST_BUILD_STARGAZERS: ${{ steps.stats.outputs.stars }}
54+
TEMPEST_BUILD_LATEST_RELEASE: ${{ steps.stats.outputs.latest_tag }}

src/GitHub/GetLatestRelease.php

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,21 @@ public function __construct(
1717

1818
public function __invoke(): ?string
1919
{
20-
// Added by Aidan Casey to combat the GitHub rate limits.
21-
// We will inject the GH_TOKEN using our workflow.
22-
$headers = [];
23-
24-
if ($githubToken = env('GH_TOKEN')) {
25-
$headers['Authorization'] = 'Bearer ' . $githubToken;
20+
if ($latestRelease = env('TEMPEST_BUILD_LATEST_RELEASE')) {
21+
return $latestRelease;
2622
}
2723

2824
// Default release to the currently running version of Tempest.
2925
$defaultRelease = sprintf('v%s', Kernel::VERSION);
3026

3127
try {
3228
$body = $this->httpClient
33-
->get(
34-
uri: 'https://api.github.com/repos/tempestphp/tempest-framework/releases/latest',
35-
headers: $headers,
36-
)
29+
->get('https://api.github.com/repos/tempestphp/tempest-framework/releases/latest')
3730
->body;
3831

3932
return json_decode($body)->tag_name ?? $defaultRelease;
40-
} catch (Throwable $e) {
41-
ll($e);
42-
return Kernel::VERSION;
33+
} catch (Throwable) {
34+
return $defaultRelease;
4335
}
4436
}
4537
}

src/GitHub/GetStargazersCount.php

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\GitHub;
44

5+
use PDO;
56
use Tempest\HttpClient\HttpClient;
67
use Throwable;
78

@@ -16,26 +17,28 @@ public function __construct(
1617

1718
public function __invoke(): ?string
1819
{
19-
// Added by Aidan Casey to combat the GitHub rate limits.
20-
// We will inject the GH_TOKEN using our workflow.
21-
$headers = [];
20+
if ($stargazers = $this->getStargazersCount()) {
21+
return $stargazers > 999
22+
? (round($stargazers / 1000, 1) . 'K')
23+
: $stargazers;
24+
}
25+
26+
return null;
27+
}
2228

23-
if ($githubToken = env('GH_TOKEN')) {
24-
$headers['Authorization'] = 'Bearer ' . $githubToken;
29+
private function getStargazersCount(): ?int
30+
{
31+
if ($stargazers = env('TEMPEST_BUILD_STARGAZERS')) {
32+
return $stargazers;
2533
}
2634

2735
try {
28-
$body = $this->httpClient->get(
29-
uri: 'https://api.github.com/repos/tempestphp/tempest-framework',
30-
headers: $headers,
31-
)->body;
32-
$stargazers = json_decode($body)->stargazers_count ?? null;
36+
$body = $this->httpClient
37+
->get(uri: 'https://api.github.com/repos/tempestphp/tempest-framework')
38+
->body;
3339

34-
return $stargazers > 999
35-
? (round($stargazers / 1000, 1) . 'K')
36-
: $stargazers;
37-
} catch (Throwable $e) {
38-
ll($e);
40+
return $stargazers = json_decode($body)->stargazers_count ?? null;
41+
} catch (\Throwable) {
3942
return null;
4043
}
4144
}

0 commit comments

Comments
 (0)