Skip to content

refactor: fetch github data at build time #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,21 @@ jobs:
- name: List Installed Dependencies
run: composer show -D

- name: Deploy
run: php ./tempest deploy
- name: Get latest GitHub statistics
id: stats
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
set -e
STARS=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework | jq '.stargazers_count // empty')
TAG=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework/releases/latest | jq -r '.tag_name // empty')
echo "Stars: $STARS"
echo "Latest version: $TAG"
echo "stars=$STARS" >> "$GITHUB_OUTPUT"
echo "latest_tag=$TAG" >> "$GITHUB_OUTPUT"



- name: Deploy
run: php ./tempest deploy
env:
TEMPEST_BUILD_STARGAZERS: ${{ steps.stats.outputs.stars }}
TEMPEST_BUILD_LATEST_RELEASE: ${{ steps.stats.outputs.latest_tag }}
18 changes: 5 additions & 13 deletions src/GitHub/GetLatestRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,21 @@ public function __construct(

public function __invoke(): ?string
{
// Added by Aidan Casey to combat the GitHub rate limits.
// We will inject the GH_TOKEN using our workflow.
$headers = [];

if ($githubToken = env('GH_TOKEN')) {
$headers['Authorization'] = 'Bearer ' . $githubToken;
if ($latestRelease = env('TEMPEST_BUILD_LATEST_RELEASE')) {
return $latestRelease;
}

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

try {
$body = $this->httpClient
->get(
uri: 'https://api.github.com/repos/tempestphp/tempest-framework/releases/latest',
headers: $headers,
)
->get('https://api.github.com/repos/tempestphp/tempest-framework/releases/latest')
->body;

return json_decode($body)->tag_name ?? $defaultRelease;
} catch (Throwable $e) {
ll($e);
return Kernel::VERSION;
} catch (Throwable) {
return $defaultRelease;
}
}
}
33 changes: 18 additions & 15 deletions src/GitHub/GetStargazersCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\GitHub;

use PDO;
use Tempest\HttpClient\HttpClient;
use Throwable;

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

public function __invoke(): ?string
{
// Added by Aidan Casey to combat the GitHub rate limits.
// We will inject the GH_TOKEN using our workflow.
$headers = [];
if ($stargazers = $this->getStargazersCount()) {
return $stargazers > 999
? (round($stargazers / 1000, 1) . 'K')
: $stargazers;
}

return null;
}

if ($githubToken = env('GH_TOKEN')) {
$headers['Authorization'] = 'Bearer ' . $githubToken;
private function getStargazersCount(): ?int
{
if ($stargazers = env('TEMPEST_BUILD_STARGAZERS')) {
return $stargazers;
}

try {
$body = $this->httpClient->get(
uri: 'https://api.github.com/repos/tempestphp/tempest-framework',
headers: $headers,
)->body;
$stargazers = json_decode($body)->stargazers_count ?? null;
$body = $this->httpClient
->get(uri: 'https://api.github.com/repos/tempestphp/tempest-framework')
->body;

return $stargazers > 999
? (round($stargazers / 1000, 1) . 'K')
: $stargazers;
} catch (Throwable $e) {
ll($e);
return $stargazers = json_decode($body)->stargazers_count ?? null;
} catch (\Throwable) {
return null;
}
}
Expand Down
Loading