Skip to content

Fix GitHub rate limit #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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
32 changes: 30 additions & 2 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,38 @@ 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

# Get stargazers
STARS=$(curl -s -H "Authorization: Bearer $GH_TOKEN" https://api.github.com/repos/tempestphp/tempest-framework | jq '.stargazers_count // empty')
STARS_K=""
if [ -n "$STARS" ]; then
if [ "$STARS" -gt 999 ]; then
STARS_K=$(awk "BEGIN { printf \"%.1fK\", $STARS/1000 }")
else
STARS_K=$STARS
fi
fi

# Get latest release tag
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_K"
echo "Latest Version: $TAG"

# Push these values to the output
echo "stars=$STARS_K" >> "$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 }}


13 changes: 3 additions & 10 deletions src/GitHub/GetLatestRelease.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ 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;
Expand Down
15 changes: 5 additions & 10 deletions src/GitHub/GetStargazersCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,14 @@ 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 ($stargazers = env('TEMPEST_BUILD_STARGAZERS')) {
return $stargazers;
}

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

return $stargazers > 999
Expand Down
Loading